var Instinct_Menu__bRootVisible = null;		//root menu whose menu items are visible

nStatus = 0;
//- Instinct_Menu -//
function Instinct_Menu(oInstinct, oMenubar, cName) {
//- Model - Initialize -//
	this.name = cName;
	this.menubar = oMenubar;

//- Model - Initialize -//
	this.kDropDirection_Down = 0;
	this.kDropDirection_Right = 1;

	//+ sub-menu options
	this.hasBeenMoved = false;								//has the menu layers been positioned?
	this.openSubMenu = null;								//sub-sub-menu visible

	//+ callback
	this.onMouseOver_Delegate_Hash = new Array();
	this.onMouseOut_Delegate_Hash = new Array();

	//+ default values for menu items
	this.menuLayerName = 'mb0_' + cName;
	this.menuWidth = 0;
	this.menuHeight = 0;
	this.bodyWidth = this.menubar.bodyWidth;
	this.left = 0;
	this.top = 0;
	this.menuItemWidth = this.menubar.menuItemWidth;
	this.menuItemHeight = this.menubar.menuItemHeight;
	this.menuItemOffset_Left = this.menubar.menuItemOffset_Left;
	this.menuItemOffset_Top = this.menubar.menuItemOffset_Top;
	this.menuItemOffset_Right = this.menubar.menuItemOffset_Right;
	this.menuItemOffset_Bottom = this.menubar.menuItemOffset_Bottom;
	this.menuItemOffset_Inner = this.menubar.menuItemOffset_Inner;
	this.menuDropDirection = this.menubar.menuDropDirection;
	this.menuContainerBgColor = this.menubar.menuContainerBgColor;
	this.cssForeId = this.menubar.cssForeId;
	this.zIndex = this.menubar.zIndex;
	
	this.imgAnchor = null;
	this.isMenuBufferOpen = false;
	this.menuBuffer = '';

//- Model - Link -//
	this.addMenuItem = addMenuItem;
	this.generateMenu = generateMenu;
	this.onLoad = onLoad;

	this.openMenuBuffer = openMenuBuffer;
	this.closeMenuBuffer = closeMenuBuffer;

	this.moveTo = moveTo;
	this.show = show;
	this.hide = hide;
	this.isRoot = isRoot;

//- Mode - Implementation -//
	//- Implementation : Menu -//
	function openMenuBuffer() {
		if (this.isMenuBufferOpen == true) {
			return;
		}
		this.isMenuBufferOpen = true;
		this.menuWidth = this.menuItemWidth + this.menuItemOffset_Left + this.menuItemOffset_Right;
		this.menuHeight = this.menuItemOffset_Top + this.menuItemOffset_Bottom;
		//this.menuBuffer = '<table border="1" width="' + this.menuWidth + '" cellpadding="1" cellspacing="0">';
		if (this.menuItemOffset_Top == 0) {
			this.menuBuffer = '';
		} else {
			this.menuBuffer = '<img src="/HTMLAYER_/Instinct/Tofu.gif" border="0" width="1" height="' + this.menuItemOffset_Top + '"><br>';
		}
	}
	function closeMenuBuffer() {
		if (this.isMenuBufferOpen == false) {
			return;
		}
		this.isMenuBufferOpen = false;
		if (this.menuItemOffset_Bottom > this.menuItemOffset_Inner) {
			this.menuBuffer += '<img src="/HTMLAYER_/Instinct/Tofu.gif" border="0" width="1" height="' + (this.menuItemOffset_Bottom - this.menuItemOffset_Inner) + '"><br>';
		}
		//this.menuBuffer += '</table>';
		this.menuHeight -= this.menuItemOffset_Inner;
	}

	function addMenuItem(cTitle, cImageName, cImage_O, cImage_X, cUrlAddress, cUrlTarget) {
		if (this.isMenuBufferOpen == false) {
			this.openMenuBuffer();
		}
		this.menuHeight += this.menuItemHeight + this.menuItemOffset_Inner;
		if (this.menuItemOffset_Left > 0) {
			this.menuBuffer += '<img src="/HTMLAYER_/Instinct/Tofu.gif" border="0" width="' + this.menuItemOffset_Left + '" height="1">';
		}
		this.menuBuffer += '<a href="' + cUrlAddress + '" target="' + cUrlTarget + '" style="' + this.cssForeId + '">'
		+ '<img name="' + cImageName + '" src="' + cImage_O + '" border="0" '
		+ 'onmouseover="Instinct_Image__ToggleImageSrc(\'' + cImageName + '\', \'' + cImage_O + '\', \'' + cImage_X + '\')" '
		+ 'onmouseout="Instinct_Image__ToggleImageSrc(\'' + cImageName + '\', \'' + cImage_X + '\', \'' + cImage_O + '\')"></a>';
		if (this.menuItemOffset_Right > 0) {
			this.menuBuffer += '<img src="/HTMLAYER_/Instinct/Tofu.gif" border="0" width="' + this.menuItemOffset_Right + '" height="1">';
		}
		this.menuBuffer += '<br>';
		if (this.menuItemOffset_Inner > 0) {
			this.menuBuffer += '<img src="/HTMLAYER_/Instinct/Tofu.gif" border="0" width="1" height="' + this.menuItemOffset_Inner + '"><br>';
		}
	}

	//- Implementation : Method -//
	function generateMenu() {
		this.closeMenuBuffer();
		this.menuLayer = new Instinct_Layer(oInstinct, this.menuLayerName);
		this.menuLayer.width = this.menuWidth;
		this.menuLayer.height = this.menuHeight;
		this.menuLayer.clearBuffer();
		this.menuLayer.appendBuffer(this.menuBuffer);	
		this.menuLayer.streamLayer();
		this.hide();
		this.isGenerated = true;
	}
	function moveTo(nLeft, nTop) {
		var nRightEdge = (oInstinct.Detect.getWindowWidth() + this.bodyWidth) / 2;
		if ((nLeft + this.menuItemOffset_Left + this.menuWidth + this.menuItemOffset_Right) > nRightEdge) {
			this.left = nRightEdge - (this.menuItemOffset_Left + this.menuWidth + this.menuItemOffset_Right);
		} else {
			this.left = nLeft;
		}
		this.top = nTop;
		this.menuLayer.moveTo(this.left, this.top);
	}

	function isRoot() {
		return(true);
	}

	function onLoad() {
		var menuLayer = this.menuLayer;
		menuLayer.onLoad();
		menuLayer.setZIndex(this.zIndex);
		menuLayer.oLayer.parentObject = this;
		menuLayer.setBgColor(this.menuContainerBgColor);
		//+ define event handlers
		menuLayer.setOnMouseOver(onMouseOver);
		menuLayer.setOnMouseOut(onMouseOut);

		if (this.imgAnchor.length >= 0) {
			var oImage = new Instinct_Image(oInstinct, this.imgAnchor);
			this.left = oImage.getX();
			this.top = oImage.getY() + oImage.getHeight();
			oImage = null;
		}
		this.moveTo(this.left, this.top);
		this.show();
	}

	function show() {
		this.menuLayer.show();
		Instinct_Menubar__bRootVisible = this.menubar;
		Instinct_Menu__bRootVisible = this;

		if (navigator.appName != 'Microsoft Internet Explorer') return ;
		if (document.getElementById("InstinctMenu_iFrame")) {
			document.getElementById('InstinctMenu_iFrame').style.display='';
			var n = this.menuLayer.getWidth() - 7;
				document.getElementById('InstinctMenu_iFrame').style.width=(n < 1 ? 1 : n);
			n = this.menuLayer.getHeight() - 1;
				document.getElementById('InstinctMenu_iFrame').style.height=(n < 1 ? 1 : n);
			document.getElementById('InstinctMenu_iFrame').style.top=this.menuLayer.getTop();
			document.getElementById('InstinctMenu_iFrame').style.left=this.menuLayer.getLeft();
			document.getElementById('InstinctMenu_iFrame').style.top=this.menuLayer.getTop();
		}
	}

	function hide() {
		this.menuLayer.hide();
		Instinct_Menubar__bRootVisible = null;
		Instinct_Menu__bRootVisible = null;

		if (navigator.appName != 'Microsoft Internet Explorer') return ;
		if (!document.getElementById("InstinctMenu_iFrame")) {
			var iFrame = document.createElement("<IFRAME id=\"InstinctMenu_iFrame\" style=\"filter:progid:DXImageTransform.Microsoft.Alpha(opacity=10);width:0px;height:0px;position:absolute;top:150;left:-4000;\" frameborder=0 scrolling=no marginwidth=0 src=\"\" marginheight=0></iframe>"); 
			document.body.appendChild(iFrame);
		}
		document.getElementById('InstinctMenu_iFrame').style.display='none';
		document.getElementById('InstinctMenu_iFrame').position='absolute;';
	}

	function onMouseOver() {
		Instinct_Menu__onMouseOver(this.parentObject);
	}

	function onMouseOut() {
		Instinct_Menu__onMouseOut(this.parentObject);
	}
}

