//
//	Copyright © 2003, 2004 Juha Leinonen. All Rights Reserved.
//
var gMenus = new Array();

//----------------------------
// Constructor for menuobj
function menuobj(menuid, isvertical) {
//
//	Creates a new menuobj object.
//
//	Args:
//		menuid		[in] A unique ID string. The same ID is used in functions menulick() and syncmenu().
//		isvertical	[in] An optional boolean that specifies if menu is built vertically, or horizontally.
//
	this.MenuItems = new Array();
	// Properties
	this.menuid = menuid;
	if (isvertical == null) {
		this.isvertical = false;
	} else {
		this.isvertical = isvertical;
	}
	this.classTable = "";
	this.classTd = "";
	this.classA = "";
	
	// Methods
	this.addMenuItem = zaddMenuItem;
	this.getHTML = zGetMenuHTML;
	this.writeHTML = zWriteMenuHTML;
	this.syncmenu = zSyncMenu;
	this.menuindex = gMenus.length;
	this.GetMenuItemByName = zGetMenuByName;
	this.selectedItem = null;
	this.menuWindow = window;
	this.WriteOnlyNewItems = false;
	gMenus[gMenus.length] = this;
}

function zaddMenuItem(itemID, target, href, ImgUnselected, ImgSelected, ImgHover, title) {
//
//	Adds a new menuitem object to the parent menuobj object.
//
//	Args:
//		itemID			[in] A unique ID string. The same ID is used in functions menulick() and syncmenu().
//		target			[in] An optional target frame for the href arg.
//		href			[in] An URL to be navigated when this menuitem is clicked.
//		ImgUnselected	[in] An Image URL.
//		ImgSelected		[in] An Image URL.
//		ImgHover		[in] An optional Image URL. If missing, then ImgSelected is used.
//		title			[in] An optional tooltip and statusbar message.
 	this.MenuItems[this.MenuItems.length] = new menuitem(this, this.MenuItems.length, itemID, target, href, ImgUnselected, ImgSelected, ImgHover, title);
}

function zGetMenuHTML() {
	var str = "";
	if (this.classTable == "") {
		str += '<table style="border-collapse:collapse;">\n';
	} else {
		str += '<table class="' + this.classTable + '">\n';
	}
	if (this.isvertical == false) {
		str += '<tr>\n';
	}  
	for (i = 0; i < this.MenuItems.length; i++) {
		if ((this.WriteOnlyNewItems == true && this.MenuItems[i].written == false) ||
		    this.WriteOnlyNewItems == false){
			if (this.isvertical) {
				str += '<tr>\n' + this.MenuItems[i].getHTML() + '\n</tr>\n';
			} else {
				str += '\n' + this.MenuItems[i].getHTML() + '\n';
			};
			this.MenuItems[i].written = true;
		}
	}
	if (this.isvertical == false) {
  		str += '</tr>\n';
//noteststr += '<td style="padding:0;"><img src="logo_x.gif" border="0"></td></tr>\n'; // JL/051204
	} 
	str += '</table>';
	return str;
}

function zWriteMenuHTML(aDocument) {
	var iDoc;
	if (aDocument == null) {
		iDoc = document;
	} else {	
		iDoc = aDocument;
	}
	iDoc.write(this.getHTML());
}

function zGetMenuByName(itemID) {
//	Returns the specified menuitem
	for (i = 0; i < this.MenuItems.length; i++) {
		if (this.MenuItems[i].itemID == itemID) {
			return (this.MenuItems[i]);
		}
	}
}

function zSyncMenu(index) {
	MenuClick(index);
}

//----------------------------
// Constructor for menu items
function menuitem(parentMenu, index, itemID, target, href, ImgUnselected, ImgSelected, ImgHover, title) {
	// Properties
	this.itemID = itemID;
	this.index = index;
	this.parentMenu = parentMenu;
	this.target = target;	//"catsframe";
	this.href = href;
	this.written = false;
	
	// Remember rollover URLs
	this.testImgUnselected = ImgUnselected;
	this.testImgSelected = ImgSelected;
	if (ImgHover == null) {
		this.testImgHover = ImgSelected;
	} else {
		this.testImgHover = ImgHover;
	}

	// Pre-cache menu rollover images
	if (document.images) {
//		this.ImgUnselected = new Image(); 
//		this.ImgUnselected.src = ImgUnselected;
//		
//		this.ImgSelected = new Image(); 
//		this.ImgSelected.src = ImgSelected;
//
//		this.ImgHover = new Image(); 
//		if (ImgHover == null) {
//			this.ImgHover.src = ImgSelected;
//		} else {
//			this.ImgHover.src = ImgHover;
//		}
		img1 = new Image();
		img2 = new Image();
		img3 = new Image();
		img1.src = this.testImgUnselected;
		img2.src = this.testImgSelected;
		img3.src = this.testImgHover;
	}
	if (title == null) {
		this.title = "";
	} else {
		this.title = title;
	}

	this.selected = false;
	// Methods
	this.getHTML = zCreateHtml;
	this.writeHTML = zWriteHtml;
}

