$(document).ready(function(){
	$('#topMyDrogist .btn *').click(function(){
		var block = $(this).parent().parent();
		block = block.attr('id');
		if ($('#'+block).hasClass('open')) {
			$('#'+block).removeClass('open');
			$('#'+block).addClass('closed');
		}
		else {
			$('#'+block).removeClass('closed');
			$('#'+block).addClass('open');
		}
		return false;
	});
	
	$("#q").keyup(function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 8 || code == 16 || code == 37 || code == 39 || (code >=48 && code <=90) || (code >=96 && code <=105)){
			var search;
			search = $("#q").val();
	
			if (search.length > 0){
				$.ajax({
					type: "POST",
					url: "/include/php/ajax.search.php",
					data: "search=" + search + "&cat=" + $('#cat').val(),
					success: function(message){
						$("#suggest").empty();
						if (message.length > 0){
							$("#suggest").append(decodeURIComponent( escape( message )));
							$("#suggest").show();
						}else{
							$("#suggest").hide();
						}
						/*
						$("#suggest ul li a").bind('click', function() {
							$("#q").val($(this).text());
							$("#suggest").hide();
							$("#suggest").empty();
							return false;
						});
						*/
					}
				});
			} else {
				// Empty suggestion list
				$("#suggest").hide();
				$("#suggest").empty();
			}
		}else{
			return false;
		}
	});
	
	$("#q").bind('blur', function() {
		// we have to do a timeout, because otherwise suggest is empty, before the onclick worked
		setTimeout(function() {
	        $("#suggest").hide();
	        $("#suggest").empty();
	    }, 500);
	});
	
	$("#filters h2:not('.header') a").live('click', function(){
		$(this).parent().parent().parent().find(':checked').each(function(){
			$(this).removeAttr('checked');
		});
		submitFilterForm();
		return false;
	});
	$("#filters h2.header a").live('click', function(){
		$("#filters").find(':checked').each(function(){
			$(this).removeAttr('checked');
		});
		submitFilterForm();
		return false;
	});
	$("#filters h2").not('.header').live('click', function(){
		var par = $(this).parent();
		par.find('.filterlist').toggle();
		par.find('.showhide').toggle();
		$(this).find("a").toggle();
		par.toggleClass('open');
	});
	$('.filterprice input').live('click', function(){
		$('.filterprice').find(':checked').removeAttr('checked');
		$(this).attr('checked', 'checked');
		submitFilterForm();
	});
	$('.filterbrand input').live('change', function(){
		$.address.value($(this).attr('rel'));  
		submitFilterForm();
	});
	$('.filtersale input').live('click', function(){
		$('.filtersale').find(':checked').removeAttr('checked');
		$(this).attr('checked', 'checked');
		submitFilterForm();
	});
	
	$('#selectedFilters a').live('click', function(){
		$('#filters').find('.' + $(this).attr('class')).removeAttr('checked');
		submitFilterForm();
		return false;
	});

	/*
	$('#shop .pagination a').live('click', function(){
		ajaxTimer = Math.round(new Date().getTime());
		$.get($(this).attr('href') + '&time=' + ajaxTimer, function(data){
			if(ajaxTimer == data.time){
				$('#shop').html((data.main));
				$('#ajaxFilterForm').html((data.filters));
				updateFilters();
			}
		});
		return false;
	});
	*/
	$('#shop #ipp').live('change', function(){
		ajaxTimer = Math.round(new Date().getTime());
		$.post('?time=' + ajaxTimer + "&" + $('#filterForm').serialize(), $('#ippForm').serialize(), function(data){
			if(ajaxTimer == data.time){
				$('#shop').html((data.main));
				$('#ajaxFilterForm').html((data.filters));
				updateFilters();
			}
		});
		return false;
	});
	
	updateFilters();
	
	$.Placeholder.init();
	
	$('#toggleFormCoupon').click(function(event){
		event.preventDefault();
		$(this).hide();
		$('#formCoupon').show();
	});
	$.address.init(function(event) {
        if($.address.value() != '/'){
        	$.get($.address.value(), function(data){
				$('#shop').html((data.main));
				$('#ajaxFilterForm').html((data.filters));
				updateFilters();
			});
        }
    }).change(function(event) {
    	if(ajaxTimer == 0){
    		ajaxTimer = Math.round(new Date().getTime());
    	}else{
	    	ajaxTimer = Math.round(new Date().getTime());
		    var url = event.value;
		    if(event.value == '/'){
		    	url = window.location.pathname + '?time=' + ajaxTimer;
		    }else{
		    	url += '&time=' + ajaxTimer;
		    }
		    	
			$.get(url, function(data){
				if(ajaxTimer == data.time){
					$('#shop').html((data.main));
					$('#ajaxFilterForm').html((data.filters));
					updateFilters();
					$.scrollTo($('#shop'));
				}
			});
    	}
    	return false;
    });
	    	
	var config = {    
	     over: makeTall, // function = onMouseOver callback (REQUIRED)    
	     interval: 500, // number = milliseconds delay before onMouseOut    
	     out: makeShort // function = onMouseOut callback (REQUIRED)    
	};
	
	$("#nav li").hoverIntent( config )
});

function updateFilters(){
	$(".filterprice").aPartHide({ 
  		showHTML: 'Toon alles',
  		hideHTML: 'Toon minder',
  		hidingTriggerLength: 5,
  		hideToLength: 5
  	});
	
	$(".filterbrand").aPartHide({ 
  		showHTML: 'Toon alles',
  		hideHTML: 'Toon minder',
  		hidingTriggerLength: 10,
  		hideToLength: 10
  	});
	//location.href = '#shop';
}
var ajaxTimer = 0;
function submitFilterForm(){
	$.address.value(window.location.pathname + "?" + $('#filterForm').serialize());
}

function makeTall(){
	$(this).addClass('visible');
}

function makeShort(){
	$(this).removeClass('visible');
}

function plusone_vote( obj ) {
	_gaq.push(['_trackEvent','plusone',obj.state]);
}
