/*
 * Menu specific functions
 */
function menu_position(menuId, trgId)
{
	//var trg = xGetElementById(trgId);
	var trg = $(trgId);
	var menu = $(menuId);
	//var menuWidth = menu.getDimensions().width;
	var x,y;
	
	if (xPageX(trg) < xClientWidth() / 2)
	{
		//xMoveTo(menuId, xPageX(trg), xPageY(trg) + xHeight(trg) - 1);
		x = xPageX(trg);
	}
	else
	{
		x = xPageX(trg) + xWidth(trg) - menu.getDimensions().width;
	}
	y = xPageY(trg) + xHeight(trg) - 1;
	//xMoveTo(menuId, xPageX(trg) + xWidth(trg) - menuWidth, );	
		
	menu.setStyle( { left: x+'px', top: y+'px' });
}

function menu_on(menuId, trgId)
{
	//switch_on_off_vis(menuId,''); 
	if (trgId)
		menu_position(menuId, trgId);
	switch_on_off(menuId,null); 
}
 
function menu_off(menuId)
{
	//switch_on_off_vis('', menuId); 
	switch_on_off(null, menuId); 
}

function login_menu(trgId)
{
		menu_off('login-alert'); 
		menu_on('login-alert',trgId);
}

/*
 * Declare
 */
function PopupMenu(menuId, parentmenu)
{
  this.isOpen = false;
  this.triggerId = null;
  this.submenu = null;
  this.parentmenu = parentmenu;
  if (parentmenu)
  	parentmenu.submenu = this;
  this.menuId = menuId;
  this.menu = null;
  this.returnToOffscreen = false;
}

/*
 * instantiate
 */
new PopupMenu(null,null);

/*
 * Define
 */
PopupMenu.prototype.start = function(triggerId, contentId)
{
	if (this.isOpen)
		this.onClose(false);
	this.menu = $(this.menuId);
	this.triggerId = triggerId;
	
	//log(contentId);
	var contentNode = $(contentId);
	
	//log(contentNode);
	contentNode.parentNode.removeChild(contentNode);
	//log(this.menu);
	this.menu.appendChild(contentNode);
	contentNode.show();
	this.returnToOffscreen = true;
	this.onOpen();
}
PopupMenu.prototype.startHTML = function(triggerId, content)
{
	if (this.isOpen)
		this.onClose(false);
	this.menu = $(this.menuId);
	this.triggerId = triggerId;
	this.menu.innerHTML = content;
	this.returnToOffscreen = false;
	this.onOpen();
}
PopupMenu.prototype.onRelease = function()
{
	while (this.menu.lastChild!=null)
	{
		var child = this.menu.lastChild;
		this.menu.removeChild(child);
		if (this.returnToOffscreen)
		{
			$('offscreen').appendChild(child);
		}
	}
	this.isOpen = false;		
}
PopupMenu.prototype.onClose = function(useEffect)
{
	if (this.isOpen)
	{
		xRemoveEventListener(document, 'mousemove', this.onMousemove, false);
		this.triggerId = null;
// BUGGY when traversing across top menus
//		if (useEffect)
//		{
//			Effect.BlindUp(this.menu, { queue: 'end', duration: '0.1', afterFinish: function() { 
//				log('Attempting to release on:'+this);
//				this.onRelease();
//			}.bind(this)});
//		}
//		else
		{
			this.menu.hide();
			this.onRelease();
		}
	}
}
PopupMenu.prototype.onOpen = function()
{
	//log('open '+this.triggerId);
	
	var supe = this;
	  
	this.onMousemove = function(ev)
	{
		if (supe.submenu && supe.submenu.isOpen)
			return;
		var e = new xEvent(ev);
		if (!xHasPoint(supe.triggerId, e.pageX, e.pageY, -10) &&
		    !xHasPoint(supe.menu, e.pageX, e.pageY, -1))
		{
			if (supe.isOpen)
		  		supe.onClose(true);
		  	else
		   		log('old mousemove');
	    }
	}
  
    menu_position(this.menu, this.triggerId);

	if (!this.isOpen)
	{
	    //xMoveTo(this.mnu, xPageX(this.trg), xPageY(this.trg) + xHeight(this.trg));
	    Effect.Appear(this.menu, { queue: 'end', duration: '0.2' });
	    xAddEventListener(document, 'mousemove', this.onMousemove, false);
	    this.isOpen = true;
	}
}

var COMMON_POPUP = new PopupMenu('common-popup');
var COMMON_SUBPOPUP = new PopupMenu('common-subpopup', COMMON_POPUP);

function mouseWatch(triggerId)
{
	log('Init mousewatch on:'+triggerId);
	this.triggerId = triggerId;
	this.inTrigger = true;
	
	this.onMouseout = function()
	{
		log('leaving trigger'+this.triggerId);
		this.inTrigger = false;
	}.bind(this);
    xAddEventListener(triggerId, 'mouseout', this.onMouseout, false);
	
	this.stop = function()
	{
		log('stopping watch on trigger'+this.triggerId);
		xRemoveEventListener(this.triggerId, 'mouseout', this.onMouseout, false);		
	}
}

function hover(triggerId, waitMs, f)
{
	var inTrigger = true;
	
	var onMouseout = function()
	{
		inTrigger = false;
	}
    xAddEventListener(triggerId, 'mouseout', onMouseout, false);
	
	setTimeout(function() {
		if (inTrigger)
		{
			f();
		}
		xRemoveEventListener(triggerId, 'mouseout', onMouseout, false);
	}, waitMs);
}
function menu(triggerId, contentId, waitMs)
{
	var MENU = findMenu(triggerId);
	
	if (MENU==null)
		return;
	
	if (waitMs==null)
		waitMs = 300;
		
	log('menu.start '+triggerId+' child of '+MENU.parentmenu);
	
	hover(triggerId, waitMs, function() {
		MENU.start(triggerId, contentId);
	});
}

function dynamicMenu(triggerId, uri, params, waitMs)
{
	var MENU = findMenu(triggerId);
	
	if (MENU==null)
		return;
		
	if (waitMs==null)
		waitMs = 300;
		
	log('menu.start '+triggerId+' child of '+MENU.parentmenu);
	
	hover(triggerId, waitMs, function() {
		menu_on('loading-popup', triggerId); 
		var watch = new mouseWatch(triggerId);
		new Ajax.Request(uri, { method: 'get', parameters: params, onSuccess: function(t) { 
				menu_off('loading-popup'); 
				if (watch.inTrigger)
					MENU.startHTML(triggerId, t.responseText);
				watch.stop();
		} });
	});

}

