﻿
window.onload = function() {
    _spBodyOnLoadWrapper();

    new function() { // This will create the smaller text/bigger text functionality.

        var biggerTextButton = document.getElementById('bdc_text_plus');
        var smallerTextButton = document.getElementById('bdc_text_moins');

        if (biggerTextButton == null || smallerTextButton == null) {
            return;
        }

        var delta = 0.25;
        var currentFontSize = 1;
        var allResizableTextElements = Array();

        // In the future, if we want to limit how much the user can enlarge and shrink the text we need only change these values
        var minSize = 0;
        var maxSize = 2; //Infinity;

        var fontSizeUnits = "em";

        var initFontSizeElements = function() {
            function populateFontSizeElements(curr_element, parent_element) {
                /*
                This function recursively finds all plain-text nodes in the document, and wraps them in a <span> element.
                These span elements are stored in the array 'allResizableTextElements' and have their font-size attribute modified.
                */
                if (curr_element.childNodes.length <= 0) {
                    if (curr_element.nodeName == "#text" || curr_element.nodeType == 3) {
                        if (curr_element.data.replace(/^\s*|\s*$/g, '') != "") {
                            var textspan = document.createElement("ins");
                            textspan.appendChild(document.createTextNode(curr_element.data));
                            textspan.style.margin = "0px";
                            textspan.style.padding = "0px";
                            textspan.style.borderStyle = "none";
                            textspan.style.display = "inline";
                            textspan.style.fontSize = currentFontSize + fontSizeUnits;
                            textspan.style.textDecoration = "none";
                            parent_element.replaceChild(textspan, curr_element);
                            allResizableTextElements[allResizableTextElements.length] = textspan
                        }
                    }
                } else if (!new RegExp('\\bbdc_MasterLayout\\b').test(curr_element.className)) {
                    for (var i = 0; curr_element.childNodes[i]; i++) {
                        populateFontSizeElements(curr_element.childNodes[i], curr_element);
                    }
                }
            }
            populateFontSizeElements(document.getElementsByTagName("body")[0], document);
            initFontSizeElements = function() { } /*
        											The initFontSizeElements function should only be executed once,
        											so after it is called for the first time it re-assigns itself to an empty function.
        											*/
        }; // end of function initFontSizeElements

        function resizeAllText(newFontSize) {
            initFontSizeElements();
            newFontSize = parseFloat(newFontSize);

            for (var i = 0; i < allResizableTextElements.length; i++) {
                if (!new RegExp("\\bbdc_MasterLayout\\b").test(allResizableTextElements[i].className)) { // added to avoid resized elements from severely affecting the overall page-layout/look-and-feel
                    allResizableTextElements[i].style.fontSize = newFontSize + fontSizeUnits;
                }
            }

            document.cookie = 'bdc_fontsize=' + newFontSize + ';path=/';
        } // end of function resizeAllText


        biggerTextButton.onclick = function() {
            if (currentFontSize + delta <= maxSize) {
                currentFontSize = currentFontSize + delta;
                resizeAllText(currentFontSize);
            }
        };

        smallerTextButton.onclick = function() {
            if (currentFontSize - delta >= minSize && currentFontSize - delta > 0) {
                currentFontSize = currentFontSize - delta;
                resizeAllText(currentFontSize);
            }
        };

        /* We now read the cookies to see whether or not we should initialize the font-sizes to something other than 1 */
        var cookieNameWithEqualsCharacter = 'bdc_fontsize=';
        var allCookiesInArray = document.cookie.split(';');
        for (var i = 0; i < allCookiesInArray.length; i++) {
            var aCookie = allCookiesInArray[i];
            while (aCookie.charAt(0) == ' ') {
                aCookie = aCookie.substring(1, aCookie.length);
            }

            if (aCookie.indexOf(cookieNameWithEqualsCharacter) == 0) {
                currentFontSize = parseFloat(aCookie.substring(cookieNameWithEqualsCharacter.length, aCookie.length));
                currentFontSize = Math.min(Math.max(currentFontSize, minSize), maxSize);
                resizeAllText(currentFontSize);
            }
        }

    } ();

    new function() {
        var userAgent = navigator.userAgent;
        var versionOffset = userAgent.indexOf("MSIE");
        var isIE = (versionOffset >= 0);
        var isPreIE7 = false;
        var fullVersionIE = "";
        var majorVersionIE = "";

        function CanHaveClass__CssFriendlyAdapters(element) {
            return ((element != null) && (element.className != null));
        }

        function HasAnyClass__CssFriendlyAdapters(element) {
            return (CanHaveClass__CssFriendlyAdapters(element) && (element.className.length > 0));
        }

        function HasClass__CssFriendlyAdapters(element, specificClass) {
            return (HasAnyClass__CssFriendlyAdapters(element) && (element.className.indexOf(specificClass) > -1));
        }

        function AddClass__CssFriendlyAdapters(element, classToAdd) {
            if (HasAnyClass__CssFriendlyAdapters(element)) {
                if (!HasClass__CssFriendlyAdapters(element, classToAdd)) {
                    element.className = element.className + " " + classToAdd;
                }
            }
            else if (CanHaveClass__CssFriendlyAdapters(element)) {
                element.className = classToAdd;
            }
        }

        function AddClassUpward__CssFriendlyAdapters(startElement, stopParentClass, classToAdd) {
            var elementOrParent = startElement;
            while ((elementOrParent != null) && (!HasClass__CssFriendlyAdapters(elementOrParent, topmostClass))) {
                AddClass__CssFriendlyAdapters(elementOrParent, classToAdd);
                elementOrParent = elementOrParent.parentNode;
            }
        }

        function SwapClass__CssFriendlyAdapters(element, oldClass, newClass) {
            if (HasAnyClass__CssFriendlyAdapters(element)) {
                element.className = element.className.replace(new RegExp(oldClass, "gi"), newClass);
            }
        }

        function SwapOrAddClass__CssFriendlyAdapters(element, oldClass, newClass) {
            if (HasClass__CssFriendlyAdapters(element, oldClass)) {
                SwapClass__CssFriendlyAdapters(element, oldClass, newClass);
            }
            else {
                AddClass__CssFriendlyAdapters(element, newClass);
            }
        }

        function RemoveClass__CssFriendlyAdapters(element, classToRemove) {
            SwapClass__CssFriendlyAdapters(element, classToRemove, "");
        }

        function RemoveClassUpward__CssFriendlyAdapters(startElement, stopParentClass, classToRemove) {
            var elementOrParent = startElement;
            while ((elementOrParent != null) && (!HasClass__CssFriendlyAdapters(elementOrParent, topmostClass))) {
                RemoveClass__CssFriendlyAdapters(elementOrParent, classToRemove);
                elementOrParent = elementOrParent.parentNode;
            }
        }

        function IsEnterKey() {
            var retVal = false;
            var keycode = 0;
            if ((typeof (window.event) != "undefined") && (window.event != null)) {
                keycode = window.event.keyCode;
            }
            else if ((typeof (e) != "undefined") && (e != null)) {
                keycode = e.which;
            }
            if (keycode == 13) {
                retVal = true;
            }
            return retVal;
        }

        if (isIE) {
            fullVersionIE = parseFloat(userAgent.substring(versionOffset + 5, userAgent.length));
            majorVersionIE = parseInt('' + fullVersionIE);
            isPreIE7 = majorVersionIE < 7;
        }

        function IsMenuRoot_AspNetMenu(ele) {
            if (ele.parentNode != null) {
                if (ele.parentNode.className == "topNav") {
                    return true;
                }
            }
            return false;
        }

        function SetHover__AspNetMenu() {
            var menus = document.getElementsByTagName("ul");
            for (var i = 0; i < menus.length; i++) {
                if (IsMenuRoot_AspNetMenu(menus[i])) {
                    // get the hoverClass for this menu
                    var hoverClass = "over";
                    // assign a hover to all li within this menu
                    var items = menus[i].getElementsByTagName("li");
                    for (var k = 0; k < items.length && hoverClass != ''; k++) {
                        items[k].onmouseover = function() { AddClass__CssFriendlyAdapters(this, hoverClass); }
                        items[k].onmouseout = function() { RemoveClass__CssFriendlyAdapters(this, hoverClass); }
                    }
                }
            }
        }

        if (isPreIE7) {
            SetHover__AspNetMenu();
        }
    } ();

    var bdc_leftnavbar = document.getElementById("bdc_leftnavbar");
    if (bdc_leftnavbar) {
        var navLinks = bdc_leftnavbar.getElementsByTagName("a");
        for (var n = 0; n < navLinks.length; n++) {
            new function() {
                var i = n;
                navLinks[i].onmouseover = function() {
                    navLinks[i].style.color = "#e60d2e";
                };
                navLinks[i].onmouseout = function() {
                    navLinks[i].style.color = "";
                };
            } ();
        }
    }


};
