//<![CDATA[

//var sUrl = "http://93.186.179.238/~potgieter/";
var sPreload = "<img src='"+sUrl+"images/preloader.gif' style='vertical-align: middle' /> Laden...";

var iSlideshowSpeed = 7000;
var currentSlide = 1;

jQuery(function(){
		   
	jQuery.fn.flush = function(){ return jQuery(this.context).find(this.selector); }  
	//slideDownProdSelc("slide");
	/*
	positionFooter(); 
	function positionFooter(){
		if(jQuery(document.body).height() < jQuery(window).height()){
			jQuery("#footer_wrapper").css({position: "absolute",top:(jQuery(window).scrollTop()+jQuery(window).height()-jQuery("#footer_wrapper").height())+"px"})
		}	
	}
 
	jQuery(window)
		.scroll(positionFooter)
		.resize(positionFooter)
	
	*/
	



	
/*	
	var winHeight = jQuery(window).height();
	var contentHeight = jQuery("#contentwrapper").height();
	
	if(jQuery("#fetchedContent").length) {
		var contentHeight = jQuery("#fetchedContent").height();	
	}
	console.debug(contentHeight);
	var footerHeight = jQuery("#footer_wrapper").height();
	var headerHeight = jQuery("#header_wrapper").height();
	
	var dif = winHeight - (headerHeight + footerHeight);
	console.debug(dif);
	//if(dif > 0 && dif < contentHeight) { jQuery("#contentwrapper").height(dif); }
	
	function setFooter() {
		var headerHeight = 75;
		var footerHeight = 271;	
		var contentHeight = jQuery("#content").height();
		var winHeight = jQuery(window).height();
		var totalHeight = headerHeight + footerHeight + contentHeight;	
	
		
		if(winHeight <= totalHeight) {
			//console.debug('fits');	
		}
		else {
			var neededHeight = winHeight - totalHeight;
			//console.debug('reposition');
			jQuery("#content").height(neededHeight);
			//console.debug("newContentHeight: "+neededHeight);
		}
	}

		
//	console.debug(footerHeight);
//	console.debug(headerHeight);
	
	*/
	
	function countSelected() {
		var checkboxes = jQuery("input[type='checkbox'].filter:checked").length;
		var dropdown = jQuery("select.filter").length;
		var dropdowns = 0;
		var dropdown = jQuery("select.filter option:selected").each(function(){
		if(jQuery(this).val() != '') {dropdowns++;}});
		
		//alert((checkboxes + dropdowns));
		return (checkboxes + dropdowns);
	}
	
	jQuery(".ps_button").live("click",function(){
		jQuery(this).removeClass("ps_button").addClass("ps_button_selected").find(".cb").attr("src","images/ps_checkbox_selected.png");
		jQuery(this).parent().find(".subitems").slideDown('fast');
		//slideDown('fast');
	});
	
	jQuery(".ps_button_selected").live("click",function(){
		jQuery(this).removeClass("ps_button_selected").addClass("ps_button").find(".cb").attr("src","images/ps_checkbox.png");
		jQuery(this).parent().find(".subitems").slideUp('fast').find(".ps_checkbox_selected").removeClass("ps_checkbox_selected").addClass("ps_checkbox").find(".cb").attr("src","images/ps_checkbox.png").next().attr("checked",false);
		//.slideUp('fast') find("input[type='checkbox']").attr("checked",false)
	});
	
	jQuery(".ps_checkbox,.ps_checkbox_small").live("click",function(){
		jQuery(this).find("input[type='checkbox']").attr("checked",true);													   
		jQuery(this).removeClass("ps_checkbox").addClass("ps_checkbox_selected").find(".cb").attr("src","images/ps_checkbox_selected.png");
		if(countSelected()) {	
			jQuery("#submit").attr("src","images/submit_search.png").addClass("submitSelection"); 
			jQuery("#reset").attr("src","images/reset_search.png").addClass("resetSelection"); 
		}
		else { 
			jQuery("#submit").removeClass("submitSelection").attr("src","images/submit_search_inactive.png"); 
			jQuery("#reset").removeClass("resetSelection").attr("src","images/reset_search_inactive.png"); 
		}
	});
	
	jQuery("select.filter").live("change",function(){
		if(countSelected()) {	
			jQuery("#submit").attr("src","images/submit_search.png").addClass("submitSelection"); 
			jQuery("#reset").attr("src","images/reset_search.png").addClass("resetSelection"); 
		}
		else { 
			jQuery("#submit").removeClass("submitSelection").attr("src","images/submit_search_inactive.png"); 
			jQuery("#reset").removeClass("resetSelection").attr("src","images/reset_search_inactive.png"); 
		}
	});
	
	jQuery(".ps_checkbox_selected").live("click",function(){
		jQuery(this).find("input[type='checkbox']").attr("checked",false);
		jQuery(this).removeClass("ps_checkbox_selected").addClass("ps_checkbox").find(".cb").attr("src","images/ps_checkbox.png");
		if(countSelected()) {	
			jQuery("#submit").attr("src","images/submit_search.png").addClass("submitSelection"); 
			jQuery("#reset").attr("src","images/reset_search.png").addClass("resetSelection"); 
		}
		else { 
			jQuery("#submit").removeClass("submitSelection").attr("src","images/submit_search_inactive.png"); 
			jQuery("#reset").removeClass("resetSelection").attr("src","images/reset_search_inactive.png"); 
		}
	});
	
	function slideUpProdSelc(){
		jQuery("#product_selector").slideUp("fast");
		jQuery("#showhider").attr("src","images/productselector_show.png");	
	}
	
	jQuery("#showhide_product_selector").toggle(
		function(){	slideDownProdSelc("slide"); },
		function(){ slideUpProdSelc(); }
	);
	
	jQuery(".homeproductLink").live('hover',function(ev) {
										  
		if(ev.type == 'mouseover') {  
			jQuery(this).find(".itemhome").css("display","block");
		}
		if(ev.type == 'mouseout') {	  
			jQuery(this).find(".itemhome").css("display","none");				
		}
	});
	
	jQuery(".item").live('hover',function(ev) {
		if(ev.type == 'mouseover') {  
			var bg = jQuery(this).css("background-image");
			var newbg = bg.replace(".jpg","_over.jpg");
			//jQuery(this).css("background-image",newbg).find(".item_overlay").css("display","block");
			jQuery(this).find(".item_overlay").css("display","block");
		}
		if(ev.type == 'mouseout') {	  
			var bg = jQuery(this).css("background-image");
			var newbg = bg.replace("_over.jpg",".jpg");
			//jQuery(this).css("background-image",newbg).find(".item_overlay").css("display","none");	
			jQuery(this).find(".item_overlay").css("display","none");				
		}
	});

	
	jQuery("#submitDealerLogin input").live("keypress",function(e){
		if(e.which == 13){	checkFields();	}
	});
		
	
	jQuery("#submitForm").click(function(){
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if(emailReg.test(jQuery("#sendForm input[name='emailadres']").val()) && jQuery("#sendForm input[name='achternaam']").val() != '' && jQuery("#sendForm textarea").val() != '') {
		
			jQuery("#sendForm").submit();
		}
		else {
			alert('U hebt niet alle velden correct ingevuld!');	
		}
		
	});
	
	
	
	jQuery("#submit.submitSelection").live("click", function(){
	//	var selected = new Array();
	//	jQuery("input[type='checkbox'].filter:checked").each(function(){	selected.push(jQuery(this).val()); });
	//	var variables = selected.join(",");
	//	alert(variables);
	//alert('test');
		submitFilterSearch();
		
	});
	
	jQuery(".resetSelection").live("click", function(){								 
													 
		jQuery("input[type='checkbox'].filter:checked").each(function(){
			jQuery(this).attr("checked",false).prev().attr("src","images/ps_checkbox.png").parent().removeClass("ps_checkbox_selected").addClass("ps_checkbox");			
		});
		jQuery("#reset").removeClass("resetSelection").attr("src","images/reset_search_inactive.png");
		jQuery("#submit").removeClass("submitSelection").attr("src","images/submit_search_inactive.png"); 
		
		//alert('tester');
		//submitFilterSearch();	
	});
/*	
	jQuery(".item").live("click", function(){
		jQuery("#contentLoader").css("display","block");						  
		jQuery("body").append("<div id='projectInfo'>test</div>");
	});
	
	jQuery("#contentLoader, #closeProject").live("click",function(){
		jQuery("#projectInfo").remove().flush();
		jQuery("#contentLoader").css("display","none");	
	});
*/	
//	var mpw = jQuery("#mainpagewrapper").height();
//	var spb = jQuery("#content").height();
	
//	if(mpw < spb) { jQuery("#mainpagewrapper").height(spb); }
	//setFooter();
	
	jQuery('#mycarousel').jcarousel({
        auto: 3,
		scroll: 6,
		animation: 800,
		wrap: 'circular',
		itemFallbackDimension: 164,
        initCallback: mycarousel_initCallback
    });
	
	
	jQuery("#addToBrochure").click(function(){
		var productID = jQuery("#currentProduct").val();
		
		jQuery.ajax({
			url: 		sUrl+"includes/addToBrochure.php",
			data: 		{ product: productID},
			type: 		"get",
			dataType:	"html",
			success: 	function(data) {
			//	jQuery("body").append("<div id='popupBox'>"+data+"</div>");
			//	Cufon.replace('h2', {fontFamily: 'MyriadPro' });
				if(data == "succes") {
					
					var oldcount = jQuery("#count").html();
					var newcount = Number(oldcount) +1;
					
					if(Number(oldcount)>0){
						//alert('wel:'+oldcount);
						jQuery("#count").html(newcount);
					} else {
						//alert('null:'+oldcount);
						jQuery("#count").html(newcount);
						jQuery("#product_count").append(" product toegevoegd");
					}
					alert('Product toegevoegd aan Mijn Brochure!');
					jQuery("#addToBrochure").attr("src","images/toegevoegd.gif").removeClass("addToBrochure").addClass("addedToBrochure");
					jQuery(".addbuttons").append("<br><a href='brochure/'><img src='images/mijnbrochure.gif' id='mijnbrochure' /></a>")
					//Cufon.replace('.round', { fontFamily: 'gothic' });
				}
			}
		});
		
	});
	
	function checkFields() {
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if(emailReg.test(jQuery("#createBrochure input[name='emailadres']").val()) && jQuery("#createBrochure input[name='naam']").val() !== '' && jQuery("#createBrochure input[name='plaats']").val() !== '' ) { jQuery("#createBrochure").submit(); 	}
		else { alert("U hebt niet alle verplichte velden correct ingevuld!");return false; }
	}
	
	jQuery("#saveBrochure").click(function(){
		// alert(jQuery("#count").html());
		if(jQuery("#count").html()!= '')
			checkFields();
		else
			alert('U heeft nog geen producten toegevoegd');
	});
	
	jQuery(".delFromSelection").click(function(){
		var conf = confirm("Dit product uit uw brochureselectie verwijderen?");	
		if(conf == true) {
			var currentID = jQuery(this).attr("id").replace(/[^0-9]+/g,'');	
			var current = jQuery(this).attr("id");	
			
			jQuery.ajax({
				url: 		sUrl+"includes/removeFromBrochure.php",
				data: 		{ product: currentID},
				type: 		"get",
				dataType:	"html",
				success: 	function(data) {
				//	jQuery("body").append("<div id='popupBox'>"+data+"</div>");
				//	Cufon.replace('h2', {fontFamily: 'MyriadPro' });
					if(data == "succes") {
						jQuery("#"+current).parent().parent().remove();
						var oldcount = jQuery("#count").html();
						var newcount = Number(oldcount) -1;
						
						if(Number(oldcount)>1){
							//alert('wel:'+oldcount);
							jQuery("#count").html(newcount);
						} else {
							//alert('null:'+oldcount);
							//jQuery("#count").html(newcount);
							jQuery("#product_count").html("<span id='count'></span>");
						}
						//Cufon.replace('.round', { fontFamily: 'gothic' });
					}
				}
			});
			
			

		}
	});
	
	jQuery(".home_brands").click(function(){
		var brandid = jQuery(this).attr("id").replace("_brands","");
		setBrand(brandid);
	});
	
	jQuery("#offerteaanvraag").click(function(){
		jQuery(this).css('display','none');
		jQuery("#offerteformdiv").slideDown();
	});
	jQuery("#saveAanvraag").click(function(){
		jQuery("#aanvraagForm").submit();
		
	});
	
		//load offerteaanvraag formulier 
	if(jQuery("#aanvraagForm").length){
		jQuery("#aanvraagForm").validate({					  
		  rules: {  
		     'voornaam': "required",
			 'naam': "required",
			 'plaats': "required",
			 'telefoon': {
				required: true,
			    digits: true,
				minlength:10
			 },
			 'emailadres': {
				required: true,
			    email: true,
			 }
			 
		   },
		  messages: {
			 'plaats': 'Dit veld is vereist', 
			 'telefoon':  {
			   required: 'Dit veld is vereist',
			   digits: 'Ongeldig telefoonnummer, alleen cijfers zijn toegestaan.',
			   minlength: 'Nummer moet een 10 cijfig getal zijn!'
			 },
			 'voornaam': 'Dit veld is vereist', 
			 'naam': 'Dit veld is vereist', 
			 'emailadres': {
			   required: 'Dit veld is vereist',
			   email: 'Ongeldig emailadres'
			 }
		   },
		   
		   	submitHandler: function(form) {  
				form.submit();
		   }
		});
	}
	
	
	/*
	jQuery('#slider').slider({
		range: true,
		values: [17, 67]
	});
	*/
});

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
 /*   carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
*/
    // Pause autoscrolling if the user moves with the cursor over the clip.

    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
	
		jQuery("#mycarousel").show();
};

