// VIETSON DYNAMIC CONTEXT MENU 1.0
// This web component is designed for Programmers.
// Browser: MSIE 4.0 or greater.
// Created by Long Ngo, 12/10/2000
// Email: LongNgo78@hotmail.com
// URL: http://www.VietSon.com

var _MENU_SKIN = 1;
var _DISPLAY_URL = 0;
var _MENU_DEMO = 0;
var _CM_IMG_DIR = '';
function ContextMenu(CMID)
{
	this.ID = CMID;
	this.aActiveX = new Array();
	this.Y = 0;
	this.Width = 160;
	this.aActivePID = new Array();	
	this.aItem = new Array();
	this.AddItem = CMAddItem;
	this.Create = CMCreate;
	this.Show = CMShow;
	this.Hide = CMHide;	
	this.HideAllSub = CMHideAllSub;
	this.HideAll = CMHideAll;	
	this.CountSubMenu = CMCountSubMenu;
	
}
function CMCountSubMenu(PID)
{
	var nCount = 0;
	var Len = this.aItem.length;
	for (var i=0; i<Len; i++)		
		if (this.aItem[i].PID == PID)
			nCount++;	
	return nCount;
}
function CMShow(X, Y, PID)
{	
	if (PID == this.aActivePID[this.aActivePID.length-1]) return;
	var DivName = 'CM_'+ this.ID + '_' + PID;
	if (document.all[DivName].style.visibility == 'visible') return;
	
	this.aActivePID[this.aActivePID.length] = PID;		
	var R = document.body.clientWidth - event.clientX
	var B = document.body.clientHeight - event.clientY
	var W = document.all[DivName].offsetWidth;
	var H = document.all[DivName].offsetHeight;
	if (R<W) X -= W*(this.aActivePID.length); 
	if (B<H) Y -= H;
	this.aActiveX[this.aActiveX.length] = X;	
	this.Y = Y;	
		
	document.all[DivName].style.pixelLeft = X;
	document.all[DivName].style.pixelTop = Y;
	if (_MENU_DEMO > 0)
		DivShowDemo(DivName);
	else
		document.all[DivName].style.visibility = 'visible';	
}
function CMHide(PID)
{
	var DivName = 'CM_'+ this.ID + '_' + PID;
	if (document.all[DivName].style.visibility != 'visible') return;
	this.aActiveX.length--;
	for (var i=0; i<this.aActivePID.length; i++)
		if (this.aActivePID[i] == PID)
		{
			for (var j=i; j<this.aActivePID.length-1; j++)
				this.aActivePID[j] = this.aActivePID[j+1];
			this.aActivePID.length--;
			break;
		}	
	document.all[DivName].style.visibility = 'hidden';	
	var Len = this.aItem.length;		
	for (var i=0; i<Len; i++)
		if ((this.aItem[i].PID == PID) && (this.aItem[i].ID != PID))
		{		
			if (this.CountSubMenu(this.aItem[i].ID) > 0)
				this.Hide(this.aItem[i].ID);
		}
}
function CMHideAllSub(PID)
{
	var Len = this.aItem.length;		
	for (var i=0; i<Len; i++)
		if ((this.aItem[i].PID == PID) && (this.aItem[i].ID != PID))
		{		
			if (this.CountSubMenu(this.aItem[i].ID) > 0)
				this.Hide(this.aItem[i].ID);
		}
}
function CMHideAll()
{
	var Len = this.aItem.length;		
	for (var i=0; i<Len; i++)
		if (this.CountSubMenu(this.aItem[i].ID) > 0)
			this.Hide(this.aItem[i].ID);
}

