(function($) {
	var site = window.site = function() {
		return {
			data : {
				// js data
			},
			func : {
				setUCartDisplay : function(){
					if($("#widget-but-ucart").attr("class") == "u-cart-hover"){
						$("#widget-but-ucart").attr("href","javascript:void(0)");
						$("#widget-but-ucart").click(function(){ 
							window.location = "/checkout/basket.jsp"
						});
						$("#widget-but-ucart").mouseover(function(){
							if($("#widget-ucart").css("display") != "block"){
								showBasket('show'); 
							}	
						});
					}
					else if($("#widget-but-ucart").attr("class") == "u-cart-click"){
						$("#widget-but-ucart").attr("href","javascript:void(0)");
						$("#widget-but-ucart").click(function(){
							if($("#widget-ucart").css("display") != "block"){
								showBasket('show'); 
							}	
						});
					} else {
						$("#widget-but-ucart").click(function() {
							hideBasket();
							window.location = "/checkout/basket.jsp";
						});
					}
				}
			}
		};
	}($)
})($);

// on body load
$(function() {
	
	// for universal cart display
	site.func.setUCartDisplay();

	// preload images for the on state for global nav and create a array of the off images for use later in the on/off roll over
					var filePath = "/assets/images/cms/header/" ;
					var globalOnNavImages = ["books-nav-on.gif", "ebooks-nav-on.gif", "music-nav-on.gif", "mp3s-nav-on.gif", "movies-nav-on.gif", "games-nav-on.gif", "electronics-nav-on.gif", "trends-nav-on.gif", "used-nav-on.gif", "giftcards-nav-on.gif"];
					 var globalOffNavImages = ["books-nav-off.gif", "ebooks-nav-off.gif", "music-nav-off.gif","mp3s-nav-off.gif", "movies-nav-off.gif", "games-nav-off.gif", "electronics-nav-off.gif", "trends-nav-off.gif", "used-nav-off.gif", "giftcards-nav-off.gif"];
					 var totalImages = globalOnNavImages.length;

					 for(var i = 0; i < totalImages; i++) {
					 var theImage = filePath + globalOnNavImages[i];
					 lib.image.preload(theImage);
					 }
                     var borderColor = ["#338C26", "#338C26","#9EAB05","#9EAB05","#01AEDA","#DE8703","#9999cc","#996699"];
					 var leftAlign = ["-2px","52px","100px","160px","200px","260px", "320px","410px"];
					 $("#global-nav ul li.js-nav").each(function (i) {
					$(this).mouseover(function(){
					$("img", this)[0].src = filePath + globalOnNavImages[i];
                    if(i < borderColor.length){
                    $("#flyout-menu-" + i).css({
						   display:'block',
						   left:leftAlign[i],
                           borderTopColor:borderColor[i]
						   });
						}
					 }).mouseout(function(){
					 $("img", this)[0].src = filePath + globalOffNavImages[i];
					  $("#flyout-menu-" + i).css('display','none');
					 });
	}); 


     
     // remove instruction text for header search input field
     var theSearchInput = $("#global-search");
     lib.input.defaultText("#global-search",{defaultText: "Enter keyword or item #"});
     lib.input.defaultText("#store-finder",{defaultText: "Enter zip code"});
     lib.input.defaultText("#email",{defaultText: "Enter email address"});
     // div layer pop up for giftcard balance
     
     $("#inc-foo-bal-lookup").click(function() {
     	lib.layer.add("#popup-div");
     	lib.layer.loadHTML("#popup-div", this.href, ".close-popup");
     	return false;
     });
     
     // header select drop down box
     selectOptions();
	 
	 if(lib.utils.isIE6()) {
		if (document.getElementById) {
			var topVal = $("body").height() - 81 + "px";
			$("#footer").css("bottom","auto");
			$("#footer").css("top",topVal);
		}
	 }

    $("input[name=find-btn]").bind("click",function(){
        unicaFindStore($("#store-finder").val());
    });
    $("input[name=signup-btn]").bind("click",function(){
        unicaEmailSignUp($("#email").val());
    });
});

function selectOptions() {
	
	var isOpen = false;
	
	$("#header-select-box").toggle(
	  function () {
	  	if(!isOpen) {
	    	$("#select-box-options").show();
	    	isOpen = true;
	    } else {
	    	$("#select-box-options").hide();
	    	isOpen = false;
	    }
	  },
	  function () {
	   if(!isOpen) {
	    	$("#select-box-options").show();
	    	isOpen = true;
	    } else {
	    	$("#select-box-options").hide();
	    	isOpen = false;
	    }
	  }
	);
	
	function hideList() {
      if(isOpen) {
	    	$("#select-box-options").hide();
	    	isOpen = false;
	   }
    }
	
	$("#select-box-options").hover(
      function () {
        $("body").unbind('click', hideList);
      }, 
      function () {
        $("body").bind('click', hideList);
      }
    );
    
	$("#select-box-options ul li").mouseover(function(){
      	this.id = "current";
    }).mouseout(function(){
      	this.id = "";	
    });
    
    $("#select-box-options ul li").click(function () {
      	$("#header-select-box span").html($(this).html());

      	var selectBox = $("#header-select-box span");
      	selectBox.attr("class", $(this).attr("class"));
        if ($(this).children("span").html() != "") {
            selectBox.attr('id', $(this).children("span").html());
            $("#option-map-key").attr("value", $(this).children("span").attr("id"));
        }
      	$("#select-box-options").hide();
	  	isOpen = false;
    });
}