function showPopup(data) {
	jQuery("#contentLoader").css("display","block");
	jQuery("body").append("<div id='popupBox'><div style='margin: 25px 25px 0 25px;'>"+data+"</div></div>");
}

function showPopupMessage(title,text) {
	jQuery("#contentLoader").css("display","block");
	
	jQuery.ajax({
		url: 		sUrl+"includes/fillPopup.php",
		data: 		{ title: title, text: text},
		type: 		"get",
		dataType:	"html",
		success: 	function(data) {
			jQuery("body").append("<div id='popupBox'>"+data+"</div>");
			Cufon.replace('h2', {fontFamily: 'MyriadPro' });
		}
	});
}

function slideDownProdSelc(method){
	
	jQuery.ajax({
		url: 		sUrl+"includes/getProductSelector.php",
		type: 		"get",
		dataType:	"html",
		success: 	function(data) {
			if(method == "slide") { jQuery("#product_selector").html(data).slideDown("fast"); }
			if(method == "show") { jQuery("#product_selector").html(data).css("display","block"); }
			jQuery("#showhider").attr("src","images/productselector_hide.png");	
			//submitFilterSearch();
			Cufon.replace('h2', {fontFamily: 'gothic' });
		}
	});
	
}
//jQuery(function(){
function submitFilterSearch() {
	var fields = jQuery("input[type='checkbox'].filter:checked").serialize();
	fields += "&"+jQuery("select.filter").serialize();
	//console.debug(fields);	

	jQuery.ajax({
		url: 		sUrl+"includes/getProducts.php",
		type: 		"get",
		data: 		{ selection: fields },
		dataType:	"html",
		timeout:	20000,
		tryCount: 	0,
		retryLimit:	3,
		success: 	function(data) {
			if(data !== "error") { 
				jQuery("#content").html(data).height('auto');
				//setFooter();
				
			}
		},
		error: 		function(xhr, textStatus, errorThrown ) {
			if (textStatus == 'timeout') {
				this.tryCount++;
				if (this.tryCount <= this.retryLimit) {
				//try again
				jQuery.ajax(this);
				return;
			}
			jQuery("#content").html('' + this.retryLimit + ' maal mislukt. Probeer het nog eens...');
				return;
			}
			if (xhr.status == 500) { jQuery("#content").html('Oeps! Er lijkt een serverprobleem te zijn, probeer het later nog eens.'); } 
			else { jQuery("#content").html('Oeps! Er is een probleem opgetreden, sorry.');	}
		}
	});	
}

