﻿/*
Taken Without Permission from www.thelightingsuperstore.co.uk
There are areas of these scripts that have been put in to enable us to trace unauthorised copying, 
please contact us before using any scripts/text/images found on the Lighting Superstore Web Site/s

*/

/* a few sniffs to circumvent known browser bugs */
var sUserAgent = navigator.userAgent.toLowerCase();
var isIE=document.all?true:false;
var isNS4=document.layers?true:false;
var isOp=(sUserAgent.indexOf('opera')!=-1)?true:false;
var isMac=(sUserAgent.indexOf('mac')!=-1)?true:false;
var isMoz=(sUserAgent.indexOf('mozilla/5')!=-1&&sUserAgent.indexOf('opera')==-1&&sUserAgent.indexOf('msie')==-1)?true:false;
var isNS6=(sUserAgent.indexOf('netscape6')!=-1&&sUserAgent.indexOf('opera')==-1&&sUserAgent.indexOf('msie')==-1)?true:false;
var dom=document.getElementById?true:false;

/* sets time until menus disappear in milliseconds */
var iMenuTimeout=500;

var aMenus=new Array;
var oMenuTimeout;
var iMainMenusLength=0;

/* the following boolean controls the z-index property if needed */
/* if is only necessary if you have multiple mainMenus in one file that are overlapping */
/* set bSetZIndeces to true (either here or in the HTML) and the main menus will have a z-index set in descending order so that preceding ones can overlap */
/* the integer iStartZIndexAt controls z-index of the first main menu */
var bSetZIndeces=true;
var iStartZIndexAt=1000;
var aMainMenus=new Array;

