/*
 * ============================================================================
 *                   The ProQuality Inc.
 *         Rapid Efficent Low cost And Xtreme (RELAX)
 *	           Advanced Solutions Framework (ASF)
 *                  Cristian Teodorescu
 *                      Version 6.0
 * ============================================================================
 * 
 *    Copyright (C) 1999 The ProQuality Inc. Foundation. All rights reserved.
 * 
 * The use of this software in any form is strictly prohibited unless you have 
 * purchased a licence from ProQuality Inc. or you have a software agreement with 
 * ProQuality Inc.
 * 
 * Redistributions of source code must retain copyright statements
 * and notices. Redistributions must also contain a copy of this
 * document.
 *
  THIS SOFTWARE IS PROVIDED BY PROQUALITY AND CONTRIBUTORS ``AS IS'' AND
  ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PROQUALITY OR ITS
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  DAMAGE.

 */
var RCMenuHandler = {
	rcMenu : null,
	init   : function (optionsArray)
	         {
	         	this.rcMenu = new RCMenu(optionsArray);
	         }
}

function RCMenu()
{
	this.options  = arguments[0];
	
	this.active = false;
	
	this.make = RCMenu_make;
	this.deactivate = RCMenu_deactivate;
	this.show = RCMenu_show;
	
	this.make();
}

function RCMenuItem(text, onclicktext, url, disabled)
{
	this.text = text;
	this.onclicktext = onclicktext;
	this.url = url;
	this.disabled = disabled;
}

function RCMenu_make()
{
	this.menuDiv = document.createElement("DIV");
	this.menuDiv.className = "rcMenu";
	this.menuDiv.style.visibility = "hidden";
	//this.menuDiv.menuObj = this;
	
	var inString = "";
	
	for(var i = 0; i < this.options.length; i++)
	{
		var option = this.options[i];
		if(!option.text && !option.onclicktext)
		{
			inString = inString + "<hr />";
		}
		else if(!option.url)
		{
			inString = inString + "<a href=\"#\" " + (option.disabled ? "class=\"disabled\"" : "clicktext=\"" + option.onclicktext + "\" onclick=\"RCMenu_itemClicked(this)\"") +" >" + option.text + "</a>";
		}
		else
		{
			inString = inString + "<a " + (option.disabled ? "class=\"disabled\"" : "href=\"" + option.url + "\" onclick=\"RCMenu_itemClicked(this)\"") +" >" + option.text + "</a>";
		}
	}
	
	this.menuDiv.innerHTML = inString;
	this.body = document.getElementsByTagName("body")[0];
	this.body.appendChild(this.menuDiv);
	document.oncontextmenu = RCMenu_activate;
}

function RCMenu_activate(evt)
{
	//dbg("activate");
	var menu = RCMenuHandler.rcMenu;
	if(menu.options.length > 0)
	{
		menu.active = true;
		menu.show(evt);
		RCMenu.addEvent(document, "mousedown", RCMenu_mouseDown);
	}
	RCMenu.stopEvent(evt);
}

function RCMenu_show(e)
{
	//dbg("show");
	var rightedge = RCMenu.isIE? document.body.clientWidth - event.clientX : window.innerWidth - e.clientX ;
	var bottomedge = RCMenu.isIE? document.body.clientHeight - event.clientY : window.innerHeight - e.clientY;

	if (rightedge < this.menuDiv.offsetWidth){
		this.menuDiv.style.left = RCMenu.isIE? document.body.scrollLeft + event.clientX - this.menuDiv.offsetWidth : window.pageXOffset + e.clientX - this.menuDiv.offsetWidth;
	}
	else{
		this.menuDiv.style.left = RCMenu.isIE? document.body.scrollLeft + event.clientX : window.pageXOffset + e.clientX;
	}		
	if (bottomedge < this.menuDiv.offsetHeight){
		this.menuDiv.style.top = RCMenu.isIE? document.body.scrollTop + event.clientY - this.menuDiv.offsetHeight : window.pageYOffset + e.clientY - this.menuDiv.offsetHeight;
	}
	else{
		this.menuDiv.style.top = RCMenu.isIE? document.body.scrollTop + event.clientY : window.pageYOffset + e.clientY;
	}	
	this.menuDiv.style.visibility = "visible";
	return false;
}

function RCMenu_mouseDown(evt)
{
	//dbg("mouseDown");
	if (!RCMenuHandler.rcMenu.active)
	{
		return false;
	}
	var el = RCMenu.getTargetElement(evt);
	for (; el != null && el != RCMenuHandler.rcMenu.menuDiv; el = el.parentNode);
	if (el == null)
	{
		RCMenuHandler.rcMenu.deactivate();
		//return RCMenu.stopEvent(evt);
	}
}

function RCMenu_deactivate()
{
	//dbg("deactivate");
	this.active = false;
	this.menuDiv.style.visibility = "hidden";
	RCMenu.removeEvent(document, "mousedown", RCMenu_mouseDown);
}

function RCMenu_itemClicked(menuItem)
{
	//dbg("itemClicked");
	RCMenuHandler.rcMenu.deactivate();
	var clicktext = menuItem.getAttribute("clicktext");
	eval(clicktext);
}

RCMenu.isIE = (document.attachEvent ? true : false);

RCMenu.isNN = (document.addEventListener ? true : false);

RCMenu.stopEvent = function(ev) {
	if (RCMenu.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
	return false;
}

RCMenu.addEvent = function(el, evname, func) {
	if (el.attachEvent) // IE
	{
		el.attachEvent("on" + evname, func);
	} 
	else if (el.addEventListener) // Gecko / W3C
	{
		el.addEventListener(evname, func, true);
	}
}

RCMenu.removeEvent = function(el, evname, func) {
	if (el.detachEvent) // IE 
	{
		el.detachEvent("on" + evname, func);
	} 
	else if (el.removeEventListener) // Gecko / W3C
	{
		el.removeEventListener(evname, func, true);
	}
}

RCMenu.getTargetElement = function(ev) 
{
	if (RCMenu.isIE)
	{
		return window.event.srcElement;
	}
	else
	{
		return ev.target;
	}
}
 