function zWriteHtml() {
	document.write(zCreateHtml());
}

function zCreateHtml() {
	var str = "";
	if (this.parentMenu.classTd == "") {
		str += '<td style="padding:0;">';
	} else {
		str += '<td class="' + this.parentMenu.classTd + '">';
	}

	if (this.parentMenu.classA == "") {
		str +=  '<a href="javascript:zMenuClick(' + this.parentMenu.menuindex + ',' + this.index + ');"';
//		str +=  '<a href="' + this.href + '" target="' + this.target + '"';
	} else {
		str +=  '<a class="' + this.parentMenu.classA + '" href="javascript:zMenuClick(' + this.parentMenu.menuindex + ',' + this.index + ');"';
//		str +=  '<a class="' + this.parentMenu.classA + '" href="' + this.href + '" target="' + this.target + '"';
	}
	str +=  ' onMouseOver="hover(' + this.parentMenu.menuindex + ',' + this.index + ',1,';
	str +=  "'" + this.title + "');return true;";
	str +=  '"';
	str +=  ' onMouseOut="hover(' + this.parentMenu.menuindex + ',' + this.index + ',0);return true;">';
	str +=  '<img id="MenuID' + this.parentMenu.menuindex + '_' + this.index + '" src="' + this.testImgUnselected + '" border="0" alt="' + this.title + '">';
	str +=  '</a></td>';
	return str;
}
//----------------------------
function hover(menuindex, ix, sDir, caption) {
//    This func updates the colors of the Menu item when the mouse hovers over it.
//    Usage:	Internally by func MenuItem(), where a ref to this func is embedded.

	var oIMG = self.document.getElementById("MenuID" + menuindex + "_" + ix);
	try {
		var oMenuItem = gMenus[menuindex].MenuItems[ix];
		if (sDir == 1) {
  			if (oMenuItem.selected) {
				// Hovering over selected
			//	oIMG.src = oMenuItem.ImgHover.src;
				oIMG.src = oMenuItem.testImgHover;
			} else {
				// Hovering over deselected
			//	oIMG.src = oMenuItem.ImgHover.src;
				oIMG.src = oMenuItem.testImgHover;
			};
		} else {
  			if (oMenuItem.selected) {
				// Leaving hover over selected
			//	oIMG.src = oMenuItem.ImgSelected.src;
				oIMG.src = oMenuItem.testImgSelected;
			} else {
				// Leaving hover over deselected
			//	oIMG.src = oMenuItem.ImgUnselected.src;
				oIMG.src = oMenuItem.testImgUnselected;
			};
		}
		zUpdateStatusBar(caption);
	} catch(e) {
	}
}
//----------------------------
function zMenuClick(MenuIndex, MenuItemIndex) {
	try {
		var oItem = gMenus[MenuIndex].MenuItems[MenuItemIndex];
		if (oItem != null) {
			//*** temp solution ***
			try {
  				window.top.frames(oItem.target).navigate(oItem.href); // topmenu
			} catch(e) {
				oItem.parentMenu.menuWindow.frames(oItem.target).navigate(oItem.href); //catmenu, etc
  			}
			//*** -------------- ***
		}
	} catch(e) {
		// For Mozilla and Opera
		window.open(oItem.href,oItem.target);
	}
}
//----------------------------
function menuclick(menuid, itemID) {
//	Programmatic menu click performed by user script
 
	try {
		var oItem = zGetMenuItem(menuid, itemID);
		if (oItem != null) {
			//*** temp solution ***
			try {
  				window.top.frames(oItem.target).navigate(oItem.href); // topmenu
			} catch(e) {
				oItem.parentMenu.menuWindow.frames(oItem.target).navigate(oItem.href); //catmenu, etc
  			}
			//*** -------------- ***
		}
	} catch(e) {
		// For Mozilla and Opera
		window.open(oItem.href,oItem.target);
	}
}
//----------------------------

