
	// jquery scripts
	$(document).ready(function () {


		// lightbox laten werken
		$('a.lightbox').lightBox();
		
		// slideshow
		$('#slideshow').cycle({ 
		    fx:    'fade', 
		    speed:  7000,
		    pause: 	1 
		 });


        $("select.ProductAttributesSelect").sb({
            fixedWidth: true
        });


		$(".product-carousel").carouFredSel({
			items		: 3,
			scroll : {
				items			: 1,
				duration		: 1000,
				pauseOnHover	: true
			}
		});
        

 
		// modal voor de eigenschappen
		$("a.modalpop").click(function(){
			$('#dialog').jqm({ajax: 'includes/misc/eigenschap_details.php', trigger: 'a.ex2trigger'});
			$('#dialog').jqmShow(); 
			return false;
		});
		
		 
		$("#images-large").carouFredSel({
			auto 	: false,
			items	: 1,
			scroll 	: {
				items			: 1,
				duration		: 1000,
				pauseOnHover	: true
			},
			prev 	: {   
				button  : "#prev_large",
				key     : "left"
			},
			next 	: {
				button  : "#next_large",
				key     : "right"
			}			
		
		});
		

		//$("#firstColumn").css('height',maxheight);		
		//$("#secondColumn").css('height',maxheight);		
		//$("#pageLong").css('height',maxheight);		
		//$("#thirdColumn").css('height',maxheight);	

		//console.log('page: '+ page +'\n');
		//console.log('maxheight: '+ maxheight +'\n');
		//console.log('firstflex: '+ heightfirstflex + ' + height_col_container '+ height_col_container +'\n');
		//console.log(height_col_container +' - '+ addheight );
		
		$('a.selectLang').click(function(){
			var lang = $(this).attr('id');
			$('body').translate(lang);

			$.ajax({
			   type: "POST",
			   url: "includes/actions/action_setTranslateSession.php",
			   data: "lang="+ lang,
			   success: function(msg){
			     // alert( "Data: " + msg );
			   }
			});

			return false;
		});
		
		

		// form valideren
		$("#id_buybutton").click(function(){

			// check fields
			var checkfields 			= true;
			var errorMsg 				= '';

			var voornaam 				= $('input#id_naam_voornaam').val();
			//var achternamen 			= $('input#id_naam_achternaam').val();
			var adres_straatnaam 		= $('input#id_adres_straatnaam').val();
			var adres_huisnummer 		= $('input#id_adres_huisnummer').val();
			var adres_postcode 			= $('input#id_adres_postcode').val();
			var adres_woonplaats 		= $('input#id_adres_woonplaats').val();
			var emailadres 				= $('input#id_emailadres').val();
			var emailadres_confirm		= $('input#id_emailadres_confirm').val();



			
			var algemeneVoorwaarden		= $('input#id_voorwaarden').attr('checked'); 
			
			if(!notEmpty(voornaam)){ outlineInput('id_naam_voornaam', true); checkfields = false; errorMsg += "Voornaam is een verplicht veld \n"; } else { outlineInput('id_naam_voornaam', false);  }
			//if(!notEmpty(achternamen)){ outlineInput('id_naam_achternaam', true); checkfields = false; errorMsg += "Achternaam is een verplicht veld \n";} else { outlineInput('id_naam_voornaam', false);  }
			if(!notEmpty(adres_straatnaam)){ outlineInput('id_adres_straatnaam', true); checkfields = false; errorMsg += "Straatnaam is een verplicht veld \n";} else { outlineInput('id_adres_straatnaam', false);  }
			if(!notEmpty(adres_huisnummer)){ outlineInput('id_adres_huisnummer', true); checkfields = false; errorMsg += "Huisnummer is een verplicht veld \n";} else { outlineInput('id_adres_huisnummer', false);  }
			if(!notEmpty(adres_postcode)){ outlineInput('id_adres_postcode', true); checkfields = false; errorMsg += "Postcode is een verplicht veld \n";} else { outlineInput('id_adres_postcode', false);  }
			if(!notEmpty(adres_woonplaats)){ outlineInput('id_adres_woonplaats', true); checkfields = false; errorMsg += "Woonplaats is een verplicht veld \n";} else { outlineInput('id_adres_woonplaats', false);  }
			if(!emailCheck(emailadres)){ outlineInput('id_emailadres', true); checkfields = false; errorMsg += "Een geldig emailadres is een verplicht veld\n"; } else { outlineInput('id_emailadres', false);  }
			if(!emailCheck(emailadres_confirm)){ outlineInput('id_emailadres_confirm', true); checkfields = false;  errorMsg += "Een bevestiging van uw emailadres is  verplicht \n"; } else { outlineInput('id_emailadres_confirm', false); }

			if(emailadres != emailadres_confirm){  
				outlineInput('id_emailadres', true); 
				outlineInput('id_emailadres_confirm', true);
				checkfields = false; 
				errorMsg += "De emailadressen komen niet overeen, controleer de emailadressen \n";
			} else { 
				outlineInput('id_emailadres', false);
				outlineInput('id_emailadres_confirm', false);
			}


			if(!algemeneVoorwaarden){ outlineInput('borderThis', true); checkfields = false; errorMsg += "U moet akkoord gaan met de algemene voorwaarden \n"; } else { outlineInput('borderThis', false);  }

			if(checkfields){
				$('form#formkopen').submit();
				return false;
			} else {
				alert(errorMsg);
				return false;
			}
			
		});


		
		$("#buy-confirm-button").click(function(){
			$('form#form-bevestig-kopen').submit();
			return false;

		});	
		
		$("#buy-confirm-ogone").click(function(){
			//alert('X');

			$.ajax({
				type: 	'POST',
				data: 	"ajax_request=yesy",
				url: 	'https://www.topbikes.nl/includes/actions/action_webwinkel.php',
				success:function(data){
					//alert(data);
					$('form#ogonepayment').submit();
				}
			});
			return false;
		});	
		
		
		$("#id_bezorgadres_check").click(function(){
			var check_status = $('#id_bezorgadres_check').is(':checked');
			if(check_status) {
				var pageLongHeight = $("#pageLong").height();
				var newheight = pageLongHeight + 175;
				$("div#pageLong").animate({
					height:newheight
				}, 250, "linear");
				$('div#cart-personalia-bezorgadres').slideDown();
			} else {
				$("div#pageLong").css('height','');
				$('div#cart-personalia-bezorgadres').slideUp();

			}
		
			
		});
		
		$("input#fillform").click(function(){
			$('input#id_naam_voornaam').val('Johan');
			$('input#id_naam_achternaam').val('Berkhout');
			$('input#id_emailadres').val('johan@eye-c.nl');
			$('input#id_adres_straatnaam').val('Binckhorstlaan');
			$('input#id_adres_huisnummer').val('36');
			$('input#id_adres_postcode').val('2516BE');
			$('input#id_adres_woonplaats').val('Den Haag');
			$('input#id_voorwaarden').attr('checked','checked');
			$('input#id_telefoon').val('0703856422');
		});


		$("a#keep-me-posted-link").toggle(
			function () {
				$("div#tell-a-friend").slideUp();
				$("div#keep-me-posted").slideDown();
				return false;
			},
			function () {
				$("div#keep-me-posted").slideUp();
				return false;
			}
		);
		
		$("a#tell-a-friend-link").toggle(
			function () {
				$("div#keep-me-posted").slideUp();
				$("div#tell-a-friend").slideDown();
				return false;
			},
			function () {
				$("div#tell-a-friend").slideUp();
				return false;
			}
		); 


		$('input.cart-aantal').blur(function(){
			var inputid = $(this).attr('id');
			var cartid 	= inputid.substr(7,20);
			var formid	= 'updateCart-'+ cartid;
			$("form#"+formid).submit();
		});		
			
		/*	
		$("ul li a.current").toggle(
			function () {
				var parentli = $(this).parent().attr('id');
				$("li#"+ parentli +" ul").slideUp();
				return false;
			},
			function () {
				var parentli = $(this).parent().attr('id');
				$("li#"+ parentli +" ul").slideDown();
				return false;
			}		
		);
		
		$("ul li a.currentsub").toggle(
			function () {
				var parentli = $(this).parent().attr('id');
				$("li#"+ parentli +" ul").slideUp();
				return false;
			},
			function () {
				var parentli = $(this).parent().attr('id');
				$("li#"+ parentli +" ul").slideDown();
				return false;
			}		
		);	*/	
			


		$("#searchboxed").coolautosuggest({
			url:"http://shop.topbikes.nl/includes/misc/ajax_search.php?query=",
			showThumbnail:true,
			showDescription:true,
			idField:$("#searchid"),
			width:300,
			minChars:2,
			submitOnSelect:true
		});


		/*
		var searchTimeout;
		$("#searchboxed").keyup(function(){

	        if (searchTimeout != undefined) clearTimeout(searchTimeout);
	        searchTimeout = setTimeout(callServerScript, 500);


			var searchstr = $("#searchboxed").val();
			if(searchstr.length > 1){
				$.ajax({
				   type: "GET",
				   url: "http://shop.topbikes.nl/includes/misc/ajax_search.php",
				   data: "query="+searchstr,
				   success: function(msg){
				     alert( "Data Saved: " + msg );
				   }
				 });

			}

			return false;
			

		});
		*/


		setHeight();
	});




    function callServerScript() {
        alert('gestopped met typen');
    }	
	


	function setHeightA(){
	
	}
	
	function setHeight(){

		Array.prototype.max = function(){
			return Math.max.apply({},this)
		}

		var page 						= $("#page").height() ;

		var page_margin 				= $('div#secondColumn').css('margin-top');
		var page_margin_webshop 		= $('div#secondShortColumn').css('margin-top');
		if(parseFloat(page_margin_webshop) > 0){
			var page_margin = page_margin_webshop;
			var height_col_two_long 		= $("#secondShortColumn").height();
			var height_col_two_long_margin 	= height_col_two_long + 233;			
		} else {
			var height_col_two_long 		= $("#secondLongColumn").height();
			var height_col_two_long_margin 	= height_col_two_long + 466;		
		}
		page_margin = page_margin.substr(0,3);
		// console.log(page_margin);
		
		var pageLong 					= $("#pageLong").height() ;
		var pageLong_margin				= pageLong + 466;
		var height_col_one 				= $("#firstColumn").height() ;
		var height_col_container 		= $("#firstContainer").height();
		var height_col_one_margin 		= height_col_one + 186;
		var height_col_two 				= $("#secondContainer").height();
		var height_col_two_margin 		= height_col_two + 466;
		//var height_col_two_long 		= $("#secondLongColumn").height();
		//var height_col_two_long_margin 	= height_col_two_long + 466;
		var height_col_three			= $("#thirdColumn").outerHeight();
		var height_col_three_container	= $("#thirdContainer").outerHeight();
		
		var ary = [height_col_one_margin, height_col_two_long_margin, height_col_two_margin, height_col_three, page_margin, pageLong];
		var maxheight 				= ary.max();

		var heightfirstflex = maxheight-(186 + height_col_container);
		if(height_col_one_margin < maxheight){
			$("#firstFlex").css('height', heightfirstflex );
		}
		
		if(height_col_two_long_margin < maxheight){
			$("#height_col_two_long").css('height',maxheight - page_margin + height_col_container);
		}

		if(height_col_three < maxheight){
			$("#thirdFlex").css('height',(maxheight - height_col_three_container));
		}
		
		if(pageLong < maxheight) {
			$("#pageLong").css('height',maxheight - page_margin);
		}
		
		if(page < maxheight) {
			$("#page").css('height',maxheight - page_margin);
		}

		// alert('setsize');

	}

	// validatie functies
	function postcodeCheck(postcode) {
		var result 	= false;
		var filter  = /^[0-9]{4}[ A-Za-z]{2,3}$/;
		
		if ((postcode.length) > 0) {
			if (filter.test(postcode)) {
				result = true;
			}
		}
		return result
	}	

	function emailCheck(addres) {
		
		var result 	= false;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if ((addres.length) > 0) {
			if (filter.test(addres)) {
				result = true;
			}
		}
		return result;
	}
	
	function notEmpty(str) {
		
		var result 	= false;
		//var filter  = /^[a-zA-Z0-9_\-,\.\'']{255}$/;
		
		if ((str.length) > 0) {
			//if (filter.test(str)) {
				result = true;
			//}
		}
		return result;
	}	
	
	function isDate(date) {
		
		var result 	= false;
		var filter  = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
		
		if ((date.length) > 0) {
			if (filter.test(date)) {
				result = true;
			}
		}
		return result;
	}	

	// velden aanduiden of ze verplicht zijn
	function outlineInput(id, status){
		if(status){
			$('#'+id).addClass('outline');
			$('#'+id).focus();
		} else {
			$('#'+id).removeClass('outline');
		}
	}
	
	function normalInput(id){
		$('#'+id).removeClass('outline');
	}
	
	// WEBWINKEL / CART FUNCTIES
	function toggleBezorgadres(){
		var bezorgadres = $('select#id_bezorgadres').val();
		if(bezorgadres == 2){
			var pageLongHeight = $("#pageLong").height();
			var newheight = pageLongHeight + 175;
			$("div#pageLong").animate({
				height:newheight
			}, 250, "linear");
			$('div#cart-personalia-bezorgadres').slideDown();
		} else if(bezorgadres == 1){
			$("div#pageLong").css('height','');
			$('div#cart-personalia-bezorgadres').slideUp();
		}
	}
	
	
	
	function keepMePosted() {

		// check fields
		var checkfields 			= true;
		var errorMsg 				= '';
		
		var emailadres 				= $('input#keep-me-posted-email').val();
		if(!emailCheck(emailadres)){ outlineInput('keep-me-posted-email', true); checkfields = false; errorMsg += "Een geldig emailadres is een verplicht \n"; } else { outlineInput('keep-me-posted-email', false);  }
		
		if(checkfields){
			$('form#keep-me-posted-form').submit();
			return false;
		} else {
			alert(errorMsg);
			return false;
		}
		
	};
