$(function() {
	var sqm = $("#sqm").val();
	if(sqm != null && sqm > 0){
		n = $(".checkbox input:checked").length;
		
		var legservice = 'nee';
		if(n > 0){
			legservice = 'ja';
		}	
	
		calcOfferte($("#serie_id").val(),$("#kleur_id").val(), $("#sqm").val(), legservice);
		
		return false;
	}
	
	$('div#logo').click(function() {
		document.location.href=baseurl+'/index';
	});
	
	$('.product').click(function() {
        $(this).find('a[href]').eq(0).each(function() {
                document.location.href = $(this).get(0).href;
        });
        return false;
	});
	
	$("div.header_picture_switch").bind('mouseover', function(){
		$(this).addClass('header_picture_switch_selected');
	});
	
	$("div.header_picture_switch").bind('click', function(){
		$(this).siblings('.header_picture_switch_selected_click').removeClass('header_picture_switch_selected_click');
		$(this).addClass('header_picture_switch_selected_click');
	});
	
	$("div.header_picture_switch").bind('mouseout', function(){
		$(this).removeClass('header_picture_switch_selected');
	});
	
	$("div.header_picture_switch").click(function(){	
		$(this).parent().siblings('img').attr('src', baseurl+'/img/banner-'+$(this).attr("id").match(/\d+/)+'.png');
	});
	
	// Laatste product image geen margin anders komt ie op de volgende regel en
	// dat is lelijk
	$('div.product div.product_image:last').css('margin-right', '0');
	
	// Laatste separator weghalen
	$('div#filters div.filter_separator:last').css('display', 'none');
		
	// Bij klik op staal dan big image vervangen
	$('div.staal img').click(function(){
		//$('div#serie_detail_img_big').css('background-image', 'url("'+$(this).attr('src')+'")');
		$('img#serie_detail_img_big').attr('src', $(this).attr('src'));
		$('img#serie_detail_img_big').attr('alt', $(this).attr('alt'));
		$('img#serie_detail_img_big').attr('title', $(this).attr('title'));
		
		
		$('form.order_product_form input.kleur_id').val($(this).attr("id").match(/\d+/));
		$('form.add_product_form input.kleur_id').val($(this).attr("id").match(/\d+/));
		
		$('#serie_detail .title_center').html($(this).attr('alt'));
		$('div.staal').each(function(){
			$(this).removeClass('staal_selected');			
		})
		$(this).parent().addClass('staal_selected');
	});
	
	$("div#offerte_datums input").click(function(){
		$(this).val('');		
	});
	
	$("div#offerte_datums input[name=lev_d]").blur(function(){
		if($(this).val() == ''){
			$(this).val('dd');
		}
	});
	
	$("div#offerte_datums input[name=lev_m]").blur(function(){
		if($(this).val() == ''){
			$(this).val('mm');			
		}
	});
	
	$("div#offerte_datums input[name=lev_y]").blur(function(){
		if($(this).val() == ''){
			$(this).val('jjjj');	
		}
	});
	
	$("div#offerte_datums input[name=leg_d]").blur(function(){
		if($(this).val() == ''){
			$(this).val('dd');
		}
	});
	
	$("div#offerte_datums input[name=leg_m]").blur(function(){
		if($(this).val() == ''){
			$(this).val('mm');
		}
	});
	
	$("div#offerte_datums input[name=leg_y]").blur(function(){
		if($(this).val() == ''){
			$(this).val('jjjj');
		}
	});
	
	$("#sqm").keyup(function(){

		n = $(".checkbox input:checked").length;
		
		var legservice = 'nee';
		if(n > 0){
			legservice = 'ja'
		}	
		calcOfferte($("#serie_id").val(), $("#kleur_id").val(), $("#sqm").val(), legservice);
	});
	
	$(".checkbox").bind('click', function() {
		var legservice = 'nee'
		if($(this).hasClass("checkbox_active")) {
			$(this).removeClass("checkbox_active");
			$(this).children('input').removeAttr('checked');
			$('input',this).removeAttr('checked');
		} else {
			$(this).addClass("checkbox_active");
			$('input',this).attr('checked','checked');
			$(this).children('input').attr('checked','checked');
			if($(this).children('input:checked')){
				legservice = 'ja'
			}
		}
		
		calcOfferte($("#serie_id").val(), $("#kleur_id").val(), $("#sqm").val(), legservice);
		
		return false;
	});
		
	$('#footer_content img').hover(
		function() {
			var src = $(this).attr('src').split('.png');
			$(this).attr('src', src[0] +"_hover.png");
		},
		function() {
			var src = $(this).attr('src').split('_hover.png');
			$(this).attr('src', src[0]);
		}
	);
	
});

function calcOfferte(serie, kleur, sqm, legservice){
	
	if(sqm == '' || sqm == 0){
		$("#sqm").addClass('required_input');
		$("#sqm").focus();
		alert('Vul alstublieft het aantal vierkante meter in!'); 
		return false;
	}
	
    $.ajax({
			type: "POST",
			url: baseurl+"/bestel/berekenofferte/",
			data: 'serie_id='+serie+'&kleur_id='+ kleur +'&sqm='+sqm+'&legservice='+legservice,
			dataType: "json",
			success: function(data){
				$("td.netto_benodigd").html(data.benodigd+'m&sup2;');
				$("td.snijverlies").html(data.snijverlies+'m&sup2;');
				$("td.totaal_te_bestellen").html(data.totaal_te_bestellen+'m&sup2;');
				$("td.std_prijs").html('€ '+data.std_prijs);
				$("td.onze_prijs").html('€ '+data.onze_prijs);
				$("td.kl_order_toeslag").html('€ '+data.toeslag);
				$("td.legkosten").html('€ '+data.legkosten);
				$("td.totaal").html('€ '+data.totaal);
			}
		});
}
