/* 

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.
				
*/

if (top.location == self.location)
{
	top.location.href = 'default.asp?url=' + escape(top.location.href);
}

/* preload images */
var aImages = ['images/red-plus.gif','images/red-minus.gif','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 intervalID;
var businessPartners = [];
var alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var sClassNames = ',LMI,SLMI,SLMI-Scroll SLMI-Scroll-Up,SLMI-Scroll SLMI-Scroll-Down,LMIOn,SLMIOn,LMI-NC,SLMI-NC,LMI-NCOn,SLMI-NCOn,';
var intervalID;

// Controls how 'fast' the menu items scroll
var scrollSpeed = 10;
var scrollInterval = 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 )
}

// Does the work on continuously scrolling the menu items (up or down) by controlling the inner
// menu content wrapper's position from the bottom of its relative position within the outer content wrapper.
function scroll(menuID, down)
{
	var menu = document.getElementById(menuID);
	var pos = parseInt(menu.style.bottom.replace(/px/, ''));
	
	if (isNaN(pos))
		pos = 0;
	
	if (down)
		pos = pos + scrollSpeed;
	else
		pos = pos - scrollSpeed;

	// Ensure that the menu items do not 'scroll' too far up or down (i.e. the scrolling will appear to stop at the first and last menu item)
	if (pos < 0)
		pos = 0;
	else if (pos > menu.offsetHeight - menu.parentNode.offsetHeight)
		pos = menu.offsetHeight - menu.parentNode.offsetHeight;

	menu.style.bottom = pos + 'px';
	
	if (scrollInterval == null)
		scrollInterval = setInterval('scroll("' + menuID + '", ' + down + ');', 100);
}

// Stops the current scroll by clearing the interval
function stopScroll()
{
	if (scrollInterval != null)
		clearInterval(scrollInterval);
		
	scrollInterval = null;
}

// Required to set the correct height of the inner menu content wrapper. Without this the inner menu content wrapper has
// no height because all of its child elements use absolute positioning.
function setHeight(menuID)
{
	var menu = document.getElementById(menuID);
	
	for (var i = menu.childNodes.length - 1; i >= 0; i--)
	{
		// Find the last child menu item
		if (menu.childNodes[i].tagName && menu.childNodes[i].tagName == 'A')
		{
			var height = parseInt(menu.childNodes[i].style.height.replace(/px/, ''));
			var top = parseInt(menu.childNodes[i].style.top.replace(/px/, ''));
			
			menu.style.height = (top + height + 5) + 'px';
			return;
		}
	}
}

// Assigns a 'scrollable' property to the specified menu element.
function setScrollable(menuID, scrollable)
{
	var menu = document.getElementById(menuID);
	
	if (menu)
		menu.scrollable = scrollable;
}

function IncCounter(iTop) {
	iMenuTop = iTop
}

function PositionMenu(bPDF) {

	if (!bPDF) {

		//Get height of dashboard
		if (GetObj('Dashboard')) {
			GetObj('Dashboard').style.display = 'block';
			GetObj('Dashboard').style.top = iMenuTop + 'px';
			iHeight = GetObj('Dashboard').clientHeight; //Offset is 20 from bottom of main menu items
		} else {
			GetObj('LeftMenuFooter').style.display = 'block';
			GetObj('LeftMenuFooter').style.top = iMenuTop + 'px';
			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) + 'px'; //Add 20 for firefox
		
		var oDF = document.getElementById('docfusion');
		if (oDF) {
			oDF.height = (iMenuTop + iHeight) + 'px';
		}

	}
	
}

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);
	
	// Get the bottom-position offset of the inner menu content wrapper (if the menu is scrollable)
	var bottom = parseInt(oCaller.parentNode.style.bottom.replace(/px/, ''));
	if (isNaN(bottom))
		bottom = 0;
		
	// Tweak to position required for scrollable menus - the 'scrollable' property is set on page load
	var top = 0;
	if (oMenu.scrollable)
		top = 11;
		
	o.style.top			= (oMenu.offsetTop + oCaller.offsetTop + top - bottom) + 'px';
	o.style.left		= ((oMenu.offsetLeft + oCaller.offsetWidth) - 10) + '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 ToggleExpandNew(id) {

    var i = GetObj(id);
    if (i.style.backgroundImage.indexOf('red-plus') > -1 || i.style.backgroundImage == '') {
        i.style.backgroundImage = 'url(\'images/red-minus.gif\')';
    }
    else {
        i.style.backgroundImage = 'url(\'images/red-plus.gif\')';
    }

    /*if (i.src.indexOf('red-plus') > -1) {
    i.src = 'images/red-minus.gif';
    } else {
    i.src = 'images/red-plus.gif';
    }*/
}

