var ppl = {};

ppl.main = {

	init: function() {
	
		$('#main-navigation ul > li').hover(over, out);
		function over(event) {
        	$(this).children('ul').show();
    	}
    	function out(event) {
			$(this).children('ul').hide();
		}
    	
    	$(".pplShop-expand-element").click(function() {
			$(this).toggleClass('pplShop-expanded');
			if($(this).next().is(':hidden')) {
				$(this).next().slideDown();
			} else {
				$(this).next().slideUp();
			}
		}).next().hide();
		
		$(".pplShop-expand-element3").click(function() {
			$(this).toggleClass('pplShop-expanded3');
			if($(this).next().is(':hidden')) {
				$(this).next().slideDown();
			} else {
				$(this).next().slideUp();
			}
		}).next().hide();
		
		$('.grid-products').each(function() {
			ppl.main.setEqualHeight($(this).find('.grid-product-inner'));
		});
		
		var searchtimer = undefined;
		$("#main-search-field").keyup(function(){
		
			clearTimeout(searchtimer);
			var ms = 200;
			var term = $(this).val();
			
			searchtimer = setTimeout(function() {
				if(term.length > 2) {
					ppl.main.showLiveSearch(term);
				}
			}, ms);
		
		});
		
		var inside = false;
		$('#search-live-search').hover(function(){ 
	        inside=true; 
	    }, function(){ 
	        inside=false; 
	    });
		
	    $('body').mouseup(function(){ 
	        if(!inside) {
	        	$('#search-live-search').empty();
	        	$('#search-live-search').hide();
	        }
	    });

	
	},
	
	showLiveSearch: function(term) {
	
		$('#search-live-search').load('/pplShop/productLiveSearch?term=' + encodeURIComponent(term)).show();
	
	},
	
	setEqualHeight: function(columns) {
		var tallestcolumn = 0;
		columns.each(
			function() {
				currentHeight = $(this).height();
				if(currentHeight > tallestcolumn) {
					tallestcolumn  = currentHeight - 1;
				}
			}
		);
		columns.height(tallestcolumn);
	}

}

jQuery(document).ready(function() {
    ppl.main.init();
});
