$(function(){
	// LetterSize:
	$("#letterSize a.normal").click(function(){ 
		$(this).blur();
		createCookie('letterSize', 'normal', 14);
		$("html").animate({fontSize: "10pt"}); 
		return false;
	});
	$("#letterSize a.large").click(function(){ 
		$(this).blur();
		createCookie('letterSize', 'large', 14);
		$("html").animate({fontSize: "12pt"}); 
		return false;
	});
	$("#letterSize a.extralarge").click(function(){ 
		$(this).blur();
		createCookie('letterSize', 'extraLarge', 14);
		$("html").animate({fontSize: "14pt"}); 
		return false;
	});
	
	var letterSize = readCookie('letterSize');
	if(letterSize!=null) {
		switch(letterSize) {
			case 'normal':
				$("html").css({fontSize: "10pt"});
				break;
			case 'large':
				$("html").css({fontSize: "12pt"});
				break;
			case 'extraLarge':
				$("html").css({fontSize: "14pt"});
				break;
		}
	}
	
	// Bestellen (max 2 producten):
	$("fieldset.proefpakket input").click(function(){
		var obj = $("fieldset.proefpakket input:checked");
		if(obj.length>=2) {
			// Disable alle andere buttons:
			$("fieldset.proefpakket input").not(":checked").attr("disabled", "disabled");
		} else {
			// Enable alle andere buttons:	
			$("fieldset.proefpakket input").removeAttr("disabled");
		}
	});
	
	// FAQ:
	// $("#faq p").hide();
	$("#faq div").hide();
	$("#faq ul ul").hide();
	// Lege paragraven verwijderen:
	$("#faq ul ul li:not(:has(p))").remove();
	// Lege unordered lists verwijderen:
	$("#faq ul ul:not(:has(li))").remove();
	// Lege list-items verwijderen:
	$("#faq li:not(:has(ul))").css({color: "#ccc"});
	// Loop:
	$("#faq ul li:has(ul)").each(function(){
		$(this).css({cursor: "pointer"});
		$(this).click(function(){
			$("#faq li").removeClass("active");
			$(this).addClass("active");
			$("#faq li:not(.active) ul").slideUp();
			$("#faq li.active ul").slideDown();
		});

		$("li", this).css({color: "#666"}).each(function(){
			$(this).click(function(){
				if(!$("p", this).hasClass("active")) {
					$("#faq p").removeClass("active");
					$("p", this).addClass("active");
					// $("#faq p:not(.active)").slideUp();
					// $("#faq p:not(.active)").hide();
					// $("p.active", this).slideDown();
					
					$("#faq div").not($("div", this)).slideUp();
					// $("div", this).slideDown();
					// alert($(this).html());
					// $("p.active", this).show();
					
					$("div", this).show();
					var h = $("div", this).height();
					$("div", this).css({height: 0});
					$("div", this).animate({height: h});
					
					// $("p.active", this).slideDown();
				}
			}).hover(function(){
				$(this).css({color: "#0099dd"});
			}, function(){
				$(this).css({color: "#666666"});
			});
		});
	});
	var id = getQueryVariable('faq');
	if(id!=undefined) {
		$("li#faq_"+id+" p").show().parent().show().parent().show().parent().show();	
	}
	
	
	// Beslisboom:
	$("#beslisboom div.question").hide();
	$("#beslisboom a").each(function(){
		var href = $(this).attr("href");
		if(href!=undefined) {
			if(href!="#") {
				if(href.charAt(0)=="#") {
					$(this).click(function(){
						var a = href.split("#");					
						var container = $(this).parent().parent().parent();
						container.fadeOut(function(){
							showQuestion(a[1]);
						});
						return false;
					});	
				}
			}
		}
	});
	showQuestion("a");
	
	
	// Popup:
	// Hoogte instellen:
	if($(document).height() > $("#popup").height()) { $("#popup").height($(document).height() + 100); }
	// Voor productfoto's
	$(".thumbnail a").click(function(){
		// Tekst vullen:
		$("#frame h1").text($(".innerContent h1").text());
		// afbeelding laden:
		var filename = $(this).attr("href");
		$("#frame .image").html("<img src=\"" + filename + "\" />");
		// Afmetingen laden:
		$.get("/getimagesize.php", {file: filename}, function(data){
			a = data.split(',');			
			// scrolltop berekenen:
			var ScrollTop = document.body.scrollTop;
			if (ScrollTop == 0) {
				if (window.pageYOffset) {
					ScrollTop = window.pageYOffset;
				} else {
					ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
				}
			}
			
			// ScrollTop -= (a[1]/2);
			ScrollTop += 100;
			$("#frame").css({width: a[0] + "px", height: a[1], marginLeft: "-" + (a[0]/2) + "px", top: ScrollTop + "px"});

			$("#popup").show();
            $("#frame").show();
		});
		return false;
	});

    $("#popup:not(.beslisboom)").click(function(){
        $("#popup").hide();
        $("#frame").hide();
        $("#player").hide();
    });
	
	// Algemeen:
	$("#frame a.close").click(function(){
		$("#popup").hide();
        $("#frame").hide();
        $("#player").hide();
		return false;
	});
	
	
	
	// Plaatsnaamzoeker:
	/*
	$("fieldset.contact input[name=postcode]").keyup(function(){
		var val = $(this).val();
		if(val.length==4) {
			// Plaatsnaam erbij zoeken:
			$.ajax({
				url : 'http://6pp.kvdb.net/lookup',
				data : {postcode: val, tg_format: "json"},
				jsonp: 'onJsonPLoad',
				success : function(data) {
					alert(data);
				}
			});
		}
	});
	*/
	
	// Dropdown menu:
	
	if(navigator.userAgent.toLowerCase().indexOf('msie 6.0') == -1) {
	    // Browser is geen IE6
		$("#bottomNavigation ul li.active ul").hide().slideDown("slow");
	}

	// SWFObject:
	// swfobject.embedSWF("/assets/templates/absorin/swf/header.swf", "flashHeader", "576", "107", "9.0.0", false, {}, {}, {});
	
	// Paden oplossen (Rariteit in ModX)
	$("img").each(function(){
		if($(this).attr("src").charAt(0)!='/') {
			var src = '/' + $(this).attr("src");
			$(this).attr("src", src);
		}
	});
	
	// Inputvelden
	$('#bestelForm').submit(function(){
		var ok = true;
		$("input.is_required", this).each(function(){
			if($(this).val()=='') {
				ok = false;
				$(this).css({background: '#fcc'});
			} else {
				$(this).css({background: 'transparent'});
			}
		});
		if(!ok) {
			alert('Niet alle verplichte velden zijn ingevuld!');
		} else {
			// window.location = '/nl/consument/content/advies.html';
		}
		return ok;
	});

	
});

