// Replace all major headers with Cufon
Cufon.replace("h1, h2, h3, h4, #menu ul.level1>li>span, #cart>div.title, #testimonials div.testimonial div.quote, #testimonials div.testimonial div.author");

// ELEMENTEN GELIJKE HOOGTE
	function equalHeight(obj){
		topHeight = 0;
		obj.each(function(){
			topHeight = ($(this).height() > topHeight ? $(this).height() : topHeight)
		});
		obj.height(topHeight);
	}

$(window).load(function(){
	equalHeight($("div.groupProduct"));
});

$(function() {
	$("div.groupProduct:nth-child(2)").addClass("top");
	$("div.groupProduct:nth-child(3)").addClass("top");
	$("div.groupProduct:nth-child(4)").addClass("top");
	$("div.groupProduct:nth-child(3n+1)").addClass("third");
});

// Initialize Shadowbox
Shadowbox.init({
	language:	"nl",
	players:	["img"],
	onFinish: function() {
		Cufon.replace("#sb-title-inner");
		$("#sb-title-inner").fadeIn();
	}
});



// Apply jCarousel to crossSell
$(function() {
	$("#crossSell>ul").jcarousel({
		scroll: 1,
		animation: "slow",
		auto: 10,
		wrap: "both",
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: crossSellCallback,
		visible: 3
	});
});

function crossSellCallback(crossSellCarousel) {

	$('#crossSellContainer>div.scrollerRight').bind('click', function() {
		crossSellCarousel.next();
		return false;
	});

	$('#crossSellContainer>div.scrollerLeft').bind('click', function() {
		crossSellCarousel.prev();
		return false;
	});
}

// Mouse-over + click-effect for filters
$(function() {
	$("#filter div.filterContainer").click(function(e) {
		if ($(this).hasClass("clicked")) {
			$(this).next("div.filter").slideUp();
			$(this).removeClass("clicked");
		} else {
			$(this).parent().find("div.filterContainer.clicked").next("div.filter").slideUp("fast").end().removeClass("clicked");
			$(this).addClass("clicked");
			$menu = $(this);
			$(this).next("div.filter").css({
				"top": $menu.position().top + ($menu.outerHeight() - 1),
				"left": $menu.position().left,
				"marginLeft": $menu.css("marginLeft")
			}).show();
		}
		return false;
	});


	$("#filter div.filterContainer").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});

	$("#filter div.filter").click(function(e) {
		e.stopPropagation();
		return ;
	});

	$("#filter input:checkbox").change(function() {
		commitFilter();
		
	});

	$(document).click(function(e) {
		$("#filter div.filterContainer").removeClass("clicked").next("div.filter").slideUp();
	});

});

function commitFilter(){
		var cFilter = "";

		var nLength = $("#filterGeslachtMan").parent().children(":checked").length;
		if (nLength > 0 && nLength < 3) {
			cFilter += ($("#filterGeslachtMan").is(":checked") ? (cFilter == "" ? "" : ", ") + "geslacht:man" : "");
			cFilter += ($("#filterGeslachtVrouw").is(":checked") ? (cFilter == "" ? "" : ", ") + "geslacht:vrouw" : "");
			cFilter += ($("#filterGeslachtUni").is(":checked") ? (cFilter == "" ? "" : ", ") + "geslacht:unisex" : "");
		}


		var nLength = $("#filterPolarizedJa").parent().children(":checked").length;
		if (nLength > 0 && nLength < 2) {
			cFilter += ($("#filterPolarizedJa").is(":checked") ? (cFilter == "" ? "" : ", ") + "polarized:ja" : "");
			cFilter += ($("#filterPolarizedNee").is(":checked") ? (cFilter == "" ? "" : ", ") + "polarized:nee" : "");
		}


		var nLength = $("#filterTransitionJa").parent().children(":checked").length;
		if (nLength > 0 && nLength < 2) {
			cFilter += ($("#filterTransitionJa").is(":checked") ? (cFilter == "" ? "" : ", ") + "transition:ja" : "");
			cFilter += ($("#filterTransitionNee").is(":checked") ? (cFilter == "" ? "" : ", ") + "transition:nee" : "");
		}


		var nLength = $("#filterVentedJa").parent().children(":checked").length;
		if (nLength > 0 && nLength < 2) {
			cFilter += ($("#filterVentedJa").is(":checked") ? (cFilter == "" ? "" : ", ") + "vented:ja" : "");
			cFilter += ($("#filterVentedNee").is(":checked") ? (cFilter == "" ? "" : ", ") + "vented:nee" : "");
		}

		var nLength = $("#filterMontuurKunststof").parent().children(":checked").length;
		if (nLength > 0 && nLength < 2) {
			cFilter += ($("#filterMontuurKunststof").is(":checked") ? (cFilter == "" ? "" : ", ") + "montuur:kunststof" : "");
			cFilter += ($("#filterMontuurMetaal").is(":checked") ? (cFilter == "" ? "" : ", ") + "montuur:metaal" : "");
		}

		var vx=''; var vMinVal=0; var vMaxVal=0;
		var nLength = $("#slider").length;
		if (nLength > 0){
			vMinVal = $("#slider").slider("values", 0);
			vMaxVal = $("#slider").slider("values", 1);
		}
		if(vMinVal>0){ vx += '/minprice=' + vMinVal;}
		if(vMaxVal>0){ vx += '/maxprice=' + vMaxVal;}
		document.location = $.actual.serverpad + "pageid=" + $.actual.pageid + "/filter=" + cFilter + vx + "/" + $.actual.pagetitle + ".html";

}

