function positive(n){
	return (n>-1);
}


var CMTools = new Object() ;

CMTools.PreventDefaultContextMenu=function(el){
	if(arguments.length==0) el=window.document;
	el.oncontextmenu = function(e){
		if (e) e.preventDefault() 	// This is the Gecko way to do that.
		return false ;				// This is the IE way to do that.
	}
}


//static CreateContextMenu
function CreateContextMenu(strcallee,strcaller,options){
	cm = new contextMenu(strcallee,strcaller,options);	
	return cm;
}


function contextMenu(strcallee,strcaller,options){	
	this.callee=document.getElementById(strcallee);
	this.caller=document.getElementById(strcaller);
	//cLoc=CMTools.GetElementPosition(this.caller);
	this.Toggler=new DisplayTogglerControl(this.caller,this.callee);
	this.style=this.callee.style;
	this.style.position="absolute";
	this.state=CM.STATUS.CLOSED;
	var innerhash=CreateHashtableByString(options);	
	var _o=innerhash.getOption('openmode',CM.OPENMODE.CLICK);
	a=[[CM.OPENMODE.CLICK,'click'],[CM.OPENMODE.RIGHTCLICK,'rightclick']];
	this.openmode=commonControls.getFromSymbol(_o,a);
	_o=innerhash.getOption('position',CM.POSITION.BOTTOM);
	var p=innerhash.getOption('position',"")
	this.centerScreen=(_o=="centerScreen");
	this.position=null;
	if(!this.centerScreen){
		a=[[CM.POSITION.BOTTOM,'bottom'],[CM.POSITION.LEFT,'left'],[CM.POSITION.RIGHT,'right'],	[CM.POSITION.TOP,'top']];
		this.position=commonControls.getFromSymbol(_o,a);
	}
	this.marginx= parseInt(innerhash.getOption('marginx',0));// marginx||0;	
	this.marginy= parseInt(innerhash.getOption('marginy',0)); //marginy||0;
	this.callerOffClass= innerhash.getOption('calleroff',null)
	this.callerOnClass= innerhash.getOption('calleron',null)	
	this.elementToAlign=innerhash.getOption('alignto',null);
	
	if(window.cmenus==null)window.cmenus=this.createContext();
	window.cmenus.Add(this);	
	this.Hide();	
	this.setup();
	this._lastopenmode=-1;
}



contextMenu.prototype._watchopenmode=function(){
if(this._lastopenmode==-1 || this._lastopenmode!=this.openmode){
	if (this.openmode==CM.OPENMODE.RIGHTCLICK) {
		commonControls.PreventDefaultContextMenu(this.caller);
		commonControls.PreventDefaultContextMenu(this.callee);
		}
	else {
		this.caller.oncontextmenu=null;
		this.callee.oncontextmenu=null;
		}
	this._lastopenmode=this.openmode;
	}
}

contextMenu.prototype.Show=function(){
	if(window.cmenus.opened>0) window.cmenus.HideAll();
	this._watchopenmode();
	this.display();
	if(this.callerOnClass!=null)  this.caller.className=this.callerOnClass;
	this.state=CM.STATUS.OPEN;
	window.cmenus.opened += 1;	
}

contextMenu.prototype.Hide=function(){
	this.Toggler.hide();
	if(this.callerOffClass!=null) this.caller.className=this.callerOffClass;
	this.state=CM.STATUS.CLOSED;
}

contextMenu.prototype.setup=function(){
	if(BrowserInfo.IsGecko) this.setup_gecko();
	else this.setup_ie();
}

contextMenu.prototype.setup_gecko=function(){
	this.caller.onclick=window.cmenus.geckoOnCallerClick;
	this.caller.onmousedown=window.cmenus.geckoOnRightClick;
	window.document.onclick=window.cmenus.geckoOnDocumentClick;
}

contextMenu.prototype.setup_ie=function(){
	window.document.onclick=function(){
		e = window.event.srcElement;
		window.cmenus.OnDocumentClick(e);		
		}
	this.caller.onclick=function(){
		window.cmenus.OnCallerClick(window.event.srcElement)
		}
	this.caller.onmousedown=window.cmenus.ieOnRightClick;
}


