
if (!bt) var bt = {};
if (!bt.Global) bt.Global = {};


(function(o, $) {

    var $this = o;

    $this.pageLoad = function() {

        // Font resizing - using jQuery plugin jquery.cookie.js
        // Reset Font Size
        var originalFontSize = $('html').css('font-size');
        var $bt_fontSize = "bt-FontSize";
        // if exists load saved value
        if ($.cookie($bt_fontSize)) {
            var $getSize = $.cookie($bt_fontSize);
            $("html").css({
                fontSize: $getSize + ($getSize.indexOf("px") != -1 ? "" : "px")
            }); // IE fix for double "pxpx" error
        }
        $(".resetFont").click(function() {
            $('html').css('font-size', originalFontSize); //reset to original size of HTML - 16px
            $.cookie($bt_fontSize, '', {
                expires: -1
            }); // delete cookie
            return false;
        });
        // Increase Font Size
        $(".increaseFont").click(function() {
            //alert($.cookie('bt-FontSize'));
            if ('18' > $.cookie($bt_fontSize) || $.cookie($bt_fontSize) == null) {
                var currentFontSize = $('html').css('font-size');
                var currentFontSizeNum = parseFloat(currentFontSize, 10);
                var newFontSize = currentFontSizeNum + 1; //1px increase up to 20px
                $('html').css('font-size', newFontSize);
                $.cookie($bt_fontSize, newFontSize);
            }
            return false;
        });
        // Decrease Font Size
        $(".decreaseFont").click(function() {
            //alert($.cookie('bt-FontSize'));
            if ('14' < $.cookie($bt_fontSize) || $.cookie($bt_fontSize) == null) {
                var currentFontSize = $('html').css('font-size');
                var currentFontSizeNum = parseFloat(currentFontSize, 10);
                var newFontSize = currentFontSizeNum - 1; //1px decrease down to 14px
                $('html').css('font-size', newFontSize);
                $.cookie($bt_fontSize, newFontSize);
            }
            return false;
        });

        //flash a-feature on home page settings
        var flashvars = {};
        var params = {};
        var attributes = {};
        params.wmode = "opaque";
        attributes.id = "a_feature";
        flashvars.linkURL = "/funds/funds.asp";
        swfobject.embedSWF("/lib/swf/advance_a_feature.swf", "flashFeatureSWF", "900", "270", "9.0.0", false, flashvars, params, attributes);

        //megamenu col background stretch
        stretch('#super', '.content', '12');
        //alignJSfix - general col stretch
        stretch('.alignJSFix', '.genericBox', '12');

        //tabs JS functionality
        var tabs = $('.idTabsDiv');
        var tabNav = $("ul#tabNav li a[href*='#']", tabs);
        tabNav.each(function() {
            $("#" + $(this).attr('href').split('#')[1]).hide();
        });
        $('ul#tabNav').click(function(e) {
            if (e.target.tagName.toLowerCase() != 'a') return false;
            var oldTabId = $('li a[class=selected]', this).removeClass('selected').attr('href');
            var currentTabId = $(e.target).addClass('selected').attr('href');
            $(oldTabId).hide();
            $(currentTabId).show();
            return false;
        });
        $('.idTabsDiv ul#tabNav').each(function() {
            $('li a:first', this).click();
        });

        //funds page cyclo #cyclo
        stop = 'false'; //global variable set to control arrow "clicked status" in nonstopCyclo function
        $('#rightArrow').click(function() {
            stop = 'true';
            cycle('right', '50');
        });
        $('#leftArrow').click(function() {
            stop = 'true';
            cycle('left', '50');
        });
        nonstopCyclo('right');

        //setup text box hints
        wireUpDisplayTextboxes();

        //setup mega menu
        var megaConfig = {
            interval: 50,
            sensitivity: 4,
            over: addMega,
            timeout: 50,
            out: removeMega
        };
        //use  non-hoverIntent solution for IE6 & page containing #flashFeature
        if ($('#flashFeature').length > 0) { // $.browser.msie && $.browser.version.substr(0, 1) < 7 && 
            //jQuery solution for megamenu hovering class switch
            $("li.menu-item").mouseover(function() {
                $(this).addClass("hovering");
            });
            $("li.menu-item").mouseout(function() {
                $(this).removeClass("hovering");
            });
        } else $("li.menu-item").hoverIntent(megaConfig); //use hoverIntent in any other case      

        /*IE 6 li:hover background menu fix*/
        $('#leftNav li').mouseover(function() {
            $(this).addClass('shover');
        });
        $('#leftNav li').mouseout(function() {
            $(this).removeClass('shover');
        });

        //png images transparency fix
        $("#overlayVideo").pngFix();
        $("#landingTopBoxWrapper .interactiveBox").pngFix();
        $(".marketOutlookRight").pngFix();
        $("#content .box").pngFix(); //#advanceOnMarket box was removed since IE6 "More insights from the Advance market experts" link non-clickable issues
        
        // adding classes for styling to the main nav
        $('.topNavLv1 ul li:first').addClass('first');
        $('.topNavLv1 ul li:last').addClass('last');

    };

    //function stretch for aligning boxes
    function stretch(div, boxesStretch, baseFontSize) {
        $(div).each(function() {
            var maxHeight = 0;
            $(this).find(boxesStretch).each(function() {
                var thisHeight = $(this).height();
                if (thisHeight > maxHeight) {
                    maxHeight = thisHeight
                }
            });
            var baseFontSizeNum = parseFloat(baseFontSize, 10);
            var emHeight = maxHeight / baseFontSizeNum;
            emHeight = emHeight + 'em';
            $(this).find(boxesStretch).height(emHeight);
        });
    };

    //recursive function for non-stop rotation of cyclo on 02 template
    function nonstopCyclo(direction) {
        setTimeout(function() {
            if (stop == 'true') return; //breaking recursive call when clicked on arrows to allow user to click
            cycle(direction, '1000');
            nonstopCyclo(direction);
        }, '10000');
    }

    //function cycle for cyclo on 02-funds template
    function cycle(direction, timeout) {
        var thisLI = $("#cycloList li.selected");
        if (direction == 'right') {/*right rotation settings*/
            var overLI = $("#cycloList li:first");
            var newLI = $("#cycloList li.selected").next();
        } else {/*left rotation settings*/
            var overLI = $("#cycloList li:last");
            var newLI = $("#cycloList li.selected").prev();
        }
        $(thisLI).removeClass('selected').fadeOut(timeout, function(callback) {
            if (newLI.length) {
                $(newLI).fadeIn(timeout * 4).addClass('selected');
            } else {
                $(overLI).fadeIn(timeout * 4).addClass('selected');
            }
        });
    }

    // displays hint text on any input element with the 'title' attribute set
    function wireUpDisplayTextboxes() {
        var el = $('input[Title]');

        // show the display text
        el.each(function(i) {
            $(this).attr('value', $(this).attr('title'));
        });

        // enable the blur & focus
        el.focus(function() {
            if ($(this).attr('value') == $(this).attr('title')) {
                $(this).attr('value', '');
            }
            $(this).addClass('on');
        }).blur(function() {
            if ($(this).attr('value') == '') {
                $(this).attr('value', $(this).attr('title'));
                $(this).removeClass('on');
            }
        });
    }

    //functions for megamenu hovering class trigger
    function addMega() {
        $(this).addClass("hovering");
    }
    function removeMega() {
        $(this).removeClass("hovering");
    }


})(bt.Global, jQuery);

jQuery(bt.Global.pageLoad);

