$(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").live("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(($(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();
		}
		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.
	$("table#customer_details :input").keyup(function() {
		if(!$("input[name=deliveryID]").val()) {
			var current = $("#"+ ($(this).attr("id").replace(/customer/, "delivery")));
			if(!$(current).is(":disabled")) { $(current).val($(this).val()); }
		}
	}).blur(function() {
		if(!$("input[name=deliveryID]").val()) {
			var current = $("#"+ ($(this).attr("id").replace(/customer/, "delivery")));
			if(!$(current).is(":disabled")) { $(current).val($(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.
	$("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 confirm that this is the full postcode of your required delivery address.")) {
			return true;
		} else {
			return false;
		}
	});
	
});