function ajaxLoadContributorProduct(div,page,params) {
	params = "rId=" + new Date().getTime() + "&" + params;
    $.ajax({
   		type: "GET",
   		url: page,
   		data: params,
   		dataType: "html",
   		success: function(msg) {
		    $("#"+div+" *").remove();
   			$("#"+div).html("");
   			$("#"+div).append(msg);
   		},
		failure: function(msg) {
		  alert(msg);
		}
	   });
};

function ajaxLoadRecentlyViewedItems(div,page,params) {
	params = "rId=" + new Date().getTime() + "&"+params;
    $.ajax({
   		type: "GET",
   		url: page,
   		data: params,
   		dataType: "html",
   		success: function(msg) {
    	 $("#"+div+" *").remove();
			$("#"+div).html("");
			$("#"+div).append(msg);
   		},
		failure: function(msg) {
		  alert(msg);
		}
	   });
};

function ajaxLoadCertonaRecommendedItems(div,page,params) {
	params = "rId=" + new Date().getTime() + "&" + params;
	$.ajax({
		type: "GET",
		url: page,
		data: params,
		dataType: "html",
		success: function(msg)
		{
			$("#"+div+" *").remove();
			$("#"+div).html("");
			$("#"+div).append(msg);
		},
		failure: function(msg)
		{
			alert(msg);
		}
	});
};

function moveToLink(sUrl) {
	location = sUrl;
};

function unicaFindStore(sZipCode){
    ntptAddPair( "ZipCode",  sZipCode);
    ntptEventTag("ev=Find_a_Store");
}
function unicaEmailSignUp(sEmailAddr){
    ntptAddPair( "EmailAddress",  sEmailAddr);
    ntptEventTag("ev=Email_Signup");

}
function unicaAddToCart(productName,quantity,product, price){
    console.log("Here");
	ntptAddPair("sProduct", productName);
	ntptAddPair("iQuantity ", quantity);
	ntptAddPair("rta", product);
    ntptAddPair("fPrice", price);
	ntptEventTag("ev=CART_ADD");
}
function unicaCheckout(customer, productNames, products, total)
{
	ntptAddPair("CustomerID",  customer);
	ntptAddPair("ProductName", productNames);
	ntptAddPair("rtc", products);
	ntptAddPair("rtt", total);
	ntptEventTag("ev=CHECKOUT_START");
}

function unicaDeleteItem(customer, productName, product)
{
	ntptAddPair("CustomerID",  customer);
	ntptAddPair("ProductName", productName);
	ntptAddPair("rtr", product);
	ntptEventTag("ev=CART_REMOVE");
}

function unicaMoveWishList(customer, productName, sku, quantity, price)
{
	ntptAddPair("CustomerID", customer);
	ntptAddPair("ProductName", productName);
	ntptAddPair("SKU", sku); 
	ntptAddPair("Quantity", quantity);
	ntptAddPair("Price", price);
	ntptEventTag("ev=CART_WISHLIST");	
}

function unicaEditItem(customer, productName, sku, quantity, price)
{
	ntptAddPair("CustomerID", customer);
	ntptAddPair("ProductName", productName);
	ntptAddPair("SKU", sku); 
	ntptAddPair("Quantity", quantity);
	ntptAddPair("Price", price);
	ntptEventTag("ev=CART_EDIT");
}

function unicaEditItemQuickView(params)
{
	var listParams = params.split(";");
	ntptAddPair("CustomerID", listParams[0]);
	ntptAddPair("ProductName", listParams[1]);
	ntptAddPair("SKU", listParams[2]); 
	ntptAddPair("Quantity", listParams[3]);
	ntptAddPair("Price" , listParams[4]);
	ntptEventTag("ev=CART_EDIT");
}

function unicaUpdateCart(customer, productName, sku, quantity, price)
{
	ntptAddPair("CustomerID", customer);
	ntptAddPair("ProductName", productName);
	ntptAddPair("SKU", sku); 
	ntptAddPair("Quantity", quantity);
	ntptAddPair("Price" , price);
	ntptEventTag("ev=CART_UPDATE");
}

function unicaProductView(eventName, skus)
{
	ntptAddPair("rtv", skus);
	ntptEventTag("ev=" + eventName);
}

function unicaEmailFriend(productName, sku)
{
	ntptAddPair("ProductName", productName);
	ntptAddPair("SKU", sku);
	ntptEventTag("ev=EMAIL_FRIEND");
}

function unicaViewCustomerReviews()
{
	ntptEventTag("ev=DETAILS_REVIEW");
}

function unicaViewEditorialReviews()
{
	ntptEventTag("ev=DETAILS_EDITORIAL");
}

function hideUniversalCart() {
	// hide the universal cart
	$("#widget-but-ucart").removeClass("u-cart-click");
	site.func.setUCartDisplay();
}



