contentPageLoad();

//id of element to check for and insert control
TextResizeDetector.TARGET_ELEMENT_ID = 'wrapper';
//function to call once TextResizeDetector has init'd
TextResizeDetector.USER_INIT_FUNC = init;

function init() {
    var iBase = TextResizeDetector.addEventListener(onFontResize, null);
}

function onFontResize(e, args) {
    setPageElementHeights();
}

/*$(function () {
    if (isIe8() | isIe7() | isIe6()) {
        $(".panel SELECT.withButton")
            .focusin(function () {
                $(this).css("width", "300px");
                $("#iWantTo").css("display", "none");
                $("#iWantTo .ieCnrLeftButton").css("display", "none");
                $("#iWantTo .blue").css("display", "none");
                $("#iWantTo .ieCnrRightButton").css("display", "none");
            })

            .blur(function () {
                $(this).css("width", "170px");
                $("#iWantTo").css("display", "block");
                $("#iWantTo .ieCnrLeftButton").css("display", "block");
                $("#iWantTo .blue").css("display", "block");
                $("#iWantTo .ieCnrRightButton").css("display", "block");
            });
        }
    });*/

function contentPageLoad() {
    jQuery(document).ready(function () {
        $("#MSOMenu_Help").remove();

        $("#tabs").tabs();

        // Set up openers fo open close section
        $('div.accordion').addClass('closed'); $('div.accordion').addClass('closed');
        $('div.accordion h3').click(function () {
            $(this).parent().parent().toggleClass('closed')
            $(this).next().toggle('fast', setPageElementHeights);
            return false;
        }).next().hide();


        $("#nav ul li.dropMenu, #nav ul li.highlight").hover(
			function () { $(this).addClass("hover"); },
			function () { $(this).removeClass("hover"); }
		);

        $("map area").click(function (e) {
            var x = e.pageX;
            var y = e.pageY;

            // Get the position relative to the content div
            var localContext = $(".imageMap");
            var position = localContext.offset();
            var positionX = x - position.left;
            var positionY = y - position.top;

            showMapPopup(this.id, positionX, positionY)
        });

        $(".mapPopup").mouseleave(function () {
            $(".mapPopup").hide();
        });

        setPageElementHeights();

        // If Javascript is enabled replace the url in the map areas with # to enable the
        // popups. If Javascript is not enabled clicking on a map area will go to the link.
        adjustMapLinks();

        var spacerHeight = 0;
        var navPadding = 0;
        var conPadding = 6;
        var navHeight = $("div#menu").height() + navPadding;
        var conHeight = $("div#content").height() + conPadding;
        if (navHeight > conHeight) {
            spacerHeight = navHeight - conPadding;
            $("#content").css("height", spacerHeight)
        } else if (conHeight > navHeight) {
            spacerHeight = conHeight;
            $("#menu").css("height", spacerHeight)
        }

        //I want to Dropdown 
        $("#iWantTo").show();
        $(".ieDropdown .select .title").click(function () {
            var isOpen = ($(this).children(".options").css("display") == "block");
            if (isOpen) {
                $(this).children(".options").slideUp();
            } else {
                $(this).children(".options").slideDown();
            }
        });
        $(".option").click(function () {
            var optionValue = $(this).text();
            var optionKey = $(this).attr("title");
            $(this).parent(".options").prev(".title").text(optionValue).attr("title", optionKey);
            $(this).parent(".options").slideUp();
        });
        $("#iWantToButton").click(function () {
            var currentKey = $(this).parent(".buttonWrapper").prev(".ieDropdown").children(".select").children(".title").attr("title");
            window.location.replace(currentKey);
        });

        //if no support for border-radius
        if (!supportsBorderRadius()) {
            if ($(".panel.yellow .ieCnrTopOrange").length == 0) {
                $(".panel.yellow .inner").before("<div class=ieCnrTopOrange></div>").after("<div class=ieCnrBotOrange></div>");
            }
            if ($(".panel.blue .ieCnrTopBlue").length == 0) {
                $(".panel.blue h3").before("<div class=ieCnrTopBlue></div>");
            }
            if ($(".panel.blue .ieCnrBotBlue").length == 0) {
                $(".panel.blue .inner").after("<div class=ieCnrBotBlue></div>");
            }
            if ($("#tabs .ieCnrBotBlue").length == 0) {
                $("#tabs .inner").after("<div class=ieCnrBotBlue></div>");
            }
            if ($("#content.landing .ieCnrTopHome").length == 0) {
                $("#content.landing .inner").before("<div class=ieCnrTopHome></div>").after("<div class=ieCnrBotHome></div>");
            }
            if ($("#content.subscribe .ieCnrTopBlueWide").length == 0) {
                $("#content.subscribe .panel h3").before("<div class=ieCnrTopBlueWide></div>").after("<div class=ieCnrBotBlueWide></div>");
            }
            if ($("#searchControl .ieCnrTopSearchControl").length == 0) {
                $("#searchControl .outer").before("<div class=ieCnrTopSearchControl></div>").after("<div class=ieCnrBotSearchControl></div>");
            }

            if ($(".options3Col .option .ieCnrBotOptions3Col").length == 0) {
                $(".options3Col .option .inner").after("<div class=ieCnrBotOptions3Col></div>");
            }

            if ($(".options2Col .option .ieCnrBotOptions2Col").length == 0) {
                $(".options2Col .option .inner").after("<div class=ieCnrBotOptions2Col></div>");
            }

            if ($(".options2ColLrg .option .ieCnrBotOptions2ColLrg").length == 0) {
                $(".options2ColLrg .option .inner").after("<div class=ieCnrBotOptions2ColLrg></div>");
            }

            if ($(".options2Col .option .inner .button .ieCnrBotOptionsButton2Col").length == 0) {
                $(".options2Col .option .inner .button a").before("<div class=ieCnrTopOptionsButton2Col></div>").after("<div class=ieCnrBotOptionsButton2Col></div>");
            }

            if ($(".options3Col .option .inner .button .ieCnrBotOptionsButton3Col").length == 0) {
                $(".options3Col .option .inner .button a").before("<div class=ieCnrTopOptionsButton3Col></div>").after("<div class=ieCnrBotOptionsButton3Col></div>");
            }

            if ($(".options2ColLrg .option .inner .button .ieCnrBotOptionsButton2ColLrg").length == 0) {
                $(".options2ColLrg .option .inner .button a").before("<div class=ieCnrTopOptionsButton2ColLrg></div>").after("<div class=ieCnrBotOptionsButton2ColLrg></div>");
            }

            /*other IE Fixes*/
            $(".option").addClass("ie78");
            /*$("#nav ul li:first-item").addClass("firstItem");*/

            if ($(".buttonWrapper.start .ieCnrLeftButton").length == 0) {
                $(".buttonWrapper.start a").before("<div class=ieCnrLeftButton></div>").after("<div class=ieCnrRightButton></div>");
            }

            if ($("li.highlight .dropMenu .buttonWrapper .ieCnrLeftButtonOrange").length == 0) {
                $("li.highlight .dropMenu .buttonWrapper input.orange").before("<div class=ieCnrLeftButtonOrange></div>").after("<div class=ieCnrRightButtonOrange></div>");
            }

            if ($(".buttonWrapper .ieCnrLeftButton").length == 0) {
                $(".buttonWrapper input.blue").before("<div class=ieCnrLeftButton></div>").after("<div class=ieCnrRightButton></div>");
            }

            if ($(".panel.yellow .buttonWrapper .ieCnrLeftButtonOrange").length == 0) {
                $(".panel.yellow .buttonWrapper input.orange").before("<div class=ieCnrLeftButtonOrange></div>").after("<div class=ieCnrRightButtonOrange></div>");
            }

            if ($("#tabs .ieTabLeft").length == 0) {
                $("#tabs .inner ul li.first").prepend("<div class=ieTabLeft></div>");
                $("#tabs .inner ul li.last").append("<div class=ieTabRight></div>");
            }

            pdfLinks();
        }

        //IE7-specific tweaks
        if (isIe7()) {
            $("#nav>ul").addClass("ie7TopNav");
            //$(".panel.blue .inner").addClass("ie7TopMargin");
            //$("#nav ul").css("width", "991px !important").css("margin", "0px auto !important");
        }


        hideAllSections();
        $("#summary").show();
        $("li#itemSummary").addClass("selected");
        $("li#itemSummary, li#itemDocuments, li#itemSubstances").click(function () {
            hideAllSections();
            switch ($(this).attr("id")) {
                case "itemSummary":
                    $("#summary").show();
                    break;
                case "itemDocuments":
                    $("#documents").show();
                    break;
                case "itemSubstances":
                    $("#substances").show();
                    break;
                default:
                    $("#summary").show();
            }
            $(this).addClass("selected");
        });
        function hideAllSections() {
            $(".sectionItem").hide();
            $(".sectionNav li").removeClass("selected");
        }
    });
}

