
var modelSubMenu = [];
var otherSubMenu = [];
var currentSubMenu = [];
var currentMenu = null;
var currentChld = null;
var childsParent = null;

var displayTimer = null;
var subMenuTimer = null;
var subChldTimer = null;
var clrChldTimer = null;

function initialiseMenuFrames() {

	if ((top.frames[1].document.getElementById("xmlData") == null) || (top.frames[1].document.getElementById("xmlData").value == "")) {
		setTimeout(initialiseMenuFrames,500);
		return;
	}		

	document.getElementById('jdht_lnk').innerHTML = eval("JDHT" + languageCode);
	document.getElementById('part_lnk').innerHTML = eval("PARTS_HOME" + languageCode);
	document.getElementById('jagr_lnk').innerHTML = eval("JAGUAR_MODELS" + languageCode);
	document.getElementById('othr_lnk').innerHTML = eval("OTHER_PARTS" + languageCode);
	document.getElementById('news_lnk').innerHTML = eval("NEWS_EVENTS" + languageCode);

	document.getElementById('srch_inp').value = HtmlDecode(eval("MENU_SEARCH" + languageCode));
	document.getElementById('srch_btn').value = eval("GO" + languageCode);
	document.getElementById('cdrm_lnk').innerHTML = "<a href=\"#\" onclick=\"directToSubCat();\" class=\"text11greenBold\">" + eval("BUY_CD" + languageCode) + "</a>";

	document.getElementById('link_div').style.width = document.body.offsetWidth + "px";
	document.getElementById('menu_div').style.width = document.body.offsetWidth + "px";
	document.getElementById('line_div').style.width = document.body.offsetWidth + "px";
	document.getElementById('link_tbl').style.width = document.body.offsetWidth + "px";


	//pick up model info for pop-up menu...
	var splitArray = unescape(top.frames[1].document.getElementById("xmlData").value).split(']');
	for (i = 0; i < splitArray.length; i++) {
		var elementValues = splitArray[i].substring((i == 0 ? 1 : 2));
		elementValues = elementValues.replace('[','');
		var elementArray = readCSVValues(elementValues,3);
		if (elementArray.length == 3) {
		   modelSubMenu[i] = elementArray;
		   modelSubMenu[i][2] = removeSingleQuotes(modelSubMenu[i][2]);
		}
	}

	//build the "other" pop-up menu...
	otherSubMenu[0] = [0,10051,'','JCPSpecialOffers.html'];
	otherSubMenu[1] = [0,10051,'','JCPClassicNews.html'];
	otherSubMenu[2] = [0,10051,'','JCPOlderVehicles.html'];

	otherSubMenu[0][2] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + eval("SPECIAL_OFFERS_LABEL" + languageCode) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	otherSubMenu[1][2] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + eval("CLASSIC_NEWS_LABEL" + languageCode) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	otherSubMenu[2][2] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + eval("OLDER_VEHICLES_MM" + languageCode) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

}

function setMenuColor(divName,frameIndex,reset,subMenu) {

	var thisDiv = top.frames[frameIndex].document.getElementById(divName+"_lnk");
	if ((reset == 0) || (reset == 4)) {	

		thisDiv.style.backgroundColor = "#990000";
		thisDiv.style.color = "#FFFFFF";
		
		if ((subMenu != null) && (subMenu.length > 0)) {
			currentSubMenu = eval(subMenu);
			displayTimer = setTimeout(displaySubMenu,500);
			currentMenu = divName;
		}		
		
		if (subMenuTimer != null) {
			clearTimeout(subMenuTimer);
		}
		if ((reset == 4) && (subMenu != null)) {
			subChldTimer = setTimeout(displayChildMenu,500);
			currentChld = parseInt(subMenu);
			childsParent = divName;
		}		
		
	}
	else if (reset == 1) {
		thisDiv.style.backgroundColor = "#fffce9";
		thisDiv.style.color = "#000000";
		if (displayTimer != null) {
			clearTimeout(displayTimer);
			displayTimer = null;
			currentMenu = null;
		}
		if (top.frames[2].document.getElementById("subMenuDiv") != null) {
			top.frames[2].document.body.removeChild(top.frames[2].document.getElementById("subMenuDiv"));
		}			
	}
	else if (reset == 2) {
		thisDiv.style.backgroundColor = "#fffce9";
		thisDiv.style.color = "#000000";
		subMenuTimer = setTimeout(clearSubMenu,500);
	}
	
}

