/* Written by Justin Wehrman
 *
 * $LastChangedDate: 2009-06-09
 */
   
(function($) {
              
    // Render Kids Navigation when window loads
    $(window).load(function() {
        $('*').PositionKdsNav();
        $('*').RenderKidsNav();
        $('*').HorizontalScrollDetector();
    });
       
    $(window).scroll(function() {
        $('*').HorizontalScrollDetector();
    });
       
    $(window).resize(function() {
        $('*').PositionKdsNav();
        $('*').HorizontalScrollDetector();
    });

    $.fn.PositionKdsNav = function() {
        // Position nav using UnifiedNav location
        var UniNavLeft = $('#unifiednav_footer4').offset().left + 'px';
        var LogoContainer = $('#Colgate-Kids-Logo-Container');
        var TabContainer = $('#KN_TabList');
        $(LogoContainer).css('left',UniNavLeft);
        $(TabContainer).css('left',UniNavLeft);
    };

    $.fn.RenderKidsNav = function() {
        // Check for Multi-Line Links in Navigation
        // Via the use of <br> or <br /> tags
        var AllLinks = $('div[class*=KN_Link]');
        var BreakCount = 0; // initialize Break Count
        $(AllLinks).each(function(i) {
            var BreakSearch = $(AllLinks[i]).html();
            var hasBreak = $(BreakSearch).find('br');
            if ($(hasBreak).size() != 0) {
                if (BreakCount == 1) {
                    BreakCount = BreakCount + 1;
                } else if (BreakCount != 1) {
                    BreakCount = BreakCount + 1;
                }
                $(AllLinks).addClass('NoBreak');
                $(this).removeClass('NoBreak').addClass('LinkBreak');
            } else {
                // do nothing
            }
        });
           
        var BreakSize = 1;
        var KidsNav = $('div[id=KidsNavigation]');
        var KidsNavHeight = $(KidsNav).height();
        var GreyBar = $('div[id=KN_GreyBar]');
                                              
        if (BreakCount == 1) {
            BreakSize = 14;
            $(GreyBar).css('top','-131px');
        } else if (BreakCount == 2) {
            BreakSize = 12;
            $(GreyBar).css('top','-129px');
        } else if (BreakCount >= 3) {
            BreakSize = 16;
            $(GreyBar).css('top','-133px');
        }
        var KidsNewHeight = KidsNavHeight + BreakSize; // if 1 break: height = 16, if 2: height = 8, 3 break: height = 4, 4 break: height = 4
        $(KidsNav).height(KidsNewHeight);
        // Kids Navigation Setup
        var KidsNav = $('div[id=KidsNavigation]');
        // Render Navigation Gradient
        $(KidsNav).gradient({
            from: 'FE0000',  // Light Red
            to: 'D00000',     // Dark Red
            direction: 'horizontal'
        })
        // Set Tab Height 
        var KidsNavHeight = $(KidsNav).height();
        $('div[id=KN_TabList]').css('height', KidsNavHeight);
        // Render On/Off States of Nav Links
        var LinkItem = $('.KN_Link');
        var ActiveLinkItem = $('.KN_Link_ON');
        // Mouseover
        $(LinkItem).mouseover(function(event) {
            // Remove inactive class
            $(this).removeClass('KN_Link');
            // Add active class
            $(this).addClass('KN_Link_ON');
            // Render Drop Shadow
            $(this).dropShadow({left: 3, top: 4, opacity: 0.4, blur: 3});
            // Render Corners
            $(this)
                .corner('tl round 8px #F10000')   // Top Left
                .corner('tr round 8px #E50000')   // Top Right
                .corner('bl round 8px #C70000')   // Bottom Left
                .corner('br round 8px #930000');  // Bottom Right
        });
        // Mouseout
        $(LinkItem).mouseout(function(event) {
            // Remove active class
            $(this).removeClass('KN_Link_ON');
            // Add inactive class
            $(this).addClass('KN_Link');
            // Remove Drop Shadow
            $(this).removeShadow();
            // Remove Corners
            $(this).corner('destroy');
        });
        // Active Tab Rendering
        // Render Drop Shadow
        $('.KN_Link_ON').dropShadow({left: 3, top: 4, opacity: 0.4, blur: 3});
        // Render Corners
        $(ActiveLinkItem)
            .corner('tl round 8px #F10000 ie6fixonload')   // Top Left
            .corner('tr round 8px #E50000 ie6fixonload')   // Top Right
            .corner('bl round 8px #C70000 ie6fixonload')   // Bottom Left
            .corner('br round 8px #930000 ie6fixonload');  // Bottom Right
    };
      
    // If Window is Scrolled Horizontally or Resized
    $.fn.HorizontalScrollDetector = function() {
        // Non-IE Scroll var
        var netHScroll = window.pageXOffset;
        // IE Scroll var
        var ieHScroll = document.documentElement.scrollLeft;
        var KidsNav = $('div[id=KidsNavigation]');
        var KidsNavWidth = $('div[id=KidsNavigation]').width();
        var GreyBar = $('div[id=KN_GreyBar]');
        var GreyBarWidth = $('div[id=KN_GreyBar]').width();
        // Test for Inner Width of Window
        var hWidth = 0;
        if (typeof(window.innerWidth) == 'number') {
            //Non-IE
            hWidth = window.innerWidth;
        } else if (document.documentElement && (document.documentElement.clientWidth)) {
            //IE 6+ in 'standards compliant mode'
            hWidth = document.documentElement.clientWidth;
        } else if (document.body && (document.body.clientWidth)) {
            //IE 4 compatible
            hWidth = document.body.clientWidth;
        }
        // Update Navigation width depending on browser
        // IE
        if ($.browser.msie) {
            var newNavWidth = ieHScroll + KidsNavWidth;
            $(KidsNav).css('width', hWidth + ieHScroll);
            $(GreyBar).css('width', hWidth + ieHScroll);
        // Non-IE
        } else {
            var newNavWidth = netHScroll + KidsNavWidth;
            $(KidsNav).css('width', hWidth + netHScroll);
            $(GreyBar).css('width', hWidth + netHScroll);
        }
    };
      
}) (jQuery);