/*
Full-View Select Menu - A Fix for IE's Select Menu Cutoff Problem
Copyright (c) 2005 Thomas Peri, http://www.tumuski.com/

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The Software shall be used for Good, not Evil.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 * Convert a select menu.
 * 
 * Arguments:
 * 
 *   (String)  id    - The id of the html element containing the select menu.
 * 
 *   (boolean) right - (optional) Whether to anchor the expanded menu 
 *                     to the right instead of the the left.
 * 
 *   (boolean) force - (optional) Whether to force the menu behavior even if
 *                     the browser does not appear to be Internet Explorer.
 *
 * v 0.3.4 - December 7, 2005
 */
function fullViewMenu(id, right, force)
{
	// Does the browser need this hack?
	var browserNeedsHack =
	(
		(navigator.platform == 'Win32')
		&&
		(
			(navigator.appName == 'Microsoft Internet Explorer')
			||
			(navigator.userAgent.toLowerCase().indexOf('opera') >= 0)
		)
	);
	
	// Is this a browser that can't handle this hack?
	var browserCantHandleHack =
	(
		(navigator.platform.indexOf('Mac') >= 0)
		&&
		(
			(navigator.appName == 'Microsoft Internet Explorer')
			||
			(navigator.userAgent.toLowerCase().indexOf('camino') >= 0)
		)
	);
	
	// Should we in fact proceed with the hack?
	var proceed = 
	(
		// proceed if either (a) the browser needs the 
		// hack, or (b) we don't care whether it needs it...
		(browserNeedsHack || force)
		&&
		// but not if we know the browser can't handle it.
		(!browserCantHandleHack)
	);
	
	// decide f'real
	if (!proceed)
	{
		return;
	}
	
	// Get the container and the select menu
	var container = document.getElementById(id);
	var select = container.getElementsByTagName("select");
	if (select.length == 0)
	{
		// Why are there are no select menus in the container?  Are you nuts?!
		return;
	}
	// OK, got it.
	select = select[0];
	
	// Shortcuts to their respective styles
	var c = container.style;
	var s = select.style;
	var big = c.width;
	var small = s.width;
	var z = c.zIndex;
	
	// set up the container
	c.position = "relative";
	c.top = "0px";
	c[right?"right":"left"] = "0px";
	c.width = small;
	c.height = s.height;
	
	// set up the menu
	s.position = "absolute";
	s.top = "0px";
	s[right?"right":"left"] = "0px";
	
	// runtime stuff:
	
	var hasFocus = false;
	var enable = true;
	var lastEnlarge = 0;
	
	function enlarge()
	{
		if (enable)
		{
			s.width = big;
			c.zIndex = 5000;
			
			// stamp this as the most recent time the menu was enlarged
			lastEnlarge = (new Date()).getTime();
		}
	}
	
	function shrink()
	{
		var span = (new Date()).getTime() - lastEnlarge;
	
		if (enable)
		{
			s.width = small;
			c.zIndex = z;
	
			// if we're re-shrinking the menu amost immediately
			// after enlarging it, assume that we've got the sizes 
			// backwards (the menu would naturally be smaller than 
			// it is), so disable the shrinking/expanding for a little while
			if (span < 20)
			{
				enable = false;
			}
		}
		// just in case it was disabled by accident (by the mouse 
		// passing over it momentarily), re-enable it after another 
		// short period of time.
		else if (span > 250)
		{
			enable = true;
		}
	}

	select.onfocus = function()
	{
		hasFocus = true;
		enlarge();
	};
	select.onblur = function()
	{
		hasFocus = false;
		shrink();
	};
	select.onmouseover = function()
	{
		enlarge();
	};
	select.onmouseout = function()
	{
		if (!hasFocus)
		{
			shrink();
		}
	};
	
} // fullViewMenu
