﻿$(document).ready(function() {
    $("img").error(function() { SetNoFotoForProduct(this); });
    //Set beta image(for ie)
    $(".topBetaLogo").css("border", "0px solid red");
    //Set first tab(for ie6)
    if ($.browser.msie && $.browser.version == "6.0") {
        $(".boxStandardLargeTabs > .inner > .head li.first.selected").css("margin", "0 2px");
        // $(".boxStandardLargeTabs > .inner > .head li.first.selected a").css("padding-right", "10px");
    }

    //$("img").css("border", "solid 1px red");
    //    $("img").error(function() { SetNoFotoForProduct($(this).get(), NoProductImageSizes.Size200x200); });
    //$(".topLogo img").css("border", "solid 1px red"); //.error(function() { alert("tagName=" + $(this).get().tagName); });

    // Main menu.
    $("#menuSub").hide();
    var menuParentMouseMovingTimer = null;
    var listItemToShow = null;

    OpenMenu = function(listItem) {
        if ($("#menuSub").is(":hidden")) {
            if ($("#menuParent > ul > .selected")[0])
                $("#menuParent > ul > .selected")[0].className = "";

            listItem.className = "selected";

            $("#menuSub").html($(listItem).html());
            $("#menuSub").toggle("blind", { direction: "vertical" }, 300);

            // Showing the submenu causes the pricegraph image to be moved down
            // on the page, so until the user hovers the images, hide the price box.
            if (window.priceGraph !== undefined)
                window.priceGraph.hide();
        }
        else {
            $("#menuSub").html($(listItem).html());

            if ($("#menuParent > ul > .selected")[0])
                $("#menuParent > ul > .selected")[0].className = "";

            listItem.className = "selected";
        }
    };

    $("#menuParent").mousemove(function(e) {
        if (menuParentMouseMovingTimer != null)
            window.clearTimeout(menuParentMouseMovingTimer);
        if ($("#menuSub").is(":hidden"))
            menuParentMouseMovingTimer = window.setTimeout(menuParentMouseMovingEnded, 200);
        else
            menuParentMouseMovingEnded();
    });

    $("#menuParent").mouseleave(function(e) {
        if (menuParentMouseMovingTimer != null)
            window.clearTimeout(menuParentMouseMovingTimer);
    });

    $("#menuParent > ul > li").mouseenter(function(e) {
        listItemToShow = this;
    });

    menuParentMouseMovingEnded = function() {
        if (listItemToShow != null)
            OpenMenu(listItemToShow);
    };

    //Global Expand
    GlobalExpand = function(elm) {
        $("." + elm + "> .expand").toggle("blind", { easing: "easeOutQuad", times: 1 }, 300);
    };

    //Image hover
    ToggleImageHover = function(elm, close, text, imageLink) {
        if (close) {
            $(".productListImageHover").remove();
        }
        else {
            var offset = $(elm).offset();
            var style = "left:" + (offset.left + 82) + "px; top:" + (offset.top - 90) + "px;";
            var imageHtml = "<img src=\"" + imageLink + "\" alt=\"\" />";
            $("<div id=\"productListImageHover\" class=\"productListImageHover\" style=\"" + style + "\">" + imageHtml + "<p>" + unescape(text) + "</p></div>").appendTo("body");
        }
    };

    //Image hover 2
    ToggleImageHover2 = function(elm) {
        if ($(".productListImageHover2").is(":visible")) {
            $(".productListImageHover2").remove();
        }
        else {
            var offset = $(elm).offset();
            var hoverDiv = document.createElement("div");
            hoverDiv.innerHTML = $(elm).parent().get(0).innerHTML;
            hoverDiv.className = "productListImageHover2";

            $(hoverDiv).appendTo("body");
            hoverDiv.style.left = (offset.left - 70) + "px";
            hoverDiv.style.top = (offset.top - 95) + "px";
        }
        if ($(".productListImageHover2 > .image").get(0)) {
            $(".productListImageHover2 > .image").get(0).onmouseover = function() { }
            $(".productListImageHover2").get(0).onmouseout = function() { $(".productListImageHover2").remove(); }
        }
    };



    //Rating hover
    ToggleRating = function(elm, text, type) {
        if (text == "") {
            $(".ratingHover").remove();
        }
        else {
            var offset = $(elm).offset();
            var style = "left: " + (offset.left + 10) + "px; top: " + (offset.top + 15) + "px";
            if (type == "dealer")
                $("<div class=\"ratingHover hoverText\" style=\"" + style + "\"><p>" + text + "</p></div>").appendTo("body");
            else
                $("<div class=\"ratingHover\" style=\"" + style + "\"><p class=\"" + type + " \">" + text + "</p></div>").appendTo("body");
        }
    };



    //Scroll bar box   
    SetScrollBar = function(id, itemWidth) {
        var elm = $("#" + id);
        var width = ($("#" + id + " li").length * itemWidth) + "px";
        elm.css("width", width);

        elm.wrap("<div class=\"scrollActive\" style=\"width: 597px; overflow: auto; height: 195px\"></div>");
    };



    //Compare functions
    CompareClose = function(elm) {
        var image = $("#" + elm + " > .top")[0];
        image.className = (image.className == "top") ? "top closed" : "top";

        $("#" + elm + " > table").toggle("blind", { direction: "vertical" }, 300);
    };

    CompareDelete = function(row) {
        var count = $(".boxStandardCompare .delete").length;
        if (count > 2) {
            for (var i = 0; i < $(".boxStandardCompare table tr").length; i++) {
                $(".boxStandardCompare table tr:eq(" + i + ") td:eq(" + row + ")").remove();

            }
            CompareSetWidth();
        }

        for (var r = 0; r < $(".delete").length; r++) {
            $(".delete")[r].id = r + 1;
        }

        if (count == 3) {
            $(".boxStandardCompare .delete").hide();
        }
    };



    //Product info toggle
    ProductInfo = function(elm, info) {
        var height = $("#" + info + " h3").height();
        if ($("#" + info).height() == 56) {
            //$(elm).fadeOut(200);
            $(elm).html("Skjul tekst");
            $("#" + info).animate({ height: height, easing: "easeOutQuad" }, 300);
        }
        else {
            $(elm).html("Læs mere");
            $("#" + info).animate({ height: 56, easing: "easeOutQuad" }, 300);
        }
    };



    //DealerInfo toggle
    ToggleDealerInfo = function(elm, show) {
        var obj = $("#" + elm);
        if (typeof (show) == undefined) show = obj.is(":hidden;");

        if (show) obj.fadeIn(300);
        else obj.css("display", "none");
    };


    //Full list expand
    FulllistExpand = function(elm, btn) {
        var minHeight = 0;
        var maxHeight = $("#" + elm + " ul").height() + 15;

        for (var i = 0; i < $("#" + elm + " ul li").length; i++) {
            if ($("#" + elm + " ul li")[i] && i < 5) {
                minHeight += $("#" + elm + " ul li")[i].offsetHeight;
            }
        }

        if ($("#" + elm)[0].offsetHeight == minHeight) {
            $("#" + elm).animate({ height: maxHeight, easing: "easeOutQuad" }, 300);
            btn.className = "btnClose";
        }
        else {
            $("#" + elm).animate({ height: minHeight, easing: "easeOutQuad" }, 300);
            btn.className = "btnFulllist";
        }
    };

    //Full list set height
    SetListHeight = function(elm) {
        var height = 0;

        for (var i = 0; i < 5; i++) {
            if ($("#" + elm + " ul li")[i]) {
                height += $("#" + elm + " ul li")[i].offsetHeight;
            }
        }

        $("#" + elm)[0].style.height = height + "px";
    };

    //Toggle product spec sub level
    ProductSpecToggle = function(elm) {
        for (var i = 0; i < $(elm).nextAll().length; i++) {
            if ($(elm).nextAll()[i].nodeName == "UL") {
                $(elm).parent().get(0).className = ($(elm).parent().get(0).className == "sub") ? "sub open" : "sub";
                $(elm).nextAll()[i].style.display = ($(elm).nextAll()[i].style.display == "" || $(elm).nextAll()[i].style.display == "none") ? "block" : "none";
            }
        }
    };

    //Toggle all product spec sub levels
    ProductSpecToggleAll = function(elm) {
        for (var i = 0; i < $(".boxStandardProductSpecList h2.sub").length; i++) {
            ProductSpecToggle($(".boxStandardProductSpecList h2.sub")[i]);
        }
    };



    /* Gallery */
    var selPos = 0;
    var imgElements = $("#galleryList li").length;

    GalleryLoadList = function() {
        $("#galleryList li:first").attr("class", "selected");
        $("#galleryList li").bind("click", function() {
            GallerySetLargeImage(this);
        });
    };

    GallerySetLargeImage = function(elm) {
        $("#galleryList li").attr("class", "");
        elm.className = "selected";
        $("#galleryLargeImage").html("<img src=\"" + elm.lastChild.innerHTML + "\" />");
        selPos = $("#galleryList li").index(elm);
    };

    GalleryPrevNext = function(next) {
        if (next) {
            selPos++;
            if (selPos == imgElements) {
                selPos = 0;
            }
        }
        else {
            selPos = selPos - 1;
            if (selPos < 0) {
                selPos = imgElements - 1;
            }
        }
        GallerySetLargeImage($("#galleryList > li:eq(" + selPos + ")")[0]);
    };
});

