$(document).ready(function() {
	$("ul.productmenu li").click(function(){
	
	});
	
	$("ul.menu li").mouseover(function(){
		$(this).addClass("Active");
		$(this).children("ul").addClass("hover");
	});
	$("ul.menu").children("li").mouseout(function(){
		$(this).removeClass("Active");
		$(this).children("ul").removeClass("hover");
	});
	$("ul.products").children("li:odd").addClass("odd");
	$("ul.products").children("li:even").addClass("even");
	
	$("ul.paging li:odd a").addClass("c1");
	$("ul.paging li:even a").addClass("c2");
	                    
	$("ul.cycle li:odd").addClass("odd");
	$("ul.cycle li:even").addClass("even"); 
	/* ************************* General ******************** */	
	
	setTimeout("loadHeightFunctions()" , 300);
	
	$("a").css("cursor", "pointer");
	
	
	if($("div.timer u").length > 0){
		time 	= parseInt($("div.timer u").html());
		obj 	= $("div.timer u");
		formObject = $("form[name=paymentForm]");
		startTimer();
	}
	$("input.zoekbox").focus(function(){
		
		if ($(this).hasClass("onopvallend")){
			$(this).val("");
			$(this).removeClass("onopvallend");	
		} else{
			$(this).select();	
		}	
	});

	$("span.moreOptions").click(function(){
		$(this).next().next().toggle("fast");
		$(this).next().show();
		$(this).hide();
	});
	$("span.lessOptions").click(function(){
		$(this).next().toggle("fast");
		$(this).prev().show();
		$(this).hide();
	});

	$("input[type=button]").addClass("submit");
	$("input[type=button]").mouseover(function(){ 	$(this).addClass("hover");	});
	$("input[type=button]").mousedown(function(){ 	$(this).addClass("click");	});
	$("input[type=button]").mouseout(function(){ 		$(this).removeClass("hover"); $(this).removeClass("click");	});


	$("input[type=submit]").addClass("submit");
	$("input[type=submit]").mouseover(function(){ 	$(this).addClass("hover");	});
	$("input[type=submit]").mousedown(function(){ 	$(this).addClass("click");	});
	$("input[type=submit]").mouseout(function(){ 		$(this).removeClass("hover"); $(this).removeClass("click");	});
	
	$("div.one input.submit").click(function(){
		if($("div.one div.moreOptions:visible").length == 0){
			$("div.moreOptions").remove();
		}
		this.form.submit();
	});
	
	$("select[name=numResults]").change(function(){ this.form.submit(); });
	
	$("ul.fontsizeswitcher li").customFadeIn("fast");
	
	// wysiwyg editor gives images a border
	$("img").each(function(){
		if($(this).attr("border") != ''){
			$(this).css("border-width" , $(this).attr("border").replace("px","") + "px");
			$(this).attr("border" , "");
		}
		if ($(this).attr("hspace") != ''){
				$(this).css("padding-left" 		, $(this).attr("hspace") + "px");
				$(this).css("padding-right" 	, $(this).attr("hspace") + "px");
				$(this).attr("hspace" , "");
		}
		if ($(this).attr("vspace") != ''){
				$(this).css("padding-top" 		, $(this).attr("vspace") + "px");
				$(this).css("padding-bottom" 	, $(this).attr("vspace") + "px");
				$(this).attr("vspace" , "");
		}
	});
	
	/* ************************* Shop ******************** */
	$("form[name=shopToolbox] select").change(function(){
		this.form.submit();	
	});
	
	/* **************** product scroll **************** */	
	if($("div.relatedProducts").length > 0){
		timerId = setTimeout(function(){								
			$("div.relatedProducts ul").slideDown(2000);
		}, 1000);								
		
		
		$("a.thickbox").each(function(){ 
			$(this).prepend('<div class="thickboxSpyglass"></div>'); 
			$(this).children('div').show("slow"); 
		});
		if ($("div.relatedProducts li").length > 4){
			
			$("div.relatedProducts").mouseover(function(){
				$("div.relatedProducts div.last").show();
				$("div.relatedProducts div.first").show();
				stopSlideShow();
			});
			$("div.relatedProducts").mouseout(function(){
				$("div.relatedProducts div.last").hide();
				$("div.relatedProducts div.first").hide();
				
				direction = 'first';
				slideTime = 4000;
				startSlideShow();
			});
			startSlideShow();
			/*
			$("div.relatedProducts").append('<div class="first"></div><div class="last">');
			
			$("div.relatedProducts div.first").mouseover(function(){
				direction = 'last';
				slideTime = 1000;
				startSlideShow();
				
			});
			
			$("div.relatedProducts div.last").mouseover(function(){
				direction = 'first';
				slideTime = 1000;
				stopSlideShow();
				startSlideShow();
			});
			*/
		}
	}
	/* ***************************************************** */
	/* ******************** Sitemap************************* */
	/* ***************************************************** */
	$("div.sitemap ul li a").each(function(){
		$(this).attr("onclick" , "return sitemapClick();");
	});
	$("div.sitemap a[name=expandAll]").click(function(){
		$("div.sitemap").find("ul.productmenu").find("li").each(function(){
				if($(this).children("ul").length > 0){
					$(this).addClass("active").show();
				}
		});
	});
	$("div.sitemap a[name=collapseAll]").click(function(){
		$("div.sitemap ul.productmenu li").removeClass("active").children("ul").slideUp("slow");
	});
	
	$("div.sitemap ul li a").click(function(){
		if ($(this).hasClass("selected")){
			//document.location = $(this).attr("href");
		} else {
			$(this).parent().prevAll().removeClass("active").children("ul").slideUp("slow").parent().prevAll().removeClass("active").children("ul").slideUp("slow").children("li").children("ul").slideUp("slow");
			$(this).parent().nextAll().removeClass("active").children("ul").slideUp("slow").parent().nextAll().removeClass("active").children("ul").slideUp("slow").children("li").children("ul").slideUp("slow");;
			$(this).parent().find("li").removeClass("active");
			$("div.sitemap ul li a").removeClass("selected");	
			$("div.sitemap ul li a.viewLink").remove();
			
			$(this).after('<a href="' + $(this).attr("href") +'" title="'+ $("div.sitemap span").html() + '" class="selected viewLink">' + $("div.sitemap span").html() + '&nbsp;&raquo;</a>');
			
			$(this).addClass("selected");
			if($(this).parent().children("ul").length > 0){
				$(this).parent().children("ul").slideDown("slow");    
				$(this).parent().addClass("active");
			}
		}
		return false;
	});
	$("div.sitemap ul li a").dblclick(function(){
		document.location = $(this).attr("href");
	});
	
	/* ***************************************************** */
	/* ******************** 360degrees ********************* */
	/* ***************************************************** */
	$("div#map img.selectroom").mouseover(function(){
		if ($(this).attr("animating") != 'on'){
			$(this).animate({ opacity: 1 } , 1000 );
			
			$(this).nextAll("img").animate({ opacity: 0 } , 100 );
			$(this).prevAll("img").animate({ opacity: 0 } , 100 );
			
			$(this).queue(function(){
				$(this).attr("animating" , "off");
				$(this).dequeue();
			});
		}
	});
	/*
	$("area.thickbox").click(function(){
		$("body").append('<div class="thickbox"><table><tr><td><iframe src="' + $(this).attr("href") + '"></iframe></td></tr></div>');
		return false;
	});
	*/
	
	/*
	$("div#map img.selectroom").mouseout(function(e){	
			if ($(this).attr("anmimating")  != 'on'){
				$(this).attr("anmimating" , "on");
				$(this).animate({ opacity: 0 } , 100 );
				
				$(this).queue(function(){
					$(this).attr("anmimating" , "off");
					$(this).dequeue();
				});
			}
		}
	});
	*/
	
	
	
	/* ***************************************************** */
	/* ******************** Winkelwagen ******************** */	
	/* ***************************************************** */
	var subtotal;
	$("div.shoppingcart div.price input").keyup(function(){
			if($(this).val() + 1 > 0){
				
				amount= $(this).val();
				price = $(this).parent().children("input[type=hidden]").val();
				
				$(this).parent().children("label.subtotal").html("&euro; " + formatAsMoney((amount*price)/100).replace("." , ","));	
				
				updateTotals();
				$.get("/ajax/change_shoppingcart_amount.php?product=" + $(this).attr("name") + "&amount=" + amount);
			} else{
				this.value = parseInt($(this).val()) + 0;
			}
	});
	
	$("div.shoppingcart a.remove").click(function(){
		if(confirm($(this).attr("title"))){
			$.get("/ajax/remove_shoppingcart_product.php?product_id=" + $(this).parent().parent().attr("id").replace("p_",""));
			
			if($(this).parent().parent().parent().children("li").length <= 1){
				$("div.relatedProducts ul li." + $(this).parent().parent().parent().parent().attr("class") ).remove();
				$(this).parent().parent().parent().parent().remove();
			} else {
				$(this).parent().parent().remove();
			}
			if ($("div.shoppingcart form ul li").length == 0){
				document.location = document.location;	
			}
			updateTotals();
		}
	});
	
	/* ***************************************************** */
	/* ******************** Order			 ********************* */	
	/* ***************************************************** */
	
	$("div.order table td").each(function(){
		$(this).html($(this).html().replace('*','<span class="required">*</span>'));
	});
	
	$("div.order table th a[name=continue]").click(function(){
		$("form").each(function(){
			if($(this).attr("name") == "orderProducts"){
				$(this).find("input[type=text]").each(function(){
					if($(this).parent().prev().html().indexOf("*") > -1 && $(this).val().length < 1){
						$(this).parent().parent().addClass("required");
					}else if($(this).attr("name") == 'email' && !emailCheck($(this).val())){
						$(this).parent().parent().addClass("required")
					} else {
							$(this).parent().parent().removeClass("required");
					}
				});
				if ($(this).find("tr.required").length == 0){
						this.submit();
				}
			}
		});
	});
	
	$("div.order a[name=confirm]").click(function(){
		if(confirm($(this).attr("title"))){
			$("form[name=orderProducts]").submit();
		}
	});
	
	/* ***************************************************** */
	/* ******************** Forms 			 ******************* */
	/* ***************************************************** */
	error = false;
	$("input[type=submit]").click(function(){
		
		error = false;
		frm = this.form;
		
		$(frm).find("input,textarea").each(function(){
			if ($(this).attr("name") == 'email' && !emailCheck($(this).val())){
				error = true;
				$(this).parent().parent().children().css('color', 'red');
			} else if ($(this).parent().parent().html().indexOf("*") > -1 && $(this).val().length == 0){
				error = true;
				$(this).parent().parent().children().css('color', 'red');
			} else {
				$(this).parent().parent().children().css('color', '');
			}	
		});
		
		if (!error){
			if ($(this).next("input[type=hidden]").length > 0){
				if($(this).next("input[type=hidden]").val().length > 0){
					if (confirm($(this).next("input[type=hidden]").val())){
						return true;
					}
				} else {
					return true;
				}
			}else {
				return true;
			}
		}
		return false;
	});
	
	/* ***************************************************** */
	/* ******************** Google maps		  **************** */
	/* ***************************************************** */
	if($("div.google_maps").length > 0){
		
		setTimeout(function(){
			GEvent.trigger(marker, "click");
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			
			$(".gmnoprint").each(function(){
				$(this).customFadeIn(1000);				
			});
			$("div#map_canvas").find("div.gmnoprint").customFadeIn(1000);
		}, 400);
		
	}
	/* ***************************************************** */
	/* ******************** Search Results  **************** */
	/* ***************************************************** */
	$("ul.searchResults li").click(function(){
		if ($(this).children("a.viewLink").attr("href") != ''){
			document.location = $(this).children("a.viewLink").attr("href");
		}
	});

	/* ***************************************************** */
	/* ******************** offer rightside **************** */
	/* ***************************************************** */
	$("div.three ul.offers li:first").css("visibility" , "visible");

	if ($.browser.msie === false || parseInt($.browser.version) > 6){
		$("div.three ul.offers li a.productImg span").each(function(){
			backImg = $(this).css("background-image").replace(".gif" , ".png");	
			$(this).css("background-image" , backImg);
		});
	}
	$("div.three ul.offers li").each(function(){
		if($(this).children("a.productImg").height() > 20){
			h = parseInt($(this).children("a.productImg").height());
			$(this).children("a.productImg").height(h-3);
			$(this).children("p").height(h-13);
			
		} else {
			if ($(this).children("p").height() > 40){
				$(this).children("a.productImg").height($(this).children("p").height())
			} else {
				$(this).children("a.productImg").height(40);
				$(this).children("p").height(30);
			}
		}
	});

	if($("div.three ul.offers li").length > 1){
		$("div.three ul.offers li:first").nextAll().hide().css("visibility" , "visible").css("display" , "none");
		timerId = setTimeout("startOffershow()" , 10000);
	}	
	/* ***************************************************** */
	/* ******************** end of jquery******************* */
	/* ***************************************************** */
	$("div.gmnoprint").remove();
});

