/*modified by jon
1)Distance mouse has to move from menu for it to close is set in menuCleanUp().

2)Don't ask me WHY, but if you have two menu items with the same url, the style sheet doesn't get applied correctly.

3)Added menuwidth and menuheight to menucleanup so each menu has different mouseouts.
*/


//menu sticky rollover
var ie = (document.all)?true:false;									// IE 4+
var ns = (document.layers)?true:false;	
var moz = (document.getElementById && !document.all)?true:false;	// Mozilla 5.x / NS 6
var rolloverProtect = new Array();
var menuVisible = "";
var menuRollDownImg = new Image()
menuRollDownImg.src = "images/spacer.gif";
var menuRollDown = new Image()
menuRollDownImg.src = "images/spacer.gif";

function showMenu(whichOne,state) {
	if (menuVisible != "") {
		obj2 = getObject(menuVisible);
		if (ie) {
			obj2.style.visibility = "hidden";
		} else if (moz) {
			obj2.style.visibility = "hidden";
		} else if (ns) {
			obj2.visibility = "hidden";
		}
		menuVisible = "";
	}
	obj = getObject(whichOne);
				
		switch(state) {
			case "show":
				if (ie) {
					obj.style.visibility = "visible";
				} else if (moz) {
					obj.style.visibility = "visible";
				} else if (ns) {
					obj.visibility = "visible";
				}
				menuVisible = whichOne;
				break;
			case "hide":
				if (ie) {
					obj.style.visibility = "hidden";
				} else if (moz) {
					obj.style.visibility = "hidden";
				} else if (ns) {
					obj.visibility = "hidden";
				}
				menuVisible = "";
				break;
		}
}

function showMenuChild(whichOne,arrowimg) {
	if (ie) {
		obj = getObject(whichOne).style;
	} else if (moz) {
		obj = getObject(whichOne).style;
	} else if (ns) {
		obj = getObject(whichOne);
		return null;
		//alert(obj.display);
	}
	if (obj.display=='none'){
		obj.display='';
		imgSwp(arrowimg,'images/spacer.gif');
		rolloverProtect.push(arrowimg);
	} else {
		obj.display='none';
		imgSwp(arrowimg,'images/spacer.gif');
		rolloverProtect.pop(arrowimg);
	}
}

function createMenus(menuNumber,language) {
	menuArray = new Array()
	switch(menuNumber) {
		//about
		case 0:
			//menuArray[0] = 'name="company profile" url="page.php?page=_company" index="0" isParent="0"';
			break;
		case 1:
			menuArray[0] = 'name="Aero Mechanical Conveyors" url="page.php?page=amc_introduction" index="0" isParent="0"';
			menuArray[1] = 'name="Sack Tip Station" url="page.php?page=sacktipcabinets_introduction" index="0" isParent="0"';
			menuArray[2] = 'name="Automatic Sack Openers" url="page.php?page=automaticsackopeners_introduction" index="0" isParent="0"';
			//menuArray[3] = 'name="Vacuum Sack Lifters" url="page.php?page=vacuumsacklifters_introduction" index="0" isParent="0"';
			//menuArray[4] = 'name="Sack Compactors" url="page.php?page=sackcompactors_introduction" index="0" isParent="0"';
			menuArray[3] = 'name="FIBC Filling Station" url="page.php?page=FIBC_fillers_introduction" index="0" isParent="0"';
			menuArray[4] = 'name="FIBC Discharging Station" url="page.php?page=FIBC_dischargers_introduction" index="0" isParent="0"';
			//menuArray[7] = 'name="Mixers" url="page.php?page=mixers_introduction" index="0" isParent="0"';
			menuArray[5] = 'name="Electric Vibrators" url="page.php?page=electricvibrators_introduction" index="0" isParent="0"';
			menuArray[6] = 'name="Turnkey Solutions" url="page.php?page=turnkeysolutions_introduction" index="0" isParent="0"';
			break;
		case 2:
			menuArray[0] = 'name="spares" url="page.php?page=_spares" index="0" isParent="0"';
			menuArray[1] = 'name="servicing" url="page.php?page=_servicing" index="0" isParent="0"';
			break;
		case 3:
			menuArray[0] = 'name="Latest news" url="news.php" index="0" isParent="0"';
			menuArray[1] = 'name="Applications" url="page.php?page=_applications" index="0" isParent="0"';
			menuArray[2] = 'name="Case studies" url="page.php?page=_casestudies" index="0" isParent="0"';
			break;
		case 4:
			menuArray[0] = 'name="Brochures" url="page.php?page=downloads_brochure" index="0" isParent="0"';
			menuArray[1] = 'name="Terms and Conditions" url="page.php?page=downloads_termsandconditions" index="0" isParent="0"';
			break;
		case 5:
			menuArray[0] = 'name="Contact" url="page.php?page=_contact" index="0" isParent="0"';
			menuArray[1] = 'name="Distributors" url="page.php?page=distributors" index="0" isParent="0"';
			break;
		case 6:
			menuArray[0] = 'name="Company Profile" url="page.php?page=_company" index="0" isParent="0"';
			menuArray[1] = 'name="Test Facility" url="page.php?page=company_testFacility" index="0" isParent="0"';
			break;

	}
	//document.write("<span id=\"menuspacer" + menuNumber + "\"><img src=\"http://www.entecon.co.ukimages/spacer.gif\" height=\"4\" width=\"136\"></span>");
	drawMenu(menuArray,menuNumber,language);
	var menuRollDownImg = new Image()
	menuRollDownImg.src = "images/spacer.gif";
}