function submitFilterBrand(brandid) {
	//console.debug(fields);	
	var fields = 'merken='+brandid;
	jQuery.ajax({
		url: 		sUrl+"includes/getProducts.php",
		type: 		"get",
		data: 		{ selection: fields },
		dataType:	"html",
		timeout:	20000,
		tryCount: 	0,
		retryLimit:	3,
		success: 	function(data) {
			
			if(data !== "error") { 
				jQuery("#content").html(data).height('auto');
			} 
		},
		error: 		function(xhr, textStatus, errorThrown ) {
			if (textStatus == 'timeout') {
				this.tryCount++;
				if (this.tryCount <= this.retryLimit) {
				//try again
				jQuery.ajax(this);
				return;
			}
			jQuery("#content").html('' + this.retryLimit + ' maal mislukt. Probeer het nog eens...');
				return;
			}
			if (xhr.status == 500) { jQuery("#content").html('Oeps! Er lijkt een serverprobleem te zijn, probeer het later nog eens.'); } 
			else { jQuery("#content").html('Oeps! Er is een probleem opgetreden, sorry.');	}
		}
	});	
}

function setBrand(merk) {
	
	jQuery.ajax({
		url: 		sUrl+"includes/setBrand.php",
		type: 		"get",
		data: 		{ brand: merk },
		dataType:	"html",
		timeout:	20000,
		tryCount: 	0,
		retryLimit:	3,
		success: 	function(data) {
			if(data !== "error") { 
				window.location.assign(sUrl+"producten/");
			}
		},
		error: 		function(xhr, textStatus, errorThrown ) {
			if (textStatus == 'timeout') {
				this.tryCount++;
				if (this.tryCount <= this.retryLimit) {
				//try again
				jQuery.ajax(this);
				return;
			}
			jQuery("#content").html('' + this.retryLimit + ' maal mislukt. Probeer het nog eens...');
				return;
			}
			if (xhr.status == 500) { jQuery("#content").html('Oeps! Er lijkt een serverprobleem te zijn, probeer het later nog eens.'); } 
			else { jQuery("#content").html('Oeps! Er is een probleem opgetreden, sorry.');	}
		}
	});	
}

//});
//]]>