function showQuestion(name) {
	var container = $("#beslisboom a[name=" + name + "]").parent();
	$("#beslisboom .header h2").text($("h3", container).text());
	$("h3", container).hide();
	container.fadeIn();
	
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}


	
// Bij beslisboom:
function openPopup(naam, maat, artikelNr, ziNr, productURL, imageURL) {
	// alert(naam + ',' + maat + ',' + artikelNr + ',' + ziNr + ',' + productURL);
	// Hoogte instellen:
	if($(document).height() > $("#popup").height()) { $("#popup").height($(document).height()); }
	// Variabelen vullen:
	$("#popup span.naam").text(naam);
	$("#popup span.maat").text(maat);
	$("#popup span.artikelnr").text(artikelNr);
	$("#popup span.zinummer").text(ziNr);
	
	$("input[name=productnaam]").val(naam);
	$("input[name=maat]").val(maat);
	$("input[name=artikelnr]").val(artikelNr);
	$("input[name=zinr]").val(ziNr);
	
	// Afbeelding instellen:
	imageURL = '/assets/snippets/thumbnail/thumbnail.php?file='+imageURL+'&width=148&height=0&quality=80';
	$(".right .image").html("<img src=\"" + imageURL + "\" />");
	
	// Link instellen:
	$("a.productURL").attr("href", productURL);
	
	// Popup tonen:
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0) {
		if (window.pageYOffset) {
			ScrollTop = window.pageYOffset;
		} else {
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
	}
	
	if(maat=='') {
		$(".showmaat").hide();
	} else {
		$(".showmaat").show();
	}
	
	// ScrollTop -= (a[1]/2);
	ScrollTop += 50;
	$("#frame").css({width: "640px", marginLeft: "-320px", top: ScrollTop + "px"});
	$("#popup").show();
	
	// False retourneren:
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/*
function onJsonPLoad() {
		
}
*/
