// ## globalNav.js
// 	Contains the dynamic functionality for the drop down navigation found at the top of most templates.

// FUNCTION LIST:
// 	init();
// 	menuOn();
// 	menuOff();
//	menuLinkOn();
// 	menuLinkOff();
// 	linkSwap();
// 	arrowSwap();

// TEMPLATE(S) USED:
// 	brochure_request.html
// 	coming_veh.html
// 	contact.html
// 	corporate.html
// 	dealer_results.html
// 	gallery.html
// 	line_up.html
// 	main.html
// 	performance.html
// 	specs.html
// 	vehicles_home.html

var menuTimeout;
var currentMenu;
var imgPathNav = "images/menu/";	// Image path location for Global Nav menu images.
var imgPath = "images/";			// Image path location for all other images.
var navIsLoaded = false;

// Initializing the global navigation

// change init() to globalNavInit()
function init() {
	DynLayerInit();
	navIsLoaded = true;	// boolean to true when initialization is complete
}

// Display the dropdown menu.
// inputs: menuName  - name of the menu DIV layer without the Div.  ie. vehicles
// output: none
function menuOn(menuName) {
	var theMenu;
	if (!navIsLoaded) return;	// check if initialization is complete
	if (menuTimeout) clearTimeout(menuTimeout);	// clear the timeout
	
	if (currentMenu) {			// hide the current menu
		theMenu = eval(currentMenu);
		theMenu.hide();
	}
	currentMenu = menuName;	
	theMenu = eval(currentMenu);
	theMenu.show();				// display menuName
	menuTimeout = setTimeout(currentMenu + '.hide()', 3000);		// set the timeout 
}

// Hides the dropdown menus
// inputs: menuName - name of the menu DIV layer without the Div.  ie. vehicles
// outputs: none
function menuOff(menuName) {
	
	if (!navIsLoaded) return;	// check if initialization is complete
	
	if (menuTimeout) clearTimeout(menuTimeout);	// clear the current timeout
	menuTimeout = setTimeout(menuName + '.hide()', 330)		// set the new timeout
}

// Swaps the menu image to the ON state.
// input: imgName - name of the image to turn on
// output: swaps the image to the ON state
function menuLinkOn(imgName) {
	var theImage;
	
	if (menuTimeout) clearTimeout(menuTimeout);
	//theImage = (is.ns4) ? eval("document.layers['" + currentMenu + "Div'].document.images['" + imgName + "']") : eval("document." + imgName);
	//theImage.src = imgPathNav + "tnav_lnk_" + imgName + "_on.gif";
	turnOn(imgName);
}

// Swaps the menu image to the OFF state.
function menuLinkOff(imgName) {
	var theImage;
	//theImage = (is.ns4) ? eval("document.layers['" + currentMenu + "Div'].document.images['" + imgName + "']") : eval("document." + imgName);
	//theImage.src = imgPathNav + "tnav_lnk_" + imgName + ".gif";
	turnOff(imgName);

	menuOff(currentMenu);
}

// Swaps the link image.
// input: 	imgName - name of the image
//			ON - on or off state
function linkSwap(imgName, ON) {
	var theImage = eval("document." + imgName);
	theImage.src = imgPath + imgName + "_lnk" + ((ON) ? "_on":"") + ".gif"
}

// Swaps the arrow image.
// input: 	imgName - name of the image
//			ON - on or off state
function arrowSwap(linkName, ON, obj, obj2) {
	var theImage = 'document.'
	
	if (obj && is.ie) {
		theImage += 'all.'
	} else if (obj && is.ns) {
	 	theImage += obj + '.document.'
	} 
	
	if (obj2 && is.ns) {
		theImage += obj2 + '.document.'
	}

	theImage += linkName
	theImageObj = eval(theImage);

	theImageObj.src = imgPath + "g_arrow" + ((ON) ? "_on":"") + ".gif";
}


// Swaps the arrow image.
// input: 	imgName - name of the image
//			ON - on or off state
function arrowSwapUp(linkName, ON, obj, obj2) {
	var theImage = 'document.'
	
	if (obj && is.ie) {
		theImage += 'all.'
	} else if (obj && is.ns) {
	 	theImage += obj + '.document.'
	} 
	
	if (obj2 && is.ns) {
		theImage += obj2 + '.document.'
	}

	theImage += linkName
	theImageObj = eval(theImage);

	theImageObj.src = imgPath + "page_up" + ((ON) ? "_on":"") + ".gif";
}
function getPass() {
 	var popURL = "getPass.aspx";
	var popWindow = window.open( popURL,'passPop', 'width=450,height=320,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=0,resizable=1' ); 
	popWindow.focus();
}
	function popReqs(newURL) {
	if(typeof newURL != 'string') newURL = "sysreq.aspx"; 
	var reqWindow = window.open( newURL,'reqPop', 'width=470,height=500,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1' ); 
	reqWindow.focus();
	}
	
