// Created: 2008-10-01
// Author: Patricia Adamsson, patricia.adamsson@cloudnine.se

jQuery.noConflict();

    var $j = jQuery;
    $j(document).ready(function() {
        hover4IE6();
        hideSearchButton();
        changeLangTool();
        showMap();
        openCloseNav();
        safariCss();
        setSortOnWidth();
        removeMargin();
        scrollHorizontal();
        scrollToActiveBox();
        cheekyBox();
        newsletterValidation();
        brochureValidation();
        popGuides();
        extWindows();
    });

    $j(window).load(function() {
        if (document.getElementById("map")) {
            loadMap();
        }
    });
    function popGuides() {
        $j(".productDescription #ColorGuide").click(function(e) {
            e.preventDefault();
            $j("#colorGuidePop .hasScroll").html('<img class="theGuide" src="" alt="" />');
            var source = $j("a", this).attr("href");
            $j("#colorGuidePop .theGuide").attr("src", source);
            $j("#colorGuidePop").show();
        });
        $j("#colorGuidePop .exit").click(function() {
            $j("#colorGuidePop").hide();
        });

        $j(".productDescription #SizeGuide").click(function(e) {
            e.preventDefault();
            $j("#sizeGuidePop .hasScroll").html('<img class="theGuide" src="" alt="" />');
            var source = $j("a", this).attr("href");
            $j("#sizeGuidePop .theGuide").attr("src", source);
            $j("#sizeGuidePop").show();
        });
        $j("#sizeGuidePop .exit").click(function() {
            $j("#sizeGuidePop").hide();
        });
    }

    function openCloseNav() {
        $j(".nav .expanded #hideFilter").click(function() {
            $j(".nav .expanded #hideFilter").hide();
            $j(".nav .expanded #showFilter").show();
            $j(".nav .expanded .hidden").animate({ height: "toggle" }, 250);
            return false;
        });
        $j(".nav .expanded #showFilter").click(function() {
            $j(".nav .expanded #showFilter").hide();
            $j(".nav .expanded #hideFilter").show();
            $j(".nav .expanded .hidden").animate({ height: "toggle" }, 250);
            return false;
        });
    }

    function brochureValidation() {
        $j("#brochureForm #id_matrix").click(function() {

            var ok = true;

            $j("#brochureForm .dropField select").each(function() {
                if ($j(this).val() == "") {
                    $j("#brochureForm .dropField").addClass("error");
                    var ok = false;
                }
                else {
                    $j("#brochureForm .dropField").removeClass("error");
                    var ok = true;
                }
            });

            $j("#brochureForm .formField").each(function() {
                if ($j(this).children("input").val() == "") {
                    $j("#brochureForm  p.error").show();
                    $j(this).addClass("error");

                    var ok = false;
                }
                else {
                    $j(this).removeClass("error");
                    var ok = true;
                }
            });
            return ok;
        });
    }

    function newsletterValidation() {
        $j("#newsletterForm #id_matrix").click(function() {
            $j("#newsletterForm .formField").each(function() {
                var ok = true;

                if ($j(this).children("input").val() == "") {
                    $j("#newsletterForm  p.error").show();
                    $j(this).addClass("error");

                    var ok = false;
                }
                else {
                    $j(this).removeClass("error");
                    var ok = true;

                }
            });
        });
    }

    function hideSearchButton() {
        if ($j("#retailers .submitSearch")) {
            $j(".submitSearch").hide();
        }
    }
    function showMap() {
        if ($j("#mapHolder").hasClass("displayNone")) {
            $j("#mapHolder").removeClass("displayNone");
        }
    }

    function safariCss() {
        if (jQuery.browser.safari) {
            $j("head").append('<link rel="stylesheet" type="text/css" href="/UI/styles/safari.css" />');
            $j("#compare").hide();
        }
    }

    function setSortOnWidth() {
        var b = $j("#content .chosenCategory .left").width();
        $j("#content .chosenCategory .left").width(b + 1);
    }

    function scrollToActiveBox() {
        if ($j("#wrapper").children("li").children("div").hasClass("conceptHeader")) {
            var thePos = $j("#scrollingBox .active").position();
            var whereToScroll = (thePos.left) - 300 // - 850 because it seems to add an extra 541px to the offset, and to make it scroll to the middle
            $j("#scrollingBox").scrollTo({ top: 0, left: whereToScroll }, 800);
        }
    }
    function removeMargin() {
        if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
            $j("#activityContainer li.activity, #conceptNav li").css({ marginRight: "3px" });
        }
        else {
            $j("#activityContainer li.activity, #conceptNav li").css({ marginRight: "8px" });
            $j("#activityContainer li:last, #conceptNav li:last").css({ marginRight: "0px" });
        }
    }
    function changeLangTool() {
        $j("#languageTool #lang").click(function() {
            $j("#languageTool .languages").slideToggle();
        });
    }

    // hoverEffects for IE 6
    function hover4IE6() {
        if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
            // Menu, underlines when hover
            $j(".header ul.menu li").mouseover(function() {
                $j(this).removeClass("paHover");
                $j(this).addClass("paHover");
            });
            $j(".header ul.menu li").mouseout(function() {
                $j(this).removeClass("paHover");
            });

            // All productboxes, grey border
            var box = $j(".conceptWrapper .productBox, .productDescription .picWrapper div, #scrollingBox li ul.products li, .additionalProducts .productBox");
            box.mouseover(function() {
                $j(this).removeClass("boxHover");
                $j(this).addClass("boxHover");
            });
            box.mouseout(function() {
                $j(this).removeClass("boxHover");
            });

            // Activities
            $j("#conceptNav li").mouseover(function() {
                $j(this).removeClass("tabOpacity");
                $j(this).addClass("tabOpacity");
            });
            $j("#conceptNav li").mouseout(function() {
                $j(this).removeClass("tabOpacity");
            });
        }
    }

    // Cheeky box with hidden layers
    function cheekyBox() {
        var li = $j("#content .cheeky li");
        var div = $j("#content .cheeky div");
        var divWidth = $j(".cheeky").parent().innerWidth();
        var contentExtras = 42 //border+padding+margin taken from #content .cheeky .content {}
        //alert(divWidth)

        li.show();
        $j(".cheeky .content:eq(0)").css({ display: "block" });
        div.addClass("cheekyStyle");
        $j(".cheeky .content").width(divWidth - contentExtras);

        $j("#content .cheeky li span").each(function() {
            var aa = $j(this).html();
            if (aa == "") {
                $j(this).parent().parent("li").hide();
            }
        });



        li.click(function() {
            li.removeClass("active");
            $j(this).addClass("active");

            var index = li.index(this);
            $j(".cheeky .content").hide();
            $j(".cheeky .content:eq(" + index + ")").show();
        });
    }


    function scrollHorizontal() {
        var productboxMargin = 3; // li.productbox margin:3px 0 3px 3px;
        var conceptLisMargin = 0; // li.x margin-right:0px

        var amountConceptLis = $j("#scrollingBox #wrapper").children("li").length;
        var conceptLisTotalMargin = amountConceptLis * conceptLisMargin;

        var totalBoxes = $j("#scrollingBox #wrapper .products").children("li").length;
        var totalBoxWidth = ($j("#scrollingBox #wrapper .products").children("li").outerWidth()) + productboxMargin;

        $j("#scrollingBox #wrapper").children("li").each(function() {
            var amountBoxes = $j(this).children().children("li").length;
            var boxTotalWidth = ($j(this).children().children("li").outerWidth()) + productboxMargin;
            var conceptWidth = $j(this).width(amountBoxes * boxTotalWidth);
        });


        var scrollBoxWidth = $j("#scrollingBox").width();
        //alert(scrollBoxWidth)
        $j("#scrollingBox").css({ overflowY: "hidden" })//.width(scrollBoxWidth);
        $j("#scrollingBox #wrapper").width((totalBoxes * totalBoxWidth) + conceptLisTotalMargin);
    }

    // extWindows() makes links open in a different window. use rel="external" instead of target="_blank" and rel="popup" or rel="popup|[width]|[height]" for popups
    function extWindows() {
        $j("a[rel=external]").attr("target", "_blank");
        $j("a[rel^=popup]").click(function() {
            theHref = $j(this).attr("href");
            $j(this).attr("href", "javascript:void(0)");
            var popupWidth, popupHeight
            var relSplit = $j(this).attr("rel").split("|");
            if (relSplit[1]) {
                popupWidth = relSplit[1];
                popupHeight = relSplit[2];
            } else {
                popupWidth = 435;
                popupHeight = 350;
            }
            popMeUp(theHref, popupWidth, popupHeight)
        });

    }
    function popMeUp(strURL, strWidth, strHeight) {
        theWin = window.open(strURL, "popupWin", "scrollbars,resizable,height=" + strHeight + ",width=" + strWidth);
        theWin.focus();
    }