/* load up the submenus */
function loadMenus(){
	if(!dom)return;
	var aLists=document.getElementsByTagName('ul');
	for(var i=0;i<aLists.length;i++){
		if(aLists[i].className=='navMenu')aMenus[aMenus.length]=aLists[i];
	}
	var aAnchors=document.getElementsByTagName('a');
	var aItems = new Array;
	for(var i=0;i<aAnchors.length;i++){
		if(aAnchors[i].className=='navItem')aItems[aItems.length] = aAnchors[i];
	}
	var sMenuId=null;
	var oParentMenu=null;
	var aAllElements=document.body.getElementsByTagName("*");
	if(isIE)aAllElements=document.body.all;
	/* loop through navItem and navMenus and dynamically assign their IDs */
	/* each relies on it's parent's ID being set before it */
	for(var i=0;i<aAllElements.length;i++){
		if(aAllElements[i].className.indexOf('mainMenu')!=-1){
			/* load up main menus collection */
			if(bSetZIndeces)aMainMenus[aMainMenus.length]=aAllElements[i];
		}
		if(aAllElements[i].className=='navItem'){
			oParentMenu = aAllElements[i].parentNode.parentNode;
			if(!oParentMenu.childMenus) oParentMenu.childMenus = new Array;
			oParentMenu.childMenus[oParentMenu.childMenus.length]=aAllElements[i];
			if(aAllElements[i].id==''){
				if(oParentMenu.className=='mainMenu'){
					aAllElements[i].id='navItem_'+iMainMenusLength;
					//alert(aAllElements[i].id);
					iMainMenusLength++;
				}else{
					aAllElements[i].id=oParentMenu.id.replace('Menu','Item')+'.'+oParentMenu.childMenus.length;
				}
			}
		} else if(aAllElements[i].className=='navMenu'){
			oParentItem = aAllElements[i].parentNode.firstChild;
			aAllElements[i].id = oParentItem.id.replace('Item','Menu');
		}
	}
	/* dynamically set z-indeces of main menus so they won't underlap */
	for(var i=aMainMenus.length-1;i>=0;i--){
		aMainMenus[i].style.zIndex=iStartZIndexAt-i;
		//---2008-8-03添加--
		eval('aMainMenus[i].onmouseover=function(){window.clearTimeout(oMenuTimeout);}');
		eval('aMainMenus[i].onmouseout=function(){window.clearTimeout(oMenuTimeout);oMenuTimeout=window.setTimeout("hideMainMenu()",iMenuTimeout);}');
	}
	/* set menu item properties */
	for(var i=0;i<aItems.length;i++){
		sMenuId=aItems[i].id;
		sMenuId='navMenu_'+sMenuId.substring(8,sMenuId.lastIndexOf('.'));
		/* assign event handlers */
		/* eval() used here to avoid syntax errors for function literals in Netscape 3 */
		eval('aItems[i].onmouseover=function(){modClass(true,this,"activeItem");window.clearTimeout(oMenuTimeout);showMenu("'+sMenuId+'");};');
		eval('aItems[i].onmouseout=function(){modClass(false,this,"activeItem");window.clearTimeout(oMenuTimeout);oMenuTimeout=window.setTimeout("hideMenu(\'all\')",iMenuTimeout);}');
		eval('aItems[i].onfocus=function(){this.onmouseover();}');
		eval('aItems[i].onblur=function(){this.onmouseout();}');
		//aItems[i].addEventListener("keydown",function(){keyNav(this,event);},false);
	}
	var sCatId=0;
	var oItem;
	for(var i=0;i<aMenus.length;i++){
		/* assign event handlers */
		/* eval() used here to avoid syntax errors for function literals in Netscape 3 */
		eval('aMenus[i].onmouseover=function(){window.clearTimeout(oMenuTimeout);}');
		eval('aMenus[i].onmouseout=function(){window.clearTimeout(oMenuTimeout);oMenuTimeout=window.setTimeout("hideMenu(\'all\')",iMenuTimeout);}');
		sCatId=aMenus[i].id;
		sCatId=sCatId.substring(8,sCatId.length);
		oItem=document.getElementById('navItem_'+sCatId);
		if(oItem){
			if(!isOp && !(isMac && isIE) && oItem.parentNode)modClass(true,oItem.parentNode,"hasSubMenu");
			else modClass(true,oItem,"hasSubMenu");
			/* assign event handlers */
			eval('oItem.onmouseover=function(){window.clearTimeout(oMenuTimeout);showMenu("navMenu_'+sCatId+'");}');
			eval('oItem.onmouseout=function(){window.clearTimeout(oMenuTimeout);oMenuTimeout=window.clearTimeout(oMenuTimeout);oMenuTimeout=window.setTimeout(\'hideMenu("navMenu_'+sCatId+'")\',iMenuTimeout);}');
			eval('oItem.onfocus=function(){window.clearTimeout(oMenuTimeout);showMenu("navMenu_'+sCatId+'");}');
			eval('oItem.onblur=function(){window.clearTimeout(oMenuTimeout);oMenuTimeout=window.clearTimeout(oMenuTimeout);oMenuTimeout=window.setTimeout(\'hideMenu("navMenu_'+sCatId+'")\',iMenuTimeout);}');
			//oItem.addEventListener("keydown",function(){keyNav(this,event);},false);
		}
	}
}

function addValue(id) {
var num = (parseInt("1"));
document.getElementById(id).value = (parseInt(document.getElementById(id).value)) + num;
if (document.getElementById(id).value < 1) document.getElementById(id).value = 1;
}
function subtractValue(id) {
var num = (parseInt("1"));
document.getElementById(id).value = (parseInt(document.getElementById(id).value)) - num;
if (document.getElementById(id).value < 1) document.getElementById(id).value = 1;
}

/* this will append the loadMenus function to any previously assigned window.onload event */
/* if you reassign this onload event, you'll need to include this or execute it after all the menus are loaded */
function newOnload(){
	MM_preloadImages('images/motorcycle1.gif','images/scooter1.gif','images/atv1.gif','images/mo-about1.gif','images/service1.gif','images/part1.gif','images/trade1.gif','images/contact1.gif');	
	if(typeof previousOnload=='function')previousOnload();
	setPos();
	document.getElementById("divMenu").style.display="";
	loadMenus()	;
}
function setPos()
{
	//---------2008-7-30添加  陈伟峰--------
	var obj=document.getElementById("menu");
	var coors = findPos(obj);
	var oLayer=document.getElementById("divMenu");
	oLayer.style.left = coors[0] + "px";
	oLayer.style.top = (coors[1]) + "px" ;
	//-----------结束---------   
}

