//onMouseOver="openMenu('menu1', 10, 70); MM_swapImage('home','','images/homeOver.gif',1);" onMouseOut="closeMenu(); MM_swapImgRestore();
var closetimer;
var outtimer;
var swapselectIndex;
var swapselectGroup;
var swapselectArray;
var swapselectObject;
var minusleftofmenu;

minusleftofmenu = 4;

var menuHash = new Object();

function menuOCollection(imglayer, imgcallmenu, imgarray, imggroup, imgindex, secondone, idlevel){
	this.layer = imglayer;
	this.caller = imgcallmenu;
	this.array = imgarray;
	this.group = imggroup;
	this.index = imgindex;
	this.otravez = secondone;
	this.arraylength = imgarray.length;
	this.level = idlevel;
}//end structure for menuOCollection


function openSwapTool(callmenu, menuid, menuleft, menutop, imgarray, imggroup, imgindex){
	


	//alert("openSwapTool" + callmenu + ", " + menuid);
	var iobject;
	var leftofmenu = 0;
	var arglen = openSwapTool.arguments.length;
	var scorecount;
	var scoreindex;
	var openlevel;
	var xoffset2 = 0;	
	//alert(window.DOMObjects);
	if(window.DOMObjects){
		//1) Swap Images First
		justSwapOver(callmenu, imgarray, imggroup, imgindex);
		//alert('swap over');
		//2) Open Menu Next
		openlevel = 1;		
		//alert(menuid + " is an " + window.DOMObjects[menuid]);
		if((window.DOMObjects[menuid])&&(menuid != "")){
			if(openSwapTool.arguments[8]){
				xoffset2 = openSwapTool.arguments[8];
			}//then we have an additional offset
			leftofmenu = menuleft;
			if ((arglen <= 7)||((openSwapTool.arguments[7])&&(openSwapTool.arguments[7] < 0))){
				//alert("left of menu is len moins 7 " +menuleft);
				if (menuleft == -1){
					//alert("index of image is " + imgindex);
					leftofmenu = GetLeftOF(callmenu, imgindex, false);
					if((leftofmenu + xoffset2) < 0){
						leftofmenu = 0;
					} else {
						leftofmenu += xoffset2;
					}//					
				}//
				//alert("Left of Menu is " + leftofmenu);
				openMenu(menuid, leftofmenu, menutop);
				
			} else {		
				//alert("more than 7 arguments. Level is " + openSwapTool.arguments[7]);
				//alert("left of menu is len plus 7 " +menuleft);
				if (menuleft == -1){
					leftofmenu = GetLeftOF(callmenu, imgindex, true);
					if((leftofmenu + xoffset2) < 0){
						leftofmenu = 0;
					} else {
						leftofmenu += xoffset2;
					}//					
				}//
				//alert("Left of Menu is " + leftofmenu);
				openlevel = openSwapTool.arguments[7];
				openMenu(menuid, leftofmenu, menutop, openSwapTool.arguments[7]);
			}//end if
		}//if the menuid is an item, we want to do it.
		if ((menuid == "")&&(callmenu != "")&&(callmenu != "toolbar")){
			if(window.DOMObjects[callmenu]){
				//alert("yOffset is " + yOffset);
				//alert("xOffset is " + xOffset);
				//alert("callmenu is " + callmenu.name);
				//alert(DOMObjects[callmenu]);
				//alert("DOMElementGetOffsetTop(DOMObjects[callmenu]) value is " + DOMElementGetOffsetTop(DOMObjects[callmenu]));
				//alert("DOMElementGetOffsetLeft(DOMObjects[callmenu]) value is " + DOMElementGetOffsetLeft(DOMObjects[callmenu]));
				//alert(document.getElementById("mbaritem1").style.top);
				menutop = DOMElementGetOffsetTop(DOMObjects[callmenu])- yOffset;
				leftofmenu = DOMElementGetOffsetLeft(DOMObjects[callmenu]) - xOffset;
				if((leftofmenu + xoffset2) < 0){
					leftofmenu = 0;
				} else {
					leftofmenu += xoffset2;
				}//				
				if((document.getElementById)&&(!menutop)){
					menutop = parseInt(document.getElementById(callmenu).style.top) - yOffset;
				}//Safari cannot see DOMObjects April 28, 2006
				if((document.getElementById)&&(!leftofmenu)){
					leftofmenu = parseInt(document.getElementById(callmenu).style.left) - xOffset;
					if((leftofmenu + xoffset2) < 0){
						leftofmenu = 0;
					} else {
						leftofmenu += xoffset2;
					}//					
				}//Safari cannot see DOMObjects April 28, 2006
				//alert("Top of menu will be " + menutop + " and left of menu will be " + leftofmenu);
				scorecount = 0;
				scoreindex = 0;
				//alert(callmenu.indexOf("_", scoreindex));
				while (callmenu.indexOf("_", scoreindex) != -1){
					scoreindex = callmenu.indexOf("_", scoreindex) + 1;
					scorecount ++;
				}//after the count, we will know this index
				//alert("how many scores does " + callmenu + " have? " + scorecount);
				if(scorecount < 0){
					openMenu(callmenu, leftofmenu, menutop);				
				} else {
					openlevel = scorecount + 1;
					openMenu(callmenu, leftofmenu, menutop, scorecount + 1);
				}//end if condition
			}//end condition		
		}//end if condition
		
		//ADD THIS ITEM TO A COLLECTION
		if(!menuHash[imggroup + imgindex]){
			menuHash[imggroup + imgindex] = new menuOCollection(menuid, callmenu, imgarray, imggroup, imgindex, false, openlevel);
		} else {
			menuHash[imggroup + imgindex].otravez = true;
			for (var n in menuHash){
				if (menuHash[n].level > 0){
					if (menuHash[n].level <= menuHash[imggroup + imgindex].level){
						menuHash[n].otravez = true;
					}//end condition
				}//end outer condition of levels
			}//end condition
			outing = true;
			closeMenuTimer();
		}//ONLY ADD TO HASH TABLE IF IT IS NOT DEFINED PREVIOUSLY
	}//end condition window.DOMObjects
}//end function openSwapTool()