function CMAddItem(PID, ID, Title, OnClick, ImgName)
{
	var Len = this.aItem.length;
	this.aItem[Len] = new Array();
	this.aItem[Len].ID = ID;
	this.aItem[Len].PID = PID;
	this.aItem[Len].Title = Title;
	this.aItem[Len].OnClick = OnClick;
	this.aItem[Len].ImgName = ImgName;
}
function CMCreate(MyPID, bMain)
{
	var CMID = this.ID;	
	var _TAB = "&nbsp;&nbsp;&nbsp;";
	var aSubMenuID = new Array();
	var DivName = 'CM_'+ CMID + '_' + MyPID;
	
	document.write('<div id=CM_'+ CMID + '_' + MyPID +' class=CMSkin0>');
	document.write('<Table class=CMTable width=100% cellpadding=1 cellspacing=0>');
	var Index = 0;
	for (var i=0; i<this.aItem.length; i++) if(this.aItem[i].PID == MyPID)
	{		
		var cArrow = '';
		var OnMouseOverFunc = this.ID+'.HideAllSub('+MyPID+');';
		var OnMouseClickFunc = this.ID+'.HideAll();';
		if ( (this.aItem[i].ID != MyPID) && this.CountSubMenu(this.aItem[i].ID)>0)
		{			
			aSubMenuID[aSubMenuID.length] = this.aItem[i].ID;
			cArrow = '<Font face=Webdings>4';
			var X = this.ID+ '.aActiveX['+ this.ID +'.aActiveX.length-1]+' + this.ID + '.Width';
			var Y = 'document.all[\''+ DivName +'\'].style.pixelTop + '+ Index +'*20';
			OnMouseOverFunc += this.ID + '.Show('+ X +', '+ Y +','+this.aItem[i].ID+');';
			OnMouseClickFunc = '';
		}
		else
		{
			OnMouseClickFunc += this.aItem[i].OnClick;
		}
		if (this.aItem[i].Title =='_')
		{
			document.write('<TR class=CMItem>');
				document.write('<TD width=100% colspan=3 height=5 style="cursor:default"><HR></TD>');
		}
		else
		{			
			document.write('<TR class=CMItem onmouseover="CMHiLightItem(this);'+OnMouseOverFunc+'" onmouseout=CMLoLightItem(this)  OnClick="'+OnMouseClickFunc+'">');
			if (this.aItem[i].ImgName != null)
				document.write('<TD width=20><Img src='+_CM_IMG_DIR+this.aItem[i].ImgName+'></TD>');
			else
				document.write('<TD width=20>'+_TAB+'</TD>');
				
			document.write('<TD width=100% height=20 nowrap>'+this.aItem[i].Title+'</TD>');
			document.write('<TD>'+cArrow+'</TD>');
		}
		document.write('</TR>');
		Index++;
	}
	
	document.write('</Table>');
	document.write('</div>');
	
	if(aSubMenuID.length == 0)	
		return;
	else
		for (var i=0; i<aSubMenuID.length; i++)
			this.Create(aSubMenuID[i], false);		
}

function CMHiLightItem(Obj)
{
	Obj.style.backgroundColor = 'Navy';
	Obj.style.color = 'White';
}
function CMLoLightItem(Obj)
{
	Obj.style.backgroundColor = '';
	Obj.style.color = 'Black';

}
//-----------------------------------------------------------------------
var _DEMO_INDEX = 1;
var _DEMO_STEP_NUM = 10;
var _DEMO_STEP_DELAY = 5;
var _DEMO_INT;
var _DEMO_OBJ_NAME = 'DEMO_DIV';
var _DEMO_ORG_OBJ_NAME;
var _DEMO_OBJ_W;
var _DEMO_OBJ_H;
var _DEMO_OBJ_nW;
var _DEMO_OBJ_nH;
function CreateDemoDiv()
{
	document.write('<Div class=CMSkin0 style="z-index:1000" id=DEMO_DIV></Div>');
}
function DivSet2D()
{
	if(_DEMO_INDEX >= _DEMO_STEP_NUM) 
	{
		_DEMO_INDEX = 1;		
		clearInterval(_DEMO_INT);
		document.all[_DEMO_ORG_OBJ_NAME].style.visibility = 'visible';
		document.all[_DEMO_OBJ_NAME].style.visibility = 'hidden';
		return;
	}		
	var W = _DEMO_INDEX*_DEMO_OBJ_nW;
	var H = _DEMO_INDEX*_DEMO_OBJ_nH;
	var A = _DEMO_INDEX*100/_DEMO_STEP_NUM;
	document.all[_DEMO_OBJ_NAME].style.width = W;
	document.all[_DEMO_OBJ_NAME].style.height = H;
	document.all[_DEMO_OBJ_NAME].style.filter = 'alpha(opacity='+ A +')';
	_DEMO_INDEX++;	
}
function DivShowDemo(ObjName)
{
	document.all[ObjName].style.visibility = 'hidden';	
	_DEMO_ORG_OBJ_NAME = ObjName;
	_DEMO_OBJ_W = document.all[ObjName].offsetWidth;;
	_DEMO_OBJ_H = document.all[ObjName].offsetHeight;		
	_DEMO_OBJ_nW = Math.round(_DEMO_OBJ_W/_DEMO_STEP_NUM);
	_DEMO_OBJ_nH = Math.round(_DEMO_OBJ_H/_DEMO_STEP_NUM);
	
	document.all[_DEMO_OBJ_NAME].style.left = document.all[ObjName].style.left;
	document.all[_DEMO_OBJ_NAME].style.top = document.all[ObjName].style.top;
	document.all[_DEMO_OBJ_NAME].style.width = 0;
	document.all[_DEMO_OBJ_NAME].style.height = 0;
	document.all[_DEMO_OBJ_NAME].style.visibility = 'visible';
	_DEMO_INT = setInterval("DivSet2D()", _DEMO_STEP_DELAY);	
}
CreateDemoDiv();
