// close layer when click-out
document.onclick = closeOnClick; 
document.onmousehover = closeOnHover;

var menuItem=0;
var subMenuItem=0;
var mainCanBeClosed=true;
var subCanBeClosed=true;

//open hidden layer
function openMenu(id){
	var prevMenuItem=menuItem; // store previous layer	
	menuItem = gId(id); // get new layer
	
	if (prevMenuItem.id==menuItem.id){
		//Same layer, so toggle visibility
		if (menuItem.style.visibility=='visible'){
			if (menuItem) menuItem.style.visibility = 'hidden';
			if (subMenuItem) subMenuItem.style.visibility = 'hidden';
		}
		else{
			if (menuItem) menuItem.style.visibility = 'visible';
		}
	}
	else{
		//Changed layer. Show and hide previous
		if (prevMenuItem) prevMenuItem.style.visibility = 'hidden';
		if (menuItem) menuItem.style.visibility = 'visible';
	}
}

function openSubMenu(id,idx){	
	var parentTop
	var parentLeft
	var parentWidth
		
	// close old layer
	if(subMenuItem) subMenuItem.style.visibility = 'hidden';	
		
	//Reposition new layer and show it	
	parentTop=idx*24;
	parentLeft=gId(id).parentNode.clientLeft;
	parentWidth=gId(id).parentNode.clientWidth;
		
	subMenuItem = gId(id);
	subMenuItem.style.left=parentLeft+parentWidth + "px";
	subMenuItem.style.top=parentTop;
		
	subMenuItem.style.visibility = 'visible';
}

function closeSubMenu(id){
	subMenuItem=gId(id)
	setTimeout("if (subCanBeClosed){subMenuItem.style.visibility='hidden';}",100);
}

//close main menu
function closeOnClick(){
	if (mainCanBeClosed){
		if(menuItem) menuItem.style.visibility = 'hidden';
	}
	
	if (subCanBeClosed){
		if(subMenuItem) subMenuItem.style.visibility = 'hidden';
	}
}

//close sub menu
function closeOnHover(){
	if (subMenuItem)
	{
		if (subCanBeClosed){
			if(subMenuItem) subMenuItem.style.visibility = 'hidden';
		}
	}
}

//close menu and layers above it
function closeMenu(obj){
	closeParent(obj);
	//closeChildren(obj);
	
	if (obj.tagName.toLowerCase()=='div'){
		obj.style.visibility = 'hidden';
	}
}

//close all parents
function closeParent(obj){
	
	//Check not top level div before deleting
	if (obj.parentNode.tagName.toLowerCase()!='li'){
		if (obj.parentNode.tagName.toLowerCase()=='div'){
			obj.parentNode.style.visibility = 'hidden';
			closeParent(obj.parentNode);
		}
	}
}

//close all children
function closeChildren(obj){
	if (obj.childNodes.length>0){
		for (i=0;i< obj.childNodes[0].childNodes.length;i++){
			if (obj.childNodes[0].childNodes[i].tagName.toLowerCase()=='div'){
				closeChildren(obj.childNodes[0].childNodes[i]);
				obj.childNodes[0].childNodes[i].style.visibility = 'hidden';
			}
		}
	}
}

function preventSubClose(keepOpenDiv){
	parentDiv=gId(keepOpenDiv).parentNode;
	if (parentDiv.childNodes.length>0){
		for (i=0;i< parentDiv.childNodes.length;i++){
			if (parentDiv.childNodes[i].tagName.toLowerCase()=='div'){
				if (parentDiv.childNodes[i].id!=keepOpenDiv){
					//closeChildren(obj.childNodes[i]);
					parentDiv.childNodes[i].style.visibility = 'hidden';
				}
			}
		}
	}
}

function allowAllClose(){
	mainCanBeClosed=true;
	subCanBeClosed=true;
}

function preventAllClose(){
	mainCanBeClosed=false;
	subCanBeClosed=false;
}

