$(document).ready(function() {		

	manageProductMenu();
	manageBreadcrumbs();

	processCustomisations();
   
	// Hook to certain click events ALWAYS
        $(".productSubmitInput").live("click", function(){ processCartCustomisations(); });
	$(".productitemcell a").live("click", function(){ processCartCustomisations(); });	
	$("input.update").live("click", function(){ processCartCustomisations(); });

	// slider for the home page
	$('#slider').nivoSlider({
		  effect:'fade',
		  controlNav: false,
		  directionNav: false,
		  animSpeed:1200, // Slide transition speed
		  pauseTime:6000 // How long each slide will show
	});

	$("#catwebformform42059").formToWizard({ 
		submitButton: 'catwebformbutton2' 
	});

	var progressText = $("span#progressText");
	if(progressText) {
		var currentStepText = $("#steps .current span").text();
		progressText.text(currentStepText);	
	}

// set current country selection
var url = document.domain;
//alert(url);
if(url.indexOf("us.") >= 0)  {
		$("#usOption").attr("checked", "checked");
		$(".usOnly").show();
		$("#euOption").removeAttr("checked");
		$(".euOnly").hide();
	} else {
	$("#usOption").removeAttr("checked");
		$(".usOnly").hide();
		$("#euOption").attr("checked", "checked");
		$(".euOnly").show();
	}

});

function processCustomisations()  {

	heightMatch();
	processCartCustomisations();
	processOnSaleProducts();

	// hide poplets with no images to display
	$('.poplets img[src$="/-tiny.jpg"]').hide();

   // change region selector class
   var element = $("#worldTextColour").val();
	if(element == "white") {
		$("#regionSelector").removeClass("blackText");
		$("#regionSelector").removeClass("whiteText");
	}

	var shippingOptions = $(".shippingDropDown");
	if(shippingOptions) {
		sortShippingOptions();
	}
	
	// fix blog images
	$(".post-body a img").attr("align", "center");
}

function processOnSaleProducts() {
	var isOnSale = $("#onSale").val();
	//alert('isOnSale = ' + isOnSale);
	
	if(isOnSale == 1) {
		$("#normalPrice").hide();
		$("#salePrice").show();		
	}	
}

function processCartCustomisations()  {

	// change cart text accordingly
	var cartLink = $("td.cartSummaryItem a"); 
	if(cartLink.text() != "") {
		cartLink.text("View bag");
	} else { 
		var cartItem = $("td.cartSummaryItem");  
		cartItem.text("Shopping bag is empty");
	}

	var removeLink = $(".removeLink a");
	if(removeLink) {
		removeLink.text("Update");
	}

	// hide/show the gift message
	$(".catProdAttributeItem input").click(function() {
		$(".gift-msg").removeClass("hide");
	});

	// set the default price for the gift wrapping service
	var giftWrapText = $('.catProdAttributeItem span').text().indexOf("4.13");
	if(giftWrapText > 1) {	
		$('.catProdAttributeItem span').replaceWith("<span>Gift wrap this item please &pound;4.95</span>");  	
	}
}

function sortShippingOptions() {

	// get the options and loop through them
	var $options = $(".shippingDropDown option");
	var arrVals = [];
	$options.each(function(){
		// push each option value and text into an array
		arrVals.push({
			val: $(this).val(),
			text: $(this).text()
		});
	});

	// sort the array by the value (change val to text to sort by text instead)
	arrVals.sort(function(a, b){
		return a.val - b.val;
	});

	// loop through the sorted array and set the text/values to the options
	for (var i = 0, l = arrVals.length; i < l; i++) {
		$($options[i]).val(arrVals[i].val).text(arrVals[i].text);
	}
}


function manageBreadcrumbs() {
	// TODO:  Hook into the add to cart and clear basket buttons
	var leatherBCLink = $(".breadcrumbs a[href^='/leather']");
	if(leatherBCLink != null) {  
	  leatherBCLink .attr("href", "/leather.htm");
	  leatherBCLink .text("Leather accessories");
	}

	var cashmereBCLink = $(".breadcrumbs a[href^='/cashmere']:first");
	if(cashmereBCLink!= null) {  
	  cashmereBCLink.attr("href", "/cashmere-comfort.htm");
	}

	var leatherCartLink = $(".cartLink[href^='/leather']");
	if(leatherCartLink != null) {  
	  leatherCartLink .attr("href", "/leather.htm");
	}	
}

function manageProductMenu() {
$(".hide").parent("li").attr("style","display:none;");
// hide all submenus
$("#nav_620205 li ul").hide();
// show submenu based on page/path
var path = location.pathname.substring(1);	
	if ( path ) {
		// Find the navigation item for the page
		   var node = $('#nav_620205 a[href$="' + path + '"]');                           
		if(node != null) {
					//alert('node');
					node.parent().parent().show();
					node.parent().children().show();
				}
   }

$("ul#nav_620205 li").click(function() {
  $("ul li", this).show();
	  heightMatch();          
});	
}

function heightMatch()  {
	  var menuHeight = $(".productMenu").height();
	  var mainHeight = $(".shop-main").height();  // should we use mainText?
	  $(".shop-main").css("min-height",menuHeight+"px");
}

function setValue(elemId, text) {
	$("#"+elemId).text(text);	
}

function clearBillingAddress() {
	document.getElementById("BillingAddress").value = "";
	document.getElementById("BillingCity").value = "";
	document.getElementById("BillingState").value = "";
	document.getElementById("BillingZip").value = "";
	setValue("billingAddressSummary", "");
	setValue("billingCitySummary", "");
	setValue("billingStateSummary", "");
	setValue("billingPostcodeSummary", "");
}

/* Populates the delivery address from the billing address */
function populateBillingAddress() {
	document.getElementById("BillingAddress").value = document.getElementById("ShippingAddress").value;
	document.getElementById("BillingCity").value = document.getElementById("ShippingCity").value;
	document.getElementById("BillingState").value = document.getElementById("ShippingState").value;
	document.getElementById("BillingZip").value = document.getElementById("ShippingZip").value;
	setValue("billingAddressSummary", document.getElementById("ShippingAddress").value);
	setValue("billingCitySummary", document.getElementById("ShippingCity").value);
	setValue("billingStateSummary", document.getElementById("ShippingState").value);
	setValue("billingPostcodeSummary", document.getElementById("ShippingZip").value);
}

