// JavaScript Document

function PopupMenu (id,config) {
	this.id = id;
	this.config = config;
	this.items = [];
	this.itemIds = [];
	this.contentDiv = document.createElement("div");
	this.contentDiv.style.zIndex = 50000001;
	this.shieldDiv = document.createElement("div");
	this.shieldDiv.style.zIndex = 50000000;
	this.contentTable = document.createElement("table");
	this.contentTBody = document.createElement("tbody");
	if(!document.vPopupArray)
		document.vPopupArray = []
	document.vPopupArray.push(this);
	this.shieldFrame = document.createElement('iframe');
	with(this.shieldDiv) {
		appendChild(this.shieldFrame);
		style.position = 'absolute';
		style.display = 'none';
	}
	with(this.shieldFrame) {
		width = '100%';
		height = '100%';
	}
	with(this.contentTable) {
		width = '100%';
		className = 'popup_borda';
		appendChild(this.contentTBody);
		cellPadding = 2;
		cellSpacing = 0;
	}
	with(this.contentDiv) {
		style.position = 'absolute';
		className = 'borda';
		style.display = 'none';
		style.width = 100;
		appendChild(this.contentTable);
	}
	this.show = function (left,top) {
		if(document.vPopupOpened != null)
			document.vPopupOpened.hide();
		document.body.appendChild(this.contentDiv);
		document.body.appendChild(this.shieldDiv);
		document.onclick = function() {
			if(document.vPopupOpened) {
				document.vPopupOpened.hide();
			}
		}
		f_get_scroll();
		with(this.contentDiv) {
			style.top = top + scrOfY;
			style.left = left + scrOfX;
			style.display = '';
			style.width = offsetWidth;
		}
		with(this.shieldDiv) {
			style.top = this.contentDiv.style.top;
			style.left = this.contentDiv.style.left;
			style.width = this.contentDiv.offsetWidth;
			this.shieldDiv.style.height = this.contentDiv.offsetHeight;
			this.shieldDiv.style.overflow = 'hidden';
			style.display = '';
		}
		document.vPopupOpened = this;
	}
	this.hide = function () {
		this.contentDiv.style.display = 'none';
		this.shieldDiv.style.display = 'none';
		if(document.vPopupOpened == this)
			document.vPopupOpened = null;
	}
	this.MenuItem = function(popupMenu,name,separador,label,icon,onclick) {
		this.parent = popupMenu;
		this.parent.items.push(this);
		this.parent.itemIds[name] = this;
		this.contentTr = document.createElement('tr');
		this.parent.contentTBody.appendChild(this.contentTr);
		this.separador = separador;
		if(separador) {
			this.contentSeparator = document.createElement("td");
			with(this.contentSeparator) {
				colSpan = 2;
				height = 2;
				className = 'separador_h';
			}
			this.contentTr.appendChild(this.contentSeparator);
		} else {
			this.iconTd = document.createElement("td");
			this.contentTr.appendChild(this.iconTd);
			this.contentTr.style.cursor = 'pointer';
			this.contentTr.onmouseover = function() {
				this.className = 'linha_1';
			}
			this.contentTr.onmouseout = function() {
				this.className = '';
			}
			this.image = document.createElement("img");
			this.image.width = 16;
			this.image.height = 16;
			this.image.align = 'absmiddle';
			with(this.iconTd) {
				className = 'popup_degrade';
				appendChild(this.image);
				width = '1%';
			}
			this.labelTd = document.createElement("td");
			this.labelTd.className = 'texto';
			this.contentTr.appendChild(this.labelTd);
			this.setIcon = function(icon) {
				this.image.src = icon;
				this.image.style.display = (icon.length == 0)?'none':'';
			}
			this.setLabel = function(label) {
				this.labelTd.innerHTML = '<nobr>'+label+'</nobr>';
			}
			this.setLabel(label);
			this.setIcon(icon);
			this.setOnClick = function(value) {
				this.contentTr.onclick = value;
			}
			this.setOnClick(onclick);
		}
		this.setVisible = function (value) {
			this.contentTr.style.display = value?'':'none';
		}
	}
	this.addItem = function (name,label,icon,onclick) {
		return new this.MenuItem(this,name,false,label,icon,onclick);
	}
	this.addSeparator = function(name) {
		return new this.MenuItem(this,name,true);
	}
	this.contextMenu = function(event) {
		if(!event) {
			v_left = window.event.clientX;
			v_top = window.event.clientY;
		} else {
			v_left = event.clientX;
			v_top = event.clientY;
		}
		this.show(v_left,v_top);
		return false;
	}
}
