//Menu Manager functions
var aMM_Menu = new Array();

//Other definitions
//Object.prototype.clone = MM_clone;

function MenuManager(sID)
{
	//Menu Properties
	this.id = sID;
	this.classMain = '';
	this.classPad = '';
	this.buttons = new Array();

	//Methods
	this.addButton = addButton;
	this.show = show;
	this.setSelectedLink = setSelectedLink;
	this.getButtonID = getButtonID;
}

function addButton(oMenuButton)
{
	this.buttons[this.buttons.length] = oMenuButton;
}

function show()
{
	var sHTMLCode =
		'<table cellspacing="0" ' +
		'cellpadding="0" border="0" ' +
		'class="' + this.classMain + '">';
	var sHTMLMouseOver;
	var sHTMLMouseOut;
	var sHTMLBorderLeft;
	var sHTMLBorderRight;
	var oTmp = null;

	for(var i = 0; i < this.buttons.length; i++)
	{
		sHTMLMouseOver = '';
		sHTMLMouseOut = '';
		sHTMLBorderLeft = '';
		sHTMLBorderRight = '';

		this.buttons[i].setSelected();

		if(this.buttons[i].hasSubMenu())
		{
			sHTMLMouseOver = 
				'MM_showMenu(aMM_Menu[' +
				aMM_Menu.length + '], 0, ' + 
				'30' + 
				', null, ' +
				'\'' + this.buttons[i].getMainID(this.id, i) + '\');';

			sHTMLMouseOut =
				'MM_startTimeout();';

			oTmp = this.buttons[i].submenu.createMenu(this.id);

			aMM_Menu[aMM_Menu.length] = oTmp;
		}

		sHTMLMouseOver += 
			' onMouseOver="' + sHTMLMouseOver +
			'MM_setClass(\'' + this.id + '\', '
			+ i + ', \'' +
			this.buttons[i].classLeftHL + '\', \'' +
			this.buttons[i].classMainHL + '\', \'' +
			this.buttons[i].classRightHL + '\', \'' +
			this.buttons[i].classFontHL + '\');" ';

		sHTMLMouseOut += 
			' onMouseOut="' + sHTMLMouseOut +
			'MM_setClass(\'' + this.id + '\', '
			+ i + ', \'' +
			this.buttons[i].classLeft + '\', \'' +
			this.buttons[i].classMain + '\', \'' +
			this.buttons[i].classRight + '\', \'' +
			this.buttons[i].classFont + '\');" ';

		if(this.buttons[i].classLeft)
			sHTMLBorderLeft =
				'<td class="' + this.buttons[i].classLeft + '" ' +
				'id="menu_' + this.id + '_left_id_' + i + '"' +
				sHTMLMouseOver +
				sHTMLMouseOut +
				'>&nbsp;</td>';

		if(this.buttons[i].classRight)
			sHTMLBorderRight =
				'<td class="' + this.buttons[i].classRight + '" ' +
				'id="menu_' + this.id + '_right_id_' + i + '"' +
				sHTMLMouseOver +
				sHTMLMouseOut +
				'>&nbsp;</td>';

		if(this.classPad && i < this.buttons.length - 1)
			sHTMLBorderRight +=
				'<td class="' + this.classPad + '"></td>';

		sHTMLCode +=
			sHTMLBorderLeft +
			'<td class="' + this.buttons[i].classMain + '" ' +
			'id="menu_' + this.id + '_main_id_' + i + '"' +
			sHTMLMouseOver +
			sHTMLMouseOut +
			'>' +
			this.buttons[i].getLink(this.id, i) +
			'</td>' +
			sHTMLBorderRight;
	}

	if(oTmp)
		oTmp.writeMenus();
	
	sHTMLCode += '</table>';

	//alert(sHTMLCode);
	document.write(sHTMLCode);
}

function setSelectedLink()
{
	var bSelected = false;

	for(var i = 0; i < this.buttons.length; i++)
	{
		if(	MM_getPageName(this.buttons[i].link) ==
			MM_getPageName(window.location.href) &&
			!this.buttons[i].blank)
			bSelected = true;
		else
		{
			if(this.buttons[i].hasSubMenu())
				for(var j = 0; j < this.buttons[i].submenu.items.length; j++)
					if(	MM_getPageName(this.buttons[i].submenu.items[j].link) ==
						MM_getPageName(window.location.href) &&
						!this.buttons[i].submenu.items[j].blank)
					{
						bSelected = true;
						break;
					}

		}

		if(bSelected)
		{
			this.buttons[i].selected = true;
			break;
		}
	}
}

function getButtonID(oButton, sType)
{
	var sID = -1;

	for(var i = 0; i < this.buttons.length; i++)
		if(oButton == this.buttons[i])
		{
			sID = i;
			break;
		}

	if(sType && sID != -1)
		sID = 'menu_' + this.id + '_' + sType + '_id_' + sID;

	return sID;
}
//Menu Button functions
function MenuButton(sLabel, sLink)
{
	//Properties
	this.label = sLabel;
	this.link = sLink;
	this.classLeft = '';
	this.classMain = '';
	this.classRight = '';
	this.classFont = '';
	this.classLeftHL = '';
	this.classMainHL = '';
	this.classRightHL = '';
	this.classFontHL = '';
	this.blank = false;
	this.js = false;
	this.submenu = null;
	this.selected = false;

	//Methods
	this.addSubMenu = addSubMenu;
	this.createSubMenu = createSubMenu;
	this.getLink = getLink;
	this.getMainID = getMainID;
	this.hasSubMenu = hasSubMenu;
	this.setSelected = setSelected;
}