function supportsBorderRadius() {
    if ($.browser.msie & $.browser.version >= 9.0 || $.browser.opera & $.browser.version >= 10.5
    | $.browser.mozilla & $.browser.version >= 1.0 || $.browser.webkit & parseInt($.browser.version) >= 533) {
        return true;
    }
    return false;
}

function isIe8() {
    return ($.browser.msie & $.browser.version == 8.0);
}

 function isIe7() {
    return ($.browser.msie & $.browser.version == 7.0);
}

function isIe6() {
    return ($.browser.msie & $.browser.version == 6.0);
}

function viewAll(group) {
    $(group).children('div.accordion').removeClass('closed').find('div').show();
    setPageElementHeights();

    return false;
}

function setPageElementHeights() {
    // Make all bottom link columns the same height
    // Replaced by bottom padding, negative margin overflow auto technique
    //$("#bottomLinks .inner .column").height('auto');
    //$("#bottomLinks .inner .column").height(equalHeight($("#bottomLinks .inner .column")));
    /* 
    $("#menu, #content .inner, #content, #colRight").height('auto');

    if($('#content .inner').length > 0) { 
    $("#menu, #content .inner, #colRight").height(equalHeight($("#menu, #content .inner, #colRight")));
    }
    else {
    $("#menu, #content, #colRight").height(equalHeight($("#menu, #content, #colRight")));
    }
    */
    //$("#content .inner, #colRight").height('auto');
    //$("#content .inner").height(equalHeight($("#content .inner, #colRight")));

    /*$(".ui-tabs-panel").height(equalHeight($(".ui-tabs-panel")));*/
}