function closeSwapTool(callmenu, menuid, imgarray, imggroup, imgindex){
	//1)Close Menu First
	closeMenu();
	//2) If Menu is Closed, then Swap Image to Original
	if(window.DOMObjects){
		if(window.DOMObjects[menuid]){
			//alert("wait to close");
			swapselectGroup = imggroup;
			swapselectIndex = imgindex;
			swapselectArray = imgarray;
			swapselectObject = callmenu;
			closetimer = window.setTimeout('swapOutToolbar()', 500);
		} else {
			//alert("close now " + imggroup + imgindex);
			closing = true;
			window.clearTimeout(closetimer);
			var iobject = eval('document.' + imggroup + imgindex);
			if (iobject){
				iobject.src = imgarray[imgindex].out.src;
			} else if (document.layers){
				if (DOMObjects[callmenu]){
					iobject = eval("document.layers['" + callmenu + "']." + "document.images." + imggroup + imgindex);
					if (iobject){
						iobject.src = imgarray[imgindex].out.src;
					} else {
						iobject = eval("document.layers['" + callmenu + "']." + "document.layers['inside'].document.images." + imggroup + imgindex);
							if (iobject){
								iobject.src = imgarray[imgindex].out.src;
							}						
					}//end another condition
				}//end netscape layer
			}//end if condition
			
			
			//ONLY CALL THIS FOR THIS CONDITION
			if(menuHash[imggroup + imgindex]){
				//alert("Going to Delete in CLOSESWAP " + imggroup + imgindex + " from HashTable");
				delete menuHash[imggroup + imgindex];
			}//delete this key
			
		}//end if menuid is a layer object
		
	}//if the menuid is an item, we want to do it.
}//end function closeSwapTool()