function drawMenu(menuArray,menuNumber,language) {
	//document.write("<img src=\"/images/pix.gif\" height=\"4\" width=\"136\"><BR>");
	for (i=0;i<menuArray.length;i++) {
		switch (language) {
			case "en":
				itemName = searchString(menuArray[i],'name=');
				break;
			case "fr":
				itemName = searchString(menuArray[i],'name_fr=');
				break;
		}		
		itemURL = searchString(menuArray[i],'url=');
		index = parseInt(searchString(menuArray[i],'index='));
		prevIndex = i > 0 ? parseInt(searchString(menuArray[i-1],'index=')):0;
		isParent = parseInt(searchString(menuArray[i],'isParent='));
		activate = "";
		tempHTML = "";
		
		if (prevIndex > index) tempHTML += "</span>";
		tempHTML += "<span id=\"menu" + menuNumber + i + "\" !class=\"menuspan\">";
		if (isParent) { activate = " onclick=\"showMenuChild('menu" + menuNumber + i + parseInt(index+1) +"','menu" + menuNumber + i + "');winStatus('" + itemName + "');return true;\" "; itemURL = "javascript:void(0);"; }
		if (index >= 1 ) tempHTML += '<img src="/images/pix.gif" width="' + 6*index + '" height="1">';
		tempHTML += "<img src=\"images/spacer.gif\" height=\"7\" width=\"4\" name=\"menu" + menuNumber + i + "\" hspace=\"5\" !alt=\"menu" + menuNumber + i + "\">";
		tempHTML += "<a href=\"" + itemURL + "\"" + activate + " class=\"menuItem\" onmouseover=\"winStatus('" + itemName + "');return true;\" onmouseout=\"winStatus('');return true;\">" + itemName + "</a><img src=\"images/spacer.gif\" height=\"14\" width=\"1\"><br></span>";
		if (isParent) tempHTML += "<span id=\"menu" + menuNumber + i + parseInt(index+1) + "\" style=\"display:none;\">";
		document.write(tempHTML)
	}
}



function menuArrow(w,s,l) {
	checkAgainst = rolloverProtect.toString();
	if (checkAgainst.indexOf(w) <= -1) {
		if (ie) {
		document.images[w].src = "images/spacer.gif";
		} else if (moz) {
		document.images[w].src = "images/spacer.gif";
		} else if (ns) {
		document.layers[l].document.images[w].src = "images/spacer.gif";
		}
	}
}


