$(function(){

	///

	$("#page").css("min-height", $("#navigation").height() - 53);

	///

	$("#browse form select[name=make]").change(function(){

		$.ajax({

			url:"/engine/ajax/products.php",
			data:"make=" + $(this).val(),
			type:"get",
			dataType:"json",
			success:function(data)
			{
			
				$("#browse form select[name=model]").empty();
				$("#browse form select[name=engine]").empty();

				for (i = 0; i < data.length; i++)
				{
					$("#browse form select[name=model]").append("<option value='" + data[i]["id"] + "'>" + data[i]["label"] + "</option>");
				}

				$("#browse form select[name=model] option:first-child").attr("selected", "selected");
				$("#browse form select[name=model]").trigger('change');

			}

		});

	});

	$("#browse form select[name=model]").change(function(){

		$.ajax({

			url:"/engine/ajax/products.php",
			data:"make=" + $("#browse form select[name=make]").val() + "&model=" + $(this).val(),
			type:"get",
			dataType:"json",
			success:function(data)
			{

				$("#browse form select[name=engine]").empty();
			
				for (i = 0; i < data.length; i++)
				{
					$("#browse form select[name=engine]").append("<option value='" + data[i]["id"] + "'>" + data[i]["label"] + "</option>");
				}
			
			}

		});

	});

	$("#browse form button[type=submit]").click(function(){

		if (document.forms[0].engine.value != "")
		{
			document.forms[0].action = document.forms[0].engine.value;
			document.forms[0].submit();
		}
		
		return false;

	});

	///

	$("#order #page form").show();

	$("#order #page form table tr td.quantity select").change(function(){

		order_calculate(); return false;

	});

	$("#order #page form div.coupon button").click(function(){

		order_calculate(); return false;

	});

	$("#order #page form select[name=country]").change(function(){

		order_calculate(); return false;

	});

	$("#order #page form select[name=type]").change
	(
		function()
		{
			var type = $(this).val();

			if (type == "Maestro" || type == "Solo")
			{
				$("#order #page form select[name=issued_month]").parent().show();
			}
			else
			{
				$("#order #page form select[name=issued_month]").parent().hide();
			}
		}
	);

	///

	$("#modal-leads").show();

	$("#modal-leads form p button").click
	(
		function()
		{
			var name = $("#modal-leads form input[name='name']");
			var email = $("#modal-leads form input[name='email']");

			if (name.val() == "")
			{
				alert("Please enter your first name.");
				name.focus();
				return false;
			}

			if (email.val() == "")
			{
				alert("Please enter your email address.");
				email.focus();
				return false;
			}

			$(this).attr("disabled", "disabled");

			$.post
			(
				"/engine/ajax/leads.php",
				{
					action:"insert",
					name:name.val(),
					email:email.val()
				},
				function (data)
				{
					if (data == "ok")
					{
						$("#modal-leads form").hide();
						$("#modal-leads").append("<div class='success'><p><strong>Thanks and good luck!</strong></p></div>");
					}
				}
			);

			return false;
		}

	);

	///

	$("#shipping").hover(function(){

		$("#shipping img").stop().animate({height:"318px", width:"307px"}, 475);
		$("#shipping span").stop().animate({height:"318px", width:"307px"}, 500);
		
	},function(){

		$("#shipping img").stop().animate({height:"50px",width:"48px"}, 275);
		$("#shipping span").stop().animate({height:"48px",width:"48px"}, 250);

	});

	///

	var feedback;

	feedback  = "<ul>";
	for (i = 1; i <= 7; i++) {
	feedback += "<li><img src='/content/images/sidebar/feedback-"+i+".jpg' alt='' height='252' width='252' /></li>";
	}
	feedback += "</ul>";

	$("#sidebar").append(feedback);

	if ($("#sidebar ul").length > 0)
	{

		$("#sidebar ul").cycle("fade");

	}

	///

	$("<img />").attr("src", "/content/images/sidebar/video-1.gif").load(function(){

		$("#sidebar div.video.video-1").css("background-image", "url(/content/images/sidebar/video-1.gif)");

	});

	$("<img />").attr("src", "/content/images/sidebar/video-2.gif").load(function(){

		$("#sidebar div.video.video-2").css("background-image", "url(/content/images/sidebar/video-2.gif)");

	});

	$("<img />").attr("src", "/content/images/sidebar/video-3.gif").load(function(){

		$("#sidebar div.video.video-3").css("background-image", "url(/content/images/sidebar/video-3.gif)");

	});

	///

	if ($("a").length > 0)
	{

		$("a[href^='/modal/video.php?id=1']").colorbox({fixedWidth:558, fixedHeight:482, iframe:true});
		$("a[href^='/modal/video.php?id=2']").colorbox({fixedWidth:558, fixedHeight:482, iframe:true});
		$("a[href^='/modal/video.php?id=3']").colorbox({fixedWidth:558, fixedHeight:482, iframe:true});

		$("a[href^='/modal/cvc.php']").colorbox({fixedWidth:480, fixedHeight:636, iframe:true});
		$("a[href^='/modal/leads.php']").colorbox({fixedWidth:480, fixedHeight:288, iframe:true});
		$("a[href^='/modal/refunds.php']").colorbox({fixedWidth:600, fixedHeight:240, iframe:true});
		$("a[href^='/modal/tell.php']").colorbox({fixedWidth:480, fixedHeight:462, iframe:true});
		$("a[href^='/modal/units.php']").colorbox({fixedWidth:600, fixedHeight:372, iframe:true});
		$("a[href^='/modal/why.php']").colorbox({fixedWidth:600, fixedHeight:540, iframe:true});
	
		$("a[href^='http://finance.google.com/']").colorbox({fixedWidth:432, fixedHeight:204, iframe:true});

	}

	///

});

