/*=================================================================
    Header Script - Nav and Dropdown panels
=================================================================*/
$J(document).ready(function () {

    initDropdownLinks();
    initMainNavDropdownLinks();

});


/*=================================================================
    Dropdown panels
=================================================================*/
var contentSource;
var currentDropdownLink;
var menuOverlayWindow;
var menuOverlayActive = false;
var overlayContentLeft;
var overlayContentTop;
var rolloutTimer;
var rolloutTimerLimit = 200;
var rolloverTimer;
var rolloverTimerLimit = 200;
var tempHoverItem;

// Init link actions
function initDropdownLinks() {

    /* Focus On */
    $J('.dropdownLink.linkFocusOn').click(function() {
        $J(this).blur();
        initMainNavClass();
        return false;
    });        
      	
    $J('.dropdownLink.linkFocusOn').mousemove(function() {
        startHoverTimer(this);
        initMainNavClass();
	});

	$J('.dropdownLink.linkFocusOn').hover(function () {
	    startHoverTimer(this);
	    initMainNavClass();

	}, function () {
	    startRolloutTimer(this);

	});
	

    /* Our Websites */
    $J('.dropdownLink.linkOurWebsites').click(function() {
        $J(this).blur();
        return false;
    });
      	
    $J('.dropdownLink.linkOurWebsites').mousemove(function() {
        startHoverTimer(this);
        initMainNavClass();
	});
	
	$J('.dropdownLink.linkOurWebsites').hover(function() {

	    startHoverTimer(this);
	    initMainNavClass();

    }, function (){
	
		startRolloutTimer(this);
	
	});
    
	
}


// Init Main nav hover actions
function initMainNavDropdownLinks() {

	/* Services */   	
    $J('#mainNav a.services').mousemove(function() {
		startHoverTimer(this);
	});
	
    $J('#mainNav a.services').hover(function() {
		
		startHoverTimer(this);

    }, function (){
	
		startRolloutTimer(this);
	
	});
		 

//	/* Projects */ 	
//    $J('#mainNav a.projects').mousemove(function() {
//		startHoverTimer(this);
//	});
//	
//    $J('#mainNav a.projects').hover(function() {
//		
//		startHoverTimer(this);

//    }, function (){
//	
//		startRolloutTimer(this);
//	
//	});
  

	/* Investor */	
    $J('#mainNav a.investor').mousemove(function() {
		startHoverTimer(this);
	});    
	
    $J('#mainNav a.investor').hover(function() {
		
		startHoverTimer(this);

    }, function (){
	
		startRolloutTimer(this);
	
	});


	/* Career */    	
    $J('#mainNav a.careers').mousemove(function() {
		startHoverTimer(this);
	});
	
    $J('#mainNav a.careers').hover(function() {
		
		startHoverTimer(this);

    }, function (){
	
		startRolloutTimer(this);
	
	});
	

	/* Responsibility */    	
//    $J('#mainNav a.responsibility').mousemove(function() {
//		startHoverTimer(this);
//	});
//	
//    $J('#mainNav a.responsibility').hover(function() {
//		
//		startHoverTimer(this);

//    }, function (){
//	
//		startRolloutTimer(this);
//	
//	});

}


// Start hover timer or kill rollout timer
function startHoverTimer(item){
	
	
	// No active overlay and not currently hovering this item
	if (menuOverlayActive == false && tempHoverItem != item) {
			
		tempHoverItem = item;
		
		rolloverTimer = setTimeout( function() {
		
			clearTimeout(rolloverTimer);
			setMainNavOverlay();
		
		}, rolloverTimerLimit);
	
	
	// Already open but new nav item
	} else if (menuOverlayActive == true && tempHoverItem != item) {
		
		tempHoverItem = item;
		setMainNavOverlay();
			
	}
	
	if (rolloutTimer != null) clearTimeout(rolloutTimer);
	
}


// Start rollout timer or kill hover timer
function startRolloutTimer(item){
		
	// No current overlay so kill the hover
	if (menuOverlayActive == false && rolloverTimer != null) { 
		
		tempHoverItem = null;
		clearTimeout(rolloverTimer);
	
	
	// Active over for this item
	} else {
	
		rolloutTimer = setTimeout(removeMenuOverlay, rolloutTimerLimit);	
	
	}
	
}


