/*
 *
 *	Author: Ashutosh Raghuwanshi.
 *	E-Mail: ashutosh@pragya.net.in
 *	URL   : http://pragya.net.in
 *  Notice: You may freely use this script as long as you keep this banner as it is.
 *
 */
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
   try{window.status = "Using JScript version " + @_jscript_version;}catch(exception){}
   @else @*/
   alert("You need a more recent script engine.\nMinimum Internet Explorer 5.0 is required.");
/*@end @*/

function Popup_AddItem(ItemID, Icon, Caption, Shortcut, Action){
var id = ItemID + '_' + this.ItemCount;
	this.ItemList[this.ItemCount++] = new Array(id, Icon, Caption, Shortcut, Action);
}

function Popup_AttachPopup(){
var i=0;
	document.writeln('<table id="' + this.ID + '" border="0" style="position:absolute;visibility:hidden;top:-900px;left:-900px;" oncontextmenu="return false;">');
	document.writeln('<tr><td>');
	document.writeln('	<table border="0" cellpadding="1" cellspacing="2" style="top:5px;left:5px;height:0px;position:absolute;filter:Blur(Add=0,Direction=315,Strength=3);">');
	document.writeln('	<tr><td>');
	document.writeln('		<table id="menu_shadow" border="0" cellpadding="1" cellspacing="2" style="height:0px;width:0px;background-color:#000000;filter:Alpha(Opacity=40, FinishOpacity=0);">');
	document.writeln('		<tr><td></td></tr>');
	document.writeln('		</table>');
	document.writeln('	</td></tr>');
	document.writeln('	</table>');
	document.writeln('	<table id="menu_window" class="AppMenu" cellpadding="2" cellspacing="0" style="top:0px;left:0px;position:absolute;">');
	document.writeln('	<tr><td>');
	document.writeln('		<table border="0" cellpadding="0" cellspacing="0" style="cursor:inherit;color:#000000;">');
	for(i=0; i<this.ItemCount; i++){
		if(this.ItemList[i][2] == "-"){
			document.writeln('	<tr><td height="8" colspan="4"><table width="100%" height="1" cellspacing="1"><tr><td class="AppMenuSeparator"></td></tr></table></td></tr>');
		}
		else{
			document.writeln('	<tr id="' + this.ItemList[i][0] + '" class="AppMenubarItems" onmouseover="MenuControler.mOver(this);" onmouseout="MenuControler.mOut(this);" onmouseup="MenuControler.hide_menu();' + unescape(this.ItemList[i][4]) + '">');
			document.writeln('		<td width="16" valign="absmiddle">' + ((this.ItemList[i][1] == "") ? "" : '<img src="' + this.ItemList[i][1] + '" border="0" width="16" height="16" hspace="2">') + '</td>');
			document.writeln('		<td style="padding:1 7 3 2;">' + this.ItemList[i][2] + '</td>');
			document.writeln('		<td style="padding:1 3 3;">' + this.ItemList[i][3] + '</td>');
			document.writeln('		<td width="16"></td>');
			document.writeln('	</tr>');
		}
	}
	if(this.ItemCount == 0){
		document.writeln('		<tr id="Empty" class="AppMenubarItems" onmouseover="MenuControler.mOver(this);" onmouseout="MenuControler.mOut(this);">');
		document.writeln('			<td width="16" valign="absmiddle"></td>');
		document.writeln('			<td class="AppMenuEmpty" style="padding:1 7 3 2;">(Empty)</td>');
		document.writeln('			<td style="padding:1 3 3;"></td>');
		document.writeln('			<td width="16"></td>');
		document.writeln('		</tr>');
	}
	document.writeln('		</table>');
	document.writeln('	</td></tr></table>');
	document.writeln('</td></tr></table>');
}

