$(document).ready(function() {
				
	// Initialte a few jQuery classes.
	$(document).pngFix();
	
	$(".lightbox").colorbox();
	$(".preview_page").colorbox({contentIframe: true, contentWidth: 1000, contentHeight: 600});
	
	$("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() {
					if(confirm("Please confirm this is the full postcode of the delivery address, NOT billing address.")) {
						checkout("country="+ $("#del_country").val() +"&postcode="+ $("#del_postcode").val()); return false;
					} else {
						return false;
					}
				});
				$("#del_method").change(function() { checkout("country="+ $("#del_country").val() +"&postcode="+ $("#del_postcode").val() +"&method="+ $("#del_method").val()); });
			}
		});
	}
	
	if($("form#delivery_charge").length > 0) { $("form#delivery_charge").ready(function() { checkout(); }); }
	
	
	// Copy the customer data to the delivery table.
	function copy_address(that) {
		if(!$("input[name=deliveryID]").val()) {
			var current = $("#"+ ($(that).attr("id").replace(/customer/, "delivery")));
			if(!$(current).is(":disabled")) { $(current).val($(that).val()); }
		}	
	}
	
	$("table#customer_details :input").keyup(function() { copy_address(this); }).blur(function() { copy_address(this); });
	
	
	// 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.
	$("select#card_type").change(function() {					
		if($(this).val() == "Switch/Maestro") {
			$("tr#issue_number").show();
		} else {
			$("tr#issue_number").hide();
		}
	});
	
	
	// Show the issue number if it's a solo card.
	//$("select#card_type").change(function() {					
	//	if($(this).attr("rel") == "in") {
	//		$("tr#issue_number").show();
	//	} else {
	//		$("tr#issue_number").hide();
	//	}
	//});
	
	
	// 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); });
				$("table#customer_details :input").keyup(function() { copy_address(this); }).blur(function() { copy_address(this); });
			}
		});
	}
	
	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) {}
	});
	
	
	
});