function loadHeightFunctions(){
if (window.screen.availHeight > $("div.canvas").height()){
		$("div.overlay").append('<div class="borderBotom"></div>');	
	}
	if ($("div.one").height() < 450){
			$("div.one").height(450);
	}
	if ($("div.musicKey").height() < 450){		
			$("div.musicKey").height(450);
	}
	
	
	if ($("div.one").height() > $("div.large").height()){
		$("div.large").height($("div.one").height());
	}	
}

function sitemapClick(){
	return false;
}

var time;
var obj;
var formObject;

function startTimer(){
	if (time > 1){
		time--;
		$(obj).html(time);
		clearTimeout(timerId);
		timerId = setTimeout("startTimer()" , 1000);
	} else{
		$(formObject).find("input[type=submit]").click()
		//formObject.submit();
	}
}

direction = 'first';
slideTime = 4000;
var timerId = 0;
var widthLi = 0;

function startSlideShow(){
	if($("div.relatedProducts li").length > 6){
		timerId = setTimeout("moveSlideShow()" , slideTime);
	}
}
function moveSlideShow(){
	 if(timerId) {
      clearTimeout(timerId);
      timerId  = 0;
   }
   
   widthLi = $("div.relatedProducts li:"+direction).width();

   if (direction == 'first'){
   	
	  $("div.relatedProducts li:first").animate({
				width:0,
				opacity: 0
			}, slideTime/2);
			$("div.relatedProducts li:first").queue(function () {
				$(this).appendTo($("div.relatedProducts ul"));
				$(this).attr("style" , "");
				$(this).dequeue();
			});
		}else{
			
			$("div.relatedProducts li:last").css("width" , "0px");
			$("div.relatedProducts li:last").css("opacity" , "0");
			$("div.relatedProducts li:last").prependTo($("div.relatedProducts ul"));
			
			$("div.relatedProducts li:first").show(2000);
			$("div.relatedProducts li:first").animate({	
				width: widthLi,
				opacity: 1
			}, slideTime/2);
		}
		
		timerId = setTimeout("moveSlideShow()", slideTime);
}