ShowMessage = function (message, redirectUrl) {

    redirectUrl = (typeof redirectUrl == "undefined") ? "" : redirectUrl;
    if (redirectUrl != "") {
        messageOnCloseRedirect = function (hash) { window.location = redirectUrl; };
    }

    var $dialog = $("#dialogWindow");
    if ($dialog.size() == 0) {
        $("<div id=\"dialogWindow\" class=\"jqmWindow\"><img src=\"/gfx/common/icon/cross_48.png\" class=\"jqmClose\" style=\"float:right;\" /><div class=\"jqmWindowContent\"></div><input type=\"button\" class=\"button-action jqmClose\" value=\"Luk\" /></div>").appendTo("body");
        $dialog = $("#dialogWindow");
        $dialog.jqm({ overlay: 60 });
        $(document).keyup(function (e) {
            if (e.keyCode == 27) { // esc
                $("#dialogWindow").jqmHide();
            }
        });
    }
    if (redirectUrl != "") {
        $dialog.jqm({ onHide: messageOnCloseRedirect });
    }
    $dialog.find(".jqmWindowContent").html(message);
    $dialog.jqmShow();
};

//Word wrap util
WordWrap = function(str)
{
    var newStr = str.replace("/", "/ <br />");
    newStr = newStr.replace("-", "- <br />");
    
    return newStr;
};

