﻿window.customAdditionalImages = true;
//window.customPageNavigation = false;
window.customGroupNavigation = true;
//window.customProdImagePopup = false;
window.customMiniLogin = true;
//window.customGenPopup = false;
$(window).load(function() {
    var wMax = $(".pnlPageNavigation").width();
    var wUl = $(".pnlPageNavigation .ulPageTreeRoot").outerWidth();
    var wLiTot = 0;
    $(".liPageNodeDepth1").each(function() {
        wLiTot += $(this).outerWidth();
    });
    var addToEach = Math.floor((wMax - wLiTot) / $(".liPageNodeDepth1").length);
    var leftOver = (wMax - wLiTot - (addToEach * $(".liPageNodeDepth1").length))
    //alert(wMax + " " + wUl + " " + wLiTot + " " + addToEach + " " + leftOver)
    $(".liPageNodeDepth1").each(function() {
        $(this).outerWidth($(this).outerWidth() + addToEach);
    });
    $(".liPageNodeDepth1:last").each(function() {
        $(this).outerWidth($(this).outerWidth() + leftOver);
    });
    if ($("body").hasClass("Default")) {
        lineUpObjectsInside($(".pnlNotis3, .pnlNotis4, .pnlNotis5, .pnlNotis6, .pnlNotis7, .pnlNotis8, .pnlNotis9, .pnlNotis10"), $(".pnlMainLeft"));
    }
});
function lineUpObjectsInside(objects, inside) {
    var maxWidth = inside.width();
    var lineObjects = new Array();
    var currWidth = 0;
    $(objects).each(function() {
        if (($(this).outerWidth(true) + currWidth) > maxWidth) {
            var height = 0;
            for (var j = 0; j < lineObjects.length; j++) {
                if (lineObjects[j].height() > height) {
                    height = lineObjects[j].height();
                }
            }
            for (var j = 0; j < lineObjects.length; j++) {
                lineObjects[j].height(height);
            }
            lineObjects = new Array();
            currWidth = 0;
        }
        currWidth += $(this).outerWidth(true);
        lineObjects[lineObjects.length] = $(this);
    });
}
$(document).ready(function() {
    $(".ulGroupTreeRoot .liGroupSelected ul:first").show();
    var maxDepth = 999;
    var tag = $(".ulGroupTreeRoot .liGroupSelected");
    if (tag.length > 0) {
        if (tag.hasClass("hasSubGroups")) {
            tag.removeClass("hasSubGroupsCollapsed");
            tag.addClass("hasSubGroupsExpanded");
        }
        while (!tag.hasClass("ulGroupTreeRoot") && (0 < maxDepth)) {
            tag = tag.parent();
            if ("UL" == tag.attr("tagName").toUpperCase()) {
                tag.show();
            }
            if (tag.hasClass("hasSubGroups")) {
                tag.addClass("hasSubGroupsExpanded");
            }
            maxDepth--;
        }
    }
    if ($("body").hasClass("news")) {
        $("#_ctl0__ctl4_pageNode676").addClass("liPageSelected");
        $("#_ctl0__ctl4_pageNode675").addClass("liPageSelected");
    }
    $(".ulGroupTreeRoot .hasSubGroups").click(function() {
        toggleGroupNavigNode($(this).attr("liId"));
    });
    $(".ulGroupTreeRoot .hasSubGroups a").click(function(event) {
        event.stopPropagation();
        //addExpandedNode($(this).attr("liId"));
    });
    var a = getExpandedNodeArray();
    for (i = 0; i < a.length; i++) {
        var node = $("." + a[i]);
        node.removeClass("hasSubGroupsCollapsed");
        node.addClass("hasSubGroupsExpanded");
        node.find("ul:first").show();
        //$("." + a[i] + " ul:first").show();
    }
    if ($("body").hasClass("Default")) {
        $(".pnlPageNavigation .ulPageTreeRoot .liFirst").addClass("liPageSelected");
    }
    if ($(".pnlMiniBask .pnlCartContent").hasClass("pnlCartNotEmpty")) {
        $(".pnlMiniBask .caption").addClass("miniBaskCaptionWhenNotEmpty");
    }
    if ($("body").hasClass("dept")) {
        $(".drpProdOrderBy").each(function() {
            $('option', this).each(function() {
                $(this).text($(this).text().replace(":", ""));
            });
        });
    }
    if ($("body").hasClass("prod")) {
        if ($(".hlkProdImage").length > 0) {
            $(".pnlAdditionalImagesInstruction").show();
        }
        $(".additional-prod-image").each(function() {
            $(this).click(function() {
                $hlk = $(".hlkProdImage")
                if (0 < $hlk.size()) {
                    $hlk.attr("href", $(this).attr("open"));
                    $img = $(".hlkProdImage").find("img");
                } else {
                    $img = $(".imgProdImage")
                }
                if (0 < $img.size()) {
                    $img.attr("src", $(this).attr("rel"));
                }
            });
        });
    }
    if ($("body").hasClass("payment")) {
        if ($(".pnlErrorStockcount").length > 0) {
            $(".pnlBlanket").show();
            $(".pnlErrorStockcount").addClass("pnlErrorStockcountOnBlanket");
            $(".pnlBlanket").click(function() {
                $(this).hide();
                $(".pnlErrorStockcount").removeClass("pnlErrorStockcountOnBlanket");
            });
        }
    }
    if ($("body").hasClass("wpe-635")) {
        $(".pnlContent a").click(function() {
        $(".pnlContent").html("");
            $(".pnlContent").append($('<p class="sidrubrik">LAWICEL NYHETSBLAD</p>'));
            $(".pnlContent").append($('<iframe class="iFrameNewsletter" src="' + $(this).attr("href") + '"></iframe>'));
            return false;
        });
    }
	if ($("body").hasClass("find")){
		$(".produktlistakompakt tr").each(function(){
			var images = $(this).find("img:last");
			//var img = null;
			//$.each(images,function(i,val){
			//	img = $(this);
			//});
			if (images != null){
				$(this).find("td:first").append(images);
				images.show();
			}
		});
	}
});
function inArray(a, v) {
    for (i = 0; i < a.length; i++) {
        if (a[i] == v) {
            return true;
        }
    }
    return false;
}
function getExpandedNodeArray() {
    var c = $.cookie("expandedNodes");
    var a = new Array();
    if (c != null) {
        a = c.split(";");
    }
    return a;
}
function addExpandedNode(v) {
    var a = getExpandedNodeArray();
    if (!inArray(a, v)) {
        a[a.length] = v;
        added = true;
    }
    $.cookie("expandedNodes", a.join(";"), { path: '/' });
}
function removeCollapsedNode(v) {
    var newA = new Array();
    var a = getExpandedNodeArray();
    for (i = 0; i < a.length; i++) {
        if (a[i] != v) {
            newA[newA.length] = a[i];
        }
    }
    $.cookie("expandedNodes", newA.join(";"), { path: '/' });
}
function toggleGroupNavigNode(v) {
    var node = $("." + v);
    
    var subUl = $("." + v + " ul:first");
    if (subUl.length) {
        if (!subUl.is(':visible')) {
            node.removeClass("hasSubGroupsCollapsed");
            node.addClass("hasSubGroupsExpanded");
            addExpandedNode(v);
            subUl.show(300);
        }
        else {
            node.removeClass("hasSubGroupsExpanded");
            node.addClass("hasSubGroupsCollapsed");
            removeCollapsedNode(v);
            subUl.hide(300);
        }
    }
}