function menuClick(clickURL, clickTarget) {
	if ((clickTarget != null) && (clickTarget.length > 0)) {
		if (clickTarget == "mainFrame") {
			top.frames[2].location = clickURL;
		}
		else if (clickTarget == "_top") {
			top.document.location.href = clickURL;
		}
	}
}

function displaySubMenu() {

	displayTimer = null;
    var subMenuDiv = null;	
    
	if (top.frames[2].document.getElementById("subMenuDiv") != null) {
		top.frames[2].document.body.removeChild(top.frames[2].document.getElementById("subMenuDiv"));
	}
	
	subMenuDiv = top.frames[2].document.createElement("DIV");

	subMenuDiv.style.position = "absolute";
	subMenuDiv.style.left = top.frames[1].document.getElementById(currentMenu+"_td").offsetLeft+"px";
	subMenuDiv.style.top = "0px";
	subMenuDiv.style.color = "white";
	subMenuDiv.style.visibility = "visible";
	subMenuDiv.id = "subMenuDiv";
	
	var menuText = "<table border=0 cellpadding=0 cellspacing=0>";

	if (currentSubMenu == modelSubMenu) {	
		var menuIndex = 0;
		for (var i = 0; i < currentSubMenu.length; i++) {	
			if (hasParentMenuOption(currentSubMenu[i][1]) == true) {		
				menuText = menuText + "<tr><td width='100%' id='subCol"+menuIndex+"'><div class='dropMenuDiv' id='sub"+menuIndex+"_lnk' style='width:100%'";
				menuText = menuText + " onmouseover=\"top.frames[1].setMenuColor('sub"+menuIndex+"',2,0)\" onmouseout=\"top.frames[1].setMenuColor('sub"+menuIndex+"',2,2)\"";				
				menuText = menuText + "onclick=\"top.frames[1].menuClick('JCPCategoryMenu.html?modelId="+getMenuOptionModelId(currentSubMenu[i][0])+"&modelName="+currentSubMenu[i][2]+"&waterMarkModelId="+currentSubMenu[i][0]+"','mainFrame')\" >";
				menuText = menuText + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + currentSubMenu[i][2] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></td></tr>";
				menuIndex++;
			}
		}
	}
	else if (currentSubMenu == otherSubMenu) {
		for (var i = 0; i < currentSubMenu.length; i++) {	
			if (hasParentMenuOption(currentSubMenu[i][1]) == false) {		
				menuText = menuText + "<tr><td width=100%><div class='dropMenuDiv' id='sub"+i+"_lnk' style='width:99%' onmouseover=\"top.frames[1].setMenuColor('sub"+i+"',2,0)\" onmouseout=\"top.frames[1].setMenuColor('sub"+i+"',2,2)\" onclick=\"top.frames[1].menuClick('"+currentSubMenu[i][3]+"','mainFrame')\" >";
				menuText = menuText + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + currentSubMenu[i][2] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></td></tr>";
			}
		}	
	}
	menuText = menuText + "</table>"

	subMenuDiv.innerHTML = menuText;	
	top.frames[2].document.body.appendChild(subMenuDiv);

	
	var menuIndex = 0;
	var maxWidth = 0;
		
	for (var i = 0; i < currentSubMenu.length; i++) {
		if (hasParentMenuOption(currentSubMenu[i][1]) == true) {		
			if ( (top.frames[2].document.getElementById("sub"+menuIndex+"_lnk").offsetWidth + 5) > maxWidth) {
				maxWidth = (top.frames[2].document.getElementById("sub"+menuIndex+"_lnk").offsetWidth + 5);
			}
			menuIndex++;
		}		
	}

	menuIndex = 0;
	for (var i = 0; i < currentSubMenu.length; i++) {
		if (hasParentMenuOption(currentSubMenu[i][1]) == true) {
			top.frames[2].document.getElementById("sub"+menuIndex+"_lnk").style.width = maxWidth+"px";
			menuIndex++;
		}
	}

}