CompareSetWidth = function(cols)
{
    var cols = $(".boxStandardCompare .delete").length;
    $(".boxStandardCompare table td").css("width", Math.round((433 / cols)) + "px");
    $(".boxStandardCompare table .info").css("width", "131px");
};

CompareSetInfotext = function()
{
    for(var i = 0; i < $(".boxStandardCompare table .info").length; i++)
    {
        var infoText = $(".boxStandardCompare table .info:eq(" + i + ")").text();
        $(".boxStandardCompare table .info:eq(" + i + ")").html(WordWrap(infoText));
    }
};

// User rating submission
SetStarRating = function(stars, elmId, hiddenId)
{
	$("#" + hiddenId)[0].value = stars;
		
	switch(stars)
	{
		case 1:
			$("#" + elmId).css("width","15px");
			break;
		case 2:
			$("#" + elmId).css("width","27px");
			break;
		case 3:
			$("#" + elmId).css("width","39px");
			break;
		case 4:
			$("#" + elmId).css("width","51px");
			break;
		case 5:
			$("#" + elmId).css("width","65px");
			break;
	}
};

TabManagerSwitchTab = function(elm, id, css) {
    // Set the 'selected' css class on the newly selected tab
    // Remove it from all other tabs.
    var selectedTab = $(elm).parent().get(0);
    var tabs = $(elm).parent().parent().children("li");

    for (var i = 0; i < tabs.length; i++) {
        var currentTab = tabs[i];

        $(currentTab).removeClass("selected");

        if (currentTab == selectedTab) {
            $(currentTab).addClass("selected");
        }
    }

    // Hide all the non-selected tabs.
    // Show the selected tab.
    var tabContent = $(elm).parent().parent().parent().parent().children("div > .content").children("div");
    for (var i = 0; i < tabContent.length; i++) {
        var currentTab = tabContent[i];

        $(currentTab).css("display", "none");

        if (currentTab.id == id) {
            $(currentTab).css("display", "block");
        }
    }

    // Switch overall css class according to the currently selected tab's specified css class
    var container = $(elm).parent().parent().parent().parent().parent()[0];
    container.className = css;

    // Handle price graph price box, on product detail pages.
    if (window.priceGraph !== undefined) {
        window.priceGraph.hide();
    }
}