function swapOutToolbar(){
	window.clearTimeout(closetimer);
	var iobject;
	if(closing){
		if((swapselectIndex == -1)||(swapselectGroup == "")){
			return;
		}//Exit, Or Else... cause an Error
		iobject = eval('document.' + swapselectGroup + swapselectIndex);
		if (iobject){
			iobject.src = swapselectArray[swapselectIndex].out.src;
		} else if (document.layers){
			if (swapselectObject){
				iobject = eval("document.layers['" + swapselectObject + "']." + "document.images." + swapselectGroup + swapselectIndex);
				if (iobject){
					iobject.src = swapselectArray[swapselectIndex].out.src;
				} else {
					iobject = eval("document.layers['" + swapselectObject + "']." + "document.layers['inside'].document.images." + swapselectGroup + swapselectIndex);
						if (iobject){
							iobject.src = swapselectArray[swapselectIndex].out.src;
						}				
				}//end condition
			}//end netscape layer
		}//end if condition
		
		if(menuHash[swapselectGroup + swapselectIndex]){
			//alert("Going to Delete in SWAPOUT " + swapselectGroup + swapselectIndex + " from HashTable");
			delete menuHash[swapselectGroup + swapselectIndex];
			//alert("menuHash[" + swapselectGroup + swapselectIndex + "] is now " + menuHash[swapselectGroup + swapselectIndex]);
		}//delete this key
		
	}//close all toolbars to out
}//end function

function justSwapOut(callmenu, imgarray, imggroup, imgindex){
	var iobject = eval('document.' + imggroup + imgindex);
	if ((isArray(imgarray))&&(imgarray[imgindex])){
		if (iobject){
			iobject.src = imgarray[imgindex].out.src;
		} else if (document.layers){
			if (DOMObjects[callmenu]){
				iobject = eval("document.layers['" + callmenu + "']." + "document.images." + imggroup + imgindex);
				if (iobject){
					iobject.src = imgarray[imgindex].out.src;
				} else {
					iobject = eval("document.layers['" + callmenu + "']." + "document.layers['inside'].document.images." + imggroup + imgindex);
						if (iobject){
							iobject.src = imgarray[imgindex].out.src;
						}
				}//end netscape first layer image
			}//end netscape layer
		}//end if condition
	}//end if condition
}//end function justSwapOut()

function justSwapOver(callmenu, imgarray, imggroup, imgindex){
	var iobject = eval('document.' + imggroup + imgindex);
	if ((isArray(imgarray))&&(imgarray[imgindex])){
		if (iobject){
			iobject.src = imgarray[imgindex].over.src;
		} else if (document.layers){
			if (DOMObjects[callmenu]){
				iobject = eval("document.layers['" + callmenu + "']." + "document.images." + imggroup + imgindex);
				if (iobject){
					iobject.src = imgarray[imgindex].over.src;
				} else {
					iobject = eval("document.layers['" + callmenu + "']." + "document.layers['inside'].document.images." + imggroup + imgindex);
						if (iobject){
							iobject.src = imgarray[imgindex].over.src;
						}
				}//end Netscape Layer
			}//end netscape layer
		}//end if condition
	}//end if condition
	for (var i = 0; i < imgarray.length; i++){
		//alert(i + " v.s. " + imgindex);
		if(i != imgindex){
			iobject = eval('document.' + imggroup + i);
			if((isArray(imgarray))){
				if (iobject){
					iobject.src = imgarray[i].out.src;
				} else if (document.layers){
					if (DOMObjects[callmenu]){
						iobject = eval("document.layers['" + callmenu + "']." + "document.images." + imggroup + i);
						if (iobject){
							iobject.src = imgarray[i].out.src;
						} else {
							iobject = eval("document.layers['" + callmenu + "']." + "document.layers['inside'].document.images." + imggroup + i);
								if (iobject){
									iobject.src = imgarray[i].out.src;
								}
						}//end netscape latyer
					}//end netscape layer
				}//end if condition
			//ONLY CALL THIS FOR THIS CONDITION
				if(menuHash[imggroup + i]){
					//alert("Going to Delete in SWAPOVER " + imggroup + i + " from HashTable");
					delete menuHash[imggroup + i];
				}//delete this key
			}//ok we now switch				
		}//only revert those that are not the index
	}//clear image array	
}//end function justSwapOut()