function ToggleExpand(id) {

    var i = GetObj(id);    
    
    if (i.src.indexOf('red-plus') > -1) {
		i.src = 'images/red-minus.gif';
	} else {
		i.src = 'images/red-plus.gif';
	}
}

function ToggleFAQ(id)
{
	d = GetObj(id);
	
	HideFAQs();
	
	if (d.style.display == '' || d.style.display == 'none') {
		d.style.display = 'block';
	}
	else
	{
		d.style.display = 'none';
	}
	
}

function HideFAQs()
{
	for ( i = 0 ; i < aFAQs.length ; i++ )
	{
		GetObj(aFAQs[i]).style.display = 'none';
	} 
}

function LoadFAQ(id)
{
	aFAQs[aFAQs.length] = id;
}

function Debug(sText) {

	GetObj('Debug').value += sText + '\n';
	
}

function OpenWin(sUrl, iWidth, iHeight, sOptions) {

	window.open(sUrl, 'Win1', 'WIDTH=' + iWidth + ',HEIGHT=' + iHeight + ',' + sOptions);
	
}

function UnCheckAll(sName, sForm, cb) {
	
	if (typeof cb == 'string') {
		GetObj(cb).checked = !GetObj(cb).checked
	}
	
	var f = GetObj(sForm);
	var e;

	for (i = 0; i < f.elements.length; i++) {
	
		e = f.elements[i];
	
		if (e.type == 'checkbox' && e.name == sName) {
		
			if (e.value != '*') e.checked = false
		
		}
	
	}

}

function UnCheck(sName) {
	GetObj(sName).checked = false;
}

function Disable(bDisabled) {

	for ( i = 1 ; i < Disable.arguments.length ; i++ ) {
	
		if (GetObj(Disable.argument[i])) GetObj(Disable.arguments[i]).disabled = bDisabled;
		
	}

}

function Check(bChecked) {

	for ( i = 1 ; i < Check.arguments.length ; i++ ) {
	
		GetObj(Check.arguments[i]).checked = bChecked;
		
	}

}

function preloadImages() {
	
	for ( i = 0 ; i < aImages.length ; i++ ) {
		
		oImage = new Image();
		oImage.src = aImages[i];
		
	}
	
}

function SetMSWidth()
{
	var iWidth = GetObj('MS').clientWidth - 400;
	
	//If model selector is larger than available width, 
	if (iWidth > 0)
	{
		GetObj('Wrapper').style.width = GetObj('Wrapper').clientWidth + (iWidth) + 'px';
		GetObj('Content').style.width = GetObj('MS').clientWidth + 180 + 'px';
	}
	
}

function LoadBlankImage(e)
{
	if (window.event)
		t = window.event.srcElement;
	else
		t = e.target;
	
	t.src = 'images/blank.gif';
}

function setMapRegion(region, reset)
{
	if (region.length == 0)
	{
		intervalID = setTimeout('document.getElementById(\'map\').src= \'images/blank.gif\'', 1000);
	}
	else
	{
		clearTimeout(intervalID);
		if (reset)
			intervalID = setTimeout('document.getElementById(\'map\').src= \'images/regions-' + region + '.gif\'',1000);
		else
			document.getElementById('map').src= 'images/regions-' + region + '.gif';			
	}
	
}