var previousOnload;
if(window.onload!=null)previousOnload=window.onload;
window.onload=newOnload;
window.onresize=setPos;

/* show menu and hide all others except ancestors of the current menu */
function showMenu(sWhich){
	var oWhich=document.getElementById(sWhich);
	if(!oWhich){
		hideMenu('all');
		return;
	}
	var aRootMenus=new Array;
	aRootMenus[0]=sWhich
	var sCurrentRoot=sWhich;
	var bHasParentMenu=false;
	if(sCurrentRoot.indexOf('.')!=-1){
		bHasParentMenu=true;
	}
	/* make array of this menu and ancestors so we know which to leave exposed */
	/* ex. from ID string "navMenu_12.3.7.4", extracts menu levels ["12.3.7.4", "12.3.7", "12.3", "12"] */
	while(bHasParentMenu){
		if(sCurrentRoot.indexOf('.')==-1)bHasParentMenu=false;
		aRootMenus[aRootMenus.length]=sCurrentRoot;
		sCurrentRoot=sCurrentRoot.substring(0,sCurrentRoot.lastIndexOf('.'));
	}
	//----------2008-7-30添加---------
	var temp=document.getElementById(aRootMenus[aRootMenus.length-1].replace('navMenu_','navItem_'));
	//----------结束--------------
	for(var i=0;i<aMenus.length;i++){
		var bIsRoot=false;
		for(var j=0;j<aRootMenus.length;j++){
			var oThisItem=document.getElementById(aMenus[i].id.replace('navMenu_','navItem_'));
			if(aMenus[i].id==aRootMenus[j])bIsRoot=true;
		}
//		//----------2008-7-30添加---------
//		if(aMenus[i].id.indexOf(".")==-1)
//		{
//			var tmp=document.getElementById(aMenus[i].id.replace('navMenu_','navItem_'))
//			if(tmp.innerHTML.indexOf("1.")!=-1)
//			{
//				tmp.innerHTML=tmp.innerHTML.replace("1.gif",".gif")
//			}
//		}
//		//----------结束--------------
		if(bIsRoot && oThisItem)modClass(true,oThisItem,'hasSubMenuActive');
		else modClass(false,oThisItem,'hasSubMenuActive');
		if(!bIsRoot && aMenus[i].id!=sWhich)modClass(false,aMenus[i],'showMenu');
	}
	//else if(oWhich.id.indexOf('navMenu_')!=-1)
//	{
//		oWhich.style.top="-1px";
//	}
	modClass(true,oWhich,'showMenu');
	//if(oWhich.id.indexOf('navMenu_')!=-1)
	//{
		var coors = findPos(oWhich);
		//modClass(true,oWhich,'navMenu');
		//alert(oWhich.id);
		//alert(document.documentElement.offsetHeight);
		//alert(oWhich.offsetHeight+ '\r\n' + coors[1]);
		//alert(document.documentElement.scrollTop+ '\r\n' + coors[1]);
		//alert(oWhich.offsetHeight-document.documentElement.scrollTop+coors[1]);
		//alert(oWhich.offsetHeight-document.documentElement.scrollTop+coors[1] +'\r\n'  + coors[1] +'\r\n' + oWhich.offsetHeight);
		//----重新计算位置-----
		//if(oWhich.id.indexOf('navMenu_')!=-1)
		//{
			var pn=oWhich.offsetHeight+coors[1]-document.documentElement.scrollTop+10;
			var pn1=document.documentElement.offsetHeight;
			if(pn>pn1)
			{
				if(oWhich.offsetHeight>=document.documentElement.offsetHeight)
				{
					oWhich.style.top="-" + coors[1] + "px";
				}
				else
				{
					//oWhich.style.top="-" + coors[1] + "px";
					//var n1=oWhich.offsetHeight+coors[1]+10;
					var n1=26;
					//alert(n1);
					while((pn-n1)>pn1)
					{
						n1+=26;
					}
					//var n1=(document.documentElement.offsetHeight-oWhich.offsetHeight)/2;
					//alert(coors[1]-document.documentElement.scrollTop-n1);
					//oWhich.style.position="absolute";
					oWhich.style.top="-" + n1 + "px";
					//oWhich.style.top="-" + (n1-oWhich.offsetHeight-coors[1]) + "px";
					//alert(oWhich.offsetParent.offsetTop);
					//oWhich.style.top="-" + (coors[1]-oWhich.offsetParent.offsetTop-document.documentElement.scrollTop-n1+20) + "px";
				}
			//alert(oWhich.offsetHeight+ '\r\n' + coors[1]);
			//alert(oWhich.offsetHeight-document.documentElement.scrollTop+coors[1]);
			
			}
		//}
		
		//alert(document.documentElement.offsetHeight);
		//alert(document.documentElement.scrollTop + '\r\n' + coors[1] );
		//if((coors[1]+oWhich.style.offsetHeight)>document.body.scrollHeight)
		//{
			//oWhich.style.top="-" + (coors[1]-document.documentElement.scrollTop) + "px";
		//}
		//oWhich.style="top:-" + coors[1] + "px !important;position: absolute;display:none;left:147px;padding:0px;margin:0px;width:120px;color:#000;background-color:transparent;";//"-" + coors[1] + "px !important;" ;
	//}
	var oItem=document.getElementById(sWhich.replace('navMenu_','navItem_'));
	if(oItem)modClass(true,oItem,'hasSubMenuActive');
	//----------2008-7-30添加---------
	modClass(false,temp,'hasSubMenuActive');
	if(temp.innerHTML.indexOf("1.")==-1)
	{
		for(var j=1;j<9;j++)
		{
			var tmp1=document.getElementById('navItem_' + j);
			if(tmp1.id!=temp.id)
			{
				tmp1.innerHTML=tmp1.innerHTML.replace("1.gif",".gif");
			}
		}
		temp.innerHTML=temp.innerHTML.replace(".gif","1.gif");
		//alert(temp.innerHTML);
	}
	//----------结束--------------
}

