$(document).ready(function() {
				
	// Initialte a few jQuery classes.
	//$(document).pngFix();
	//$(document).heatmap();
	
	$(".lightbox").colorbox();
	$(".preview_page").colorbox({iframe: true, width: 1000, height: 600});
	$(document).ajaxComplete(function() { $(".lightbox").colorbox(); });
	
	$("form.validate").validate();
	
	
	// Add a hover class to all input boxes.
	$("input[type=button], input[type=submit]").live("mouseover", function() { $(this).addClass("hover"); });
	$("input[type=button], input[type=submit]").live("mouseout", function() { $(this).removeClass("hover"); });
	
	
	// Tooltips.
	$(".tooltip").live("mouseover", function(e) {
		this.t = this.title;
		this.title = "";
		$("body").append('<span id="tooltip">'+ this.t +'</span>');
		$("#tooltip").css("top",(e.pageY-10) +"px").css("left",(e.pageX-300) +"px").fadeIn("fast");
	}).live("mouseout", function() {
		this.title = this.t;
		$("#tooltip").remove();
	});	
	$(".tooltip").live("mousemove", function(e) {
		$("#tooltip").css("top",(e.pageY-10) +"px").css("left",(e.pageX-300) +"px");
	});
	
	
	// Previous and next buttons.
	$("p#product_previous a, p#product_next a").click(function() {
		for(i=1; i<100; i++) {
			if($("#product_image_"+ i).is(":visible")) {
				var prev = (i - 1); var next = (i + 1);
			}
		}
		$("p#product_previous, p#product_next").removeClass("deactivated");
		if(($(this).parent("p").attr("id") == "product_next") && ($("p#product_image_"+ next).length > 0)) {
			$("p.product_image, p.product_larger").hide();
			$("p#product_image_"+ next +", p#product_button_"+ next).show();
			if($("p#product_image_"+ (next+1)).length == 0) { $("p#product_next").addClass("deactivated"); }
		}
		if(($(this).parent("p").attr("id") == "product_previous") && ($("p#product_image_"+ prev).length > 0)) {
			$("p.product_image, p.product_larger").hide();
			$("p#product_image_"+ prev +", p#product_button_"+ prev).show();
			if($("p#product_image_"+ (prev-1)).length == 0) { $("p#product_previous").addClass("deactivated"); }
		}
		return false;
	});
	
	
	// Previous and next thumbnail images.
	$("a.product_thumbnail").live("click", function() {
		var id = $(this).attr("id").replace(/product_thumbnail_button_/, "");
		for(i=1; i<100; i++) { $("#product_image_"+ i).hide(); }
		$("#product_image_"+ id).show();
		return false;
	});
	
	
	// Star rating system.
	$(".star_rating a").live("click", function() {
		if($(this).text() == $("#review_rating").val()) {
			$("#review_rating").val(0);
			$(".star_rating li.current").css("width", "0%");
		} else {
			$("#review_rating").val($(this).text());
			$(".star_rating li.current").css("width", (20*$(this).text())+"%");
		}
	});
			
			
	// Get the shipping details.
	function checkout(getdata) {
		$.ajax({
			type: "GET",
			url: "/cart/includes/checkout.php",
			data: getdata,
			success: function(e) {
				$("#delivery_charge").html(e);
				$("#del_country").change(function() { checkout("country="+ $("#del_country").val()); });
				$("#postcode_submit").click(function() {
					checkout("country="+ $("#del_country").val() +"&postcode="+ $("#del_postcode").val()); return false;
				});
				$("#del_method").change(function() { checkout("country="+ $("#del_country").val() +"&postcode="+ $("#del_postcode").val() +"&method="+ $("#del_method").val()); });
			}
		});
	}
	
	if($("#delivery_charge").length > 0) { $("#delivery_charge").ready(function() { checkout(); }); }


	// Copy the customer data to the delivery table.
	var copy_address = function(id, value) {
		if(!$("input[name=deliveryID]").val()) {
			var rep = id.replace(/customer/, "delivery");
			var current = $("#"+ rep + ":visible");
			if($(current).length > 0) { $(current).val(value); }
		}	
	}
	
	if($("table#customer_details").length > 0) {
		$("table#customer_details input").each(function() {
			$(this).keyup(function() {
				copy_address($(this).attr("id"), $(this).val());
			}).blur(function() {
				copy_address($(this).attr("id"), $(this).val());
			});
		});
	}
	
	
	// Perform a LUHN check on the credit card number to make sure it validates.
	$("#card_number").blur(function() {
		$("#card_number").removeClass("highlighted");
		$.ajax({
			type: "GET",
			url: "/cart/includes/luhn.php",
			data: "type="+ $("#card_type").val() +"&number="+ $("#card_number").val(),
			success: function(e) {
				if(e) {
					$("#card_number").addClass("highlighted");
					alert(e);
				}
			}
		});
	});
	
	
	// Show the issue number if it's a solo card.
	$("#card_type").change(function() {		
		if($(this).children("option:selected").attr("rel") == "in") {
			$("tr#issue_number").show();
		} else {
			$("tr#issue_number").hide();
		}
	});
	
	
	
	// Validate that the payment can be submitted.
	$("input[name=payment_submit]").click(function() {
		var m="", error=false;
		var b = $("#card_name").css("border");
		if($("#card_name").val().length == 0) { m += "Please make sure you have filled in your Card Billing Name<br />"; 		error = true; $("#card_name").css({"border":"1px solid #c00"}); } else { $("#card_name").css({"border": b}); }
		if($("#card_type").val().length == 0) { m += "Please make sure you have filled in your Card Type<br />"; 				error = true; $("#card_type").css({"border":"1px solid #c00"}); } else { $("#card_type").css({"border": b}); }
		if($("#card_number").val().length == 0) { m += "Please make sure you have filled in your Card Number<br />"; 			error = true; $("#card_number").css({"border":"1px solid #c00"}); } else { $("#card_number").css({"border": b}); }
		if($("#card_end_month").val().length == 0) { m += "Please make sure you have filled in your Card Expiry Month<br />"; 	error = true; $("#card_end_month").css({"border":"1px solid #c00"}); } else { $("#card_end_month").css({"border": b}); }
		if($("#card_end_year").val().length == 0) { m += "Please make sure you have filled in your Card Expiry Year<br />"; 	error = true; $("#card_end_year").css({"border":"1px solid #c00"}); } else { $("#card_end_year").css({"border": b}); }
		if($("#card_cvc").val().length == 0) { m += "Please make sure you have filled in your Card CVC Security Code<br />"; 	error = true; $("#card_cvc").css({"border":"1px solid #c00"}); } else { $("#card_cvc").css({"border": b}); }
		if($("#card_type").children("option:selected").attr("rel") == "in") {
			if($("#card_issue").val().length == 0) { m += "Please make sure you have filled in your Card Issue Number<br />"; 	error = true; $("#card_issue").css({"border":"1px solid #c00"}); } else { $("#card_issue").css({"border": b}); }
		}
		if(error === true) {
			$("#info_message").addClass("error").html(m).fadeIn();
			return false;
		} else {
			return true;
		}
	});
	
	
	// Product filtering tab.
	$("p#filtering a").live("click", function() {
		if($("#filtering_options").is(":visible")) {
			$("#filtering_options").fadeOut();
		} else {
			$("#filtering_options").fadeIn();
		}
		return false;
	});
	
	
	
	// Update the database when the Paypal form is posted.
	$("#paypal_form input[type=submit]").live("click", function() {
		$.ajax({
			type: "GET",
			url: "/cart/includes/pending.php",
			success: function(e) { $("#paypal_form").submit(); },
			error: function(e) { return false; }
		});
		return false;
	});
	
	
	
	// Get the details of the phone payment system.
	$("a#pay_by_phone").live("click", function() {
		$.ajax({
			type: "GET",
			url: "/cart/includes/phone.php",
			data: "id="+ $(this).attr("rel"),
			success: function(e) {
				$("p.cart_buttons").hide();
				$("p#paypal_instructions").hide();
				$("#pay_by_phone_info").html(e).slideDown();
				$("#pay_by_phone_info_buttons").slideDown();
			}
		});
		return false;
	});
	
	
	
	// Print an invoice. 
	$("a#print_invoice, a.print_despatch_note, a#print_packing").click(function() {
		window.open($(this).attr("href"), "invoice", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=false,scrollbars=1,width=690,height=500");
		return false;
	});
	
	
	
	// Reorder items.
	$("form[name=reorder_items] input[type=checkbox]").click(function() {
		var count = 0;
		$("form[name=reorder_items] input[type=checkbox]").each(function() {
			if($(this).is(":checked")) { count += 1; }
		});
		if(count > 0) {
			$("input[name=submit_reorder]").fadeIn();
		} else {
			$("input[name=submit_reorder]").fadeOut();
		}
	});
	
	
	
	// Double check that the postcode submitted is for the delivery address.
	$("#postcode_submit").live("click", function() {
		if(confirm("Please click 'Ok' to confirm that this is the full postcode of your required delivery address.")) {
			return true;
		} else {
			return false;
		}
	});
	
	
	
	// Double check that the postcode submitted is for the delivery address.
	$("a#deliveryPostcodeInfo").click(function() {
		confirm("If you wish to change your delivery postcode, please click the 'Previous Page' button below in order to recalculate your shipping costs.");
		return false;
	});
	
	
	// Postcode finder system.
	
	function postcode_finder() {
		if($("#del_postcode_finder").length > 0) { var postcode = $("#del_postcode_finder").val(); } else { var postcode = ""; }
		$("#postcode_finder").html("Loading...");
		$.ajax({
			type: "GET",
			url: "/cart/includes/postcode.php",
			data: "postcode="+ postcode,
			success: function(e) {
				$("#postcode_finder").html(e);
				$("#postcode_finder_submit").click(function() { postcode_finder(); });
				$("#postcode_finder_id").change(function() { address_finder(); });
			}
		});
	}
	
	function address_finder() {
		if($("#postcode_finder_id").length > 0) { var id = $("#postcode_finder_id").val(); } else { var id = ""; }
		$.ajax({
			type: "GET",
			url: "/cart/includes/address.php",
			data: "id="+ id,
			success: function(e) {
				$("#customer_details_wrap").html(e);
				$("table#customer_details input").each(function() {
					copy_address($(this).attr("id"), $(this).val());
					$(this).keyup(function() {
						copy_address($(this).attr("id"), $(this).val());
					}).blur(function() {
						copy_address($(this).attr("id"), $(this).val());
					});
				});
			}
		});
	}
	
	if($("#postcode_finder").length > 0) { postcode_finder(); }
	
	
	// Advert click tracking, including Google Analytics if pageTracker is present
	
	$(".adlink").click(function(){
		var lnk = $(this).attr("rel");
		var name = $(this).attr("title");
		var href = $(this).attr("href");
		var val = parseFloat(lnk.substring(lnk.lastIndexOf("/")+1));
		$.ajax({     
			url: lnk
		});
		try { pageTracker._trackEvent("Advert", name, href, val); } catch(err) {}
	});
	
	$("a.gaEventTrack").click(function(){
		var name = $(this).attr("rel");
		var title = $(this).attr("title");
		try { pageTracker._trackEvent("Tracked Links", name, title); } catch(err) {}
	});
	
	
	// Switch the postcode validation based on country.
	
	function checkCountry(e,c) {
		var id = $(e).val();
		var t = $(c).parent("td").parent("tr").find("td.left").text();
		if(id == "826") {
			$(c).addClass("val_anything");
			$(c).parent("td").parent("tr").find("td.left").text(t.replace(/\*/,"") + "*");
		} else {
			$(c).removeClass("val_anything");
			$(c).parent("td").parent("tr").find("td.left").text(t.replace(/\*/,""));
		}
	}
	
	checkCountry($("#customerCountry"),"#customerPostcode");
	checkCountry($("#deliveryCountry"),"#deliveryPostcode");
	$("#customerCountry").change(function() { checkCountry(this,"#customerPostcode"); });
	$("#deliveryCountry").change(function() { checkCountry(this,"#deliveryPostcode"); });
	
	
	// How did you hear about us form.
	
	if($("select[name=c_customerSource]").length > 0) {
	
		var $s = $("select[name=c_customerSource]");
		var $o = $("input[name=marketing_source_other]");
		
		var color = $o.css("color");
		var border = $o.css("border-color");
		
		$s.change(function() {
			var title = $s.children("option:selected").attr("title");
			if(title == "other") {
				$o.attr("disabled", "").css({"border-color": "#a00", "color": "#000"}).val("");
			} else {
				$o.attr("disabled", "disabled").css({"border-color": border, "color": color}).val("");
			}
		});
		
	}
	
	
	
	// Remove items from the basket dynamically
	
	if($("table.cart td.cart_delete a").length > 0) {
		$("table.cart td.cart_delete a").click(function() {
			$e = $(this);
			$.ajax({
				url: $e.attr("href"),
				success: function(x) {
					$e.parents("tr").fadeOut(1000, function() {
						checkout();
						$.ajax({
							url: "/cart/includes/cart.php",
							success: function(c) {
								$e.parents("table").replaceWith(c);
							}
							
						});
					});
				}
			});
			return false;
		});
	}	
	
	
	/* Remove the payment confirm button after being clicked.  */
	
	$("#confirm_payment").parents("form").submit(function() {
		$("#confirm_payment").hide().after('<span style="float:right;font-weight:bold;color:#c00;">Processing, please wait... This may take up to a minute.</span>');
		return true;
	});
	
	
});
