// JavaScript Document

$(document).ready(function() {
						   

	/* Minicart. */

	$("#view_basket a").click(function() {
		$.ajax({
			url: "/custom/addons/minicart.php",
			success: function(e) {
				$("#top").append(e);
				$("#minicart").slideDown();
				$("#close_basket img").click(function() {
					$("#minicart").slideUp().remove();
				});
			}
		});
		return false;
	});
	
	
	/* Sub navigation menus. */

	$("#nav ul a").click(function() {
		if($(this).hasClass("active_tab")) {
			$(".subnav").slideUp();
			$("#nav ul a").removeClass("active_tab");
		} else {
			$("#nav ul a").removeClass("active_tab");
			$(this).addClass("active_tab");
			var id = $(this).attr("rel");
			var a = "#subnav_"+ $(this).attr("rel") +" .subnav_left a";
			$(".subnav").remove();
			$.ajax({
				url: "/custom/addons/subnav.php",
				data: "id="+ id,
				success: function(e) {
					$("#wrapper").append(e);
					$("#subnav_"+id).slideDown();
					$(a).mouseenter(function() {
						var rel = "#subnavm_"+ $(this).attr("rel");
						var relr = "#subnavr_"+ $(this).attr("rel");
						$(a).parent("li").removeClass("active_tab");
						$(this).parent("li").addClass("active_tab");
						$(".subnav_middle").hide(); $(rel).show();
						$(".subnav_right").hide(); $(relr).show();
					});
					$("#subnav_"+id).hover(function() {
						$(this).addClass("thisIsActive");
					},function() {
						$(this).removeClass("thisIsActive").slideUp().queue(function() { $(".subnav").remove(); $("#nav ul a").removeClass("active_tab"); });
					});
				}
			});
		}
		return false;
	});

	
	/* Product stock styling. */
	
	$("table#product_stock tbody tr:odd").addClass("alt");
	
	
	/* Product tabs. */
	
	$("#product_tabs a").click(function() {
		$("#product_tabs a").removeClass("active_tab");
		$(this).addClass("active_tab");
		$("#product_tab_details div.showhide").addClass("hidden").hide();
		$("#"+ $(this).attr("rel")).removeClass("hidden").fadeIn();
		return false;
	});
	
	
	/* Product tabs. */
	
	$(".builddrop").change(function() {
		$l = $("select[name=l]").children("option:selected");
		$c = $("select[name=c]").children("option:selected");
		$r = $("select[name=r]").children("option:selected");
		var html_l = '<img src="/graphics/products/'+ ($l.attr("rel") != "" ? $l.attr("rel") : "l0.jpg") +'" />';
		var html_c = '<img src="/graphics/products/'+ ($c.attr("rel") != "" ? $c.attr("rel") : "c0.jpg") +'" />';
		var html_r = '<img src="/graphics/products/'+ ($r.attr("rel") != "" ? $r.attr("rel") : "r0.jpg") +'" />';
		$(".osleft span").text($l.attr("class")+" metres");
		$(".osright span").text($r.attr("class")+" metres");
		$(".builder").html(html_l + html_c + html_r);
	});
	
	
	/* Custom Build Menu. */
	
	var dtext = $("#price_data td.red").text();
	function custom_build() {
		$len = parseFloat($("input[name=o]").val());
		$d = $("#product_discount").val() ? parseFloat($("#product_discount").val()) : 0;
		$l = parseFloat($("select[name=l] option:selected").attr("title"));
		$c = parseFloat($("select[name=c] option:selected").attr("title"));
		$r = parseFloat($("select[name=r] option:selected").attr("title"));
		var lentotal = ($c * $len);
		var subtotal = parseFloat(lentotal) + ($l>0 ? $l : 0) + ($r>0 ? $r : 0);
		var discount = ((subtotal / 100) * $d);
		var finaltotal = (subtotal - discount);
		if($len > 0 && $c > 0) {
			var price_data  = $d ? '<tr> <td>Normal Price:</td> <td class="red">£'+ subtotal.toFixed(2) +'</td> </tr>' : '';
				price_data += $d ? '<tr> <td>Discount:</td> <td class="red">'+ $d.toFixed(2) +'%</td> </tr>' : '';
				price_data += '<tr class="final"> <td>Your Price:</td> <td class="red">£'+ finaltotal.toFixed(2) +'</td> </tr>';
			$("#price_data").html(price_data);
			$("#product_buy").slideDown();
		} else {
			$("#price_data").html('<tr> <td class="red">'+ dtext +'</td> </tr>');
			$("#product_buy").slideUp();
		}
	}
	
	$("input[name=o]").keyup(function() { custom_build(); });
	$(".builddrop").change(function() { custom_build(); });

	
	/* Search box. */
	
	var searchtext = $("#qs").val();
	var searchcolor = $("#qs").css("color");
	
	$("#qs").focus(function() {
		if($(this).val() == searchtext) { $(this).val("").css("color", "#000"); }
	}).blur(function() {
		if($(this).val() == "" || $(this).val() == searchtext) { $(this).val(searchtext).css("color", searchcolor); }
	});
	
	
	/* Technical articles. */
	
	$(".tech_article a").click(function() {
		var t = this;
		var r = $(this).attr("rel");
		$(".tech_article_text").slideUp();
		$(".tech_article a").text("View Article");
		if($("#tech_"+ r).is(":hidden")) {
			$("#tech_"+ r).fadeIn();
			$.ajax({
				type: "GET",
				url: "/custom/addons/tech.php",
				data: "id="+ r,
				success: function(e) {
					$("#tech_"+ r).html(e);
					$(t).text("Close Article");
				}
			});
		}
		return false;
	});
	
	
	
	/* Display a message if the add to cart button is clicked and no option is set. */
	
	$(".product_add_main .product_add").click(function() {
		var count = 0;
		$("input[name^=productStock]").each(function() {						   
			if($(this).is(":checked") == true) { count++; }
		});
		if(count == 0) {
			alert("Please make sure you have selected a stock option before continuing.");
			return false;
		}
	});
	
	
	
	/* Leave a parcel checkbox. */
	
	$("input[name='shipping_submit']").live("click", function() {
		if($("input[name='o_orderSignature']").is(":checked")) {
			var val = $("input[name='o_orderDeliveryMessage']").val();
			if(val == "") {
				alert("Please make sure you have left delivery instructions for our courier.");
				$("input[name='o_orderDeliveryMessage']").css({"border": "1px solid #c00"}).focus();
				return false;
			}
		}
	});
	
	
	
	/* Checkout. */
	
	function checkout(getdata) {
		$.ajax({
			type: "GET",
			url: "/cart/includes/checkout.php",
			data: getdata,
			success: function(e) {
				$.ajax({
					url: "/cart/includes/cart.php",
					success: function(c) {
						$find = '<tr class="cart_total"><td class="cart_text noshow">&nbsp;</td><td class="cart_quantity noshow">&nbsp;</td>';
						$replace = '<tr class="cart_total"><td class="cart_text noshow">&nbsp;</td><td class="cart_quantity noshow"><input type="submit" name="update_cart" value="Update" /></td>';
						c = c.replace($find,$replace);
						$("table.cart:not(.cart_notes)").replaceWith(c);
					}
				});				
				$("#delivery_charge").html(e);
				$("#del_country").unbind().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").unbind().change(function() { checkout("country="+ $("#del_country").val() +"&postcode="+ $("#del_postcode").val() +"&method="+ $("#del_method").val()); });
			}
		});
	}
	
	if($("#delivery_charge").unbind().length > 0) { $("#delivery_charge").ready(function() { checkout(); }); }
	
	
	/* Slider. */
	
	$("#slider").cycle();
	
});
