// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// 
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
//   copyright notice, this list of conditions and the following disclaimer
//   in the documentation and/or other materials provided with the
//   distribution.
// * Neither the name of Danopia nor the names of its
//   contributors may be used to endorse or promote products derived from
//   this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS 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 THE COPYRIGHT
// OWNER OR 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.

/**
 * Returns the absolute X and Y positions of an object.
 * @param {HTMLObject} obj HTML Object.
 * @return {Object} Returns an accessor with .x and .y values.
 */
function getXY(obj)
{
	var curleft = 0;
	var curtop = 0;
	var border;
	if (obj.offsetParent)
	{
		do
		{
			// XXX: If the element is position: relative we have to add borderWidth
			if (getStyle(obj, 'position') == 'relative')
			{
				if (border = _pub.getStyle(obj, 'border-top-width')) curtop += parseInt(border);
				if (border = _pub.getStyle(obj, 'border-left-width')) curleft += parseInt(border);
			}
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while (obj = obj.offsetParent)
	}
	else if (obj.x)
	{
		curleft += obj.x;
		curtop += obj.y;
	}
	return {'x': curleft, 'y': curtop};
}

/**
 * Returns the specified computed style on an object.
 * @param {HTMLObject} obj HTML Object
 * @param {String} styleProp Property name.
 * @return {Mixed} Computed style on object.
 */
function getStyle(obj, styleProp)
{
	if (obj.currentStyle)
		return obj.currentStyle[styleProp];
	else if (window.getComputedStyle)
		return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
}



function doc_id(target)
{
	if(document.getElementById)
		return document.getElementById(target);
	else if(document.all)
		return document.all[target];
	else if(document.layers)
		return document.layers[target];
	else
		return false;
}


var timeout;
var timeout_menu;

function show_menu(menu)
{
	if (timeout != null)
	{
		clearTimeout(timeout);
		actually_hide_menu(timeout_menu);
	}
	
	var link = doc_id(menu);
	var menu = doc_id(menu + '_dropdown');
	
	if (menu.style)
	{
		pos = getXY(link);
		menu.style.left = (pos.x + link.offsetWidth) + 'px';
		menu.style.top = (pos.y - 1) + 'px';
		menu.style.display = 'inherit';
	}
	else
		menu.visibility = 'show';
}

function hide_menu(menu)
{
	timeout = setTimeout("actually_hide_menu('" + menu + "');", 500);
	timeout_menu = menu;
}

function actually_hide_menu(menu)
{
	menu = doc_id(menu + '_dropdown');
	
	if (menu.style)
		menu.style.display = 'none';
	else
		menu.visibility = '';
	
	timeout = null;
	timeout_menu = null;
}