function hasParentMenuOption(parentId) {
    var hasParent = false;
	for (var i = 0; i < currentSubMenu.length; i++) {
		if (currentSubMenu[i][0] == parentId) {
			hasParent = true;
			break;
		}
	}
	return hasParent;
}

function totalChildMenuOptions(parentId) {
	var totalChildren = 0;
	for (var i = 0; i < currentSubMenu.length; i++) {
		if (currentSubMenu[i][1] == parentId) {
			totalChildren++;
		}
	}
	return totalChildren;
}

function getMenuOptionModelId(modelId) {
	var realModelId = modelId;
	if (totalChildMenuOptions(modelId) == 1) {
		for (var i = 0; i < currentSubMenu.length; i++) {
			if (currentSubMenu[i][1] == modelId) {
				realModelId = currentSubMenu[i][0];
				break;
			}
		}
	}
	return realModelId;
}

function clearSubMenu() {
	if (top.frames[2].document.getElementById("subMenuDiv") != null) {
		top.frames[2].document.body.removeChild(top.frames[2].document.getElementById("subMenuDiv"));
	}
	subMenuTimer = null;
}

function displayChildMenu() {
	
	subChldTimer = null;

	if (top.frames[2].document.getElementById("childMenuDiv") != null) {
		top.frames[2].document.body.removeChild(top.frames[2].document.getElementById("childMenuDiv"));
	}

	var parentMenu = top.frames[2].document.getElementById("subMenuDiv");
	var parentLink = top.frames[2].document.getElementById(childsParent+"_lnk")

	var subMenuDiv = top.frames[2].document.createElement("DIV");
	
	subMenuDiv.style.position = "absolute";
	subMenuDiv.style.left = (parentMenu.offsetLeft + parentMenu.offsetWidth) + "px";
	subMenuDiv.style.top = (parseInt(childsParent.substring(3,4)) * 24) + "px";
	subMenuDiv.style.color = "white";
	subMenuDiv.style.visibility = "visible";
	subMenuDiv.id = "childMenuDiv";
	
	var childMenu = getChildMenu(currentChld);

	var menuText = "<table border=0 cellpadding=0 cellspacing=0>";
	for (var i = 0; i < childMenu.length; i++) {
		menuText = menuText + "<tr><td width=100%><div class='dropMenuDiv' id='child"+i+"_lnk' style='width:99%' onmouseover=\"top.frames[1].setMenuColor('child"+i+"',2,0)\" onmouseout=\"top.frames[1].setMenuColor('child"+i+"',2,2)\"";		
		menuText = menuText + "onclick=\"top.frames[1].menuClick('JCPCategoryMenu.html?modelId="+getMenuOptionModelId(childMenu[i][0])+"&modelName="+childMenu[i][2]+"&waterMarkModelId="+childMenu[i][0]+"','mainFrame')\" >";
		menuText = menuText + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + childMenu[i][2] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></td></tr>";	
	}
	menuText = menuText + "</table>"

	subMenuDiv.innerHTML = menuText;	
	top.frames[2].document.body.appendChild(subMenuDiv);
	
}

function getChildMenu(parentId) {
	var childMenu = [];
	var menuIndex = 0;
	for (var i = 0; i < modelSubMenu.length; i++) {
		if (modelSubMenu[i][1] == parentId) {
			childMenu[menuIndex] = modelSubMenu[i];
			menuIndex++;
		}
	}
	return childMenu;
}

function directToSubCat() {
	var url = "http://www.jdht.com/html/TrustFrames.html?pageType=" + "buyCD";
	top.document.location.href = url;
}