function menuCleanUp() {
	if (menuVisible != "") {
		if (ie) {
			obj = getObject(menuVisible).style;
		} else if (moz) {
			obj = getObject(menuVisible).style;
		} else if (ns) {
			obj = getObject(menuVisible);
		}
		menuLeft = parseInt(obj.left);
		menuTop = parseInt(obj.top)-25;
		menuHeight = parseInt(obj.height);
		menuWidth = parseInt(obj.width);
		menuRight = menuWidth + menuLeft;
		menuBottom = menuHeight + menuTop + 25;
		
		if (tempX < menuLeft || tempX > menuRight || tempY < menuTop || tempY > menuBottom) {
			showMenu(menuVisible,'hide');
		}
	}
	var menuTimer = setTimeout("menuCleanUp()",1500);
	
}

//
//various other functions:
//

//needed for menu:
var checkMouseOK = false;
function checkMouse() {
	checkMouseOK = true;
}

//needed for menu:
function imgSwp(who,what) {
	if (document.images) {
		document.images[who].src = what;
	}
}


// nesting layer for ns
//needed for menu:
function nsNestedLayerGetObject(layerId,nestingLayer) {
	if (ns) {
		obj = document.layers[nestingLayer].document.layers[layerId];
		return obj;
	}
}

//needed for menu:
function show(obj,nestedLayer) {
	if (nestedLayer != '') {
		if (ns) {
			obj = nsNestedLayerGetObject(obj,nestedLayer);
		} else {
			obj = getObject(obj);
		}
	} else {
		obj = getObject(obj);
	}
	if (ie || moz) {
		obj.style.visibility = 'visible';
	} else if (ns) {
		obj.visibility = 'visible';
	}
}

//needed for menu:
function getObject(layerId) {
	if (ie) {
		obj = document.all(layerId);
		//obj = document.getElementById(layerId);
		//obj = obj.style;
	} else if (ns) {
		obj = document.layers[layerId];
	} else if (moz) {
		obj = document.getElementById(layerId);
	}
	return obj;
}




//needed for menu:
function searchString(stringName, keyWord) {
	//alert(stringName);
	quoteChar = '"';
	stringLength = stringName.toString().length;

	if (stringName.indexOf(keyWord) == 0) stringName = stringName.substring(stringName.indexOf(keyWord) + keyWord.toString().length);
	else if (stringName.indexOf(' ' + keyWord) > -1) stringName = stringName.substring(stringName.indexOf(' ' + keyWord) + (keyWord.toString().length + 1));

	if (stringName.length != stringLength) {
		return stringName.substring(stringName.indexOf(quoteChar) + 1, stringName.indexOf(quoteChar,stringName.indexOf(quoteChar) + 1));
	} else {
		return '[MISSING]';
	}
}

//needed for menu:
function winStatus(msg) {
	if (typeof msg == 'string') {
		window.status = msg;
		return true;
	}
}

if (!ie) document.captureEvents(Event.MOUSEMOVE)
var tempX = 0;
var tempY = 0;
document.onmousemove = getMouseXY;

function getMouseXY(e) {
	if (ie) { // grab the x-y pos.s if browser is IE
    	tempX = event.clientX + document.body.scrollLeft
	    tempY = event.clientY + document.body.scrollTop
	} else if (moz) {  // grab the x-y pos.s if browser is NS
		tempX = e.clientX + window.scrollX;
	    tempY = e.clientY + window.scrollY
	} else if (ns) {
		tempX = e.pageX
		tempY = e.pageY
	}  

  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
  
	if (ie) {
		if (tempX != 0 && tempY != 0) {
			if (tempX <= 720 && tempY <=800) {
				if (checkMouseOK) {
					document.topleft.setVariable("jsx",(tempX*-1)/10+80);
				  	document.topleft.setVariable("jsy",(tempY*-1)/10+123);
				}
			}
		}
	}
  //window.status = "menu debug info: x="+tempX+", y="+ tempY + "  menuVisible=" + menuVisible;
  return true;
}
