
var EmpireCoversGlobalVars = {
    needToConfirmUnload: true
};
function ConfirmUnload() {
    var anItemIsChecked = false;
    $('.checkboxHolder').find('input[type="checkbox"]').each(function() {
        if ($(this).is(':checked')) {
            anItemIsChecked = true;
            return false;
        }
    });
    if (EmpireCoversGlobalVars.needToConfirmUnload == true && anItemIsChecked == true) {
        return "Your checked items will not be added to the cart unless you click the green 'Add Checked to Cart' button on the left.";
    }
}

window.onbeforeunload = ConfirmUnload;

$(window).scroll(function() {
    if ($(this).scrollTop() > 360) {
        $("div#ProductMenu").css({ "position": "fixed", "top": "0px", "z-index": "200"});
        $("div#LeftFloater").css({ "position": "fixed", "top": "0px" });
    } else {
        $("div#ProductMenu").css({ "position": "relative", "top": "0px" });
        $("div#LeftFloater").css({ "position": "absolute", "top": "0px" });
    }
});
$(document).ready(function() {
    $("#preloader").hide();
    $("input[type=checkbox]:checked").click();
    var opp = "add";
    var timeout = null;
    var interval = null;
    var t = null;
    var hover2 = false;
////	  ye olde hover script: not using because it is still super buggy and written for the old page's html
//
//    $("div.collectionDescriptionGroup").mouseenter(function() {
//        hover2 = true;
//    }).mouseleave(function() {
//        hover2 = false;
//        clearTimeout(timeout);
//    });
//    $("a.moreDetailsLink").mouseenter(function(e) {
//        t = $(this);
//        y = e.pageY - 600;
//        timeout = setTimeout(function() {
//            timeout = null;
//            $("div.collectionDescriptionGroup:visible").fadeOut("fast");
//            t.closest("div.ProductSection").prev("div.collectionDescriptionGroup").css({ "top": y + "px", "left": "180px" }).show("fast");
//        }, 500);
//    }).mouseleave(function() {
//        if (timeout === null) {
//            interval = setInterval(function() {
//                if (timeout == null && hover2 == false) {
//                    t.closest("div.ProductSection").prev("div.collectionDescriptionGroup").fadeTo(1000, 1).fadeOut("fast");
//                    clearInterval(interval);
//                    clearTimeout(timeout);
//                }
//                else if (hover2 == false) {
//                    clearTimeout(timeout);
//                }
//            }, 100);
//        } else if (timeout > 0 && timeout < 500) {
//            clearTimeout(timeout);
//        }
//    });
    $("input[type=checkbox]").click(function(e) {
        var t = e.target;
        $t = $(t);
        var id = $t.parent("span.checkboxHolder").attr("id");
        var qty = $("span.quantityHolder").filter("#" + id).children("select").children("option:selected").val();
        var price = $("span.priceHolder").filter("#" + id).text().substring(1);
        var desc = $("span.descriptionHolder").filter("#" + id).text();
        updateList(id, qty, price, desc);
    });
    $("select").change(function() {
        var qty = $(this).val();
        var id = $(this).parent("span.quantityHolder").attr("id");
        var price = $("span.priceHolder").filter("#" + id).text().substring(1);
        updateQty(id, qty, price);
    });
	// this expands any non-visible categories when clicked (sjs):
    $("a.anchorLink").click(function(){
		var targetId = $(this).attr("href");
		var thisGuy = $("div"+targetId);
		if(thisGuy.siblings("div.NewShopCollection").is(":hidden")){
		thisGuy.click();	
		}
	});
	// this will show all categories if it is hidden
	$("a.showAll").click(function(){
		$("img.imgSwap").attr("src","assets/images/branding/ShopbyCollection/ShopByCollection_HideBtn.gif");
		$("div.NewShopCollection").each(function(){
			if($(this).is(":hidden")){
				$(this).siblings("div.CollectionHead").click();
			}
		});
		return false;
	});
	//this will hide all categories if a category is hidden
	$("a.hideAll").click(function(){
		$("img.imgSwap").attr("src","assets/images/branding/ShopbyCollection/ShopByCollection_ShowBtn.gif");
		$("div.NewShopCollection").each(function(){
			if($(this).is(":visible")){
				$(this).siblings("div.CollectionHead").click();
			}
		});
		
		return false;		
	});
	
	$("div.NewCollectionHolder div.CollectionHead").css("cursor","pointer");
	
	$("div.NewCollectionHolder div.CollectionHead").toggle(
		function(){ 
			$("~.NewShopCollection", this).slideDown(1000);
			$(this).find("img.imgSwap").attr("src","assets/images/branding/ShopbyCollection/ShopByCollection_HideBtn.gif");
		},
		function(){ 
			$("~.NewShopCollection", this).slideUp(1000);
			$(this).find("img.imgSwap").attr("src","assets/images/branding/ShopbyCollection/ShopByCollection_ShowBtn.gif");
	});

});
function updateList(id, qty, price, desc) {
    var CVHTML = $("#checkedValues").html();
    var checkedHTML = "<div class=\"itemtotal\" id=\"" + id + "\"><span class=\"qty\">" + qty + "</span> " + desc + "</div>";
    var qtyChk = $("div#" + id).html();
    var tester = CVHTML.indexOf(qtyChk);
    var NEW = "original value";
    var oh = $("div#" + id).outerHTML(); //
    var length = oh.length;
    if (CVHTML.indexOf(id) !== -1 && CVHTML.indexOf(qtyChk) !== -1) {
        var index = CVHTML.indexOf(oh);
        var end = index + length;
        var clength = CVHTML.length;
        if (end == clength) {
            var a = CVHTML.substring(0, index);
            NEW = a;
            CVHTML = NEW;
        } else {
            var a = CVHTML.substring(0, index);
            var b = CVHTML.substring(end, clength);
            NEW = a + b;
            CVHTML = NEW;
        }
        opp = "sub";
    } else if (CVHTML.indexOf(id) !== -1 && CVHTML.indexOf(qtyChk) == -1) {
        updateQty(id, qty, price);
    }
    else {
        opp = "add";
        CVHTML = CVHTML + checkedHTML;
    }
    $("div#checkedValues").html(CVHTML);
    updatePrice(id, qty, price, opp);
}
function updateQty(id, qty, price) {
    var CVHTML = $("#checkedValues").html();
    if (CVHTML.indexOf(id) !== -1) {
        if (CVHTML.indexOf("<div id=\"" + id + "\" class=\"itemtotal\"><span class=\"qty\">" + qty + "</span>") === -1) {
            var currentQty = $("div#checkedValues div#" + id + " span.qty").text();
            $("div#" + id + " span.qty").text(qty);
            qty = parseInt(qty);
            if (qty > currentQty) {
                opp = "addq";
                var a = qty - parseInt(currentQty);
                price = a * parseFloat(price);
            } else if (qty < currentQty) {
                opp = "subq";
                var s = parseInt(currentQty) - qty;
                price = s * parseFloat(price);
            }
            updatePrice(id, qty, price, opp);
        } else {
        }
    } else {
    }
}
function updatePrice(id, qty, price, opp) {
    var currentPrice = $("p.pricetotal span").text();
    var value = "";
    currentPrice = parseFloat(currentPrice);
    if (opp === "add") {
        value = (parseInt(qty)) * (parseFloat(price));
        currentPrice = currentPrice + value;
    } else if (opp === "sub") {
        value = (parseInt(qty)) * (parseFloat(price));
        currentPrice = currentPrice - value;
    } else if (opp === "addq") {
        currentPrice = currentPrice + price;
    } else if (opp === "subq") {
        currentPrice = currentPrice - price;
    }
    currentPrice = (Math.round(currentPrice * 100) / 100).toFixed(2);
    $("p.pricetotal span").text(currentPrice);
}

