$(function() {
	
	$('#top-nav').superfish({
		autoArrows: false
	});
	
	/**
	 * Template
	 *
	 */
	$("#manufacturer-select").change(function() {
		window.location.href = "/brands/" + this.value;
	});
	
	$("#search-input").focus(function() {
		if (this.value == "Product Search") {
			this.value = "";
		}
	}).blur(function() {
		if (this.value == "") {
			this.value = "Product Search";
		}
	});
	
	$("#newsletter input[type=text]").focus(function() {
		if (this.value == "Email Address") {
			this.value = "";
		}
	}).blur(function() {
		if (this.value == "") {
			this.value = "Email Address";
		}
	});
	
	$(".login-email").focus(function() {
		if ($(this).val() == 'Your email address') {
			$(this).css("color", "#000").val("");
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).css("color", "#ccc").val("Your email address");
		}
	});
	
	$(".login-password").focus(function() {
		if ($(this).val() == 'password') {
			$(this).css("color", "#000").val("");
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).css("color", "#ccc").val("password");
		}
	});
	
	
	
	
	/**
	 * Checkout
	 * 
	 */
	$("#new-billing a").click(function() {
		$("#new-billing a").hide();
		$("#choose-billing").hide(0, function() {
			$("#new-billing-form").show(0, function() {
				$("#cancel-new-billing").show();
			});
		});
		$("#choose-billing .select-address input").attr("checked", false);
		$("#new-billing-address").val(1);
		$("#billingFlashMessage").hide();
		return false;
	});
	$("#cancel-new-billing a").click(function() {
		$("#cancel-new-billing").hide();
		$("#new-billing").show();
		$("#new-billing-form").hide(0, function() {
			$("#choose-billing").show(0, function() {
				$("#new-billing a").show();
			});
		});
		$("#new-billing-address").val(0);
		return false;
	});
	

	if ($("#deliver-to-billing:checked").length > 0) {
		$("#delivery-adddress-box").hide();
	}

	$("#deliver-to-billing").click(function() {
		if ($(this).is(":checked")) {
			$("#delivery-adddress-box").hide();
		}
		else {
			$("#delivery-adddress-box").show();
		}
	});

	
	$("#new-shipping a").click(function() {
		$("#new-shipping a").hide();
		$("#choose-shipping").hide(0, function() {
			$("#new-shipping-form").show(0, function() {
				$("#cancel-new-shipping").show();
			});
		});
		$("#choose-shipping .select-address input").attr("checked", false);
		$("#new-shipping-address").val(1);
		return false;
	});
	$("#cancel-new-shipping a").click(function() {
		$("#cancel-new-shipping").hide();
		$("#new-shipping").show();
		$("#new-shipping-form").hide(0, function() {
			$("#choose-shipping").show(0, function() {
				$("#new-shipping a").show();
			});
		});
		$("#new-shipping-address").val(0);
		return false;
	});
	
	/**
	 * Mini Basket Status Update
	 * 
	 */
	var basketMsg = $("#basket-update");
	if (basketMsg.length == 1) {
		setTimeout(function() {
			basketMsg.fadeOut(2000);
		}, 5000);
	}
	
	$("#basket-update #close, #basket-update #continue").click(function() {
		$("#basket-update").fadeOut();
		return false;
	});
	
	
	/**
	 * Basket
	 * 
	 */
	$("#shipping-location-select").change(function() {
		$("#basket-form").submit();
	});
	$("#del-services input").click(function() {
		$("#basket-form").submit();
	});
	
	
	$("#decrease-qty").click(function() {
		if ($(".qty-input").val() > 1) {
			$(".qty-input").val(parseInt($(".qty-input").val()) - 1);
		}
	})
	$("#increase-qty").click(function() {
		$(".qty-input").val(parseInt($(".qty-input").val()) + 1);
	})
	
	$(".basket-qty a").click(function() {
		$("#basket-form").submit();
		return false;
	});
	

	/**
	 * Product View
	 * 
	 */
	$(".decrease-qty").live("click", function() {
		if ($(".qty-input").val() > 1) {
			$(".qty-input").val(parseInt($(".qty-input").val()) - 1);
		}
	})
	$(".increase-qty").live("click", function() {
		$(".qty-input").val(parseInt($(".qty-input").val()) + 1);
	})

	$(".custom-options select").change(function() {

		$("#stock-price-buy").load(
			"/ajax/catalog/get_price_and_stock/" + productID,
			$("#product-form").serialize()
		);
		
		if (typeof valuesStockIDs === "undefined") {
			return;
		}

		var values = "";		
		$(".custom-options select").each(function() {
			values += $(this).val() + "-";
		});
		values = values.substring(0, values.length - 1);

		var stockID = valuesStockIDs[values];
		
		$("#image-box").load('/ajax/catalog/get_var_images_box/' + stockID, initZoom);

	});


	$(".custom-options img").ajaxStart(function() {
		$(this).css("visibility", "visible");
	})
	$(".custom-options img").ajaxSuccess(function() {
		$(this).css("visibility", "hidden");
	})
	
	$("#add-to-basket-submit").click(function() {
		$(this).parents("form").attr("action", "/basket/add");
	});

	

	$(".attr-filter dt a").click(function() {
		
		var attrID = $(this).attr("id").substring(5);
		var valclass = ".attr-" + attrID + "-value";		

		if ($(valclass).css("display") == "block")
		{
			$(this).css("backgroundImage", "url(/img/icons/bullet_toggle_plus.png)");
			$(valclass).hide();
		}
		else
		{
			$(this).css("backgroundImage", "url(/img/icons/bullet_toggle_minus.png)");
			$(valclass).show();
		}

		return false;

	});

	$("#manu-filter dt a").click(function() {
		
		var dd = $(this).parent().parent().find("dd");
		
		if (dd.css("display") == "block")
		{
			$(this).css("backgroundImage", "url(/img/icons/bullet_toggle_plus.png)");
			dd.hide();
		}
		else
		{
			$(this).css("backgroundImage", "url(/img/icons/bullet_toggle_minus.png)");
			dd.show();
		}

		return false;
		
	});
	
	$("dl.closed dd").hide();

});

function initZoom() {
	$('.jqzoom').jqzoom({
		zoomType: 'innerzoom',
		preloadImages: false,
		alwaysOn: false,
		zoomWidth: 342,
		zoomHeight: 342,
		title: ''
	});
}


