// ## 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();
// 	modelarrowSwap();

var menuTimeout;
var submenuTimeout;
var currentMenu;
var currentsubMenu;
var imgPathNav;
var imgPath;
var navIsLoaded = false;

var docpath = window.location.pathname;
docpath = docpath.toLowerCase();
//alert(docpath.indexOf("/en/"));
if(docpath.indexOf("/en/")!=-1)
{
	var lang = "en";
	var imgPathNav = "/en/images/global_nav/menu_images/menu/";	// Image path location for Global Nav menu images.
	var imgPath = "/en/images/global_nav/menu_images/";			// Image path location for all other images.
	var imgPathVehicles = "/en/images/global_nav/vehicle_images/";
	var imgPathLeftNav = "/en/models/images/vehicle_nav/";
	var imgPathRightNav = "/en/models/";

	if(docpath.indexOf("/dealerships/")!=-1)
	{
		imgPathLeftNav = "/en/general/dealerships/images/";
	}

	if(docpath.indexOf("/ownership/")!=-1)
	{
		imgPathLeftNav = "/en/general/ownership/images/";
	}


}
else
{
	var lang = "fr";
	var imgPathNav = "/fr/images/global_nav/menu_images/menu/";	// Image path location for Global Nav menu images.
	var imgPath = "/fr/images/global_nav/menu_images/";			// Image path location for all other images.
	var imgPathVehicles = "/fr/images/global_nav/vehicle_images/";
	var imgPathLeftNav = "/fr/models/images/vehicle_nav/";
	var imgPathRightNav = "/fr/models/";

	if(docpath.indexOf("/dealerships/")!=-1)
	{
		imgPathLeftNav = "/fr/general/dealerships/images/";
	}

	if(docpath.indexOf("/ownership/")!=-1)
	{
		imgPathLeftNav = "/fr/general/ownership/images/";
	}

}

// Used to highlight the top nav model names on the Configure page (or
// potentially anywhere else that uses model codes are used to access a
// page rather than pure file location.  Used as a suppliment to
// HighlightNavItem() (below).

// Notes: Uses the first two characters of the model code to determine
//				which image to display.  The following object is used to
//				translate the 2 digits into an image name.
//				The function will also alert if the model code is not
//				recognized -- this should help QA when new models roll out.

var objHighlightedModels = {
	'Q4':'gvnav_q45',
  'A4':'gvnav_m35_45',
  'G2':'gvnav_g35_coupe',
  'G4':'gvnav_g35_sedan',
  'F6':'gvnav_fx35_45',
  'E6':'gvnav_ex',
  '9C':'gvnav_qx56'
	};

function HighlightTopNavModelImages() {
	var strSeriesPlus = strHightlightModelName.substr(0,2);
	var theImage

	if( objHighlightedModels[strSeriesPlus] ) {
		theImage = eval('document.' + objHighlightedModels[strSeriesPlus]);
		if(theImage) {
			theImage.src =imgPathVehicles + objHighlightedModels[strSeriesPlus] + '_on.gif';
		}
	} else {
    alert('Nav Hightlight Error: model code ' + strHightlightModelName + ' unrecognized');
  }
}


// Initializing the global navigation

// change init() to globalNavInit()
function init() {
	DynLayerInit();

	//SetNav(); //Added by Adeo (Arleigh Crawford) June 2002 to do 2nd level navigation highlighting
	navIsLoaded = true;	// boolean to true when initialization is complete


	SetNav();



}

// 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
}

function submenuOn(submenuName) {
	var thesubMenu
	if (!navIsLoaded) return;	// check if initialization is complete
	if (submenuTimeout) clearTimeout(submenuTimeout);	// clear the timeout

	if (currentsubMenu) {			// hide the current menu
		thesubMenu = eval(currentsubMenu);
		thesubMenu.hide();
	}
	currentsubMenu = submenuName;
	thesubMenu = eval(currentsubMenu);theMenu = eval(currentMenu);
	thesubMenu.show();theMenu.show();				// display menuName
	submenuTimeout = setTimeout(currentsubMenu + '.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()', 600);		// set the new timeout
}

function submenuOff(submenuName) {

	if (!navIsLoaded) return	// check if initialization is complete

	if (submenuTimeout) clearTimeout(submenuTimeout)	// clear the current timeout
	submenuTimeout = setTimeout(submenuName + '.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";
}

// 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";
	menuOff(currentMenu);
}

function submenuLinkOn(imgName) {
	var theImage;

	if (menuTimeout) clearTimeout(menuTimeout);
	//Next line added by AG: 12/19/2002
	if (submenuTimeout) clearTimeout(submenuTimeout);

	//Next line modifies by AG: 12/19/2002. Changed currentMenu to currentsubMenu.
	theImage = (is.ns4) ? eval("document.layers['" + currentsubMenu + "Div'].document.images['" + imgName + "']") : eval("document." + imgName);
	theImage.src = imgPathNav + "tnav_lnk_" + imgName + "_on.gif";
}

// Swaps the menu image to the OFF state.
function submenuLinkOff(imgName) {
	var theImage;

	//Next line modifies by AG: 12/19/2002. Changed currentMenu to currentsubMenu.
	theImage = (is.ns4) ? eval("document.layers['" + currentsubMenu + "Div'].document.images['" + imgName + "']") : eval("document." + imgName);
	theImage.src = imgPathNav + "tnav_lnk_" + imgName + ".gif";

	submenuOff(currentsubMenu);
	menuOff(currentMenu);
}

