// JavaScript Document


$(document).ready(function() {
	timers = [];
	function oc(a)
	{
	  var o = {};
	  for(var i=0;i<a.length;i++)
	  {
	    o[a[i]]='';
	  }
	  return o;
	}
	
	function countrySelected(selectedCountry,fieldName,countryList){
		if(!(selectedCountry in oc(countryList))){
			$(fieldName).hide();
		}else{
			$(fieldName).show();
		}
	}
	
	/*urlParam*/
	$.urlParam = function(name){
		var results = new RegExp('[\\?&#]' + name + '=([^&#]*)').exec(window.location.href);
		if (results != null) {
			return results[1];
		}
		return 0;
	}
	
	/*Standardize height of adresses*/
	var maxHeight = 0;
	$(".firstManageBook .manageBook ul li.addressVCard").each(function() {

		  if (maxHeight < ($("p:last",this).height() + 80)) {
				maxHeight = ($("p:last",this).height() + 80);
		  }

	});
	$('.firstManageBook .manageBook ul li.addressVCard,.firstManageBook .defaultBox.addressVCard').height(maxHeight);
	
	/*Standardize height of creditCardInfos*/
	var maxHeight = 0;
	$(".firstManageBook .manageBook ul li.creditCardVCard").each(function() {

		  if (maxHeight < ($("p:last",this).height() + 120)) {
				maxHeight = ($("p:last",this).height() + 120);
		  }

	});
	$('.firstManageBook .manageBook ul li.creditCardVCard,.firstManageBook .defaultBox.creditCardVCard').height(maxHeight);

	/*Standardize height of billing addresses*/
	var maxHeight2 = 0;
	$(".secondManageBook .manageBook ul li").each(function() {

		  if (maxHeight2 < ($("p:last",this).height() + 60)) {
				maxHeight2 = ($("p:last",this).height() + 60);
		  }

	});
	$('.secondManageBook .manageBook ul li,.secondManageBook .defaultBox').height(maxHeight2);
	
	

	/*Rounded Corners for searchbox*/
	$("#searchBox").corner("3px; top; cc:#14b5df");
	$("#searchBox").corner("3px; bottom; cc:#FFF");

	$("#nav #searchField div").corner("3px");



/*Rounded Corners for talk now and chat now*/
	$(".topUtilities .top").corner("3px; top; cc:#14b5df;");
	$(".topUtilities .bottom").corner("3px; bottom;");
	
/*Rounded Corners for community tab*/
	$("#community li").corner("3px; top; cc:#14b5df;");
	
	
/*To FIX remove border on last child in main navigation*/
	//$("div#nav ul li:last-child a").css({border:"none"})


/*Rounded Corners for  breadcrumb */
	$("#breadCrumb").corner("3px;");
	
	var totalWidth = 0;
	$("#breadCrumb ul").children("li").each(function(){
		totalWidth += $(this).innerWidth();
	});

	if(totalWidth > $("#breadCrumb ul").innerWidth() || $("#breadCrumb ul li.current").height() > 12){
		$("#breadCrumb ul li.current").addClass("currentBig");
		
		// adjust the width to fit in breadcrumb total width.
		var currentWidth = $("#breadCrumb ul li.current").width();
		$("#breadCrumb ul li.current").width(currentWidth - (totalWidth - $("#breadCrumb ul").width()));
		
	}

/*Write outer div around browse + rounded corners for browse*/
	$("div.inner").corner("3px; top;").parent().css('padding', '1px').corner("round 5px top").corner("round 5px bottom");
	$("div.bottomDesc").corner("3px; bottom;");

	//setting timer..
	timer = 1;
	

	
/*hide the * in the PostalCode/State for optional country */
	var countryWithPostalCode = ['australia', 'canada', 'new zealand', 'united states'];
	var countryWithStates = ['australia', 'canada', 'united states'];
	var selectedCountry =$("#countryId :selected").text().toLowerCase();
	countrySelected(selectedCountry, "#postalCodeAsterisk", countryWithPostalCode);
	countrySelected(selectedCountry, "#stateAsterisk", countryWithStates);
	$("#countryId").change(function(){
		var selCountry= $("#countryId option:selected").text().toLowerCase();
		countrySelected(selCountry, "#postalCodeAsterisk", countryWithPostalCode);	
		countrySelected(selCountry, "#stateAsterisk", countryWithStates);
	});

	
/*Rounded Corners for left components*/
	$("#sideBar .header").corner("3px; top;");	
	$("#sideBar .moreViews_big").corner("5px; top;");
	$("#sideBarMoreIdeas .moreIdeas").corner("5px; top;");	
	
	$(".btRdGrey, .btRdGreen, .btRdBlue, .btRdOrange, .btRdBlack").corner("3px;");	
	
/*Hover effect on browse products*/
		
	$("div.outer").mouseover(function(){
		$(".header",this).css({background:"#000"});
		$(this).children("div.inner:first").children("a.fancyBoxZoom:first").show();
	}).mouseout(function(){
		$(".header",this).css({background:"#00afdb"});
		$(this).children("div.inner:first").children("a.fancyBoxZoom:first").hide();
	});
	
/*Rounded corners address book + Billing Page*/
	$(".manageBook ul li").corner("10px; top;");
/*Remove right margin on last address book in address book + Billing Page*/	
	$(".manageBook ul li:last-child").css({margin:0});
				
	/*dashed on sides of menu*/
	//$(".sf-menu a:first").css("border-left", "0px");
	$(".menuWrapper").css("border-left","1px dashed #FFF");
	$(".menuWrapper").css("border-right","1px dashed #FFF");
	
	$('span.menuOrangeBackground-left').each(function(){
		$(this).width($(this).parent('a').width() + 6);
	});
	
	jQuery.extend( {
		bindMiniCartEvents : function(){
			/*Rounded Corners for Cart Proceed Button*/	
			$("#container #cartPopup .btRdGreen ").corner("3px;");
			/*Open Popup Cart*/
			$("li.openCartPopUp").mouseover(function() {			  
			  $(timers).each(function(){
				  clearTimeout(this);
			  });			 
			  $("#headerSlot2").hide();
			  $("#cartPopup").show();
			});
			
			$("#cartPopup").mouseover(function() {
				  $(timers).each(function(){
					  clearTimeout(this);
				  });
				  $("#headerSlot2").hide();
				  $("#cartPopup").show();
				});
			
			/*Open Card Page */
			$("li.openCart").click(function(){
				window.location = "/checkout/cart.jsp";
				}); 
			
			/*Close Popup Cart*/
				$("#cartPopup .close").click(function() {
				  $("#cartPopup").hide();
				});
				
				$("#cartPopup").mouseleave(function() {
					  $("#cartPopup").hide();
					  $("#headerSlot2").show();
					});
				
				$("li.openCartPopUp").mouseleave(function() {		
					timer = window.setTimeout("$('#cartPopup').hide('fast')", 500);	
					timers.push(timer);
					$("#headerSlot2").show();
				});
				
				$("div.miniProceedCheckout").click(function(){
					window.location =  "/checkout/cart.jsp";
				});					
		}
	});
	
	jQuery.extend({
		paginateMiniCart : function (){				
				var itemSize = $('#carousel').children('li').length;
				if (itemSize > 4){			   					
					$('#carousel').jcarousel({
						vertical: true,
						itemFallbackDimension: 75,
						visible: 3,
						size: itemSize,
						scroll : 1,						
						start: itemSize-2,
						initCallback : function(){						
							setTimeout(function(){
								$('.jcarousel-next-vertical')
								.addClass('jcarousel-next-disabled')
								.addClass('jcarousel-next-disabled-vertical')
								.attr('disabled', true);																					
							},100);	
						}
					});				
				}
		}
	});	
	
	
	$("#nav > ul li").mouseover(function(){
		 
		/*remove all transparent borders*/
		$("#nav li a").removeClass("borderRightTransp");
		/*remove all hovers on LI*/
		$("#nav li").removeClass("navLIHover");
		
		/*show/hide submenu*/
		var menu = $(this).attr("aliasid");
		var leftPos = Math.floor($(this).position().left);
		
		$("#nav > div").each(function(){
								
								if ($(this).attr("id") != (menu + "Menu")) {
									$(this).hide();
								}
							});
		
		/*add class on LI mouseover*/
		$(this).addClass("navLIHover");

		/*remove mouseover on home ICON */
		
		if ($("#nav > ul li:first-child").hasClass("navLIHover")){
			$("#nav li").removeClass("navLIHover");
		}
		
		/*add on LI the transparent border on mouseover*/
		
		if ($(this).hasClass("navLIHover")){
			$(this).prev().children().addClass("borderRightTransp");
		}
		/*add on home icon the transparent border on mouseover*/
		
		if ($(this).prev().hasClass("homeIcon"))
			$(this).prev().children().addClass("borderRightTransp");
		
		/*show sub menu*/
		
		menuSize = $("#" + menu + "Menu").width();
		navSize  = $("#nav").width();
		total    = menuSize + leftPos;
		
		if ( total > navSize) {
				leftPos = leftPos - (total + 2 -navSize);
		}
		
		$("#" + menu + "Menu").show().css('left', leftPos);
		$("#" + menu + "Menu").redrawShadow();
		$("#" + menu + "Menu_dropShadow").show();
	});
	
	/*close everything on #nav container mouse leave*/
	$("#nav").mouseleave(function(){
		$("#nav > div").hide();
		$("#nav ul li").removeClass("navLIHover");
		$("#nav > ul li a").removeClass("borderRightTransp");
					
	});	
	
 
		$('#nav > div').each(function(){
			var maxHeight = 0;
	        if (maxHeight < $(this).height()) {maxHeight = $(this).height()}
	        
		    $(this).find('.shopBy').each(function(){
				$(this).height(maxHeight);
		    });
	    });
 
	
	$(".home").click(function(){
		 $.cookie("selectedCatId", null, { path: '/', expires: -10 });
	});
	
	if ($.urlParam("maxPriceRange") != 0){
	    $("#refinementPrice").val("0-" + $.urlParam("maxPriceRange"));
	}
	$.bindMiniCartEvents();

  /**
   * Fade effect for comment elements on the page
   * eg: shipping and newsletter slots
   */	
  $('.fade').hover(function(){
	  $(this).fadeTo('slow', 0.5);
  }, function(){
	  $(this).fadeTo('slow', 1);
  });
  
  /* Add shadow around submenus.*/
  $("div.mmenu").dropShadow({left: 4, top: 4, opacity: 0.5, blur:2});
  
  
  $("div#nav > ul li").click(function() {
		if($(this).attr("catId") != undefined) {
     		$.cookie("selectedCatId", null, { path: '/', expires: -10 });
     		$.cookie("selectedCatId", $(this).attr("catId"),{path: '/', expires: 30 });
		}
	});

	$("div.mmenu ul li").click(function() {
		if($(this).parents("div.mmenu").attr("catId") != undefined) {
			$.cookie("selectedCatId", null, { path: '/', expires: -10 });
			$.cookie("selectedCatId", $(this).parents("div.mmenu").attr("catId"),{path: '/', expires: 30 });
		}
	});
});	


/**
 * Fix the height for 'More Ideas' section 
 */
$.fixColumnHeights = function(){	
	if ($("#sideBarMoreIdeas").length > 0){
		var col1 = $("#mainCont2Col").height();	
		var col2 = $("#sideBarMoreIdeas").height() + $("#breadCrumb-browseBrand").height() + 20;
		if (col1 < col2){
			//expand them out so they fit together
			$("#mainCont2Col").height(col2);
			$("#sideBarMoreIdeasSection").height(col2);
		}
	}
}


