function ContextMenu(uniqueName){

	this.divPopup = null;
	this.selectedInstrumentId = null;
	this.selectedInstrumentName = null;
	this.onclickRef = null;
	this.buttons = new Array();
	this.alignedToLeft = false;
	
	document.onclick = ContextMenu.hide;	

	this.fill = function(r,n){
		
		var i,oCell,oRow;
		
		this.selectedInstrumentId = r;
		this.selectedInstrumentName = n;
		
		if(this.divPopup==null){
			this.divPopup = document.createElement("DIV");
			document.appendChild(this.divPopup);		
		}
		this.divPopup.isPopup = true;		
		this.divPopup.style.width = 150;
		this.divPopup.style.zIndex = 100;
		this.divPopup.style.backgroundColor = "#FFFFFF";
		this.divPopup.style.borderColor = "#000000";
		this.divPopup.style.borderStyle = "solid";
		this.divPopup.style.borderWidth = 1;
		
		this.divPopup.innerHTML = '';
		
		var table = document.createElement("TABLE");		
		oRow = table.insertRow(table.rows.length);
		oCell = oRow.insertCell(0);
		oCell.innerHTML = '';
		
		for(i=0;i<this.buttons.length;i++){
			oRow = table.insertRow(table.rows.length);
			oCell = oRow.insertCell(0);
			oCell.className = "button";
			oCell.id = "BTNPOPUP"+cleanName(this.buttons[i]);
			oCell.appendChild(document.createTextNode(this.buttons[i]));
			oCell.onclick = this.contextMenuOnClick;
			oCell.myContextMenu = this;
		}
		
		this.divPopup.appendChild(table);

		if(r)this.divPopup.childNodes[0].rows[0].cells[0].innerHTML = remplazarParametros(top.DynTable.msgs.anadira, n);		
	
	
	}
	
	this.show = function(element, div, alignedToLeft){
		var ie5=document.all&&document.getElementById
	
		if (!(element)) {
		    x = window.event.x;
		    y = window.event.y;
		    rightedge=document.body.clientWidth-event.clientX;
				bottomedge=document.body.clientHeight-event.clientY; 
		  } else {
		    x = element.pageX;
		    y = element.pageY;
		    rightedge= window.innerWidth-element.clientX;
		 		bottomedge= window.innerHeight-element.clientY;
		  }
		
	
		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<div.offsetWidth || alignedToLeft){
			//move the horizontal position of the menu to the left by it's width
			x=ie5? document.body.scrollLeft+event.clientX-div.offsetWidth : window.pageXOffset+e.clientX-div.offsetWidth;
			x=x-10;
		}else{
			//position the horizontal position of the menu where the mouse was clicked
			x=ie5? document.body.scrollLeft+event.clientX : window.pageXOffset+e.clientX;
			x=x+10;
		}
		//same concept with the vertical position
		if (bottomedge<div.offsetHeight)
			y=ie5? document.body.scrollTop+event.clientY-div.offsetHeight : window.pageYOffset+e.clientY-div.offsetHeight;
		else
			y=ie5? document.body.scrollTop+event.clientY : window.pageYOffset+e.clientY;
			
		div.style.left = x;
		div.style.top = y;
		div.style.visibility  = "visible";
	}
	
	this.contextMenuOnClick = function(element){

		if (!(element)) {
		  e = window.event.srcElement;
		} else {
		  e = element.target;
		}
		r = findObject(e,'TD');
				
				
		if(r.myContextMenu.onclickRef){
			var f = r.myContextMenu.onclickRef.replace("%r",r.myContextMenu.selectedInstrumentId);
			f = f.replace("%n",r.myContextMenu.selectedInstrumentName);
			f =  f.replace("%t",r.id.substr(8));
			eval(f);
		}
		
		ContextMenu.hide();
	}
	
}

ContextMenu.showContextMenu = function(element){
	if (!(element)) {
    el = window.event.srcElement;
  } else {
    el = element.target;
  }
  var div = el.myContextMenu.divPopup;
	el.myContextMenu.fill(el.rowid,el.rowname);
	el.myContextMenu.show(element, div,el.myContextMenu.alignedToLeft );
}		

ContextMenu.hide = function(element){
	if (element==null) {
	  e = window.event.srcElement;
	} else {
	  e = element.target;
	}

	if(e.rowid==null){
		var divs = document.getElementsByTagName("DIV");
		for (i in divs)
			if(divs[i].isPopup)
				divs[i].style.visibility = "hidden";
	}
}

function remplazarParametros(str, arr){
	if( typeof (arr) != "object")
	{
		var aux = new Array();
		aux[0] = arr;
		arr = aux;
	}
	for (var i = 0;i<arr.length; i++)
	{
		str = str.replace(new RegExp("%"+i, "g"),arr[i]);
	}
	return str;
}				