
/* 

default.js
---------------------------
Author:			MRC 
Date:			20041013
Description:	most of the functions in this file relate to the dropdowns in the Mitsubishi website.
				Rather than using lots of onmouseover/onmouseout attributes in the html this 
				script checks all mouseover/out events in the page and determines course of 
				action based on classname of element that called it.
				
*/

/* preload images */
var aImages = ['images/leftmenu-item.gif','images/leftmenu-item-on.gif','images/leftmenu-sub-item.gif', 'images/leftmenu-sub-item-on.gif', 'images/leftmenu-sub-item-on-expand.gif']
preloadImages();

var aFAQs = [];
var aOpenMenus = [];
var iMenuTop = 0;

var oPopup = null;

/* Constants */
var iLeftMenuBottomOffset	=	(bIE) ? 10 : 20
var bIE = navigator.appName.indexOf("Microsoft") != -1;

/* Setting up these prototypes allows us to use these methods on any string */
String.prototype.GetMenuItemID = function() { 
	return this.substring( this.indexOf('_') + 1, this.lastIndexOf('_'))
}

String.prototype.IsClickClass = function() { 
	return this.substring(this.length - 2, this.length) == 'On';
}

String.prototype.GetMenuID = function() {
	return this.substring(this.lastIndexOf('_') + 1, this.length )
}

function IncCounter(iTop) {
	iMenuTop = iTop;
}

function GetCounter() {
	return iMenuTop;
}

function PositionMenu(bPDF) {

	iHeight = GetObj('LeftMenuFooterIm').clientHeight;

	//Need to make sure the footer does not mess about so set the Menu div to height of contents inside
	GetObj('LeftMenu').style.height = (iMenuTop + iHeight + iLeftMenuBottomOffset + 150) + 'px';
}

var sClassNames = ',LMI,SLMI,LMIOn,SLMIOn,LMI-NC,SLMI-NC,LMI-NCOn,SLMI-NCOn,';
var intervalID;

if (!bIE) {
	document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
}

document.onmouseout = Hide;
document.onmouseover = Show;

function Show(e) {

	if (bIE) {
		e = window.event;
		o = window.event.srcElement;
	} else {
		o = e.target;
	}
	    
	if ( sClassNames.indexOf(',' + o.className + ',') != -1 ) {
		clearTimeout(intervalID);

		//If it is the top menu being click on, hide all layers
		ResetOpenMenus(o.id.GetMenuID());
		
		intervalID = setTimeout('ShowLogic(o)',200);
	} else {
		//Only hide if anything is open
		if (aOpenMenus.length > 0)
			PreHide();	
	}
}

function ShowLogic(o) {
	clearTimeout(intervalID)
	
	var iMenuItemID	= o.id.GetMenuItemID();
	var iMenuID		= o.id.GetMenuID();
	var m			= GetObj('m_' + iMenuItemID);

	//If menu exists
	if (m != null) {

		//If it is not already shown
		if (m.style.display != "block") {

			if (!o.className.IsClickClass())	
				o.className += 'On';

			ShowMenu(o, m.id, o.id, o.id.GetMenuID());
		}
	}
}

function Hide(e) {
	if (bIE) {
		e = window.event;
		o = window.event.srcElement;
	} else {
		o = e.target;
	}

	if ( sClassNames.indexOf(',' + o.className + ',') != -1 ) {

		var iMenuItemID	=	o.id.GetMenuItemID();
		var m			=	GetObj('m_' + iMenuItemID);
		
		if (m != null)
			PreHide(m);
		
	}
}

//Only hide if user doesn't roll back in to dropdown within 500 ms
function PreHide(o) {
	clearTimeout(intervalID);
	intervalID = setTimeout('ResetOpenMenus()',500);
}

function HideMenu(iMenuID) {
	var o	=	GetObj('m_' + iMenuID);

	if (o != null) {
		//Reset divs
		o.style.display	= 'none';
		o.style.left	= '0px';
		o.style.top		= '0px';
		o.style.zIndex  = 1;
	}	
}

function ResetMenuItem(sLinkID) {
	var l	=	GetObj(sLinkID) 
	
	if (l != null)
		(l.className.IsClickClass()) ? l.className = l.className.substring(0,l.className.length - 2) : ''
}

function ResetOpenMenus(iMenuID) {
	if (aOpenMenus.length > 0) {	

		for (var iCount = aOpenMenus.length - 1 ; iCount > -1 ; iCount--) {
		
			bStopRemoving = (aOpenMenus[iCount].GetMenuItemID() == iMenuID);
		
			if (bStopRemoving) {
				return;
			} else {
				ResetMenuItem(aOpenMenus[iCount]);
				HideMenu(aOpenMenus[iCount].GetMenuItemID());
				aOpenMenus.pop();
			}
		
		}
		
		//Show any select boxes 
//		var oElements = document.getElementsByTagName('select');
//		for (iLoop = 0; iLoop < oElements.length; ++iLoop) {
//			oElements[iLoop].style.visibility = 'visible';
//		}
//		oElements = null;
	}
}