function Popup(PopupID){
	this.ID	= (((Popup.arguments.length >= 1) ? Popup.arguments[0] : "mnu") + "_" + this.ItemCount + "_" + 'Popup');
	this.ItemList	= new Array();
	this.ItemCount	= 0;
	this.Add	= Popup_AddItem;
	this.Attach	= Popup_AttachPopup;
}

/*
 *		Start of Class "MenuBar".
 */

function MenuBar_AddPopup(ID, Caption, PopupObject){
var PopObj = PopupObject;
	PopObj.ID = (this.ID + "_" + ID + "_" + this.PopupCount + "_" + 'Popup');
	this.PopupList[this.PopupCount] = new Array(this.ID + "_" + ID + "_" + this.PopupCount, Caption, PopObj);
	this.PopupCount++;
}

function MenuBar_AttachMenuBar(){
var i=0;
	document.writeln('<table id="' + this.ID + '" class="AppMenubar" width="100%" border="0" cellspacing="0"><tr>');
	document.writeln('<td class="AppMenubarBorder" style="border-width:0 0 2;padding:0 0 1;">');
	document.writeln('	<table border="0" cellpadding="0" cellspacing="0" height="20" style="cursor:inherit;padding:0 5 0;"><tr>');
	for(i=0; i<this.PopupCount; i++)
		document.writeln('	<td id="' + (this.PopupList[i][0]) + '" class="AppMenubarItems" onmouseover="MenuControler.mOver(this, \'' + this.ID + '\', ' + i + ');" onmouseout="MenuControler.mOut(this, \'' + this.ID + '\', ' + i + ');" onmousedown="MenuControler.show_menu(\'' + this.ID + '\', ' + i + ');">' + this.PopupList[i][1] + '</td>');
	document.writeln('		<td width="100%" align="right"></td>');
	document.writeln('	</tr></table>');
	document.writeln('</td></tr></table>');

	for(i=0; i<this.PopupCount; i++)
		this.PopupList[i][2].Attach();
}

function MenuBar(MenuBarID){
	this.ID	= MenuBarID;
	this.PopupList	= new Array();
	this.PopupCount	= 0;
	this.Add	= MenuBar_AddPopup;
	this.Attach	= MenuBar_AttachMenuBar;
}

/*
 *		Start of Class "MenuCtrl".
 */

function MenuCtrl_hide_menu(){
var ActivePupupIndex = MenuControler.ActiveMenuIndex[MenuControler.ActiveMenuBarIndex];
var PopupID = MenuControler.MenuBarList[MenuControler.ActiveMenuBarIndex].PopupList[ActivePupupIndex][0];
	if(event.type == "mousedown"){
		eval('var PopupObject=document.all.' + PopupID + '_Popup;');
		if(PopupObject.contains(event.srcElement)) return;
	}
	if(ActivePupupIndex != -1){
		eval('document.all.' + PopupID + '_Popup.style.visibility = "hidden";');
		eval('document.all.' + PopupID + '.style.backgroundColor="Transparent";');
		eval('document.all.' + PopupID + '.style.color="'+MenuTextColor+'";');
	}
	MenuControler.ActiveMenuIndex[MenuControler.ActiveMenuBarIndex] = -1;

	document.onmousedown=null;
}

function MenuCtrl_show_menu(MenuID, Index){
var PupupID, i;

	for(i=0; i<this.MenuBarCount; i++)
		if(this.MenuBarList[i].ID == MenuID) this.ActiveMenuBarIndex = i;
	PupupID = this.MenuBarList[this.ActiveMenuBarIndex].PopupList[Index][0] + "_Popup";
	if(this.ActiveMenuIndex[this.ActiveMenuBarIndex] != -1) this.hide_menu();
	this.ActiveMenuIndex[this.ActiveMenuBarIndex] = Index;
	eval('document.all.' + PupupID + '.style.left = (event.clientX - event.offsetX) - ((this.Environment == "HTA") ? 0 : 1);');
	eval('document.all.' + PupupID + '.style.top = (event.clientY - event.offsetY  + document.all.' + this.MenuBarList[this.ActiveMenuBarIndex].ID + '.offsetHeight) - ((this.Environment == "HTA") ? 3 : 4);');

	eval('document.all.' + PupupID + '.all.menu_shadow.style.width = document.all.' + PupupID + '.all.menu_window.offsetWidth - 5;');
	eval('document.all.' + PupupID + '.all.menu_shadow.style.height = document.all.' + PupupID + '.all.menu_window.offsetHeight - 5;');
	eval('document.all.' + PupupID + '.style.visibility = "visible";');
	self.event.cancelBubble=true;

	document.onmousedown=this.hide_menu;
}