function allowMainClose(){
	mainCanBeClosed=true;
}

function preventMainClose(){
	mainCanBeClosed=false;
}

var level1= new Array();
var level1JS= new Array();
var level2= new Array();
var level2JS= new Array();
var level3= new Array();
var level3JS= new Array();

function getMenuArray(hideMnuFile,hideMnuLang)
{
	//Clear
	level1= new Array();
	level1JS= new Array();
	level2= new Array();
	level2JS= new Array();
	level3= new Array();
	level3JS= new Array();	

	level1 = new Array("File","Language","Help")
	level1JS = new Array("","showLanguageDialogBox(this);","")
	
	//Language
	if (hideMnuLang=='True'){
		level1.splice(1,1)
		level1JS.splice(1,1)
	}
	
	//File
	if (hideMnuFile=='True'){
		level1.splice(0,1)
		level1JS.splice(0,1)
	}
	else{
		level2["File"] = new Array("Exit");
		level2JS["File"] = new Array("document.location.href='frmWebCatHome.aspx';");
	}	
	
	//Help
	level2["Help"] = new Array("Electronic Catalogue","Ford Vin Table","Renault Laguna","BMW");
	level2JS["Help"] = new Array("","window.open('../Files/PDFs/fordvineur.pdf');","window.open('../Files/PDFs/RenLaguna.pdf')","window.open('../Files/PDFs/Bmwpipe.pdf')");

	level3["Electronic Catalogue"] = new Array("Data Development Services Ltd","Version");
	level3JS["Electronic Catalogue"]= new Array("modWindow('../Files/Helplines/DDSInfo.htm', 565,320);","modWindow('frmVersion.aspx',280,300);");
	
}

function modWindow(strURL,height,width){
	//window.showModalDialog(strURL, '', 'center=yes;dialogHeight=' + height + 'px;dialogWidth=' + width + 'px;help=no;status=no')
	modalWindow(strURL, '', 'yes',height, width ,'no', 'no')
}

function buildMenu(hideMnuFile,hideMnuLang){

	var mnuArr=getMenuArray(hideMnuFile,hideMnuLang);
	var div=gId('divMenuContainer');
	var str='<ul id="menuBar">'
	var divCt=0
	
	for (i=0;i<=level1.length-1;i++){
				
		txt1=level1[i]
		txt1JS=level1JS[i]
				
		//Top bar	
		if (txt1JS==''){
				
			//First level
			str = str +'<li><a href="#" style="width:30px;" onmousedown="openMenu(\'div' + txt1 + '\')" ' +
				'onmouseout="allowMainClose();" onmouseover="preventMainClose();">'+ translateText(txt1) + '</a>'	
			str = str + '<div id="div' + txt1 + '" onmouseout="allowMainClose();" onmouseover="preventMainClose();">'	
						
			for (j=0;j<=level2[txt1].length-1;j++){
						
				txt2=level2[txt1][j]
				txt2JS=level2JS[txt1][j]
							
				if (level2JS[txt1][j]==''){
				
					//Second level
					str = str + '<a href="#" onmouseout="allowAllClose();closeSubMenu(\'div' + txt2 + '\');" ' +
						'onmouseover="openSubMenu(\'div' + txt2 + '\','+ 0 + ');preventAllClose();">'+ translateText(txt2)+ '</a>'	
					str = str + '<div id="div' + txt2 + '" onmouseout="allowAllClose();" onmouseover="preventAllClose();">'	
					
					for (k=0;k<=level3[txt2].length-1;k++){	
					
						txt3=level3[txt2][k]
						txt3JS=level3JS[txt2][k]
					
						if (txt3JS==''){
							//Third level
							str = str +'<a href="#" onmouseout="allowAllClose();" ' +
								'onmouseover="openSubMenu(\'div' + txt3 + '\','+ txt1 + ');preventAllClose();">'+ translateText(txt3)+ '</a>'	
							str = str + '<div id="div' + txt3 + '" onmouseout="allowAllClose();" onmouseover="preventAllClose();">'	
							str = str + '</div>'
						}
						else{
							str= str +'<a href="#" onmouseup="closeMenu(this);'+ txt3JS+ '">'+ translateText(txt3)+ '</a>'
						}
					}								
					str = str + '</div>'
				}
				else{
					str= str +'<a href="#" onmouseup="closeMenu(this);'+ txt2JS+ '">'+ translateText(txt2)+ '</a>'
				}
			}
			
			str = str + '</div></li>'							
		}
		else{
			str= str +'<li><a href="#" onmouseup="closeMenu(this);'+ txt1JS+ '">'+ translateText(txt1)+ '</a></li>'
		}
	}
	div.innerHTML=str;
}