function syncmenu(menuid, itemID) {
//
//	Synchronize the specified menu with the menuitem that corresponds
//	to the page that has just been loaded/navigated to.
//
	var oItem = zGetMenuItem(menuid, itemID);
	if (oItem != null) {
		if (oItem.selected == false) {
			// Menu item is not selected.
			// This implies we got here due to a history.back
			var oOld = oItem.parentMenu.selectedItem
			if (oOld != null) {
				oOld.selected = false;
				oIMG = oOld.parentMenu.menuWindow.document.getElementById("MenuID" + oOld.parentMenu.menuindex + "_" + oOld.index);
			//	oIMG.src = oOld.ImgUnselected.src;
				oIMG.src = oOld.testImgUnselected;
			}
			oItem.parentMenu.selectedItem = oItem;
			oItem.selected = true;

			var oIMG = oItem.parentMenu.menuWindow.document.getElementById("MenuID" + oItem.parentMenu.menuindex + "_" + oItem.index);
		//	oIMG.src = oItem.ImgSelected.src;
			oIMG.src = oItem.testImgSelected;
		}
	} else {
		// Missing menus. i.e. page was opened without frames,
		// so let's add a link home

		try {
			// Recreate missing frameset jl/051227 
			var kAlbumsBase	= 'picture%20gallery';
			var indexUrl = window.location.href.substring(location.href.lastIndexOf(kAlbumsBase),location.href.lastIndexOf("/") + 1) + 'index.htm';
			WriteCookie("newBodyframeTarget",escape(indexUrl),"","","");
   			window.location.href  = "../../index.html";
		} catch(e) {
			// else as before
			var sTop = 'Page was opened without the proper framework of ';
			sTop += '<a href="http://www.coolcats.se">Aslan & Cesar\'s Place</a>.';
			sTop += "<br><nobr>&copy;&nbsp;Copyright Aslan &amp; Cesar's Place 2003 - 2005. All rights reserved.&nbsp;</nobr>";
			sTop += "<hr>";
			window.document.body.innerHTML = sTop + window.document.body.innerHTML;
		}
		
	}
}
function zGetMenuItem(menuid, itemID) {
//
//	Returns the specified menuitem object, safely.
//	I foresee a problem if menu lies in a sibling iframe, for example.
//
	var iTarget = window;
	var ixFrame = -1;
	//
	//	Scan self window, and a parent windows in the hierarchical path,
	//  including the top-level window.
	//	This implies that the menu object must always be in self, or in a hierarchical parent.
	//
	while (true) {
		try {
			if (zIsGlobalFound(iTarget)) {
				// Frame has the gMenus variable.
				var oMenu = zGetMenu(iTarget.gMenus, menuid);
				if (oMenu != null) {
					// Found wanted menu.
					var oItem = oMenu.GetMenuItemByName(itemID);
					if (oItem != null) {
						return (oItem); // i.e. break
					}
				} else {
				}
			}
		} catch(e) {}

		if (ixFrame == -1) {
			if (zIsTopLevelWindow(iTarget)) {
				// First time in top-level
				ixFrame = iTarget.frames.length;
				if (ixFrame == 0) {
					// Top-level window have already been checked, so let's quit
					// since it has no frames
					break;	// 
				} else {
					var iFrameSet =	iTarget;
				}
			} else {
				iTarget = iTarget.parent; // Up to next frame, or top-level window
			}
		} else {
			if (ixFrame == 0) {
				break;	// 
			} else {
				iTarget = iFrameSet.frames[ixFrame];
				ixFrame = ixFrame - 1;
			}
		}
	}
//	alert("Global - Missing");
}
function zIsGlobalFound(aTarget) {
// Returns True if the wanted variable, gMenus, is found in specified window
//	aTarget	- a frame or top html
	var iTest = null;
	try {
		iTest =	aTarget.gMenus;
		return (true);
	} catch(e) {
	}
}

function zGetMenu(menuArray, menuid) {
// Returns the menu obj named as menuid, if any
	for (i = 0; i < menuArray.length; i++) {
		if (menuArray[i].menuid == menuid) {
			return (menuArray[i]);
		}
	}
}
function zIsTopLevelWindow(target) {
// Returns True if specified target is the top-level Window.
// False implies that target is either a frame which is child to the top-level Window,
// or a frame that is child to another frame.

	return (target.parent == target);
}

function zUpdateStatusBar(caption) {
// Show user caption instead of func-name
	if (caption == null) { caption =  ""};
	window.status = caption;	// Show user caption instead of func-name
}