order_calculate = function()
{

	///

	var quantity = $("#order #page form table tr td.quantity select").val();
	var price = $("#order #page form input[name='original_price']").val() * quantity;
	var country = $("#order #page form select[name=country]").val();
	var shipping = 0;
	var coupon = $("#order #page form div.coupon input[name=coupon]").val().toUpperCase();
	var discount = 50 * quantity;

	///

	if (price/quantity > 100) {

		switch (country) {
	
			case "GB" :
				shipping += 7.5 * quantity; break;
			case "AT" :
			case "BE" :
			case "BG" :
			case "CY" :
			case "CZ" :
			case "DK" :
			case "EE" :
			case "FI" :
			case "FR" :
			case "DE" :
			case "GR" :
			case "HU" :
			case "IE" :
			case "IT" :
			case "LV" :
			case "LT" :
			case "LU" :
			case "MT" :
			case "NL" :
			case "PL" :
			case "PT" :
			case "RO" :
			case "SK" :
			case "SI" :
			case "ES" :
			case "SE" :
			case "CH" :
			case "GB" :
				shipping += 15 * quantity; break;
			default :
				shipping += 30 * quantity; break;
	
		}

	}

	///

	if (coupon != "") {

		if (coupon == "PAN") discount += 4;
		else if (coupon == "SEVENFIFTYCE1") discount += 7.5;
		else if (coupon == "TWENTYFIVECE1") discount += 25;
		else if (coupon == "FIFTYCE1") discount += 50;
		else if (coupon == "SEVENTYFIVECE1") discount += 75;
		else if (coupon == "ONEHUNDREDCE1") discount += 100;
		else if (coupon == "AE196BR") discount += 50;
		else if (coupon == "AUTOCAR") discount += 25;
		else if (coupon == "AUTOEXPRESS") discount += 25;
		else if (coupon == "EVOCAR") discount += 25;
		else if (coupon == "PERFORMACECAR") discount += 25;
		else if (coupon == "TOPGEAR") discount += 25;
		else if (coupon == "WHATCAR") discount += 25;
		else if (coupon == "WHATDIESEL") discount += 25;
		else if (coupon == "NOVATCE") discount += ((price - 50 + shipping) - (price - 50 + shipping)/1.175);
		else if (coupon.indexOf("OVERRIDE") == 0) discount += parseFloat(coupon.replace("OVERRIDE", ""));
		else {

			$("#order #page form div.coupon input[name=coupon]").val("");
			alert("Invalid Code");

		}

		if (discount > 50) {

			$("#order #page form div.coupon").hide();
			if (coupon == "NOVATCE") $("fieldset div.company").show();

		}

	}

	///

	$("#order #page form table tr td.price").text("\u00a3" + price.toFixed(2));
	$("#order #page form table tr td.shipping").text("\u00a3" + shipping.toFixed(2));
	$("#order #page form table tr td.discount").text("\u00a3" + discount.toFixed(2));
	$("#order #page form table tr td.total").text("\u00a3" + (price + shipping - discount).toFixed(2));

	///

	return false;

	///

}