Browser = 
{
	IE:     !!(window.attachEvent && !window.opera),
	Opera:  !!window.opera,
	WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
	Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
	MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
}

var Class =
{
	create: function()
	{
 		var constructor = function()
		{
         	arguments.callee.prototype.constructorFunction.apply(this, arguments);
		}
		constructor.prototype.constructor = constructor;
		return constructor; 
	},
	extend: function(p)
	{
 		var constructor = function()
		{
         	p.apply(this, arguments);
         	arguments.callee.prototype.constructorFunction.apply(this, arguments);
		}
		for(var property in p.prototype)
		{
			constructor.prototype[property] = p.prototype[property];
		}
		constructor.prototype.constructor = constructor;
		return constructor;
	}
}


/*
 *  DOM & CSS Helper Functions
 * 
 */

//Get Element
function $E(id)
{
	return (id == null) ? document.body : document.getElementById(id);
}
 
//Attach Element  
function $AE(p, c)
{
	if(p == null)
	{
		p = document.body;
	}
	
	p.appendChild(c);
}

//New Element 
function $NE(p, t, id, class_name)
{
	var ne = document.createElement(t);
	
	ne.id = id;
	ne.className = class_name;

	$AE(p, ne);

	return ne;
}

//Parent Element  
function $PE()
{
	var e = (typeof arguments[0] == "string") ? $E(arguments[0]) : arguments[0];
	return e.parentNode;
}

//Rmove Element
function $RE()
{
	var e = (typeof arguments[0] == "string") ? $E(arguments[0]) : arguments[0];
	var p = e.parentNode;
	
	if(p == null) { return; }
	
	//remove child
	while(e.firstChild)
	{
		$RE(e.firstChild);
	}
	
	//remove this
	p.removeChild(e);
}

//Element Property
function $EP()
{
	if(arguments.length < 2)
	{
		return null;
	}
	
	var el = (typeof arguments[0] == "string") ? $E(arguments[0]) : arguments[0];
	
	if(arguments.length == 2)
	{
		return el[arguments[1]];
	}
	
	el[arguments[1]] = arguments[2];

	return null;	
}

//Element CSS
function $ES()
{
	if(arguments.length < 2)
	{
		return null;
	}
	
	var el = (typeof arguments[0] == "string") ? $E(arguments[0]) : arguments[0];
	
	if(arguments.length == 2)
	{
		return el.style[arguments[1]];
	}
	
	el.style[arguments[1]] = arguments[2];

	return null;	
}

//css by class name
function $CSS()
{
	if(arguments.length == 0)
	{
		return null;
	}
	
	//
	// origin codes from Shawn Olson(www.shawnolson.net)
	//
	var object_cssRule = null;
	var cssRules = "";
	if(Browser.IE)
	{
		cssRules = 'rules';
	}
	else 
	{
		cssRules = 'cssRules';
	}
	
	var class_name = "." + arguments[0];
	var lengthSheets = document.styleSheets.length; 
	for(var i = 0; i < lengthSheets; i++)
	{
		var lengthRules = document.styleSheets[i][cssRules].length;	
		for(var j = 0; j < lengthRules; j++)
		{
			if(document.styleSheets[i][cssRules][j].selectorText == class_name)
			{
				object_cssRule = document.styleSheets[i][cssRules][j];
				j = lengthRules;
				i = lengthSheets;
			}
		}
	}
	
	if(arguments.length == 1)
	{
		return object_cssRule;
	}
	else if(arguments.length == 2)
	{
		return object_cssRule.style[arguments[1]];
	}
	
	object_cssRule.style[arguments[1]] = arguments[2];
	return null;
}

//Get Element Related WAFX Widget
function $WE()
{
	var e = (typeof arguments[0] == "string") ? $E(arguments[0]) : arguments[0];
	
	while(e != null)
	{
		if(typeof e['widget'] == "object")
		{
			return e.widget;
		}
		
		e = $PE(e);
	}
	
	//global Application
	return theApp;
}


window.BROWSERSAFE_EVENT = null;

var MOUSECLICK = "click";
var MOUSEOVER  = "mouseover";
var MOUSEOUT   = "mouseout";
var MOUSEDOWN  = "mousedown";
var MOUSEUP    = "mouseup";
var MOUSEMOVE  = "mousemove";
var MOUSEWHEEL = "mousewheel";

var KEYDOWN    = "keydown";
var KEYUP      = "keyup";
var KEYPRESS   = "keypress"; 
var KILLFOCUS  = "blur";
var SETFOCUS   = "focus";
var PASTE      = "paste";
var SELECT     = "select";
var DRAG	   = "drag";
var DROP       = "drop";
var MOVE       = "move";

function ON_EVENT(eType, el, handlerName)
{
	if(typeof el == "string")
	{
		el = $E(el);	
	}
	
	if(el == null)
	{
		/* warning null mapping */
		return;
	}
	
	el[eType] = handlerName;
	el["on" + eType] = ROUTE_EVENT;	
}