function popDetail(videoSku) {
	newURL = "videoPop.aspx?videoSku=" + videoSku;
	var videoPop = window.open( newURL,'vidPop', 'width=400,height=200,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=0' ); 
	videoPop.focus();
}
function popTrain(toURL) {
	toURL = "clickSet3.aspx?clickFile=" + toURL; 
	var trainWin = window.open( toURL,'trainPop', 'width=716,height=398,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=0,resizable=0' ); 
	trainWin.focus();
}
function newWindow(toURL) {
	var newWin = window.open( toURL,'new', 'width=400,height=500,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=0,resizable=0' ); 
	newWin.focus();
	return false;
}
	
//mjw 2007
function getImage(image) {
    var img;
    if(typeof image == 'string') {
        img = document[image];
        if(!img) img = document.getElementById(image);
    } else
        img = image;
    return img;
}

function turnOn(image) {
    var img = getImage(image);
    img.src= img.src.replace(/.gif/, "_on.gif");
}
function turnOff(image) {
    var img = getImage(image);
    img.src= img.src.replace(/_on.gif/, ".gif");
}
	
function ShowWait()
{
    if (typeof(Page_ClientValidate) == 'function')
        { if (Page_ClientValidate("") == false) return false;}
    // *** Create the hidden overlay
	var Overlay = document.createElement("div");
	Overlay.id = "Overlay";
    Overlay.style.position = 'absolute';
    Overlay.style.top = 0;
    Overlay.style.left = 0;
	Overlay.style.backgroundColor = "#cccccc";
	Overlay.style.opacity = .25;
	Overlay.style.filter = "alpha(opacity='25')";
    
    var dim = getHTMLDimensions();
    Overlay.style.width = dim.width + "px" ;
    Overlay.style.height =  dim.height + "px" ; 
    Overlay.style.overflow = "hidden";
    Overlay.style.zIndex = 98;
	document.body.appendChild(Overlay);
    
	var Waiting = document.createElement("div");
	var ws = Waiting.style;
    ws.width = "400px";
    ws.textAlign = "center";
    ws.border = "2px solid #ddd";
	ws.backgroundColor = "#ffffff";
	ws.color = "#000000";
	ws.fontSize = "large";
    ws.marginTop = "90px";
    ws.padding = "40px";
    ws.fontWeight = "bold";
	document.body.appendChild(Waiting);
    
    var waitImage = document.createElement("img");
    var src = "images/working.gif";
    if(window.location.pathname.indexOf("secure") != -1)
        src= "../" + src;
    waitImage.src = src;
    waitImage.alt = "Please wait";
    waitImage.id = "waitImage";
    Waiting.appendChild(waitImage);
             
    var br = document.createElement("br");
    Waiting.appendChild(br);            

    var newtext = document.createTextNode(" Please wait ");
    Waiting.appendChild(newtext);        

    ws.visibility = 'visible';
    ws.display = '';
    ws.position = 'absolute';
    ws.left = (document.body.clientWidth - Waiting.clientWidth ) /2 + "px";
    ws.top = (document.body.scrollTop + 50) + "px";
    
    ws.zIndex = 99;
    
    // *** Force scrollbar to top
    window.scrollTo(0,0);

    // *** Note: IE has problems with hidden images, so force the image
    // ***       to reload after form submission with a short timeout
    ActivateImage();

    return true;
}
function ActivateImage()
{
  setTimeout( function() {                       
        var img = document.getElementById('waitImage'); 
        img.src = img.src + "?" + new Date().getMilliseconds(); 
    },100);                    
}

function getHTMLDimensions() {
    var docHeight= 0;
    var docWidth= 0;
    if (document.documentElement && typeof(document.documentElement.scrollHeight) == 'number') 
        docHeight= document.documentElement.scrollHeight;
    else if (document.body)
        docHeight= document.body.offsetHeight;
    if (document.documentElement && typeof(document.documentElement.scrollWidth) == 'number') 
        docWidth= document.documentElement.scrollWidth;
    else if (document.body)
        docWidth= document.body.offsetHeight;
    return {width: docWidth, height: docHeight};
}