// FAQ 
$(function() {
	$("ul.faq li").click(function() {
		if ($(this).hasClass("selected")) {
			return ;
		}
		$(this).parent().find("li.selected div").slideUp().parent().removeClass("selected");
		$(this).find("div").slideDown();
		$(this).addClass("selected");
	});
});



// Show color selectors 
$(function() {
	$("#customize .dropdownOver").click(function() {
		$colors = $(this).parent().next();
		if ($colors.hasClass("selected")) {
			$colors.slideUp("slow").removeClass("selected");
		} else {
			$("#customizeKleur .colorDropdown").slideUp("slow").removeClass("selected");
			$colors.slideDown("slow").addClass("selected");
		}
	});

	// Choosing color
	$("#customizeKleur .chooseColor").click(function() {
		$(this).parent().prev().css("backgroundColor", $(this).find(".colorImage").css("backgroundColor"));
		$("#" + $(this).parents(".color").attr("lang")).val($(this).find(".colorImage").attr("lang"));
		$(this).parent().slideUp("slow").removeClass("selected");
	});

	// Choosing shape
	$("#customizeVorm .shape a").click(function() {
		$("#" + $(this).parents(".customize").attr("lang")).val($(this).attr("lang"));
		$(this).parents(".customize").find("a.selected").removeClass("selected");
		$(this).addClass("selected");
		return false;
	});

	// Choosing font 
	$("#customizeExtra .colorDropdown .chooseColor").click(function() {
		$(this).parent().prev().css("backgroundColor", $(this).find(".colorImage").css("backgroundColor"));
		$(this).parent().prev().find(".dropdownOver span").text($(this).find(".colorTitle").text());
		$("#" + $(this).parents(".color").attr("lang")).val($(this).find(".colorImage, .colorTitle").attr("lang"));
		
		$(this).parent().slideUp("slow").removeClass("selected");
	});


	$("#customizeVormMenu").click(function() {
		clearCustomizeMenu($(this));
		$("#customizeVorm").slideDown("slow").addClass("selected");
		return false;
	});

	$("#customizeKleurMenu").click(function() {
		clearCustomizeMenu($(this));
		$("#customizeKleur").slideDown("slow").addClass("selected");
		return false;
	});

	$("#customizeExtraMenu").click(function() {
		clearCustomizeMenu($(this));
		$("#customizeExtra").slideDown("slow").addClass("selected");
		return false;
	});
});

function clearCustomizeMenu($obj) {
	$("#customizeMenu a").removeClass("selected");
	$("#customize .customize.selected").slideUp("slow").removeClass("selected");
	$obj.addClass("selected");
}

$(function() {
	// Move submenu to right position
	$("ul.level3").appendTo($("ul.level2 li.selected"));

	// Remove last pipe | of menu
	$("#topmenu2 div.divider:last").remove();

	$lastBottomMenu = $("#bottom .menu li:last");
	$lastBottomMenu.html($lastBottomMenu.html().replace("|", ""));

});



// Terug-links toevoegen 
$(function() {
	$("div.terug a").click(function() {
		history.go(-1);
		return false;
	});
	
	$("#orderCart #back").click(function() {
		history.go(-1);
		return false;
	});
});


// Default value voor input-velden toevoegen
$(function() {
	$("#search .text").defaultValue();
});

