(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 = ["nav-books-on.gif", "nav-music-on.gif", "nav-movies-on.gif", "nav-games-on.gif", "nav-electronics-on.gif", "nav-trends-on.gif", "nav-used-on.gif", "nav-gift-card-on.gif", "nav-shopping-cart-on.gif"];
	var globalOffNavImages = ["nav-books-off.gif", "nav-music-off.gif", "nav-movies-off.gif", "nav-games-off.gif", "nav-electronics-off.gif", "nav-trends-off.gif", "nav-used-off.gif", "nav-gift-card-off.gif", "nav-shopping-cart-off.gif"];
	var totalImages = globalOnNavImages.length;
	
	for(var i = 0; i < totalImages; i++) {
		var theImage = filePath + globalOnNavImages[i];
		lib.image.preload(theImage);
	}
	
	// add roll over/off for the global nav links (books, games, music, movies, used, trends, electronics, buy a gift card, shopping cart)
	$("#global-nav h2 a").each(function (i) {
         $(this).mouseover(function(){
	     	$("img", this)[0].src = filePath + globalOnNavImages[i];
    	}).mouseout(function(){
	      	$("img", this)[0].src = filePath + globalOffNavImages[i];
	    }); 
     });
     
     
     // remove instruction text for header search input field
     var theSearchInput = $("#global-search");
     lib.input.defaultText("#global-search",{defaultText: "Enter keyword or item #"});
     
     // 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);
		}
	 }		
});

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 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();
}