$(document).ready(function(){
	$(".lightbox").colorbox({width:600, height:460,iframe:true,scrolling:false});
	$(".lightboxbig").colorbox({width:600, height:660,iframe:true,scrolling:false});
	$("a[rel='product_image']").colorbox({width:600});
});


function changeImage(url){
	$('#product_main_image').attr('src',url);
}

function selectColour(id){
	
	$("#colour_id").val(id);
	
	var sel_col = $("#colour_"+id);
	sel_col.removeClass('colour_show');
	sel_col.addClass('colour_hide');
	
	$("#colour_help").hide();
	
	var hideall = function(){
		$(".removeable_clear").css("clear","none");
		$("#quote_price_cont").show();
		$("#extra_cont").show();
		$("#available_colours").html('Selected Colour');
		$("#deselect_colour").show();
	};
	
	$(".colour_show").hide('slow',hideall);
	
	if(!$(".colour_show").length){
		hideall();
	}
	
	checkDimensions();

}

function deselectColour(){
	
	$(".removeable_clear").css("clear","both");
	
	$("#available_colours").html('Available Colours');
	$("#deselect_colour").hide();
	
	$("#quote_price_cont").hide();
	$("#extra_cont").hide();
	
	$(".colour_show").show('slow');
	
	$("#colour_help").show();
	
	var sel_col = $(".colour_hide");
	sel_col.addClass('colour_show');
	sel_col.removeClass('colour_hide');
}

var timer;

function timeCheck(call){
	clearTimeout(timer);
	timer = setTimeout(function (){eval(call);},'500');
}

function checkDimensions(){
	var width = $("#dim_width").val();
	var height = $("#dim_height").val();
	
	$("#drilling").hide();
	
	$("#dim_info").html('');
	$("#dim_info").activity({align:'left',length:4.1,space:1.5,width:1.2,segments:12});
	
	calcDimensions('dim');
	
}

function loadCart(msg){
	$("#cart_button")
		.addClass('disabled')
		.attr('disabled','disabled')
		.val(msg);
}

function unloadCart(){
	$("#cart_button")
		.removeClass('disabled')
		.removeAttr('disabled')
		.val('ADD TO CART');
}

function calcDimensions(type){
	
	var width = $("#dim_width").val();
	var height = $("#dim_height").val();
	
	var selected = $('#drilling_select').find("option:selected").text();
	
	if($("#product_openframed").is(':checked')) 
		var openframed = 1;
	else 
		var openframed = 0;
	
	
	loadCart('LOADING...');
				
	$.ajax({
		type: "POST",
		cache:false,
		url: "/checkDimensions.php",
		data: "rid="+ $("#group_id").val() + "&door="+ $("#prod_id").val() +"&width="+ width +"&height="+ height +"&openframed="+ openframed,
		success: function(msg){
			$("#dim_info").html(msg);
			
			if(type != 'dim') $('#drilling_select').val(selected);
			
			if($("#dim_flag").val() == 0){
				$("#dim_price_each").text($("#from_price").val());
				loadCart('CUSTOMISE DOOR');
			}
			else{
				$("#dim_price_each").text($("#calc_price").val());
				unloadCart();
			}
			
			changeQuantity();
		}
	 });
}

function checkDrilling(){
	var selected = $('#drilling_select').find("option:selected").text();
	
	loadCart('CUSTOMISE DOOR');
		
	if(selected == 'Yes'){
		$("#drilling").show();
		$("#product_price_drilling").show();
		checkDrillingDims();
	}
	else{
		$("#drilling").hide();
		$("#product_price_drilling").hide();
		calcDimensions('drilling');
	}
	
}

