$(document).ready(function(){
    $('#shoppingCartDisplay').click(function(e) {
        e.stopPropagation();
    });
    $('#quicksearchResults').click(function(e) {
        e.stopPropagation();
    });
	$('.lb_small').focusin(function() {
		$(this).blur();
	});
	$('.lb_small').click(function() {
		$(this).blur();
	});
});

$(document).click(function() {
    $('#shoppingCartDisplay').slideUp(200);
    $('#quicksearchResults').hide();
    $('.tipsy').fadeOut('fast');
});

jQuery(function($) {
	jQuery('body').delegate('.remindme','click',function(){jQuery.ajax({'type':'POST','dataType':'json','url':'/site/remindme','success':function(data){
		if(data.success > '') {
			$("#leverdetails_"+data.itemcode).attr("title", data.success);
	    }
		if(data.error > '') {
			$("#leverdetails_"+data.itemcode).attr("title", data.error);
		}
		$("#leverdetails_"+data.itemcode).tipsy("show");
		setTimeout(function(){ $("#leverdetails_"+data.itemcode).tipsy("hide"); },5000);
	},'cache':false,'data':jQuery(this).parents("form").serialize()});return true;});
});

var searchQuery = 'geen';
var timer;

function search(id, target, event) {
	clearTimeout(timer);
	var timer;
	if(checkSearchValid(id, event)) {
		timer = setTimeout(function(){
			$('#zoekbutton_image').attr('src','/images/layout/loading.gif');

			$.post('/zoeken', {
					term: $('#'+id).val(), quicksearch: "1"
				}, function(results) {
					$('#'+target).show();
					$('#'+target).html(results.html);
                    if (results.resultsFound == 0) {
                        _gaq.push(['_trackEvent', 'Zoekopdracht', 'NietGevonden', $('#'+id).val()]);
                    }
					searchQuery = 'geen';
					$('#zoekbutton_image').attr('src','/images/layout/vergrootglas-icoon.png');
				}, 'json'
			);

		},650);
	}
}

var lengte = false;


function checkSearchValid(id, event) {
	if ($('#'+id).val().length > 2) {
		var unicode = event.keyCode? event.keyCode : event.charCode;
		if(unicode != 116 && unicode != 16 && unicode != 20 && unicode != 17 && unicode != 116 && unicode != 32 && unicode != 13) {
			if(($('#'+id).val().length != lengte || lengte === false) && searchQuery == 'geen') {
				searchQuery = 'loopt';
				lengte = $('#'+id).val().length;
				return true;
			}
		}
	} else {
		$('#quicksearchResults').hide();
	}
	return false;
}


function switchOverzicht() {
	var display = $('#overzicht_content').css("display");
	if(display == 'block') {
		$('.bekijk_overzicht').animate({ width: "0px" }, 500, function() {
			$(this).html('bekijk het complete overzicht');
			$(this).animate({ width: "165px" }, 500 );
		});
	} else {
		$('.bekijk_overzicht').animate({ width: "0px" }, 500, function() {
			$(this).html('verberg het overzicht');
			$(this).animate({ width: "117px" }, 500 );
		});
	}
	$('#overzicht_content').slideToggle('slow');
}

var merkenOpen = false;
var themasOpen = false;
function switchDropdownContent(veld) {
	if(veld == 'merken') {
		switchMerken();
	} else if(veld == 'thema') {
		switchThemas();
	}
}
var scroll1active = false;
function switchMerken() {
	$('.alle_merken').slideToggle('slow', function() {
		if(!scroll1active) {
			$('#scrollbar1').tinyscrollbar();
			scroll1active = true;
		}
	});
	$('.top_merken').slideToggle('slow');
	if($('#show_alle_merken').html() == 'Meer...') {
		if(themasOpen == true) {
  			switchThemas();
  		}
	}
	$('#show_alle_merken').hide(400, function() {
	  	if($(this).html() == 'Meer...') {
	  		merkenOpen = true;
	  		$(this).html('Minder...');
	  	} else {
	  		merkenOpen = false;
	  		$(this).html('Meer...');
	  	}
		$(this).show(400);
	});
	return true;
}
var scroll2active = false;
function switchThemas() {
	$('.alle_themas').slideToggle('slow', function() {
		if(!scroll2active) {
			$('#scrollbar2').tinyscrollbar();
			scroll2active = true;
		}
	});
	$('.top_themas').slideToggle('slow');
	if($('#show_alle_themas').html() == 'Meer...') {
		if(merkenOpen == true) {
			switchMerken();
		}
	}
	$('#show_alle_themas').hide(400, function() {
	  	if($(this).html() == 'Meer...') {
	  		themasOpen = true;
	  		$(this).html('Minder...');
	  	} else {
	  		themasOpen = false;
	  		$(this).html('Meer...');
	  	}
		$(this).show(400);
	});
	return true;
}

function toggleLayer(whichLayer) {
    if (document.getElementById) {
        var style2 = document.getElementById(whichLayer).style;
		if(style2.display == 'block') {
			$('#'+whichLayer).hide('fast');
		} else {
			$('#'+whichLayer).show('fast');
		}
    }
}
function openOmschrijving(itemcode) {
    if (itemcode) {
        _gaq.push(['_trackEvent', 'ClickArtikelTab', 'Omschrijving', itemcode]);
    }
    var index = $('#tabs a[href="#omschrijvingtab"]').parent().index();
    $("#tabs").tabs({"selected": index});
}
function initLeverinfoHover() {
	$('.itemListView, .itemListView_Compact, .verlanglijst').hover(function() {
		$(this).addClass('hover');
		$('#leverinfo_'+$(this).attr('id')).slideDown(350);
	}, function() {
		$(this).removeClass('hover');
		$('#leverinfo_'+$(this).attr('id')).slideUp(150);
	});
}

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		return false;
	} else {
		return true;
	}
}



function submitForm() {
	window.location = '/zoeken/'+$('#zoekwoord').val();
}



function switchDiv(div) {
	$(div).slideToggle(500);
}



function emptyValue() {
	if(document.getElementById('zoekwoord').value == 'zoeken') {
		$('#zoekwoord').removeClass('zoekwoord_veld');
		$('#zoekwoord').addClass('zoekwoord_veld_klik');
		document.getElementById('zoekwoord').value = '';
	}
}
function uitvoering() {
	var link = $('#uitvoering_'+$("#select").val()).attr("href");
	if(link) {
		window.location = link;
	}
}


var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);



function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) { endstr = document.cookie.length; }
  return unescape(document.cookie.substring(offset, endstr));
}



function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal (j);
      }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
  return null;
}

function deleteCookie(name) {
	document.cookie = name+"=; expires=Thu, 01-Jan-70 00:00:01 GMT;";
}

function setCookie (name,value,expires,path,domain,secure) {
	deleteCookie(name);

	var now = new Date();
	var time = now.getTime();
	time += 3600 * 1000;
	now.setTime(time);


	document.cookie = name + "=" + escape (value) +
    "; expires="+now.toGMTString()+
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  }
