/**
 * common.js
 * @author Adam "NiX0n" VanBerlo
 * @copyright Copyright (c) 2008 Village Press, Inc. (http://www.villagepress.com/)
 */
Event.observe(document, 'dom:loaded', function (event)
{
	new Navigation($$('.navigation > li'), {menuSelector: '.navigation > li'});
	$$('.slideshow').each(function(s) {
		new Controls.TabbedSlideshow(s);
	});
	hideTooltips();
});

function msg(m)
{
	$('messages').insert(m + "\n");
}

function validEmail(address)
{
	var regex = /^\S+@\S+\.\w{2,}$/;
	return String(address).search(regex) != -1;
}

/**
 * Negated $F() Utility Function
 * @see $F() in prototype.js
 */
function $f(e){ return !$F(e); }



/**
 * Search haystack for needle
 * @param Hash needle to be found (partial matches allowed)
 * @param Hash haystack to be searched
 * @returns index of needle found in haystack, false otherwise
 * WARNING: 0 and false are possible returns, but strictly different.
 * 			Use === operator for testing return of this function.
 */
function array_search(needle, haystack)
{
	var index = false;
	var found = false;
	$A(haystack).each(function(straw, milkshake)
	{
		$H(needle).each(function(thread)
		{
			if(thread.value == straw[thread.key])
				found = true;
			else
			{
				found = false;
				throw $break;
			}
		});
		if(found)
		{
			index = milkshake;
			throw $break;
		}
	});
	return index;
}

/**
 * Merges arrays into a single array.  If multiple arguments
 * are passed, arguments are merged; else, `arr1` is treated
 * as a multi-dimensional array, and its elements are merged.
 * @param Array arr1 
 * @usage array_merge(arr1[,arr2[,arr3[...]]])
 */
function array_merge(arr1)
{
	var merging = (arguments.length > 1)?arguments:arr1;		
	var merged = Array();
	for(var i = 0; i < merging.length; i++)
		for(var ii = 0; ii < merging[i].length; ii++)
			merged.push(merging[i][ii]);
	return merged;
}

/**
 * Freeze a (generally) submit button to prevent multiple submissions
 * @param boolean state dictates whether the button is to become disabled
 * @param string button id of button
 * @param string newCaption (optional) new caption for button (i.e. "Working")
 */
function freezeButton(state, button)
{
	var newCaption = (null !== arguments[2])?arguments[2]:null;
	var button = $(button);
	new PeriodicalExecuter(function(pe)
	{
		button.disabled = state;
		if(null !== newCaption) button.update(newCaption);  
		pe.stop();
	}, .2);

	/*// The old way
	eval(
		"new PeriodicalExecuter(function (pe) " +
		"{"+
		"	$('" + button + "').disabled = " + (state?"true":"false") + ";"+
		"	" + ((null !== newCaption)?"$('" + button + "').innerHTML = '"+ newCaption +"';":"") + 
		"	pe.stop();"+
		"}, .2);"
	);*/
}

function getWindowDimensions()
{
	return Prototype.Browser.IE?
		{
			width: document.body.clientWidth,
			height: document.body.clientHeight
		}:
		{
			width: window.innerWidth,
			height: window.innerHeight
		}
	;
}

function showTooltip(el, x, y)
{
	offsetX = -20;
	offsetY = -20;
	limits = $(document.body).getDimensions(); //getWindowDimensions();
	dims = el.getDimensions();
//	alert("Limits: " + Object.toJSON(limits) + "\nElement: " + Object.toJSON(dims));
	if((x + offsetX + dims.width) > limits.width)
		el.style.right = "0px";
	else
		el.style.left = (x + offsetX) + "px";
	if((y + offsetY + dims.height) > limits.height)
		el.style.bottom = "0px";
	else
		el.style.top = (y + offsetY) + "px";
	el.show();
}

function hideTooltips()
{
	var tooltips = $$('.tooltip');
	for(var i = 0; i < tooltips.length; i++)
	{
		tooltips[i].hide();
		tooltips[i].style.position = "absolute";
	}
}

function allowedKeys(event, keys)
{
	var args = $A(arguments).reverse(); args.pop();
	var valid = (arguments.length > 2)?array_merge(args):keys;
	for(var i = 0; i < valid.length; i++)
		if(event.keyCode == valid[i])
			return true;
	return false;
}

/*Object.extend(Element.prototype,
{
	matches : function (regex) 
	{
		return null !== $F(this).match(regex);
	}
});*/

var NUMBER_KEYS = [48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105];
var SPECIAL_KEYS = [
	Event.KEY_TAB,		Event.KEY_RETURN, 	Event.KEY_BACKSPACE,	
	Event.KEY_ESC,		Event.KEY_LEFT,		Event.KEY_UP, 
	Event.KEY_RIGHT,	Event.KEY_DOWN,		Event.KEY_DELETE, 
	Event.KEY_HOME,		Event.KEY_END,		Event.KEY_PAGEUP,
	Event.KEY_PAGEDOWN
];
var SPECIAL_NUMBER_KEYS = array_merge(NUMBER_KEYS, SPECIAL_KEYS);