// Populate the nave overlay
function setMainNavOverlay(){
		
    if (menuOverlayActive == false) showMenuOverlay(tempHoverItem);
	
	// Expand main nav links a little
	$J('#mainNav li').removeClass('hover');
	
	if ($J(tempHoverItem).parents('#mainNav').length > 0) $J(tempHoverItem).parent().addClass('hover');
	
	
	$J(menuOverlayWindow).attr('class', '');
	
	/* Services */ 
	if($J(tempHoverItem).hasClass('services')){
		
		// Set HTML
		contentSource = $J('.hoverContentServices').html();
		$J(menuOverlayWindow).html(contentSource);
		$J(menuOverlayWindow).addClass('hoverContentServices');
        
		// Position
		overlayContentLeft = getAbsoluteLeft(tempHoverItem);
		overlayContentTop = getAbsoluteTop(tempHoverItem) + $J(tempHoverItem)[0].offsetHeight;

			
	
	/* Projects */
	} else if($J(tempHoverItem).hasClass('projects')){
	
		// Set HTML
        contentSource = $J('.hoverContentProjects').html();
        $J(menuOverlayWindow).html(contentSource);
        $J(menuOverlayWindow).addClass('hoverContentProjects');

        // Position
        overlayContentLeft = getAbsoluteLeft(tempHoverItem);
        overlayContentTop = getAbsoluteTop(tempHoverItem) + $J(tempHoverItem)[0].offsetHeight;
		
	
	/* Investor */
	} else if($J(tempHoverItem).hasClass('investor')){
	
		// Set HTML
        contentSource = $J('.hoverContentInvestor').html();
        $J(menuOverlayWindow).html(contentSource);
        $J(menuOverlayWindow).addClass('hoverContentInvestor');

        // Position
        overlayContentLeft = getAbsoluteLeft(tempHoverItem);
        overlayContentTop = getAbsoluteTop(tempHoverItem) + $J(tempHoverItem)[0].offsetHeight;
	
	
	/* Careers */
	} else if($J(tempHoverItem).hasClass('careers')){
	
		// Set HTML
        contentSource = $J('.hoverContentCareers').html();
        $J(menuOverlayWindow).html(contentSource);
        $J(menuOverlayWindow).addClass('hoverContentCareers');

        // Position
        overlayContentLeft = getAbsoluteLeft(tempHoverItem) - ($J(menuOverlayWindow)[0].offsetWidth - $J(tempHoverItem)[0].offsetWidth);
        overlayContentTop = getAbsoluteTop(tempHoverItem) + $J(tempHoverItem)[0].offsetHeight;
	
	
	/* Responsibility */
	} else if($J(tempHoverItem).hasClass('responsibility')){
	
		// Set HTML
        contentSource = $J('.hoverContentResponsibility').html();
        $J(menuOverlayWindow).html(contentSource);
        $J(menuOverlayWindow).addClass('hoverContentResponsibility');

        // Position
        overlayContentLeft = getAbsoluteLeft(tempHoverItem) - ($J(menuOverlayWindow)[0].offsetWidth - $J(tempHoverItem)[0].offsetWidth);
        overlayContentTop = getAbsoluteTop(tempHoverItem) + $J(tempHoverItem)[0].offsetHeight;
	
	
	/* Focus On */
	} else if($J(tempHoverItem).hasClass('linkFocusOn')){
	
		// Set HTML
        contentSource = $J('.hoverContentFocusOn').html();
        $J(menuOverlayWindow).html(contentSource);
        $J(menuOverlayWindow).addClass('hoverContentFocusOn');

        // Position
        overlayContentLeft = getAbsoluteLeft($J('#header')[0]) + 15;
        overlayContentTop = getAbsoluteTop(tempHoverItem) + $J(tempHoverItem)[0].offsetHeight - 1;
	
	
	/* Our Websites */
	} else if($J(tempHoverItem).hasClass('linkOurWebsites')){
	
		// Set HTML
        contentSource = $J('.hoverContentOurWebsites').html();
        $J(menuOverlayWindow).html(contentSource);
        $J(menuOverlayWindow).addClass('hoverContentOurWebsites');

        // Position
        overlayContentLeft = getAbsoluteLeft($J('#header')[0]) + 15;
        overlayContentTop = getAbsoluteTop(tempHoverItem) + $J(tempHoverItem)[0].offsetHeight - 1;
        	
	
	} else return;


    // Set extra hover actions for IE8
    if ($J.browser.msie && $J.browser.version == 8) {

        
	    $J('#menuOverlayWindow a').hover(function () {
	        $J(this).addClass('hover');

	    }, function () {
	        $J(this).removeClass('hover');
	    });

	}
    
    // Adijust alignment IE7
	if ($J.browser.msie && $J.browser.version == 7) {
	    overlayContentLeft -= 1;
	}

	$J(menuOverlayWindow).css({
		opacity: 0,
		left: overlayContentLeft + 'px',
		top: overlayContentTop + 'px'
	
	}).fadeTo(200, 1);
	

}



