$(document).ready(function() {
//-------- Main Navigation --------//

    // used for menu Delay
    var menuToDelay;
    var timerObj;
var hideMenuObj;
var menuIsShowing = false;
    
// primary navigation (used to call child menu)
var primaryLi = new Array();
primaryLi = $('ul#nav>li>a');

// Drop Down Column Array (used for changing classes)
var ulList = new Array();
ulList = $('.dropdowns .cfix').children();

// List of drop down menus
var menuList = new Array();
menuList = $('.dropdown');

//Used to find the index of the current item
findIndex = function(currentArray, itemToLocate){
var itemIndex;
for(i=0; i < currentArray.length; i++){
if(currentArray[i] == itemToLocate[0]){
itemIndex = i;
}
}
return itemIndex;
}

// drop down close btns functionality
$('.closeBtn').mousedown(function(){
parentList = $(this).parent().parent().parent().parent();
parentList.slideUp(300);
});

// hide drop down on rollout
$(menuList).hover(function() {menuIsShowing = true;}, function() {menuIsShowing = false; $(this).slideUp(300);});

primaryLi.mouseover(function(){
    menuToDelay = $(this);
var visibleMenus = $('.dropdown:visible');
var currentMenu = findIndex(primaryLi, $(this)); 
var menuToChange = findIndex(menuList, visibleMenus);

if(currentMenu != menuToChange){
if(visibleMenus.length != 0){
visibleMenus.slideUp(300);
            timerObj = setTimeout('delayMenu()', 300);
        } else {
            timerObj = setTimeout('delayMenu()', 300);
        }
    }
});

primaryLi.mouseout(function(){
hideMenuObj = setTimeout('hideMenu()', 100);
    clearTimeout(timerObj);
});


delayMenu = function(){
var visibleUls = $('.dropdown .dropdowns .cfix').children();
visibleUls.addClass(" active");
var currentMenu = findIndex(primaryLi, menuToDelay);
$(menuList[currentMenu]).slideDown(300);
}

hideMenu = function(){
if(!menuIsShowing){
$('.dropdown:visible').slideUp(300);
menuIsShowing = false;
}
clearTimeout(hideMenuObj);
}


// Change column class on rollover of a column
ulList.mouseover(function(){
ulList.removeClass(" active");
var childArray = new Array();
childArray = $(this).children();
elementToChange = childArray[0];
$(this).addClass(" active");
});
ulList.mouseout(function(){
var childArray = new Array();
childArray = $(this).children();
elementToChange = childArray[0];
$(this).removeClass(" active");
});

    
// Last Item (Used to determine last element)
var lastNodeList = new Array();
$(lastNodeList).addClass(" final");


//-------- Main Navigation menu height fix --------//
    var primaryNavGroups = $('.dropdowns div.cfix');
var lastNodeList = $('.dropdowns div.cfix ul:last-child');
    var navWidthArray = primaryNavGroups;
for(i=0; i < primaryNavGroups.length; i++){
        childGroup = $(primaryNavGroups[i]).children('ul');
if($(childGroup[i]).html()!=undefined){
var maxHeight = 0;
var totalWidth = 0;
for(var j=0; j < childGroup.length; j++){
                if($(childGroup[j]).height() >= maxHeight){
                    maxHeight = $(childGroup[j]).height();
                }
                    totalWidth += $(childGroup[j]).width();
            }
            if(totalWidth >= 980){
                $(childGroup[childGroup.length-1]).addClass('final');
            }
            $(childGroup).height(maxHeight+10);
        }
    }
    $('.dropdown').css({display: 'none'});
    $('.dropdown').css({visibility: 'visible'});


//-------- WorldWide Navigation --------//
var worldDropMenu = $('.dropdownWorld');
var worldWideIsShowing = false;
var hideWWMenuObj;
var timerWWObj;

$('ul li.map').hover(function() {
worldWideIsShowing = true;
    timerWWObj = setTimeout('delayWWMenu()', 100);
}, function() {
worldWideIsShowing = false;
});

$('ul li.map').mouseout(function(){
hideWWMenuObj = setTimeout('hideWWMenu()', 100);
});


delayWWMenu = function(){
$('.dropdownWorld').slideDown(300);
$('ul li.map').children().addClass("worldHover");
}

hideWWMenu = function(){
if(!worldWideIsShowing){
clearTimeout(timerWWObj);
}
clearTimeout(hideWWMenuObj);
if(!worldWideIsShowing){
$('.dropdownWorld:visible').slideUp(300);
$('ul li.map').children().removeClass("worldHover");
worldWideIsShowing = false;
}
}

// drop down close btns functionality
$('.closeWorldBtn').mousedown(function(){
parentList = $(this).parent().parent().parent();
worldWideIsShowing = false;
parentList.slideUp(300);
$('ul li.map').children().removeClass("worldHover");
});

// hide drop down on rollout
$(worldDropMenu).hover(function() {worldWideIsShowing = true;}, function() {worldWideIsShowing = false; $(this).slideUp(300);$('ul li.map').children().removeClass("worldHover");});
});
