﻿jQuery.extend(jQuery.expr[':'], {
    external: function(a, i, m)
    {
        if (!a.href)
        {
            return false;
        }
        if (a.hostname && a.hostname !== window.location.hostname)
        {
            var fixedHostname = a.hostname.replace(/^\s+|\s+$/g, '').toLowerCase().replace('qa.', '').replace('stage.', '').replace('www.', '');
            var fixedHref = a.href.replace(/^\s+|\s+$/g, '').toLowerCase().replace('http://','').replace('https://','').replace('qa.', '').replace('stage.', '').replace('www.', '');

            if (jQuery.inArray(fixedHostname, safeList) >= 0)
            {
                return false;
            }
            if (jQuery.inArray(fixedHref, safeUrls) >= 0) {
                return false;
            }
            if (fixedHostname.indexOf('javascript') > 0 || fixedHostname.indexOf('(') > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }
   
    }
});

// WARN ON LEAVE CODE
function warn_on_leave(site)
{
    var msg = "\nLinks which take you out of Abbott Laboratories worldwide \n" +
             "web site are not under the control of Abbott Laboratories, \n" +
             "and Abbott Laboratories is not responsible for the contents \n" +
             "of any such site or any further links from such site. Abbott \n" +
             "Laboratories is providing these links to you only as a \n" +
             "convenience, and the inclusion of any link does not imply \n" +
             "endorsement of the linked site by Abbott Laboratories.\n\n" +
             "Do you wish to leave this site?";

    if (confirm(msg))
    {
        window.open(site);
    }
    else
    {
        return;
    }
} 

function InitializeTopNavigation()
{
    $("ul#top-navigation > li").hover(
        function() 
        {
            $("div.submenu_container").hide();        
            $(this).addClass("hover");
            
            var rightSibling = $(this).next();
//            if($(rightSibling).is(".active"))
//            {
                $(this).addClass("clip_active_item");
//            }
                        
            ToggleSubmenu($(this), 'open');
        },
        function()
        { 
            $("ul#top-navigation > li").removeClass("clip_active_item");
            ToggleSubmenu($(this), 'close');
            $(this).removeClass("hover");             
            $("div.submenu_container").hide();  
        }
    );
    
    FixTopNavigationPNGs();
    
    $("ul#overhang-menu-items > li#overhang-menu-search-button").click(function() {
        ToggleSearchBox();
    });
    
    $(".search_text").keydown(function(e) {
        if(e.which == 188 || e.which == 190)
        {
            return false;
        }
    });

    $("ul#top-navigation > li > a[href=/unstoppabletour]").click(function() {
        pageTracker._trackEvent('Unstoppable-Tour', 'Click', 'EAS.com Unstoppable Tab Header Nav');
        s.tl(this, 'o', 'Unstoppable Tab Header Nav');
    });
}

function FixTopNavigationPNGs()
{
    $("div#eas-logo-container > a > img").ifixpng();
    $("div#abbott-signature-container > a > img").ifixpng();
    $("div.submenu_container > span").ifixpng();
    $("div#overhang-menu-wrapper > span").ifixpng();
}

function ToggleSubmenu(menuItem, state)
{
    var subMenu = $(menuItem).children("div.submenu_container");
            
    var hasSubMenu = $(subMenu).length == 1;
            
    if(hasSubMenu)  
    {
        if(state == 'open')
        {
            $(subMenu).show();
        }
        else if(state == 'close')
        {
            $(subMenu).hide();        
        }
    }
}

function InitializeContentTabs()
{
    $("ul.content_tabs").each(function() {
        FormatContentTabs($(this));    
    });
}

function FormatContentTabs(tabs)
{
    var tabItems = $(tabs).children("li");
    var itemWrapper = "<span class=\"tab_item\" />";
    var itemTextWrapper = "<span class=\"tab_item_text\" />";
    var corner = "<span class=\"corner\" />";
    
    $(tabItems).each(function() {
        $(this).wrapInner(itemTextWrapper).wrapInner(itemWrapper);  
        
        $(this).hover(
            function() {
                $(this).addClass("hover");
            },
            function() {
                $(this).removeClass("hover");
            }
        );
          
    });
    
    //format first tab     
    $(tabItems).first().addClass("first");
    $(tabItems).first().prepend(corner);
    
    //format last tab 
    $(tabItems).last().addClass("last");
    $(tabItems).last().prepend(corner);      
}

function InitializeLeftNavigation()
{
    $("ul#left-navigation > li").hover(function() {
        $(this).addClass("hover");
    },
    function() {
        $(this).removeClass("hover");
    });
}

function ToggleSearchBox()
{
    var searchBox = $("div#overhang-menu-search-box-container");
    var searchText = $(".overhang_menu_search_text").first();
    
    var overhangMenuWidth = $("div#overhang-menu").outerWidth();
    var searchBoxOffset = 500 - parseInt(overhangMenuWidth);

    $(searchBox).css("left", searchBoxOffset + "px");
    
    $(searchBox).toggle();
    
    if($(searchBox).is(":visible"))
    {
        $(searchText).val("");
        $(searchText).focus();    
    }
}

function SetWhiteBackground()
{
    $("div#content-container").addClass("white_background");
}