function equalHeight(group) {
    tallest = 0;

    group.each(function () {
        thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);

    return tallest;
}

function resizeText(multiplier) {
    if ((document.body.style.fontSize == '' || document.body.style.fontSize == '0.63em') && multiplier == -1) {
        return false;
    }
    if (parseFloat(document.body.style.fontSize) > 0.9 && multiplier == 1) {
        return false;
    }

    if (document.body.style.fontSize == '') {
        document.body.style.fontSize = '0.63em';
    }

    document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.15) + 'em';
    setPageElementHeights();

    return false;
}

function swapImageMap(imageMapId, imageName) {
    document.getElementById(imageMapId).src = '_layouts/images/Erma/map' + imageName + '.png';
}

function showMapPopup(imageMapArea, x, y) {
    $('.mapPopup').hide();

    $('#mapAreaIndex li').removeClass('selected');
    $('#mapAreaIndex li#' + imageMapArea + "Link").addClass('selected');

    var xOffSet = 97;
    var yOffSet = 0;
    var cssClass = "";

    // Clean up previous class changes
    $('#mapPopup' + imageMapArea).removeClass().addClass('mapPopup');

    // Set current class changes
    if (x > 300 & y > 400) {
        xOffSet = 200;
        yOffSet = 170;
        $('#mapPopup' + imageMapArea).addClass('bottomRight');
    }
    else {
        if (x > 300) {
            xOffSet = 200;
            $('#mapPopup' + imageMapArea).addClass('topRight');
        }

        if (y > 400) {
            yOffSet = 170;
            $('#mapPopup' + imageMapArea).addClass('bottomLeft');
        }
    }

    $('#mapPopup' + imageMapArea).css('top', y - yOffSet);
    $('#mapPopup' + imageMapArea).css('left', x - xOffSet);
    $('#mapPopup' + imageMapArea).show();

    return false;
}

function showPopup(popupId, sourceObject) {
    $('#' + popupId).toggle();

    var position = $(sourceObject).position();

    $('#' + popupId).css('top', position.top + $(sourceObject).height() + 25);
    $('#' + popupId).css('left', (position.left - 247) + ($(sourceObject).width() / 2));

    return false;
}

function adjustMapLinks() {
    $("map area").attr("href", "#");
}

function pdfLinks() {
    var pdfs = $("a[href$='pdf']");
    pdfs.removeAttr("ID");
    pdfs.removeAttr("title");

    $("a[href$='pdf']").addClass('pdf');
    $('.pdf').after('<div class="tooltip">Having trouble opening PDFs? Right click and select "Save As"</div>');


    $('a.pdf').tooltip({
        // tweak the position
        relative: true,
		position: "top center"
	});
}

