jQuery(document).ready(function($) {
	$(".buttons img").hover(function(){
		$(this).css('opacity',0.7);
	},function(){
		$(this).css('opacity',1.0);
	});
	
	$("a.fancy").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	400, 
		'speedOut'		:	400, 
		'overlayShow'	:	false
	});
	
	$('.col2').columnize({ columns: 2 });
	
	//AJAX div edit forms
	$('input.ajax.prijs').blur(function() {
		$.post("/pages/process_ajax.php", { 
		  prijs_id		: $(this).parent().find("[name='prijs_id']").val(),
		  art_id		: $(this).parent().find("[name='art_id']").val(),
		  hoeveelheid	: $(this).parent().find("[name='hoeveelheid']").val(),
  		  prijs			: $(this).parent().find("[name='prijs']").val()
  		});
	});
	
	$('input.ajax.art').blur(function() {
		$.post("/pages/process_ajax.php", { 
		  art_id		: $(this).parent().find("[name='art_id']").val(),
		  art_code		: $(this).parent().find("[name='art_code']").val(),
		  actie			: $(this).parent().find(".actie:checked").val(),
  		  vis			: $(this).parent().find(".vis:checked").val()
  		});
	});
	
	$('input.ajax.vis, input.ajax.actie').click(function() {
		$.post("/pages/process_ajax.php", { 
		  art_id		: $(this).parent().find("[name='art_id']").val(),
		  art_code		: $(this).parent().find("[name='art_code']").val(),
		  actie			: $(this).parent().find(".actie:checked").val(),
  		  vis			: $(this).parent().find(".vis:checked").val()
  		});
	});

	//actie update prijzen
	$(".props select").change(function() {
		//this.form.submit();
	
		//var curr_id =  $(this).parents("form").find("[name='art_id']").val();
		if ($(this).attr('name') == 'klr_id') {
			var klr_val = $(this).val();
			var hgt_val = $(this).parents("form").find("[name='hgt_id']").val();
		} else {
			var hgt_val = $(this).val();
			var klr_val = $(this).parents("form").find("[name='klr_id']").val();
		}
		var price_field = $(this).parents("form").find(".price");
		var art_id_field = $(this).parents("form").find("[name='art_id']");
		
		$.post("/pages/process_ajax.php", { 
				price : 1,
				parent_id 	: $(this).parents("form").find("[name='parent_id']").val(),
				bruto 		: $(this).parents("form").find("[name='bruto']").val(),
				aant 		: $(this).parents("form").find("[name='aant']").val(),
				hgt_id 		: hgt_val,
				klr_id 		: klr_val
			},function(data) {
				 art_id_field.val(data.art_id);
				 price_field.text(data.prijs);
			}, "JSON");
	});
	
	//tabbladen
	$('#shop_tabs').tabs();
	
	$("#art_overz")
		.tablesorter({widgets: ['zebra']})
		.tablesorterPager({container: $("#pager")});
		
	//CALCULATIES
	$("input[name^=aantal_]").bind("keyup", recalc);
	function recalc(){ 
		$("[id^=row_tot_]").calc( 
			"qty * price", { qty: $("input[name^=aantal_]"),price: $("[name^=prijs_]")},
	        	function (s){
					return s.toFixed(2); 
	        },
	        function ($this){
	        	var verzk = parseFloat($("input[name=verzk]").val());
	        	var perc = parseFloat($("input[name=btw]").val()/100);
				var sum = $this.sum();
				if (sum == 0) verzk = 0;
				var tot = parseFloat( verzk + sum);
				var btw = tot * perc;
				var grandtot = tot * (1 + perc);
				
				$("#subtot").text( sum.toFixed(2));
				$("#verztot").text(verzk.toFixed(2));
				$("#btwtot").text( btw.toFixed(2));
				$("#grandtot").text( grandtot.toFixed(2)); 
	        } 
	    ); 
	}
	$('#cart_btn').hover(function(){
		$(this).css('background','url(../picts/cart_over.png) no-repeat 270px top');
		$('#cart').slideDown('normal');
	},function(){
		$(this).css('background','url(../picts/cart.png) no-repeat  270px top');
		$('#cart').slideUp('normal');
	});
	
	//Altenatief aflever adres
    $('#alt_adr').css('display','none');
    $("input[name^=alt_adr]").click(function(){
		$("#alt_adr").slideToggle('normal');
	});
});