//- static methods -//
function Instinct_Menu_Item__hideSubMenuAll(menuLayerName) {
	var oParent = Instinct_Layer__getLayer(menuLayerName);
	if (oParent != null) {
		oParent = oParent.parentObject;
	}
	var menuBar = oParent;
	while (menuBar != null) {
		menuBar.hide();
		menuBar = menuBar.menuBarParent;
	}

	if (Instinct_Menubar__bRootVisible == null) {
		//+ send onMouseOut event
		var onMouseOut_Delegate_Hash = oParent.menubar.onMouseOut_Delegate_Hash;
		for(var hMouseOut in onMouseOut_Delegate_Hash) {
			onMouseOut_Delegate_Hash[hMouseOut](null);
		}
	}
}

function Instinct_Menu__onMouseOver(oParent) {
	if (oInstinct.LoadComplete == false) {
		return;
	}
	if ((Instinct_Menu__bRootVisible != null) && (oParent.name == Instinct_Menu__bRootVisible.name)) {
		clearTimeout(Instinct_Menubar__hTimer);
	} else if (Instinct_Menu__bRootVisible != null) {
		clearTimeout(Instinct_Menubar__hTimer);
		Instinct_Menu_Item__hideSubMenuAll(Instinct_Menu__bRootVisible.menuLayerName);
	}

	if (Instinct_Menubar__bRootVisible == null) {
		//+ send onMouseOver event
		var onMouseOver_Delegate_Hash = oParent.menubar.onMouseOver_Delegate_Hash;
		for(var hMouseOver in onMouseOver_Delegate_Hash) {
			onMouseOver_Delegate_Hash[hMouseOver](null);
		}
	}
		
	oParent.show();
}

function Instinct_Menu__onMouseOut(oParent) {
	if (oInstinct.LoadComplete == false) {
		return;
	}
	Instinct_Menubar__hTimer = setTimeout('Instinct_Menu_Item__hideSubMenuAll("' + oParent.menuLayerName + '")', 30);
}
