/* ============================================================= *//*
 * File     : event.js
 * Created  : 2008/07/08 [Y/M/D]
 * Author   : Michael Kerr (mkerr@aa.co.nz) 
 * Purpose  : To create a better way to manage events
 * History 
 * 		Date         	Version        	Description 
 * 		2008-07-08		1.0				First version
  *		2009-03-24		1.0				Added Event.blink
*//* ============================================================= */

var eType = typeof document.addEventListener != 'undefined' ? 'addEventListener' : typeof document.attachEvent != 'undefined' ? 'attachEvent' : 'none';
var ePref = this.eType == 'attachEvent' ? 'on' : '';

var Event = 
{
/*Calendar.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);
	} else {
		el["on" + evname] = func;
	}
};

Calendar.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);
	} else {
		el["on" + evname] = null;
	}
};

Calendar.createElement = function(type, parent) {
	var el = null;
	if (document.createElementNS) {
		// use the XHTML namespace; IE won't normally get here unless
		// _they_ "fix" the DOM2 implementation.
		el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
	} else {
		el = document.createElement(type);
	}
	if (typeof parent != "undefined") {
		parent.appendChild(el);
	}
	return el;
};*/

	attach: function(E, A, B, C, D)
	{
		D = typeof D != 'undefined' ? D : null;

		if (typeof C != 'undefined')
			E[eType](ePref + '' + A, function(e) { B.apply(C, new Array(e, C, E, D)); }, false);
		else
			E[eType](ePref + '' + A, function(e) { B(e); }, false);

/*
		if (C)
			E[eType](ePref + '' + A, function(e) { B.apply(C, arguments); }, false);
		else
			E[eType](ePref + '' + A, function(e) { B(e); }, false);
*/
	},

	stop: function(e)
	{
		if (e.returnValue) e.returnValue = false;
		if (typeof e.preventDefault != 'undefined' ) { e.preventDefault(); }
		return Event;
	},

	stopSelect: function(e)
	{
		if (typeof document.onselectstart != 'undefined')
			{ document.onselectstart = function() { return false; } }
		return Event;
	},

	stopNonValidKeys: function(e, A)
	{
		if (!A) A = 'URL';
		if (!e) e = window.event;

		switch (A)
		{
			case 'URL':
				if ((e.shiftKey === true && ((e.keyCode >= 48 && e.keyCode <= 54) || in_array(e.keyCode, [56, 57, 61, 190])))
					|| in_array(e.keyCode, [219, 221, 192, 220, 222, 188, 111, 106, 107, 109]) 
					|| (e.shiftKey == false && in_array(e.keyCode, [55, 59])))
					{ Event.stop(e); }
				break;
			case 'REGEX':
				if ((e.shiftKey === true && (e.keyCode >= 48 && e.keyCode <= 57)) 
					|| in_array(e.keyCode, [219, 221, 192, 220, 222, 188, 111, 106, 107, 191, 190, 59, 32, 61]))
						{ Event.stop(e); }
				break;
			default:
				if ((e.shiftKey === true && ((e.keyCode >= 48 && e.keyCode <= 54) || in_array(e.keyCode, [56, 57, 61, 190])) || in_array(e.keyCode, [219, 221, 192, 220, 222, 188, 111, 106, 107, 109]) || (e.shiftKey == false && in_array(e.keyCode, [55, 59]))))
					{ Event.stop(e); }
				break;
		}

		// console.log(e.keyCode);
	},
	
	limitCharCount: function(e, A, B)
	{
		if (!A) A = 1024;
		if (!e) e = window.event;

		if (! in_array(e.keyCode, [8, 35, 36, 37, 38, 39, 40, 46]) && B.value.length >= A)
			{ Event.stop(e); }
		/*else
			{ if (C) Utils.$(C).innerHTML = B.value.length; }*/

		// console.log(e.keyCode);
	},
	
	target: function(e, A)
	{
		if (!e) e = window.event;
		var node = typeof e.target != 'undefined' ? e.target : e.srcElement;

		if (typeof A == 'string')
		{
			var pattern = new RegExp("(?:^|\\s)" + A + "(?:\\s|$)");
			while (A && node.parentNode && !pattern.test(node.className) && node.nodeName != 'BODY')
			{
				node = node.parentNode;
			}
		}
		return node;
	},
	
	repeatDelay: function(A, B, C) 
	{
		return setInterval(function () { B.apply(C); }, A);
	},

	Delay: function(A, B, C) 
	{
		return setTimeout(function () { B.apply(C); }, A);
	},
	
	clear: function(A)
	{
		$clear(A);
	},

	/* Queue events for the onload method */
	onloadEvent: function(A, B, C)
	{
		if (typeof window.onload == 'undefined' || window.onload == null) window.onload = Event.loadRun;
		if (typeof Event.loadEvents == 'undefined' || Event.loadEvents == null) Event.loadEvents = new Array();

		if (C)
			Event.loadEvents[Event.loadEvents.length] = function() { A.apply(B, C); };
		else
			Event.loadEvents[Event.loadEvents.length] = function() { A.apply(B); };
	},

	loadRun: function()
	{
		for (var i = 0; i < Event.loadEvents.length; i++)
			{ Event.loadEvents[i](); }
	},

	blink: function(A, B)
	{
		window.blinker = Event.repeatDelay(A, Event.blinking, Utils.$(B));
	},

	blinking: function(B)
	{
		if (typeof this != 'undefined' && typeof this.style != 'undefined' && typeof this.style.visibility != 'undefined')
		{
			if (this.style.visibility == 'visible')
				this.style.visibility = 'hidden';
			else
				this.style.visibility = 'visible';
		}
		else
		{
			if (typeof console != 'undefined' && typeof console.log != 'undefined') console.log('Blinker: Unable to locate the visibility attribute of your target element');
			if (typeof window.blinker != 'undefined') Event.clear(window.blinker);
		}
	}

	/* End onload enhancement */
};