/*
*
* Lubarol custom functions
*
*
*/

// Let's define Creuna decorator pattern
var Lubarol = Lubarol || {}; Lubarol.init = function() {

	if($('#product_list').length) {
		Lubarol.listZebra();
	}
	// IE spesific conditional functions
	if($.browser.msie && parseInt($.browser.version) === 7) {
		//
	}

	// box shadow support
	function supportsBoxShadow() {
		var s = document.body.style;
		return s.WebkitBoxShadow !== undefined || s.MozBoxShadow !== undefined;
	}


	Lubarol.searchBarGrow();
	Lubarol.misc();
	Lubarol.delHover();
	Lubarol.popupNote();
	Lubarol.addThisBox();
	// Lubarol.activeState();
	Lubarol.hideElement();
}, Lubarol.searchBarGrow = function() {
	// I'm assuming that 1 letter will expand the input by 10 pixels
	var oneLetterWidth = 10;

	// I'm also assuming that input will resize when at least live characters
	// are typed
	var minCharacters = 10;
	var maxCharacters = 30;

	$('#search_query_menu').keyup(function() {
		var len = $(this).val().length;
		
		if (len > maxCharacters) len = maxCharacters;
		
		if(len > minCharacters) {
			// $('.search_button').show();
			// increase width
			$(this).width(len * oneLetterWidth).animate({
				opacity : .9
			});
		} else {
			// $('.search_button').hide();
			// restore minimal width;
			$(this).width(100);
		}
	});
}, Lubarol.misc = function() {
	$("#categories_block_left li a:contains('Sale')").addClass("saleBtn");
	if($('#wishlists > option').length == 1) {
		$('#wishlists').remove();
	}
	
	var newArrivals = $('#lang-da').find("#categories_block_left .newProds");
	if($('#lang-da').length == 1) {
			$(newArrivals).text('Nye varer');
	}

	$('.myaccount').find('li').remove();
	$('#pagination').find('form.pagination').remove();
	$("#header").find('.sf-sub-indicator').remove();
	$("#more_info_tabs").each( function() {
		$('a[href$="#ProductPrestapress"]').hide();
	});
	
/*
	function addParameter(url, param, value) {
    // Using a positive lookahead (?=\=) to find the
    // given parameter, preceded by a ? or &, and followed
    // by a = with a value after than (using a non-greedy selector)
    // and then followed by a & or the end of the string
    var val = new RegExp('(\\?|\\&)' + param + '=.*?(?=(&|$))'),
        qstring = /\?.+$/;
    
    // Check if the parameter exists
    if (val.test(url))
    {
        // if it does, replace it, using the captured group
        // to determine & or ? at the beginning
        return url.replace(val, '$1' + param + '=' + value);
    }
    else if (qstring.test(url))
    {
        // otherwise, if there is a query string at all
        // add the param to the end of it
        return url + '&' + param + '=' + value;
    }
    else
    {
        // if there's no query string, add one
        return url + '?' + param + '=' + value;
    }
}

var url = 'http://lubarol.com/manufacturer.php';

url = addParameter(url, 'p=0&n', '100');
*/

	// $('.sf-menu li:nth-child(2n) > a[href="manufacturer.php"]').attr("href", "manufacturer.php?p=0&n=100");

	$("#more_info_sheets .accessories_block ul li:nth-child(4)").addClass("last");
	$("#publication_home .block_content li:nth-child(2n)").addClass("expand");

	// $('#pagination').find('form.pagination').replaceWith("<div class='viewAll'><a href='"+location.href+"&n=1000000'>View All</a></div>");
}, Lubarol.listZebra = function() {
	$("#product_list li:nth-child(3n+1)").addClass("aProd");
	$("#product_list li:nth-child(3n+2)").addClass("bProd");
	$("#product_list li:nth-child(3n)").addClass("cProd");
}, Lubarol.delHover = function() {
	$(".std tr th:nth-child(4n), .cart_item td:nth-child(4d)").addClass("empty");
	$('.cart_quantity_delete').hide();
	$('#cart_summary').each(function() {
		$(this).mouseover(function() {
			$(this).find('.cart_quantity_delete').show();
		}).mouseout(function() {
			$(this).find('.cart_quantity_delete').hide();
		});
	});
}, Lubarol.activeState = function() {
	$(".sf-menu a").each(function() {
		if(this.href == window.location || this.href == document.location.protocol + "//" + window.location.hostname + window.location.pathname)
			$(this).removeClass("navOff");
		$(this).addClass("selected");
	});
}, Lubarol.hideElement = function() {
	$('#wrapResetImages').remove();
	$('#product_reference').remove();
},
// Equal height collumns
Lubarol.setEqualHeight = function (columns) {
    var tallestcolumn = 0;
    columns.each(function () {
        var currentHeight = jQuery(this).height();
        if (currentHeight > tallestcolumn) {
            tallestcolumn = currentHeight;
        }
    });
    columns.height(tallestcolumn);
},
Lubarol.popupNote = function (columns) {
	$("#popupNote .closeNote").click( function() {
		$("#popupNote").fadeOut("fast");
	});
},
Lubarol.addThisBox = function (columns) {
    var delay = 400;
	
    function hideMenu() {
        if (!$('.custom_button').data('in') && !$('.hover_menu').data('in') && !$('.hover_menu').data('hidden')) {
            $('.hover_menu').fadeOut('fast');
            $('.custom_button').removeClass('active');
            $('.hover_menu').data('hidden', true);
        }
    }
    
    $('.custom_button, .hover_menu').mouseenter(function() {
        $('.hover_menu').fadeIn('fast');
        $('.custom_button').addClass('active');
        $(this).data('in', true);
        $('.hover_menu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false);
        setTimeout(hideMenu, delay);
    });
},
// Ready to fire functions
$(document).ready(function() {
	Lubarol.init();
	// Lubarol.setEqualHeight($("#publication_home ul li"));
});