function addSubMenu(oSubMenu)
{
	this.submenu = oSubMenu;
}

function createSubMenu()
{
	this.addSubMenu(new SubMenu());

	return this.submenu;
}

function getLink(sMenuID, sButtonID)
{
	var sLink = 
		'<font class="' + this.classFont + '" ' +
		'id="menu_' + sMenuID + '_font_id_' + sButtonID + '"' +
		'>' +
		(this.label ? this.label : '') +
		'</font>';

	if(this.link)
		sLink =
			'<a href="' + 
			(this.js ? 'javascript:' : '') +
			this.link + 
			(this.blank ? ' target="_blank"' : '') +
			'">' + sLink + '</a>';

	return sLink;	
}

function getMainID(sMenuID, sButtonID)
{
	var sMainID = '';

	if(this.classLeft)
		sMainID = 'menu_' + sMenuID + '_left_id_' + sButtonID;
	else
		sMainID = 'menu_' + sMenuID + '_main_id_' + sButtonID;

	return sMainID;
}

function hasSubMenu()
{
	return this.submenu && this.submenu.items.length > 0;
}

function setSelected()
{
	if(this.selected)
	{
		if(this.classLeftHL)
			this.classLeft = this.classLeftHL;
		if(this.classMainHL)
			this.classMain = this.classMainHL;
		if(this.classRightHL)
			this.classRight = this.classRightHL;
		if(this.classFontHL)
			this.classFont = this.classFontHL;
	}
}

//Class Submenu
function SubMenu()
{
	//Properties
	this.width = 0;
	this.height = 0;
	this.font = '';
	this.fontsize = 12;
	this.fontcolor = '';
	this.fontcolorhl = '';
	this.bgcolor = '';
	this.bgcolorhl = '';
	this.itempadding = 0;
	this.itemspacing = 0;
	this.timeout = 0;
	this.submenuxoffset = 0;
	this.submenuyoffset = 0;
	this.bgopaque = false;
	this.vertical = true;
	this.weight = 'normal';
	this.items = new Array();

	//Methods
	this.addSubMenuLink = addSubMenuLink;
	this.addItem = addItem;
	this.removeAllItems = removeAllItems;
	this.createMenu = createMenu;
}

function addSubMenuLink(oSubMenuLink)
{
	this.items[this.items.length] = oSubMenuLink;
}

function addItem(sLabel, sLink, bBlank, bJS)
{
	this.addSubMenuLink(new SubMenuLink(sLabel, sLink, bBlank, bJS));
}

function createMenu(sID)
{
	var oMenu = new Menu(
			sID,
			this.width,
			this.height,
			this.font,
			this.fontsize,
			this.fontcolor,
			this.fontcolorhl,
			this.bgcolor,
			this.bgcolorhl,
			'left',
			'middle',	
			this.itempadding,
			this.itemspacing,
			this.timeout,
			this.submenuxoffset,
			this.submenuyoffset,
			true,
			this.bgopaque,
			this.vertical,
			0,
			false,
			false,
			this.weight);

	oMenu.hideOnMouseOut = true;
	oMenu.menuBorder = 1;

	for(var i = 0; i < this.items.length; i++)
		oMenu.addMenuItem(this.items[i].getLabel(), this.items[i].getSubMenuLink());

	return oMenu;
}

function removeAllItems()
{
	this.items = new Array();
}

//Class SubMenuLink
function SubMenuLink(sLabel, sLink, bBlank, bJS)
{
	//Properties
	this.label = sLabel;
	this.link = sLink;
	this.blank = bBlank;
	this.js = bJS;

	//Methods
	this.getLabel = getLabel;
	this.getSubMenuLink = getSubMenuLink;
}

function getLabel()
{
	return this.label.replace(/ /g, '&nbsp;');
}

function getSubMenuLink()
{
	var sLink = '';

	if(this.link)
	{
		if(this.blank)
			sLink = 'window.open(\'' + this.link + '\')';
		else if(this.js)
			sLink = this.link;
		else
			sLink = 'location=\'' + this.link + '\'';
	}

	return sLink;
}
//Static functions
function MM_getPageName(sPageAll)
{
	var sPage = sPageAll ? sPageAll : '';

	//if(sPage.lastIndexOf('?') != -1)
	//	sPage = sPage.substr(0, sPage.lastIndexOf('?'));

	sPage = sPage.substr(sPage.lastIndexOf('/') + 1);

	if(!sPage)
		sPage = 'index.php';

	return sPage;
}

function MM_setClass(sMenuID, sButtonID, sClassLeft, sClassMain, sClassRight, sClassFont)
{
	MM_changeClass('menu_' + sMenuID + '_left_id_' + sButtonID, sClassLeft);
	MM_changeClass('menu_' + sMenuID + '_main_id_' + sButtonID, sClassMain);
	MM_changeClass('menu_' + sMenuID + '_right_id_' + sButtonID, sClassRight);
	MM_changeClass('menu_' + sMenuID + '_font_id_' + sButtonID, sClassFont);
}

function MM_changeClass(sID, sClass)
{
	var oTmp = FIND(sID);

	if(oTmp && sClass)
		oTmp.className = sClass;
}

function MM_clone()
{
	var objectClone = new this.constructor();

	for(var property in this)
		if(typeof this[property] == 'object' && this[property])
			objectClone[property] = this[property].clone();
		else
			objectClone[property] = this[property];
	
	return objectClone;
}