/*=================================================================
    Show/ Remove Menu Overlay 
=================================================================*/

// Show overlay. Leveraging thickbox.js
function showMenuOverlay(link) {

    try {

        if (link != null) {
            currentDropdownLink = link;

			if ($J(currentDropdownLink).parents('#mainNav').length > 0) $J('#mainNav').addClass('hover');
			
			else if ($J(currentDropdownLink).parents('.optionItem').length > 0) $J(currentDropdownLink).parents('.optionItem').addClass('optionItemHover');
			
        }
    
        if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
            
            $J("body", "html").css({ height: "100%", width: "100%" });
            $J("html").css("overflow", "hidden");

            if (document.getElementById("menuHideSelect") === null) {//iframe to hide select elements in ie6
                $J("body").append("<iframe id='menuHideSelect'></iframe><div id='menuOverlay'></div><div id='menuOverlayWindow'></div>");
                $J("#menuOverlay").click(removeMenuOverlay);
            }
            
        } else { //all others

			if (document.getElementById("menuOverlay") === null) {
				$J("body").append("<iframe id='menuHideSelect'></iframe><div id='menuOverlay'></div><div id='menuOverlayWindow'></div>");
                $J("#menuOverlay").click(removeMenuOverlay);
            }
            
        }

        if (tb_detectMacXFF()) $J("#menuOverlay").addClass("TB_overlayMacFFBGHack"); //use png overlay so hide flash

        else $J("#menuOverlay").addClass("TB_overlayBG"); //use background and opacity

		// Fade overlay
		$J("#menuOverlay").css('opacity', 0).fadeTo(250, 0.8); // match with css opacity
				
		// Overlay content & events
        menuOverlayWindow = $J('#menuOverlayWindow')[0];
				
		$J('#menuOverlayWindow').bind('mousemove', function(){
			if (rolloutTimer != null) clearTimeout(rolloutTimer);
		});
		
		
		$J('#menuOverlayWindow').hover(function(){
			if (rolloutTimer != null) clearTimeout(rolloutTimer);
		
		},function(){
			
			if(rolloutTimer) clearTimeout(rolloutTimer);
			rolloutTimer = setTimeout(removeMenuOverlay, rolloutTimerLimit);
		
		});
		
		menuOverlayActive = true;
		
    } catch (e) { }

}


// Remove menu overlay
function removeMenuOverlay() {
	
	tempHoverItem = null;
			
	$J('#menuOverlay').fadeTo(100, 0, function(){

		if ($J(currentDropdownLink).parents('#mainNav').length > 0) {
			$J('#mainNav').removeClass('hover');
			$J('#mainNav li').removeClass('hover');
			
		} else if ($J(currentDropdownLink).parents('.optionItem').length > 0) $J(currentDropdownLink).parents('.optionItem').removeClass('optionItemHover');
			

		$J('#menuOverlayWindow').attr('class', '');
		$J('#menuOverlayWindow').unbind('mousemove');
		$J('#menuOverlayWindow, #menuOverlay, #menuHideSelect').trigger("unload").unbind().remove();
		
		if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
			$J("body", "html").css({ height: "auto", width: "auto" });
			$J("html").css("overflow", "");
		}
		
		menuOverlayActive = false;
	
	});
	
    return false;
	

}

/*===============================
function hover class
===============================*/

function initMainNavClass() {
    $J("#menuOverlayWindow li").hover(function () {
        $J(this).addClass("mainHover");
    }, function () {
        $J(this).removeClass("mainHover");
    });
}