$(function() {
	// Afwijkend afleveradres wel / niet tonen
	$("#afwijkendAfleveradres").toggle(function() {
		$(this).removeClass("off");
		$(this).addClass("on");
		$("#sbsAfwijkendAfleveradres").val("true");
		$("#afwijkendAfleveradresForm").slideDown();
		$("#sbsLandOverigDiv em").hide();
		$("#sbsAflLandOverigDiv em").hide().show();
	}, function() {
		$(this).removeClass("on");
		$(this).addClass("off");
		$("#sbsAfwijkendAfleveradres").val("false");
		$("#afwijkendAfleveradresForm").slideUp();
		$("#sbsLandOverigDiv em").show();
	});

	// Keuze voor land wel / niet tonen
	$("#sbsLand").change(function() {
		$overig = $("#sbsLandOverigDiv");
		if ($(this).val() == "Nederland") {
			$overig.slideUp("normal");
		} else {
			$overig.slideDown("normal");
			if ($("#afwijkendAfleveradres").hasClass("off")) {
				$overig.find("em").hide().show();
			}
		}
	}).change();

	// Keuze voor land wel / niet tonen (afleveradres)
	$("#sbsAflLand").change(function() {
		$overig = $("#sbsAflLandOverigDiv");
		if ($(this).val() == "Nederland") {
			$overig.slideUp("normal");
		} else {
			$overig.slideDown("normal");
			$overig.find("em").hide().show();
		}
	}).change();
});


// Show small cart functions
$(function() {

	// Remove product 
	$("#cart div.product").hoverIntent(function() {
		$(".remove", $(this)).fadeIn();
	}, function() {
		$(".remove", $(this)).fadeOut();
	});

	$(".remove a").click(function(event) {
		event.preventDefault();
		jConfirm("Weet je zeker dat je dit product uit je winkelwagen wilt verwijderen?", "Verwijderen", function(result) {
			if (result) {
				document.location = ($($(event).attr("currentTarget")).attr("href"));
			}
		});
	});
	
	// Show custom product
	$("#cart div.product .title a").click(function() {
		$product = $(this).parents(".product").find(".customProduct")
		if ($product.is(":visible")) {
			$product.hide("slow");
		} else {
			$(".customProduct").hide("slow");
			$product.show("slow");
		}
		return false;
	});

	// Close custom product
	$(".customProduct .close").click(function() {
		$(this).parents(".product").find(".customProduct").hide("slow");
		return false;
	});

});

// Recalculate custom prices
function recalculateCustomPrice() {
	
	var totalcost = parseFloat($("#defaultprice").val());
	for(var j=0;j<=50;j++) {
		$item = $("#questioncost" + j);
		if ($item.length && $item.val() != "") {
				totalcost = totalcost + parseFloat($("#questioncost" + j).val());
		}
	}
	totalcost = totalcost + ",-";
	totalcost = "&euro; " + totalcost.replace(",00");
	$("#totalpricelabel").html(totalcost);
	Cufon.replace("#totalpricelabel");
}

$(function() {
	$("#orderCart .amount img").click(function() {
		$(this).parents("form").submit();
	});
});

// Align catalogue
$(function() {
	$("ul.catalogus>li, .sitemap .menu").each(function(index) {
		if (index % 3 == 0) {
			$(this).css({"clear": "both"});
		}
	});

	var height = 0;
	$li = $("ul.catalogus li");
	$li.each(function(index) {
		if ($(this).height() >= height) {
			height = $(this).height();
		}
		if (index % 3 == 2) {
			if (index > 0) {
				$li.filter(function(itemIndex) {
					return itemIndex >= index - 2 && itemIndex <= index; 
				}).css({"min-height": height});
			}
			$(this).css({"border-right": 0});
			height = 0;
		}

		if (index > $li.length - 4) {
			$(this).css({"border-bottom": 0});
		}

	});
});


// Generic Actual function

function genadres(ahref,stijl){
	ahref=ahref.replace('\%x.\%x','@');
	ahref=ahref.replace('\$.\$','mailto:');
	document.write('<a '+ahref+' class='+stijl+'>');
}
function genadres2(ahref){
	ahref=ahref.replace('\%x.\%x','@');
	ahref=ahref.replace('\$.\$','mailto:');
	document.write(ahref);
}


//. General alerts-style
$.alerts.overlayColor ='#000';
$.alerts.overlayOpacity = .75;
$.alerts.okButton  =  '';
$.alerts.cancelButton =  '';
$.alerts.draggable =  false;