function stopSlideShow(){
	if(timerId) {
      clearTimeout(timerId);
      timerId  = 0;
   }	
}

function startOffershow(){
	clearTimeout(timerId);
	$("div.three ul.offers li:first").customFadeOut(2000);
	$("div.three ul.offers li:first").next().hide().css("visibility" , "visible");
	$("div.three ul.offers li:first").queue(function () {
		$(this).next().customFadeIn("slow");
		$(this).appendTo($("div.three ul.offers"));	
		$(this).dequeue();
	});

	
	timerId = setTimeout("startOffershow()" , 10000);
}


function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + ',-&nbsp;' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

function updateTotals(){
	subtotal = 0;
	$("div.shoppingcart div.price input[type=text]").each(function(){
		subtotal += $(this).val() * $(this).parent().children("input[type=hidden]").val();
	});
	$("div.totals span").html(formatAsMoney(subtotal/100).replace("." , ","));	
}


(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

/* ******************************************************************************************* */
/* ****************************** FORM FUNCTIONS ********************************************* */
/* ******************************************************************************************* */

function emailCheck(addres) 
	{
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if ((addres.length) > 0)
		{
			if (!filter.test(addres))
			{
				return false;
			};
		};
		return true
	}

	function pcNumberCheck(input) 
	{
		var filter  = /^([0-9])+$/;
		if ((input.length) == 4)
		{
			if (!filter.test(input))
			{
				return false;
			};
			return true;
		};
		return false;
	}

	function pcLetterCheck(input) 
	{
		var filter  = /^([a-zA-Z])+$/;
		if ((input.length) == 2)
		{
			if (!filter.test(input))
			{
				return false;
			};
			return true;
		};
		return false;
	}

	function alphaCheck(input) 
	{
		var filter  = /^([a-zA-Z])+$/;
		if (!filter.test(input))
		{
			return false;
		};
		return true;
	}

	function numberCheck(input) 
	{
		var filter  = /^([0-9])+$/;
		if (!filter.test(input))
		{
			return false;
		};
		return true;
	}

	function numberCheckDay(input) 
	{
		var filter  = /^([0-9])+$/;
		if (!filter.test(input))
		{
			alert(input);
			return false;
		};

		if (input/1 <= 0) return false;
		if (input/1 > 31) return false;


		return true;
	}
	function numberCheckMonth(input) 
	{
		
		if ((input.length) > 2) return false;
		if ((input.length) < 0) return false;

		var filter  = /^([0-9])+$/;
		if (!filter.test(input))
		{
			return false;
		};
		if ((parseInt(input)/1) <= 0) return false;
		if ((parseInt(input)/1) >= 13) return false;

		return true;
	}
	function numberCheckYear(input) 
	{
		var filter  = /^([0-9])+$/;
		if ((input.length) != 4) return false;
	
		if (!filter.test(input))
		{
			return false;
		};

		if ((parseInt(input)/1) <= 1900) return false;
		if ((parseInt(input)/1) >= 2000) return false;

		return true;
	}


	function yearCheck(input) 
	{
		var filter  = /^([0-9])+$/;
		if ((input.length) != 4) return false;
		if (!filter.test(input))
		{
			return false;
		};
		if ((parseInt(input)/1) <= 2000) return false;
		return true;
	}

	function phoneCheck(input) 
	{
		var filter  = /^([0-9])+$/;
		input = input.replace('-','');
		if ((input.length) == 10)
		{
			if (!filter.test(input))
			{
				return false;
			};
			return true;
		};
		return false;
	}
	
	function elf_proef(bankrekeningnummer) 
	{
		if ((bankrekeningnummer.length) < 9) 
		{
			var filter  = /^([0-9])+$/;
			if (!filter.test(bankrekeningnummer))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			bankrekeningnummer=bankrekeningnummer.replace(/\D/, "");
			aantal_tekens=bankrekeningnummer.length;
			var som=0;
			// loop door de 9 cijfers met de 11 proef formule
			for (i=1; i<10; i++) 
			{
				getal=bankrekeningnummer.charAt(i-1);
				som+=getal*(10-i);
			} 
			// geef resultaat van check terug
			if (som % 11==0 && aantal_tekens==9) 
			{
				return true
			} 
			else 	
			{
				return false
			}
		}
	}
	function checkEerder(){
		if(emailCheck($("input#email").val())){
			document.getElementById("eerderMeegedaan").submit();
		} else {
			alert("U dient een geldig e-mail adres in te vullen");
		}
	}