function translateText(str){
	var newStr
	
	if (mnuTranslation==undefined) return str;
	
	newStr=mnuTranslation[str]
	if (newStr){	
		return newStr;
	}
	else{
		return str;
	}

}

function buildLangTable(){
	var div=gId('langDiv');
	var str="";
	
	str=  str + "<table style='WIDTH:100%' >"
	str= str + "<tr>"
	str= str + "<td style='width:98%;'>"
	str=  str + "<table style='WIDTH:100%' cellpadding=5 >"
	str= str + "<tr>"
	str= str + "<td>"
	str= str + "<input type='radio' name='gpLang' value='English' checked> English &nbsp;"
	str= str + "<input type='radio' name='gpLang' value='German'> Deutsch &nbsp;"
	str= str + "<input type='radio' name='gpLang' value='Italian'>Italian &nbsp;<br>"
	str= str + "<input type='radio' name='gpLang' value='Spanish'> Espa&ntilde;ol &nbsp;"
	str= str + "<input type='radio' name='gpLang' value='French'>Fran&ccedil;ais &nbsp;"	
	str= str + "</td>"
	str= str + "</tr>"
	str= str + "</table>"					
	str= str + "</td>"
	str= str + "<td align='right' style='vertical-align:top'>"
	//str= str + "<button type='button' id='btnLangClose' style='height:15px;width:15px' onclick='btnLangClose_click(event);'>X</button>"
	str= str + "<image id='imgLangClose' src='../Files/backgrounds/icons/close.gif' style='cursor:pointer; margin:0px;'  onclick='imgLangClose_click(event);' />"
	str= str + "</td>"
	str= str + "</tr>"
	str= str + "<tr>"
	str= str + "<td align='center' colspan='2'>"
	str= str + "<input id='btnLangOK' style='cursor:pointer;width:30px;text-align:center;' onclick='btnLangOK_click(event,this.form);' value='Ok'>"
	str= str + "</td>"
	str= str + "</tr>"
	str= str + "</table>"
	
	div.innerHTML=str;
}

//110121 BS
function showLanguageDialogBox(obj){
	
	var gpLang;
	var selectedLang;
	var i;
	
	//build language table
	buildLangTable();
	
	//show currently selected language
	gpLang=getForm(obj).gpLang
	selectedLang= gId('TextBox1').value
	
	for (i=0; i< gpLang.length; i++){
		if (selectedLang== gpLang[i].value){
			gpLang[i].checked=true;
		}
	}
	
    gId('langDiv').style.display='block';
}

//110121 BS
function btnLangOK_click(e,frm){

	var selectedLang;
	var gpLang;
	
	if (frm)gpLang=frm.gpLang;
	
	//set selected language
	for (var i=0; i< gpLang.length; i++){
		if (gpLang[i].checked){
			selectedLang = gpLang[i].value;
		}
	}

	gId('TextBox1').value=selectedLang;
	gId('langDiv').style.display='none';
	gId('btnLanguageSelected').click();
}

function imgLangClose_click(e){
	 gId('langDiv').style.display='none';
}

//110121 BS
//Return container form
function getForm(obj){

	while (obj.tagName!="FORM"){
		obj=obj.parentNode	
	}
	return obj;
}