contextMenu.prototype.display=function(){
//	this.style.display='inline';	
	this.style.zIndex=300;
	if(!this.centerScreen){
		var relativeElement = (this.elementToAlign!=null)?document.getElementById(this.elementToAlign):this.caller;
		commonControls.Align(relativeElement,this.callee, this.position, this.marginx, this.marginy);
	}
	else{
		commonControls.centerScreen(this.callee);
	}
	this.Toggler.show();
}

contextMenu.prototype.createContext=function(){
	var contextMenus=new Object(); 

	contextMenus.opened=0; // conta menu aperti
	contextMenus.items=[];
	contextMenus.Count=function(){return contextMenus.items.length;}
	contextMenus.Add=function(cmObj){
		contextMenus.items[contextMenus.Count()]=cmObj;
	}

	contextMenus.hasCallee=function(e){
		for(i=0;i<contextMenus.Count();i++){
			if(contextMenus.items[i].callee==e) return true
		}
		return false;
	}

	contextMenus.getByCaller=function(caller){
		for(i=0;i<contextMenus.Count();i++){
			if(contextMenus.items[i].caller==caller) return contextMenus.items[i];
		}
	}
	contextMenus.hasCaller=function(e){
		for(i=0;i<contextMenus.Count();i++){
			if(contextMenus.items[i].caller==e) return true;
		}
		return false;
	}

	contextMenus.HideAll=function(){
		for(i=0;i<contextMenus.Count();i++){
			contextMenus.items[i].Hide();
		}
	}
	contextMenus.OnDocumentClick=function(e){
		while ( e )
		{		
			if ( contextMenus.hasCallee(e) || contextMenus.hasCaller(e)) return ;
			e = e.parentNode ;
		}
		contextMenus.HideAll() ;
	}

	contextMenus.OnCallerClick=function(e){
			if(contextMenus.hasCaller(e)){
				obj=contextMenus.getByCaller(e);
				if(obj.openmode!=CM.OPENMODE.CLICK) return;
				(obj.state!=CM.STATUS.OPEN) ? obj.Show() : obj.Hide();
			}	
			
	}

	contextMenus.OnRightClick=function(e){
			if(contextMenus.hasCaller(e)){
				obj=contextMenus.getByCaller(e);
				if(obj.openmode!=CM.OPENMODE.RIGHTCLICK) return;
				(obj.state!=CM.STATUS.OPEN) ? obj.Show() : obj.Hide();
			}	
	}

	//For Gecko
	contextMenus.geckoOnRightClick=function(event){
		var e = event.target ;
		if(event.which == 3) {
			contextMenus.OnRightClick(e);
		}
	}
	//For IE
	contextMenus.ieOnRightClick=function(){
		if(event.button == 2) {
			contextMenus.OnRightClick(event.srcElement);
		}
	}

	//For Gecko
	contextMenus.geckoOnCallerClick=function(event){
		var e = event.target ;
		contextMenus.OnCallerClick(e);
	}
	//For Gecko
	contextMenus.geckoOnDocumentClick=function(event){
		var e = event.target ;
		contextMenus.OnDocumentClick(e);
	}
	return  contextMenus;
}

/*
funzione di callback richiesta per tutti 
i controlli della serie commons control
la sintassi è <namespace del controllo>Create
convenzioni:
il namespace è con il segno _ al posto di : e tutto minuscolo
il parametro accettato dalla function è un DOM ELEMENT
attraverso i suoi attributi si ricavano i valori delle proprietà
dell'oggetto
*/
function commons_contextmenuCreate(node){
	var strCaller=node.getAttribute("caller")||"";
	var strCallee=node.getAttribute("callee")||"";
	var strOptions=node.getAttribute("options")||"";
	if(strCaller!='' && strCallee!='')
		CreateContextMenu(strCallee,strCaller,strOptions);
}
/*registrazione per commons controls*/
commonControls.registredTags.append("contextMenu");
/*********************************************************/

function combobox(strbox,strcallee,strcaller,options){
	this.box=document.getElementById(strbox);
	var cm=CreateContextMenu(strcallee,strcaller,options);
	cm.elementToAlign=strbox;
	this.cmenu=cm;
}






