//==MAIN PROGRAM=====================
var SEPARATOR = new SeparatorItem();
var ccContainer = new Hashtable();
var activeMenu = null;

function onShowContextMenuIE(){
	onShowContextMenu(event.clientX,event.clientY,event.srcElement.id);
}

function onShowContextMenuFF(event){	
	onShowContextMenu(event.clientX,event.clientY,this.id);
}

function onShowContextMenu(x,y,objId){
	if(activeMenu!=null) activeMenu.hideContextMenu();
	activeMenu = ccContainer.get(objId);
	activeMenu.showContextMenu(x,y,objId);
}

function onHideContextMenu(){
	if(activeMenu){
		activeMenu.hideContextMenu();
	}
}
//==CustomContextMenu================
function CustomContextMenu(){

	this.ie = (window.navigator.userAgent.toLowerCase().indexOf("ie")>-1);
	this.menuItems = new Array();
	this.div = document.createElement("div");

	this.attachTo =  function(objId){	
		obj = document.getElementById(objId);
		
		if(!this.ie){		
			obj.addEventListener("click",onShowContextMenuFF,false);
			obj.addEventListener("contextmenu",onShowContextMenuFF,false);
			document.addEventListener("click",onHideContextMenu,true);
			document.addEventListener("keydown",onHideContextMenu,true);	
		}else{
			obj.onclick = onShowContextMenuIE;
			obj.oncontextmenu = onShowContextMenuIE;
			document.onmouseup = onHideContextMenu;
			document.onkeydown=onHideContextMenu;			
		}
		this.createMenu();	
		this.id = objId;
		ccContainer.put(objId,this);
	}

	this.append = function(item){
		this.menuItems[this.menuItems.length] = item;
	}

	this.showContextMenu = function(x,y,id){
		obj = ccContainer.get(id);
		menu = obj.div;
		menu.style.display="block";
		menu.style.left = x + document.body.scrollLeft;
		menu.style.top = y + document.body.scrollTop;
	}

	this.hideContextMenu  = function(){
		var menus = ccContainer.values();
		for(i=1;i<menus.length;i++){
			menus[i].div.style.display="none";
		}
	}

	this.createMenu = function(){		
		for(i=0;i<this.menuItems.length;i++){
			this.div.appendChild(this.menuItems[i].getElement());
			if(!(this.menuItems[i] instanceof SeparatorItem)){
				this.div.appendChild(document.createElement("br"));
			}
		}	
		document.body.appendChild(this.div);
	}

	this.setStyle = function(className){
		this.div.className = className;
		this.div.style.position = "absolute";
		this.div.style.display = "none";
		if(!this.ie){
			this.div.style.backgroundColor= "#ffffff";
			this.div.style.border= "1px #000000 solid";
			this.div.style.padding= "5";
			this.div.style.width= "100";
		}
	}
}
//==MenuItem=====================
function MenuItem(html,href,className,target){
	this.element = null;
	this.href = href;
	this.html = html;
	this.className = className;
	this.getElement = function(){
		if(this.element==null){
			var a = document.createElement("a");
			a.href=this.href;
			a.innerHTML =  this.html ;
			a.className = this.className;
			a.target = target;
			this.element = a;
		}
		return this.element;
	}
}
//==Seperator=====================
function SeparatorItem(){
	this.getElement = function(){
		var element =  document.createElement("hr");
		element.size=0;
		return element;
	}
}

