$(document).ready(function(){
	////////// NAVIGATION /////////////
	$('#navigation ul li').hover(function(){
		$(this).find('a').css('color','red');	
	}, function(){
		if($(this).hasClass('page')){
			$(this).find('a').css('color','red');
			$(this).find('a').css('text-decoration','none');
		}else{
			$(this).find('a').css('color','white');
		}
	});
	
	$('#navigation ul li').click(function(){
		var url = $(this).find('a').attr('href');
		window.location.href = url;

	});
	
	$('#navigation ul')
		.find('.page')
			.css('background','#FFF')
			.find('a')
				.css('color','red');
	
	////////// PHOTOGALLERY /////////////
	$('#photoG').coinslider({ width: 605, 
							  height: 143, 
							  navigation: false, 
							  delay: 5000 });

	
	
	////////// OFFERTE /////////////
	$('#askOfferte').click(function(e){
		$error = false;
		if($('#offerteHolder #name').val()==""){
			$error = true;
			$('#offerteHolder #name').parent().parent().find('span').html('<img src="images/del.png" />');	
		}else{
			$naam = $('#offerteHolder #name').val();
			$('#offerteHolder #name').parent().parent().find('span').html('<img src="images/check.png" />');	
		}
		
		if(!checkEmail($('#offerteHolder #email').val())){
			$error = true;
			$('#offerteHolder #email').parent().parent().find('span').html('<img src="images/del.png" />');	
		}else{
			$email = $('#offerteHolder #email').val();	
			$('#offerteHolder #email').parent().parent().find('span').html('<img src="images/check.png" />');
		}
		
		if($('#offerteHolder #mobiel').val()==""){
			$error = true;
			$('#offerteHolder #mobiel').parent().parent().find('span').html('<img src="images/del.png" />');	
		}else{
			$mobiel = $('#offerteHolder #mobiel').val();
			$('#offerteHolder #mobiel').parent().parent().find('span').html('<img src="images/check.png" />');	
		}
		
		if($('#offerteHolder #adres').val()==""){
			$error = true;
			$('#offerteHolder #adres').parent().parent().find('span').html('<img src="images/del.png" />');	
		}else{
			$adres = $('#offerteHolder #adres').val();
			$('#offerteHolder #adres').parent().parent().find('span').html('<img src="images/check.png" />');
		}
		
		if($('#offerteHolder #postcode').val()==""){
			$error = true;
			$('#offerteHolder #postcode').parent().parent().find('span').html('<img src="images/del.png" />');	
		}else{
			$postcode = $('#offerteHolder #postcode').val();
			$('#offerteHolder #postcode').parent().parent().find('span').html('<img src="images/check.png" />');	
		}
		
		if($('#offerteHolder #gemeente').val()==""){
			$error = true;
			$('#offerteHolder #gemeente').parent().parent().find('span').html('<img src="images/del.png" />');	
		}else{
			$gemeente = $('#offerteHolder #gemeente').val();
			$('#offerteHolder #gemeente').parent().parent().find('span').html('<img src="images/check.png" />');	
		}
		
		if($('#offerteHolder #msg').val()==""){
			$error = true;
			$('#offerteHolder #msg').parent().parent().find('span').html('<img src="images/del.png" />');	
		}else{
			$msg = $('#offerteHolder #msg').val();
			$('#offerteHolder #msg').parent().parent().find('span').html('<img src="images/check.png" />');
		}
		
		$fax = $('#offerteHolder #fax').val();
		$meter = $('#offerteHolder #meter').val();
		
		if($error==false){
			$.ajax({
			   type: "POST",
			   url: "include/offerte.php",
			   data: "naam=" +$naam +"&email=" +$email +"&mobiel=" +$mobiel +"&fax=" +$fax +"&adres=" +$adres +"&postcode=" +$postcode +"&gemeente=" +$gemeente +"&meter=" +$meter +"&msg=" +$msg,
			   success: function(msg){
					if(msg=="OK"){
						$('#offerteHolder form input').val('');
					}
			   }	
			});
		}else{
			console.log('KO');
		}
		
		e.preventDefault();
	});
	
	
	////////// PRODUCTEN /////////////
	$('li .navCatItem').hover(function(){
		$(this).find('a').css('color','red');	
	}, function(){
		$(this).find('a').css('color','white');	
	});
	
	$('.navGroupName').click(function(){
		$('.navGroupName').css({'color':'#FFF',
							    'border-bottom':'1px solid #009bbf'});
		$(this).css({'color':'#00F',
					 'border-bottom':'1px dotted #FFF'});
		$('.navCat').hide('fast');
		$(this).next().find('.navCat').show('fast');
		//$('#listHolder ul li').find('.navCat:eq(3)').show('fast');
	});
	
	$('.navCatItem').click(function(){
		var $naam = $(this).find('a').html();
		$naam.toLowerCase();
		var url = "shop/" +$naam +"/";
		window.location.href = url;
	});	
	
	
	////////// DETAILS /////////////
	$('#tabs div').hide().filter(':first').show();
	$('#tabs a').filter(':first').addClass('tabOpen');
	
	$('#tabs a').click(function(e){
		$id = $(this).attr('id');
		//console.log($id);
		$('#tabs div').hide('fast');
		$('#tabs .' +$id).show('fast');
		
		$('#tabs a').removeClass().filter($(this)).addClass('tabOpen');
		
		e.preventDefault();
	});
	
	$('#modTable tr td').hover(function(){
		$(this).parent().css({'background-color':'#FFF',
					  'color':'#00F'
				});
		$(this).parent().find('td:last a').css('color','red');
	}, function(){
		$(this).parent().css({'background':'none',
					 'color':'#FFF'
				});
		$(this).parent().find('td:last a').css('color','#FFF');	
	});
	
	
	$('#hideOutput').live('click', function(e) {
	  $('#output').hide();
	  e.preventDefault();
	});
	
	var busy = false;
	$('.addTo').click(function(e){
		$('#output').hide();
		//alert($(this).parent().parent().find('.tModel').html());
		if(busy==false){
			busy = true;
			var item = $(this).parent().parent().find('.tModel').html();
			$("#ajaxLoader").show('fast');
			$("#ajaxLoader").css({
								top: (e.pageY) + "px",
								left: (e.pageX + 15) + "px"
							});
			$(document).mousemove(function(e){
				$("#ajaxLoader").css({
					top: (e.pageY) + "px",
					left: (e.pageX + 15) + "px"
				});
			});
			$.ajax({
			   type: "POST",
			   url: "include/shopSession.php",
			   data: "action=add&prod=" +item,
			   success: function(msg){
				 //alert(msg);
				 $("#ajaxLoader").hide('fast');
				 $('#output').show('fast');
				 $('#output').html('<a id=\'hideOutput\' href=\'#\' alt=\'Sluiten\'><img src=\'images/close.png\' alt=\'\' title=\'\' /></a> || Product' +item +' toegevoegd. || <a href=\'cart/\' title=\'\'>Ga naar de winkelwagen</a>');
				 busy=false;
			   }
			 });
		}
		e.preventDefault();
	});
	
	$('.removeFromCart').click(function(e){
		$('#output').hide();
		
		if(busy==false){
			busy = true;
		
			$id = $(this).parent().parent().attr('title');
			$prod = $(this).parent().parent().find('td:first').html();
			$stotaal = $('#stotaal').html();
			$btw = $('#btw').html();
			$totaal = $('#totaal').html();
			$decr = $(this).parent().parent().find('.prijs').html();
			$remove = $(this).parent().parent();
			//alert($decr);
			
			$("#ajaxLoader").show('fast');
			$("#ajaxLoader").css({
								top: (e.pageY) + "px",
								left: (e.pageX + 15) + "px"
							});
			$(document).mousemove(function(e){
				$("#ajaxLoader").css({
					top: (e.pageY) + "px",
					left: (e.pageX + 15) + "px"
				});
			});
			
			$.ajax({
			   type: "POST",
			   url: "include/shopSession.php",
			   data: "id=" +$id +"&prod=" +$prod +"&action=del",
			   success: function(msg){
					if(msg!="leeg"){
						//alert(msg);
						$stotaal -= $decr;
						$btw = $stotaal * 0.19;
						$totaal = $stotaal + $btw;
						
						$btw = $btw.toFixed(2);
						$stotaal = $stotaal.toFixed(2);
						$totaal = $totaal.toFixed(2);
						
						$('#stotaal').html($stotaal);
						$('#btw').html($btw);
						$('#totaal').html($totaal);
						$($remove).hide('fast');
						$("#ajaxLoader").hide('fast');
						$('#output').show('fast');
						$('#output').html('<a id=\'hideOutput\' href=\'#\' alt=\'Sluiten\'><img src=\'images/close.png\' alt=\'\' title=\'\' /></a> || Product: ' +$prod +' verwijdert');
						busy=false;
					}else{
						window.location = "cart/";	
					}
			   }
			 });
		}
		e.preventDefault();
	});
	
	////////// CART /////////////
	$emptyCart = false;
	
	$('#confirmBuy a').click(function(e){
		$('#confirm_tab').css('top', $(window).height()/2 - $('#confirm_tab').height()/2);
		$('#confirm_tab').css('left', $(window).width()/2 - $('#confirm_tab').width()/2);
		$('#confirm_tab').fadeIn('fast');
		
		e.preventDefault();
	});
	
	$('#closeConfirmTab').click(function(e){
		if($emptyCart==false){
			$('#confirm_tab').fadeOut('fast');	
		} else {
			window.location = "cart/";	
		}
		
		e.preventDefault();
	});
	
	$('#bevestig').click(function(e){
		//$('#confirm_tab input[type=text]:').empty().val('Gelieve in te vullen');
		$('#confirmStatus').html('');
		$emptyCart = false;
		
		var $naam = $('#confirm_tab #naam').val();
		var $anaam = $('#confirm_tab #anaam').val();
		var $straat = $('#confirm_tab #adres').val();
		var $gemeente = $('#confirm_tab #gemeente').val();
		var $postcode = $('#confirm_tab #postcode').val();
		var $land = $('#confirm_tab #land').val();
		var $email = $('#confirm_tab #email').val();
		var $mobiel = $('#confirm_tab #mobiel').val();
		var $msg = $('#confirm_tab #msg').val();
		
	
		
		if($naam==''){
			$('#confirm_tab #naam').focus();
			$status = 'Gelieve je naam in te vullen';
		} else if($anaam==''){
			$('#confirm_tab #anaam').focus();
			$status = 'Gelieve je achternaam in te vullen';
		} else if($straat==''){
			$('#confirm_tab #adres').focus();
			$status = 'Gelieve je adres in te vullen';
		} else if($gemeente==''){
			$('#confirm_tab #gemeente').focus();
			$status = 'Gelieve je gemeente in te vullen';
		} else if($postcode==''){
			$('#confirm_tab #postcode').focus();
			$status = 'Gelieve je postcode in te vullen';
		} else if($land==''){
			$('#confirm_tab #land').focus();
			$status = 'Gelieve je land in te vullen';
		} else if($email=='') {
			$('#confirm_tab #email').focus();
			$status = 'Gelieve je email in te vullen';
		} else if(!checkEmail($email)){
			$('#confirm_tab #email').focus();
			$status = 'Er is een fout in je emailadres';
		} else if($mobiel==''){
			$('#confirm_tab #mobiel').focus();
			$status = 'Gelieve je telefoon/GSM in te vullen';
		} else {
			$status = 'OK';
		}
	
		if($status!='OK'){
			$('#confirmStatus').html($status);
		} else {
			
			$.ajax({
			   type: "POST",
			   url: "include/confirm.php",
			   data: "naam=" +$naam +"&anaam=" +$anaam +"&straat=" +$straat +"&gemeente=" +$gemeente +"&postcode=" +$postcode +"&land=" +$land +"&email=" +$email +"&mobiel=" +$mobiel +"&msg=" +$msg,
			   success: function(msg){
					if(msg=="OK"){
						$('#confirmStatus').html('<h2>Uw bestelling is verstuurt.</h2>Er is voor jouw ook een mail gestuurt.<br />Controleer eventueel je SPAM-box.');
						$emptyCart = true;
					}else{
						$('#confirmStatus').html('<h2>Er ging iets fout. Gelieve later nog eens opnieuw te proberen.</h2>');	
					}
			   }	
			});
			/*
			$('#confirm_tab #naam').val('');
			$('#confirm_tab #anaam').val('');
			$('#confirm_tab #adres').val('');
			$('#confirm_tab #gemeente').val('');
			$('#confirm_tab #postcode').val('');
			$('#confirm_tab #land').val('');
			$('#confirm_tab #email').val('');
			$('#confirm_tab #mobiel').val('');
			*/
			
			//$('#confirmStatus').html('Uw bestelling is aangekomen.');
			
			e.preventDefault();
		}
	});
	
	function checkEmail(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((		25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
	}		
});