// Swaps the link image.
// input: 	imgName - name of the image
//			ON - on or off state
// Modified by Adeo (Arleigh Crawford) June 2002 to leave 2nd level navigation highlighting alone
// Calls IsHighlighted
function linkSwap(imgName, ON) {
	var theImage = eval("document." + imgName);
	if (!IsHighlighted(theImage.src)){
		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 'up' 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 + "gl_arrow_up" + ((ON) ? "_on":"") + ".gif";
}

// Methods added by Adeo (Arleigh Crawford) June 2002 to do 2nd level
//		navigation highlighting and French/English switcheroo
var defaultDocument = "index.html";
var enFolder = "en/"
var frFolder = "fr/"

// Called by page load handler (init())
//		Detects context from path and highlight nav items
function SetNav(){
	//parse path
	var path = window.location.pathname;
	path = path.toLowerCase();
	var m,n;
	var modelname;

	//Get the position of the "/models" in the path

	m = path.indexOf("models/")

	//Get the position of the slash after the slash in "models/"

	n = path.indexOf("/", m + 7);

	//Get the modelname from the path

	modelname = path.substring(m + 7, n);


	if (path.replace){
		//skip path up to fr or eng
		var i = path.indexOf(enFolder);
		if (i != -1){
			i += enFolder.len;
		}else{
			i = path.indexOf(frFolder);
			if (i != -1){
				i += frFolder.len;
			}else{
				i = 1;
			}
		}
		var j = path.indexOf("/", i);
		var k = 0;
		var navItemName = "";

		// If we are in the root highlight the home button
		while (j > -1){
			k = j;
			j = path.indexOf("/", j + 1);
			navItemName = path.substr(k + 1, j - k - 1);
	  	navItemName = navItemName.toLowerCase();


			//Highlight top nav items

			HighlightNavItem("gvnav_" + GetItemName(navItemName));

			//Highlight left nav items

			HighlightNavItem("lvn_" + GetItemName(navItemName));

			//Highlight right nav items

			HighlightNavItem(("rhn_" + GetItemName(navItemName)));

			//Highlight right nav items

			HighlightNavItem(("rvn_" + GetItemName(navItemName)),modelname);
		}
		//deal with filenames
		//add default document if path ends with slash
		if (k == path.length - 1) {
			path += defaultDocument;
		}
		j = path.indexOf(".", i);
		navItemName = path.substr(k + 1, j - k - 1);
  		navItemName = navItemName.toLowerCase();

  		//Highlight top nav items

		HighlightNavItem("gvnav_" + GetItemName(navItemName));

		//Highlight left nav items

		HighlightNavItem("lvn_" + GetItemName(navItemName));

		//Highlight right nav items

		HighlightNavItem(("rhn_" + GetItemName(navItemName)));

		//Highlight right nav items

		HighlightNavItem(("rvn_" + GetItemName(navItemName)),modelname);
	}


}

// Helper method, changes image src to highlight
function HighlightNavItem(imgName,modelname){
	//based on linkSwap
	//alert('imgName to be highlighted=' + imgName);
	var theImage = eval("document." + imgName);
	//alert('theImage=' + theImage);
	if (theImage){
	//alert('imgPathVehicles=' + imgPathVehicles );
		//theImage.src = imgPath + imgName + "_lnk_hl.gif";
		theImage.src = imgPath + imgName + "_on.gif";

		//alert('imgName=');

		//highlighting vehicle images
		if(imgName.indexOf('gvnav')!=-1)
		{
			theImage.src = imgPathVehicles + imgName + "_on.gif";
		}

		//highlighting vehicle images
		if(imgName.indexOf('lvn')!=-1)
		{
			theImage.src = imgPathLeftNav + imgName + "_on.gif";
		}

		//highlighting vehicle images
		if(imgName.indexOf('rhn')!=-1)
		{
			theImage.src = imgPathLeftNav + imgName + "_on.gif";
		}

		//highlighting vehicle images
		if(imgName.indexOf('rvn')!=-1)
		{
		//alert(imgPathRightNav);
			theImage.src = imgPathRightNav + modelname + "/images/" + imgName + "_on.gif";
		}

	}
}



function GetItemName(itemString)

//This function has been created just in case we need to do some massaging of the strings.

{
var strReturn;
strReturn='';
switch (itemString)
  {
    case 'q45':
    {
    //do something
    strReturn= itemString
     break;
     }

    default:
    {
    //do something
    strReturn=itemString
     break ;

    }
  }

  return strReturn;

}

// Helper method checks image src to detect highlighting
function IsHighlighted(source){
	if (source.indexOf("_hl.gif") != -1){
		return true;
	}else{
		return false;
	}
}


// Detects language context from path and switches to equivalent
//		page in other language
function writepath(string)
{
var english = "/en/";
var french = "/fr/";

		var i = string.indexOf(english);
		if (i != -1){
			secondstring = replace(string, english, french);
		}else{
			i = string.indexOf(french);
			if (i != -1){
				secondstring = replace(string, french, english);
			}else{
				secondstring = string;
			}
		}
		return secondstring;
}

// Helper method, implements VBScript Replace functionality
// 	Called by writepath()
function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