function GetLeftOF(zmenu, finalindex, calculatesublevels){
	var sleft = 0;
	var sreal = 0;
	//alert(zmenu);
	//alert(finalindex);
	if (zmenu == "toolbar"){
		//alert('toolbar');
		finalindex = finalindex;
	} else {
		//alert('not toolbar');
		if (!isNaN(zmenu.replace("mbaritem", ""))){
			//alert(parseInt(zmenu.replace("mbaritem", "")));
			sreal = parseInt(zmenu.replace("mbaritem", ""));
			sreal ++;
			finalindex = sreal;
		}//end if condition 
	}//recalculate finalindex
	
	//alert(sreal);

	//if I am a sublevel menu, i.e., not the toolbar then

	if (calculatesublevels){
		//alert(zmenu);
		  if (document.getElementById) {
			//alert("getElementbyID");
			//alert(DOMObjects[zmenu].style.left);
			//alert(parseInt(DOMObjects[zmenu].offsetWidth));
			//alert("Total is left = " + DOMObjects[zmenu].style.left + " + Width " + parseInt(DOMObjects[zmenu].offsetWidth) + " = " + (parseInt(DOMObjects[zmenu].style.left) + parseInt(DOMObjects[zmenu].offsetWidth - xOffset)));
			return parseInt(DOMObjects[zmenu].style.left) + parseInt(DOMObjects[zmenu].offsetWidth) - xOffset - minusleftofmenu;
		  } else if (document.all){    
			//alert("ALL");
			return parseInt(DOMObjects[zmenu].style.left) + DOMObjects[zmenu].clientWidth  - xOffset - minusleftofmenu;
		  } else if (document.layers){    
			//alert("Mozilla Layers");
			//alert(DOMObjects[zmenu].left);
			//alert(DOMObjects[zmenu].clip.width);
			//alert(xOffset);
			return DOMObjects[zmenu].left + DOMObjects[zmenu].clip.width - xOffset - minusleftofmenu;
		  }//
		//alert(DOMObjects[zmenu]);
		//alert(DOMObjects[zmenu].style.left);
		//alert(DOMObjects[zmenu].clientWidth);		
	}//only if second level menus
	
	//alert(DOMElementGetLeft(DOMObjects[zmenu]);
	//alert(DOMElementGetWidth(zmenu));
	//alert(finalindex + " v.s. " + toolimage.length);
	//alert("Is an array " + (isArray(toolimage)));
	//if ((isArray(toolimage))&&(finalindex <= toolimage.length)){
	//SAFARI isArray is False
	if ((isArray(toolimage))&&(finalindex <= toolimage.length)){
		for (var i = 0; i < finalindex; i ++){
			sleft += toolimage[i].width;
			
		}//end now, return length
		//alert(sleft);
		return sleft; 
		
	} else {
		//alert(0);
		return 0;
	}//end condition
}//end function GetLeftOF


function isArray(obj) {
//alert(obj.constructor);
return true;
//SAFARI does not recognize the below code
   if (obj.constructor.toString().indexOf("Array") == -1)
      {return false;}
   else
      {return true;}
}//end function isArray()


function closeMenuContainer(){
	//1)Close Menu First
	closeMenu();
	outtimer = setTimeout('closeMenuTimer()', 500);
}//end function closeMenuContainer

function closeMenuTimer(){
	clearTimeout(outtimer);
	if(outing){
		outing = false;
	//2)LOOP THROUGH EACH ITEM, YEAH!
		for (var i in menuHash){
			//alert("Going to erase and mouseout " + menuHash[i].group + menuHash[i].index);
			if (menuHash[i].otravez){
				//alert("Cannot mouseout " + menuHash[i].group + menuHash[i].index);
				menuHash[i].otravez = false;
			} else {
				//alert("YES mouseout " + menuHash[i].group + menuHash[i].index);
				//alert("array " + menuHash[i].array);
				//alert("caller " + menuHash[i].caller);
				//alert("layer " + menuHash[i].layer);
				//alert(menuHash[i].array);
				//alert((menuHash[i].caller != ""));
				//alert((menuHash[i].group != ""));
				//alert((menuHash[i].index > -1));
				//alert("within bounds " + (menuHash[i].index < menuHash[i].arraylength));
				if((menuHash[i].array)&&(menuHash[i].caller != "")&&(menuHash[i].group != "")&&(menuHash[i].index > -1)&&(menuHash[i].index < menuHash[i].arraylength)){
					//alert("PAST TEST mouseout " + menuHash[i].group + menuHash[i].index);
					justSwapOut(menuHash[i].caller, menuHash[i].array, menuHash[i].group, menuHash[i].index);			
				}//
				//clean out this HASH ITEM
				delete menuHash[i];			
			}//end condition
			//layer, caller, array, group, index
		}//going to detroy the loop?	
	}//do this when closing.
}//end function