function ON_EVENTKEY(eType, el, handlerName)
{
	if(typeof el == "string")
	{
		el = $E(el);	
	}

	if(el == null)
	{
		/* warning null mapping */
		return;
	}

	el[eType] = handlerName;
	el["on" + eType] = ROUTE_KEYEVENT;
}

function ON_EVENTMOUSE(eType, el, handlerName)
{
	if(typeof el == "string")
	{
		el = $E(el);	
	}

	try
	{
		el[eType] = handlerName;
		el["on" + eType] = ROUTE_MOUSEEVENT;
	}
	catch(e)
	{
		alert(e.description);
	}
}

function ON_EVENT_TERMINATE(eType, el)
{
	if(typeof el == "string")
	{
		el = $E(el);	
	}

	if(el == null)
	{
		/* warning null mapping */
		return;
	}
	
	var count = el.length;
	if(count)
	{
		for(var i = 0; i < count; i++)
		{
			el[i]["on" + eType] = ROUTE_TERMINATE;	
		}	
	}
	else
	{
		el["on" + eType] = ROUTE_TERMINATE;	
	}
}

function ON_EVENT_DEFAULT(eType, el)
{
	if(typeof el != 'object') { return; }
	
	var count = el.length;
	if(count)
	{
		for(var i = 0; i < count; i++)
		{
			el[i]["on" + eType] = ROUTE_DEFAULT;	
		}	
	}
	else
	{
		el["on" + eType] = ROUTE_DEFAULT;	
	}
}

function ROUTE_DEFAULT()
{
	return true;	
}

function ROUTE_TERMINATE()
{
	return false;	
}

function ROUTE_EVENT()
{
	window.BROWSERSAFE_EVENT = arguments[0] || window.event;
	
	var widget = $WE(this);	

	if(widget == null || typeof widget[this[window.BROWSERSAFE_EVENT.type]] != "function")
	{
		return true;
	}
	
	return widget[this[window.BROWSERSAFE_EVENT.type]](this);
}

function ROUTE_KEYEVENT()
{
	window.BROWSERSAFE_EVENT = arguments[0] || window.event;
	var key = (Browser.IE) ? window.BROWSERSAFE_EVENT.keyCode : window.BROWSERSAFE_EVENT.which;
	
	var widget = $WE(this);
	if(widget == null || typeof widget[this[window.BROWSERSAFE_EVENT.type]] != "function")
	{
		return true;
	}

	return widget[this[window.BROWSERSAFE_EVENT.type]](this, key);
}

function ROUTE_MOUSEEVENT()
{
	window.BROWSERSAFE_EVENT = arguments[0] || window.event;
	
	var x = 0;
	var y = 0;
	var button = 0;
		
	if(window.BROWSERSAFE_EVENT.pageX || window.BROWSERSAFE_EVENT.pageY)
	{
		//Gecko
		button = window.BROWSERSAFE_EVENT.which;
		x = window.BROWSERSAFE_EVENT.pageX;
		y = window.BROWSERSAFE_EVENT.pageY;
	}
	else
	{
		button = window.BROWSERSAFE_EVENT.button;	
		x = window.BROWSERSAFE_EVENT.clientX + document.body.scrollLeft - document.documentElement.clientLeft + 2;
		y = window.BROWSERSAFE_EVENT.clientY + document.body.scrollTop  - document.documentElement.clientTop + 2;
		
		//IE
		//Prohibit Event Bubbling
		//window.BROWSERSAFE_EVENT.cancelBubble = true; 
	}
	
	var widget = $WE(this);
	if(widget == null || typeof widget[this[window.BROWSERSAFE_EVENT.type]] != "function")
	{
		return true;
	}

	return widget[this[window.BROWSERSAFE_EVENT.type]](this, button, x, y);
}


var FORM = 
{
	copy: function()
	{
		alert(this.id);
	}
}

/*
 *	 Global Application Class 
 * 
 */
var Application = Class.create();
Application.prototype =
{
	constructorFunction: function()
	{
	},
	initInstance: function()
	{
		//Enable pixelRuller Object
		if(typeof pixelRuller == "object")
		{
			pixelRuller.init();
		}
		
		//Override Forms
		if(document.forms)
		{
			for(var i = 0; i < document.forms.length; i++)
			{
				var form = document.forms[i];
				for(var property in FORM)
				{
					if(typeof FORM[property] == "function")
					{
						form[property] = FORM[property];
					}
				}
			}
		}
	},
	exitInstance: function()
	{
	},
	onInitialLocation: function()
	{
	},
	onChangedLocation: function(hash, hashData)
	{
	}
}

/////////////////////////////////////////////////////////////////
//Instate Application
//
var theApp = null;
window.onload = function() 
{
	if(theApp == null)
	{
		return;
	}
	
	theApp.initInstance();
}