$(document).ready(function(){ 

	$(".jlmglasheader_dialog").wrapInner('<div class="jlmglasheader_dialog_outer"><div class="content inner"></div></div>');
	$(".content").prepend('<div class="t"></div>');
	$(".jlmglasheader_dialog_outer").append('<div class="b"><div class="bl"></div></div>');
	
	$(".productdetails_dialog").wrapInner('<div class="productdetails_outer"><div class="content inner"></div></div>');
	$(".content").prepend('<div class="t"></div>');
	$(".productdetails_outer").append('<div class="b"><div class="bl"></div></div>');

	$(".minicart_dialog").wrapInner('<div class="minicart_outer"><div class="content inner"></div></div>');
	$(".content").prepend('<div class="t"></div>');
	$(".minicart_outer").append('<div class="b"><div class="bl"></div></div>');

	$(".productlist_dialog").wrapInner('<div class="productlist_outer"><div class="content inner"></div></div>');
	$(".content").prepend('<div class="t"></div>');
	$(".productlist_outer").append('<div class="b"><div class="bl"></div></div>');

	var spouw = 15;
	var catid = $('input[name="cat_id"]').val();
	$.post("library/jq_isolatiewaarde.php", {catid:catid,spouw:spouw},function(data){
       	$('#isolatiewaarde').empty();
       	$('#isolatiewaarde').html(data);
   	});
// Als eerste na het laden van de prod
	
	//  fade out begin 
     jQuery.fn.center = function () {
        this.css("position","absolute");
        this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
        this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
        return this;
    }
	$("#faq").click(function(e){
 
		$("#background").css({"opacity" : "0.7"})
                            .fadeIn("slow");
		$(".productlist_dialog").fadeOut("slow");
		$(".productdetails_dialog").fadeOut("slow");
		$(".minicart_dialog").fadeOut("slow");
 		$.post("include/getveelgesteldevragen.php", function(data){
 			$("#large").html(data);
			$("#large").center();
			$("#large").fadeIn("slow");
				return false;
		});
	});
	$(document).keypress(function(e){
		if(e.keyCode==27){
			$("#background").fadeOut("slow");
			$("#large").fadeOut("slow");
		}
	});
	$("#background, #sluit").live('click',function(){
		$("#background").fadeOut("slow");
		$("#large").fadeOut("slow");
		$(".productlist_dialog").fadeIn("slow");
		$(".productdetails_dialog").fadeIn("slow");
		$(".minicart_dialog").fadeIn("slow");
	});

	$('span[id*="question_"]').live('click', function () {
		var vraag = $(this).attr('id');
		var splitted =  vraag.split("_");
		var id = splitted[1];
		$('#answer_'+id).slideToggle("slow");		
	});
  
// fade out einde
	
	var pd_id = $('#pd_id').val();
	var cat_id = $('#cat_id').val();
	action = 'getproductdetails';
    $.post("library/jq_product_details.php", {action:action,pd_id:pd_id},function(data){
		$.each(data, function(index, itemData) {
			if(index=='pd_description'){
				$('#Voeter2').append('<div class="ProductDescriptionContainer"><h3>Productbeschrijving:</h3>'+itemData['value']+'</div>');
			} else if(index=='pd_name'){
				$('#Midden').append('<div class="DetailRow"><div class="ProductNaam">'+itemData['value']+'</div></div>');
			} else if(index=='pd_image'){
				$('#Links').append('<img width="130" height="130" src="'+itemData['value']+'">');
			} else if(index=='cart_url'){
			} else {
				$('#Midden').append('<div class="DetailRow"><div class="Label">'+itemData['label']+'</div><div class="Value">'+itemData['value']+'</div></div>');
			}
		});
		$('#Rechts').append('<input type="button"  class="addToCartButton" name="btnAddProduct" id="btnAddProduct" value="Voeg toe">');
	}, "json");
	$.post('include/processProductBewerkingenForm_v2.php',{pdid:pd_id}, function(data){
		$('#container_bewerkings_form').show('slow', function() {});
		$('#Voeter1-links').append(data);
	});


	// Einde van het begin

	$('#txtWidth, #txtHeight').live('keyup', function () {
		var pd_priceper = $('#pd_priceper').val();
		var breedte = $('#txtWidth').val();
		var hoogte = $('#txtHeight').val();
		var pdid = $('#pd_id').val();
		if(!(breedte == '' || hoogte == '')) {
			var oppervlakte = round((breedte * hoogte / 1000000),2);
			$('#oppervlakte').empty();
			$('#oppervlakte').html('<b>Oppervlakte:</b><br>'+oppervlakte+' m2');
			var omtrek = round((2*breedte + 2*hoogte),0);
			$('#omtrek').empty();
			$('#omtrek').html('<b>Omtrek:</b><br>'+omtrek+' mm');
			action = 'bepaalprijs';
        	$.post("library/jq_product_details.php", {action:action,pdid:pdid,breedte:breedte,hoogte:hoogte},function(data){
	 			$('#artikelprijs').empty();
				$('#artikelprijs').html('<b>Prijs van dit artikel:<b><br>&euro; '+data+' ');
			});
		}		
	});

	function round(n,dec) {
		var n = parseFloat(n);
		if(!isNaN(n)){
			if(!dec) var dec= 0;
			var factor= Math.pow(10,dec);
			return Math.floor(n*factor+((n*factor*10)%10>=5?1:0))/factor;
		}else{
			return n;
		}
	}
	
	$('input[name*="txtQty_"]').live('change', function () {
		var ctid = $(this).attr('id');
		
		var key = $(this).val();
		change = 0;
		while (key.length > 0 && (isNaN(key) || (key % 1) != 0 )) {
//			alert((key % 1) != 0);
			if (!isNaN(key) && (key % 1) != 0 ) {
				key = key.substring(0, key.length - 2)
			} else {
				key = key.substring(0, key.length - 1)
			}
			change = 1;
		}
		$(this).val(key);
		if (change == 0){
			ct_qty = key;
			action = 'changeCtQty';
        	$.post("library/jq_product_details.php", {action : action, ctid : ctid, ct_qty : ct_qty},function(data){
        		location.reload(); 
 			});
		}
	});

	$('#cboSpouwdikte').change(function () {
		var spouw = $(this).val();
		var catid = $('input[name="cat_id"]').val();
		$.post("library/jq_isolatiewaarde.php", {catid:catid,spouw:spouw},function(data){
        	$('#isolatiewaarde').empty();
        	$('#isolatiewaarde').html(data);
       	});
	});

	$('#spouw_').live('change', function () {
		var spouw = $(this).val();
		var catid = $('input[name="cat_id"]').val();
		$.post("library/jq_isolatiewaarde.php", {catid:catid,spouw:spouw},function(data){
        	$('#isolatiewaarde').empty();
        	$('#isolatiewaarde').html('<b>Isolatiewaarde:</b><br>'+data);
       	});
	});
	
/*	$('#bewerkingsopties').click(function () {
		if($('#txtWidth').length!=0 && $('#txtWidth').val()==''){
			alert('Vul eerst hoogte en breedte (of lengte) in');
			return false;
		}
		if($('#txtHeight').length!=0 && $('#txtHeight').val()==''){
			alert('Vul eerst hoogte en breedte (of lengte) in');
			return false;
		}
		pdid = $('#pd_id').val();
		$.post('include/processProductBewerkingenForm.php',{pdid:pdid}, function(data){
			$('#container_bewerkings_form').show('slow', function() {});
			$('#container_bewerkings_form').html(data);
			$('.ft').append('<input style="float:left;" type="button" class="cancelBewerkingen" value="Cancel"/>');
			$('input[value="Cancel"]').attr('id','cancelBewerkingen');
			$('.ft').append('<input style="float:right;" type="button" class="voegtoe" value="Voeg toe"/>');
			$('input[value="Voeg toe"]').attr('id','voegtoe');
		});
	})
	*/
	$('div[id*="bew_"]').live('click', function() {
		if($('#txtWidth').length!=0 && $('#txtWidth').val()==''){
			alert('Vul eerst hoogte en breedte (of lengte) in');
			return false;
		}
		if($('#txtHeight').length!=0 && $('#txtHeight').val()==''){
			alert('Vul eerst hoogte en breedte (of lengte) in');
			return false;
		}
		var clicked = $(this).attr('id');
		var splitted =  clicked.split("_");
		var pdid = splitted[1];
		var bewid = splitted[2];
		var action = 'kiesbewerking';
		$.post('include/processKiesbewerkingen.php',{action:action,pdid:pdid,bewid:bewid}, function(data){
			$('#bewerkingcontainer').html(data);
			$('#bewerkingcontainer').append('<input style="float:left;" type="button" class="cancelBewerkingen" value="Cancel"/>');
			$('input[value="Cancel"]').attr('id','cancelBewerkingen');
			$('#bewerkingcontainer').append('<input style="float:right;" type="button" class="voegtoe" value="Voeg toe"/>');
//			$('.voegtoe"]').attr('id','voegtoe');
		});
	});

	$('select[id*="bewkeuze_"]').live('change', function() {
		clicked = $(this).attr('id');
		splitted =  clicked.split("_");
		bewattribuut = splitted[1];			// (t)op,(l)eft,(r)ight,(b)ottom,rt,lt,rb,lb
		pdid = splitted[2];
		bewid = $(this).val();
//		alert(clicked+'-'+bewid);
		$.post('include/processAddBewerkingenToCart.php',{bewattribuut:bewattribuut,pdid:pdid,bewid:bewid}, function(data){
			UpdateBewerkingenPrijs();	
		});
		$('select[id*="bewkeuze_"]').attr('class','bewkeuze');
	});

	$('.cancelBewerkingen').live('click', function() {
		var pd_id = $('#pd_id').val();
		$.post('include/processRemoveBewerkingenFromCart.php',{pdid:pdid}, function(data){
			$('#bewerkingcontainer').empty();
		});
	});

	$('.voegtoe').live('click', function() {
		var pd_id = $('#pd_id').val();
		$('#bewerkingcontainer').empty();
		UpdateGekozenBewerkingen();
	});

	function UpdateBewerkingenPrijs(){
		var breedte = $('#txtWidth').val();
		var hoogte = $('#txtHeight').val();
		var action = 'bepaalbewerkingenprijs';
       	$.post("library/jq_product_details.php", {action:action,bewid:bewid,pdid:pdid,breedte:breedte,hoogte:hoogte},function(data){
			$('#bewerkingentotaalprijs').html('Bewerkingen voor dit produkt:<br>');
			$('#bewerkingentotaalprijs').append(data);
		});
	}
	
	function UpdateGekozenBewerkingen(){
		var breedte = $('#txtWidth').val();
		var hoogte = $('#txtHeight').val();
		var action = 'bepaalbewerkingenprijs';
       	$.post("library/jq_product_details.php", {action:action,bewid:bewid,pdid:pdid,breedte:breedte,hoogte:hoogte},function(data){
			$('#gekozenbewerkingen').html('<b>Bewerkingen:</b><br>');
			$('#gekozenbewerkingen').append(data);
		});
	}
	
	$('#btnAddProduct').live('click', function() {
		var pd_priceper = $('#pd_priceper').val();
		var carturl = $('#cart_url').val();
//		alert(carturl);
		if(pd_priceper=='0'){checkFormM0(carturl);}
		if(pd_priceper=='1'){checkFormM1(carturl);}
		if(pd_priceper=='2'){checkFormM2(carturl);}
	});

	function checkFormM0(carturl){
		var aantal = $('#qty_').val();
		var referentie = $('#txtReferentie').val();
		var loc_href = carturl + '&txtQty=' + aantal +'&txtReferentie=' + referentie;
		window.location.href = loc_href;
	}

	function checkFormM1(carturl){
		var aantal = $('#qty_').val();
		var referentie = $('#txtReferentie').val();
		var txtWidth = $('#txtWidth').val();
		if (txtWidth=='') {
			alert('Voer lengte in')
			return false;
		} else {
			loc_href = carturl + '&txtwidth=' + txtWidth + '&txtQty=' + aantal + '&txtReferentie=' + referentie;
			window.location.href = loc_href;
		}
	}

	function checkFormM2(carturl){
		var aantal = $('#qty_').val();
		var referentie = $('#txtReferentie').val();
		var breedte = $('#txtWidth').val();
		var hoogte = $('#txtHeight').val();
		var spouw = $('#spouw_').val();
		var maxoppervlak = $('#max_oppervlak').val();
		if(!(spouw>0 && spouw <20)){
			spouw=0;
		}
		if (breedte=='') {
			alert('Voer breedte in')
			return false;
		} else if (hoogte==''){
			alert('Voer hoogte in')
			return false;
		}
		if (maxoppervlak != 0 && hoogte * breedte / 1000000 > maxoppervlak) {
			alert("Maximumoppervlak voor deze dikte = " + maxoppervlak + 
			"m2\n\nAndere dikte's:\n\n4-4  max 2,46 m2       5-6  max 4,50 m2\n4-5  max 2,94 m2       6-6  max 5,74 m2\n4-6  max 3,69 m2       6-8  max 7,23 m2\n5-5  max 3,94 m2       8-8  max 10,13 m2\n\nLet op, deze waarden zijn fabricagemaxima.\nNaleving van NEN2608 is de verantwoordelijkheid\n van de klant.\n");
			formElement.focus();
			return false;
		}
		loc_href = carturl + '&txtwidth=' + breedte + '&txtheight=' + hoogte + '&cboSpouwdikte=' + spouw +'&txtQty=' + aantal + '&txtReferentie=' + referentie;
		window.location.href = loc_href;
	}
}); 