function MenuCtrl_mOver(Src, MenuID, Index){
var i;
	if(Src.contains(event.fromElement)) return;
	for(i=0; i<this.MenuBarCount; i++)
		if(this.MenuBarList[i].ID == MenuID) this.ActiveMenuBarIndex = i;
	Src.style.backgroundColor=MenuHighlightColor;
	Src.style.color=MenuHighlightTextColor;
	if(Src.tagName == "TD")
		if(this.ActiveMenuIndex[this.ActiveMenuBarIndex] != -1)
			if(eval("document.all." + Src.id + "_Popup.style.visibility == 'hidden'"))
				this.show_menu(MenuID, Index);
}

function MenuCtrl_mOut(Src, MenuID, Index){
var ActivePupupIndex, i;

	for(i=0; i<this.MenuBarCount; i++)
		if(this.MenuBarList[i].ID == MenuID) this.ActiveMenuBarIndex = i;
	ActivePupupIndex = this.ActiveMenuIndex[this.ActiveMenuBarIndex];
	if(Src.contains(event.toElement)) return;
	if(Src.tagName == "TD"){
		if((ActivePupupIndex == -1)){
			Src.style.backgroundColor="Transparent";
			Src.style.color=MenuTextColor;
		}
		else if(event.toElement != null && event.toElement.tagName == "TD" && event.toElement.id != ""){
			for(i=0; i<this.MenuBarList[this.ActiveMenuBarIndex].PopupCount; i++){
				if(this.MenuBarList[this.ActiveMenuBarIndex].PopupList[ActivePupupIndex][0] != event.toElement.id){
					Src.style.backgroundColor="Transparent";
					Src.style.color=MenuTextColor;
					break;
				}
			}
		}
	}
	else{
		Src.style.backgroundColor="Transparent";
		Src.style.color=MenuTextColor;
	}
}

function MenuCtrl_AddMenu(MenuBarObject){
	this.MenuBarList[this.MenuBarCount] = MenuBarObject;
	this.ActiveMenuIndex[this.MenuBarCount++] = -1;
	MenuBarObject.Attach();
}

function MenuCtrl_AddContextMenu(ID, PopupObject){
	PopupObject.ID = ID;
	this.MenuBarList[0].Add(ID, "", PopFile);
}

function MenuCtrl(){
	this.MenuBarList		= new Array();
	this.MenuBarCount		= 1;
	this.ActiveMenuBarIndex	= -1;
	this.ActiveMenuIndex	= new Array();
	this.Environment		= "HTML";
	this.AddMenu			= MenuCtrl_AddMenu;
	this.AddContextMenu		= MenuCtrl_AddContextMenu;
	this.mOver				= MenuCtrl_mOver;
	this.mOut				= MenuCtrl_mOut;
	this.show_menu			= MenuCtrl_show_menu;
	this.hide_menu			= MenuCtrl_hide_menu;


var ContextMenuList	= new MenuBar("ContextMenu");;
	this.MenuBarList[0] = ContextMenuList;
	this.ActiveMenuIndex[0] = -1;
	if((location.pathname.length-3)==location.pathname.lastIndexOf('hta')) this.Environment = "HTA";

}

/*
 *		Create the one and only global Object "MenuControler".
 */

var MenuControler = new MenuCtrl();

