

$(document).ready(function () {

    var BNS = 'Uninitialized';
    BNS = function () {

        // let the DOM note that JS is enabled
        $('html').removeClass('no-js').addClass('js-on');

        // determine if userAgent is IE / IE6, dependant on the DOM structure
        //var isIE = ($('html').attr('id').indexOf('ie') != -1) ? true : false;        
        var isIE = $.browser.msie;
        var isIE6 = ($('#ie6').length) ? true : false;
        var mobile = false; // to be implemented

        // input clear
        $('input.clear-on-focus').each(function () {

            $(this).data('initValue', $(this).val());

            $(this).focus(function (e) {

                if ($(this).val() === $(this).data('initValue'))
                    $(this).val('');
            });
            $(this).blur(function (e) {

                if ($(this).val() === '')
                    $(this).val($(this).data('initValue'));
            });
        });

        // font-sizer
        $('#font-size a').click(function (e) {
            e.preventDefault();
            var fontSizer = $('#font-size');
            var selectedClass = $(this).parent().attr('class');
            var className = 'default';

            fontSizer.attr('class', selectedClass);

            if (selectedClass.indexOf('lrg') != -1) className = 'lrg';
            if (selectedClass.indexOf('x-lrg') != -1) className = 'x-lrg';

            $('#content,#footer').removeClass('default lrg x-lrg').addClass(className);

            $(this).blur();
        });




        // Fix hovers in IE6
        if (isIE6) {
            $('#font-size li,#footer .products li h3').hover(
            function () {
                $(this).addClass('hover');
            },
            function () {
                $(this).removeClass('hover');
            }
        );
        }



        // mega menu & service menu
        $('#mega-menu > li > a,#service-menu > li > a').keydown(function (e) {
            // tabbing forward
            if (!e.shiftKey) {
                if (e.which == 9) {
                    // If we're blurring from the "How do I?" menu item, focus the search field.
                    if ($(this).attr('id') == 'how') {
                        e.preventDefault();
                        $(this).parent().removeClass('active');
                        $(this).parent().next().find('#search-query').focus();
                    }
                    else if ($(this).attr('id') == 'mm-tab-g' || $(this).parent().is(':last-child')) {
                        e.preventDefault();
                        $(this).parent().removeClass('active');
                        $('#content').find('a:eq(0)').focus();
                    }
                    else {
                        var nextTopLevel = $(this).parent().next().find('a:eq(0)');
                        if (nextTopLevel.length) {
                            e.preventDefault();
                            $(this).parent().removeClass('active');
                            nextTopLevel.focus();
                        }
                    }
                }
            }
            //tabbing backward
            else {
                if (e.which == 9) {
                    var prevTopLevel = $(this).parent().prev().find('a:eq(0)');
                    if (prevTopLevel.length) {
                        e.preventDefault();
                        $(this).parent().removeClass('active');
                        prevTopLevel.focus();
                    }
                    if ($(this).parent().is(':first-child')) {
                        $(this).parent().removeClass('active');
                    }
                }
            }
        });

        //MCM 
        var menu = {
            services: $('#ulServiceMenu'),
            mega: $('#mega-menu')
        };

        menu.services.data('contentHeight', 240);
        menu.services.data('tabOpen', false);

        menu.mega.data('contentHeight', 320);
        menu.mega.data('tabOpen', false);

        function activateMenu(menu) {

            if (!menu)
                return;

            // capture MENU mouse leave
            menu.mouseleave(function (e) {

                var node = menu.find('li.active'),
				content = node.children('.content');

                content.css('height', 0);
                content.css('border-width', '0');
                content.css('display', 'none');
                node.removeClass('active');

                menu.data('tabOpen', false);
            });

            // default behavior
            var menuItems = menu.children('li');
            menuItems.each(function (i) {

                $(this).children('.content').css('height', '0');

                // capture MENU ITEM mouse enter
                $(this).mouseenter(function (e) {

                    var node = $(e.currentTarget),
					content = node.children('.content');

                    if (menu.data('tabOpen')) {

                        menu.find('li.active').removeClass('active');
                        content.css('display', 'block');
                        content.css('border-width', '1px');
                        content.css('height', menu.data('contentHeight') + 'px');
                        node.addClass('active');

                        menu.data('tabOpen', true);
                    }
                    else {

                        menuTimeout = window.setTimeout(function () {

                            content.css('height', '0');
                            content.css('border-width', '0');
                            content.css('display', 'block');
                            content.css('border-width', '1px');
                            content.animate({ height: menu.data('contentHeight') }, 100, 'swing');
                            node.addClass('active');

                            menu.data('tabOpen', true);
                        }, 500);
                    }
                });

                // capture MENU ITEM mouse leave
                $(this).mouseleave(function (e) {

                    var node = $(e.currentTarget),
					content = node.children('.content');
                    //MCM - this doesn't seem to be working.
                    if (typeof menuTimeout != "undefined") {
                        if (menuTimeout)
                            clearTimeout(menuTimeout);
                    }
                    $('.content:animated').stop();

                    content.css('height', '0');
                    content.css('border-width', '0');
                    content.css('display', 'none');
                    node.removeClass('active');
                });
            });

            // screen reader behavior
            var menuTabs = menu.find('a.tab');
            menuTabs.each(function (i) {

                $(this).focus(function (e) {

                    menuItems.removeClass('active');
                    $(this).parent().addClass('active');
                    //alert(1);
                });
            });

            // skip & close buttons
            menuItems = menu.children('.content a.skip').add(menu.children('.content a.close'));
            menuItems.click(function (e) {

                var tab = menu.find('li.active');

                tab.removeClass('active');
                tab.next().find('a.tab').focus();
            });
        }

        activateMenu(menu.services);
        activateMenu(menu.mega);

        // disable dead links
        $('a[href=#]').click(function (e) { e.preventDefault(); });

        learningBar = {
            open: function () {
                $('#learning-bar').data('auto-opened', true);
                $('#learning-bar').addClass('open');
                if (!isIE6) {
                    $('#learning-bar .content').animate({ height: '55px' }, 1000);
                }
                else {
                    $('#learning-bar .content').height('55px');
                    this.ie6fix();
                }
                $('#minimize-learning-bar a').text($('#learning-bar').attr('data-close-text'));
                $('#learning-bar .content a').removeAttr('tabindex');

            },
            close: function () {
                $('#learning-bar').removeClass('open');
                if (!isIE6) {
                    $('#learning-bar .content').animate({ height: '0px' }, 500);
                }
                else {
                    $('#learning-bar .content').height('0px');
                    this.ie6fix();
                }
                $('#minimize-learning-bar a').text($('#learning-bar').attr('data-open-text'));
                $('#learning-bar .content a').attr('tabindex', '-1');
            },
            ie6fix: function () {
                var barHeight = $('#learning-bar').height(),
                bar = $('#learning-bar');
                bar.css('top', $(window).scrollTop() + ($(window).height() - barHeight - 2) + "px");
            }
        };

        // Remove skip to page navigation when the page navigation does not exist
        // If the left sidebar doesn't exist, the skip to content link should point to #content, not #content-head
        // Ideally this would be handled by the application and removed from javascript
        if (!$('#side-nav').length) {
            $('#top a[href=#side-nav]').remove();
            $('#top a[href=#content-head]').attr('href', '#content');
        }

        // Enable learning bar 
        $('#minimize-learning-bar a').text($('#learning-bar').attr('data-open-text'));
        $('#learning-bar .content a').attr('tabindex', '-1');
        $(window).scroll(function () {
            if ($(this).height() + $(this).scrollTop() >= $(document).height() - 500) {
                if (!$('#learning-bar').data('auto-opened')) {
                    learningBar.open();
                }
            }
        });

        if (isIE6) {
            $(window).resize(function () {
                learningBar.ie6fix();
            });
        }

        // Minimize learning bar

        $('#minimize-learning-bar').click(function (e) {
            e.preventDefault();
            if (!$(this).parent().hasClass('open')) {
                learningBar.open();
            }
            else {
                learningBar.close();
            }
        });

        // IE 6
        if (isIE6) {

            // implement fixed positioning for the learning bar	
            $(window).scroll(function () {
                learningBar.ie6fix();
            });

            $(document).ready(function () {
                learningBar.ie6fix();
            });

            // Fix hover of question divs
            $('#questions > div').hover(
            function () {
                if (!$(this).hasClass('active')) {
                    $(this).addClass('hover');
                }
                else {
                    $(this).addClass('hover-active');
                }
            },
            function () {
                $(this).removeClass('hover hover-active');
            }
        );
        }


        /* Tooltips*/

        $('#help-tooltips strong').hide();
        $('#help-tooltips').hide();

        $('#close-help').live('click', function () {
            $(this).prev().prev().focus();
            $('#context-help,#close-help').remove();
            return false;
        });

        $('#context-help').live('blur', function () {
            //$(this).closest('.tool-tip').eq(0).focus();
            $(this).prev().focus();
            $('#context-help,#close-help').remove();
        });


        $('.tool-tip').each(function () {

            $(this).click(function (e) {
                $('#context-help, #close-help').remove();

                // Save the reference to the help button, get the href, and the content of the linked <p> element
                var $button = $(this);
                var href = $button.attr('href');
                var content = $(href).html();

                // Create the help item and insert it after the the help button in DOM order
                $button.after('<div id="context-help" role="alertdialog" aria-hidden="false" aria-labelledby="help-title" aria-describedby="help-description"><h3 id="help-title">' + $('#help-tooltips strong').html() + '</h3><div id="help-description">' + content + '</div></div>');
                var $help = $('#context-help');
                $help.find('strong').eq(0).remove();

                // Create the close button and insert it after the help text in DOM order
                $help.after('<a href="#context-help" id="close-help"><img src="/gfx/tooltip-close.gif" alt="Close glossary" /></a>');
                var $close = $('#close-help');

                // Get the top and left position of the help button for positioning the close button and help text, also set the tabindex of the help text to make it focusable

                var $position = $button.position();

                $help
				.css("top", ($position.top - 9) + "px")
				.css("left", ($position.left + 17) + "px")
				.show();

                closeFix = 2;
                if ($.browser.msie && parseInt($.browser.version) == 6 || parseInt($.browser.version) == 7) closeFix = 0;

                $close
				.css("top", ($position.top - closeFix) + "px")
				.css("left", ($position.left) + "px")
				.show();

                var help_el = document.getElementById('context-help');
                help_el.tabIndex = -1;
                help_el.focus();

                $(help_el).keydown(function (e) {
                    // ESCAPE key pressed
                    if (e.keyCode == 27) {
                        //$(this).prevAll('.help').eq(0).focus();
                        $(this).prev().focus();
                        $('#context-help, #close-help').remove();
                        return false;
                    }
                });

                return false;

            });

            //$(this).mouseleave( hideToolTip );
        });

        // collapsable Q & A
        var questions = $('.question');
        questions.click(function (e) {

            var clicked = $(e.target);
            if (clicked.parents('.content').length > 0 || clicked.is('.content'))
                return true;

            var isOpen = ($(this).hasClass('active')) ? true : false;

            questions.removeClass('active');

            if (!isOpen)
                $(this).addClass('active');

            return false;
        });

        // tabbed content
        var tabbedContent = $('.tabbed-content'),
		controller,
		tabs;

        tabbedContent.each(function () {
            controller = $(this).find('.tab-controller');
            tabs = controller.find('.tabs a');
            tabs.each(function () {
                $(this).click(function (e) {
                    e.preventDefault();
                    var className = $(this).parent().attr('class')
                    controller.attr('class', 'tab-controller ' + className);
                    var currentMarker = $('.tabs .current-marker');
                    currentMarker.insertBefore($(this));
                    $('div.content.' + className).focus();
                });
            });

        });

        // zebra stripe
        $('.zebra-stripe > :odd').each(function () {

            $(this).addClass('odd');
        });

        return 'Initialized';
    } ();
    if (BNS != 'Initialized') alert('Page could not initialize.');

    Cufon.now();


});