function ShowMenu(oCaller, o, sLinkID, oMenu) {
    

	if (typeof o != 'object')
		o = GetObj(o);

	if (typeof oMenu != 'object') 
		oMenu = GetObj('m_' + oMenu);

	o.style.top			= (oMenu.offsetTop + oCaller.offsetTop) + 'px';
	o.style.left		= ((oMenu.offsetLeft + oCaller.offsetWidth) - 25) + 'px';
	o.style.zIndex		= oMenu.style.zIndex + 1;
	o.style.display		= 'block';
	
	//Hide any select boxes 
//	var oElements = document.getElementsByTagName('select');
//	for (iLoop = 0; iLoop < oElements.length; ++iLoop) {
//		oElements[iLoop].style.visibility = 'hidden';
//	}
//	oElements = null;
	
	//Add LinkID to OpenMenusArray
	aOpenMenus.push(sLinkID)

}

function GetObj(id) {
	return document.getElementById(id);
}

//Adjust height of footer bar if toggling item in dashboard
function Toggle(id, bDashboard) {
	
	if (GetObj(id).style.display == '' || GetObj(id).style.display == 'none') {
		GetObj(id).style.display = 'block';
		if (bDashboard) GetObj('LeftMenu').style.height = GetObj('LeftMenu').clientHeight + GetObj(id).clientHeight + 'px';
	} else {
		if (bDashboard) GetObj('LeftMenu').style.height = GetObj('LeftMenu').clientHeight - GetObj(id).clientHeight + 'px';
		GetObj(id).style.display = 'none';
	}

}

function preloadImages() {
	for ( i = 0 ; i < aImages.length ; i++ ) {
		oImage = new Image();
		oImage.src = aImages[i];
	}
}

function OpenPopup(sPageUrl, iWinWidth, iWinHeight)
{
    if (sPageUrl == null || sPageUrl == '')
        return;
        
    if (oPopup != null)
    {
        oPopup.close();
        oPopup = null;
    }

    var iWinX = (window.screen.width / 2) - (iWinWidth / 2);
    var iWinY = (window.screen.height / 2) - (iWinHeight / 2);
        
    oPopup = window.open(sPageUrl, 'MDE_Popup', 'width=' + iWinWidth + ',height=' + iWinHeight + ',screenX=' + iWinX + ',screenY=' + iWinY + ',left=' + iWinX + ',top=' + iWinY + ',scrollbars=yes,toolbar=0,status=1,menubar=0,resizable=1,titlebar=no');
    oPopup.focus();
}

function ContactUsSelect(sSelectID, sPage)
{
	var oSelect = document.getElementById(sSelectID);
	
	if (oSelect)
	{
		if (oSelect.selectedIndex > 0)
		{
			OpenPopup(sPage + oSelect.options[oSelect.selectedIndex].value, 418, 280);
			oSelect.selectedIndex = 0;
		}
	}
}

function swapProductImage(imgID)
{
	var imagePlaceholder = document.getElementById('MouseOverImage');
	
	if(imagePlaceholder)
	{		
		imagePlaceholder.src = "DisplayImage.ashx?Image=" + imgID;
		imagePlaceholder.style.display = "block";
	}
}

function clearProductImage()
{
	var imagePlaceholder = document.getElementById('MouseOverImage');
	
	if(imagePlaceholder)
	{
		imagePlaceholder.style.display = "none";
	}
}


/*
function RepositionSearchBox()
{
	var x;
	x = GetCounter();

	
	var b;
	b = GetObj("ctlSearch_divSearch");	
	
	
	var leftMenu;
	leftMenu = GetObj("LeftMenu");	
	
	b.style.visibility = "visible";
	b.style.position = "absolute";
	
	if(x==0)
	{
		x = 20;
	}
	
	b.style.top = (parseInt(x)) + leftMenu.offsetTop + "px";	
	//b.style.top = (parseInt(x)) + "px";
	b.style.left = "0";		
	
	
	var button;	
	button = GetObj("ctlSearch_btnSearch");
	button.style.visibility = "visible";
	button.style.top = Number("20") + "px";
}
*/

function RepositionLogoutBox()
{
        
    var x;
    x = GetCounter();
    
    var b;
    b = GetObj("ctlLogout_divLogout");
        
    if(isObject(b))
    {
        if(b.style.visibility == "visible")
        {    
            
            var searchBox;
            searchBox = GetObj("ctlSearch_divSearch");                              
            
                
            var leftMenul
            leftMenu = GetObj("LeftMenu");
            
            if(x==0)
            {
                x = 40;
            }
            else
            {
                x += 25;
            }
            
            b.style.top = (parseInt(x)) + leftMenu.offsetTop + "px";
            b.style.left = "50px";   
                                    
                        
            
            var loggedInAs;
            loggedInAs = GetObj("loggedInAs");                    
            loggedInAs.style.position = "absolute";                                                            
            loggedInAs.style.top = parseInt(b.style.top) + 25 + "px";            
            loggedInAs.style.left = 10 + "px";
                                                                          
        }     
    }    
           
}

 function isObject(a)
 {
     return (typeof a == 'object' && !!a) || isFunction(a);
 }
 
  function isFunction(a)
 {
      return typeof a == 'function';
 }