function changeQuantity(){
	
	$("#total_price").text('Total: ...');
	
	loadCart('CUSTOMISE DOOR');
	
	if($("#product_quantity").val() < 1 || ($("#product_quantity").val() - 0) != $("#product_quantity").val() && $("#product_quantity").val().length > 0) 
		$("#product_quantity").val(1);
	
	if($("#dim_flag").val() == 1){
		$.ajax({
			type: "POST",
			cache:false,
			url: "/calcPrice.php",
			data: "quantity="+ $("#product_quantity").val() + "&dooreach="+ $("#calc_price").val(),
			success: function(msg){
				$("#total_price").text(msg);
				unloadCart();
			}
		 });
	}
	else{
		$("#total_price").text("From: £"+$("#from_price").val());
	}
	
}

function addToCart(){
	
	loadCart('LOADING...');
					
	var width = $("#dim_width").val();
	var height = $("#dim_height").val();
	
	var d_selected = $('#drilling_select').find("option:selected").text();
	
	if(d_selected == 'Yes') 
		var drilling = 1;
	else 
		var drilling = 0;
		
	var side = $('#drilling_side').find("option:selected").text();
	
	var top = $("#drilling_top").val();
	var bottom = $("#drilling_bottom").val();
	
	if($("#product_openframed").is(':checked')) 
		var openframed = 1;
	else 
		var openframed = 0;
		
	if($("#product_cornerdoor").is(':checked')) 
		var corner = 1;
	else 
		var corner = 0;
		
	var pid = $("#prod_id").val();
	
	var colour = $("#colour_id").val();
	
	var quantity = $("#product_quantity").val();
	
	var query = "pid="+ pid +"&colour="+ colour +"&openframed="+ openframed +"&width="+ width +"&height="+ height +"&drilling="+ drilling +"&side="+ side +"&top="+ top +"&bottom="+ bottom +"&corner="+ corner +"&quantity="+ quantity;
	
	$.ajax({
		type: "POST",
		cache:false,
		url: "/checkAddDoor.php",
		data: query,
		success: function(msg){
			$("#alert_box").popup_alert({
            	message: $("#group_name").val()+ ' - '+$("#prod_name").val() + ', has been added to the Cart'
            });
			
			unloadCart();
					
			refreshCart();
		}
	 });
	
}

function refreshCart(){
	
	$("#mini_cart_data")
		.css('min-height',$("#mini_cart_data").innerHeight())
		.html('');
		
	$("#mini_cart_data").activity({align:'center',color:'#fff',length:4.1,space:1.5,width:1.2,segments:12});
	
	$.ajax({
		type: "POST",
		cache:false,
		url: "/minicart.php",
		success: function(msg){
			$("#mini_cart_data")
				.html(msg)
				.css('min-height','');
		}
	});
}

function checkDrillingDims(){
	var top = $("#drilling_top").val();
	var bottom = $("#drilling_bottom").val();
	var height = $("#dim_height").val();
	
	$("#drilling_dim_info").html('');
	$("#drilling_dim_info").activity({align:'left',length:4.1,space:1.5,width:1.2,segments:12});
	
	var query =  "height="+ height +"&top="+ top +"&bottom="+ bottom;
	
	loadCart('LOADING...');
		
	$.ajax({
		type: "POST",
		cache:false,
		url: "/checkDrillingDims.php",
		data: query,
		success: function(msg){
			$("#drilling_dim_info").html(msg);
			
			if($("#drilling_dim_flag").val() == 0){
				loadCart('CUSTOMISE DOOR');
			}
			else{
				unloadCart();
			}
		}
	});
}

function tempLoad(){
	loadCart('LOADING...');
}

function removeItem(form){
	$("#"+form).val(0);
	$("#shopping_bag").submit();
}

function applyCode(){
	$("#checkout_form").attr('action',$("#checkout_load").val()).submit();
}

function removeCode(){
	$("#checkout_form").attr('action',$("#checkout_load").val()).submit();
}

function changePaymentType(element){
	var element = $(element);
	
	if(element.val() == 'paypal'){
		$('#checkout_form').attr('action',$("#paypal_url").val());
	}
	else if(element.val() == 'telephone'){
		$('#checkout_form').attr('action',$("#telephone_url").val());
	}
}