function hideMenu(sWhich){
	if(sWhich=='all'){
		/* loop backwards b/c WinIE6 has a bug with hiding display of an element when it's parent is already hidden */
		for(var i=aMenus.length-1;i>=0;i--){
			var oThisItem=document.getElementById(aMenus[i].id.replace('navMenu_','navItem_'));
			if(oThisItem)
			{
				modClass(false,oThisItem,'hasSubMenuActive');
				modClass(false,oThisItem,'showMenu');
				//----------2008-7-30添加---------
				if(oThisItem.innerHTML.indexOf("1.")!=-1)
				{
					oThisItem.innerHTML=oThisItem.innerHTML.replace("1.gif",".gif")
				}
				//----------结束--------------
			}
			modClass(false,aMenus[i],'showMenu');
		}
	}else{
		var oWhich=document.getElementById(sWhich);
		if(oWhich)modClass(false,oWhich,'showMenu');
		var oThisItem=document.getElementById(sWhich.replace('navMenu_','navItem_'));
		if(oThisItem)
		{
			modClass(false,oThisItem,'hasSubMenuActive');
			//----------2008-7-30添加---------
			if(oThisItem.innerHTML.indexOf("1.")!=-1)
			{
				oThisItem.innerHTML=oThisItem.innerHTML.replace("1.gif",".gif")
			}
			//----------结束--------------
		}
	}
}
function hideMainMenu()
{
	hideMenu('all');
}
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
  if (window.focus)
 { win.focus() }
}

/* add or remove element className */
function modClass(bAdd,oElement,sClassName){
	if(bAdd){/* add class */
		if(oElement.className.indexOf(sClassName)==-1)oElement.className+=' '+sClassName;
	}else{/* remove class */
		if(oElement.className.indexOf(sClassName)!=-1){
			if(oElement.className.indexOf(' '+sClassName)!=-1)oElement.className=oElement.className.replace(' '+sClassName,'');
			else oElement.className=oElement.className.replace(sClassName,'');
		}
	}
	return oElement.className; /* return new className */
}

//document.body.addEventListener("keydown",function(){keyNav(event);},true); 

function setBubble(oEvent){
	oEvent.bubbles = true;	
}

function keyNav(oElement,oEvent){
	alert(oEvent.keyCode);
	window.status=oEvent.keyCode;
	return false;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

