/*
	.NET-ish JS page framework
	(c) 2010 Rob Watkins
	This code is free to use for any purpose, provided the copyright notice and credit to original author is maintained.
	The code is provided AS IS. No guarantee is made on the fitness of this code for any purpose. Use at your own risk.
*/
/*   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 
 *   NO DEPENDENCIES!
 *   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  */
var HUI = 
{
	// Page functions
	Page: HUI_Page,
	
	MAIN:function(pageclass)
	{
		pageclass.prototype = new HUI.Page;
		var res = new pageclass();
		res.queryString = this.getQueryString();
		
		HUI.attachPageToFramework(res);
		res.init();
		
		// Removed references to HUI event classes to eilimate dependencies; this page structure can be used 
		// with any framework to clarify the page
		var hui = this;
		
		if(window.attachEvent) 
			window.attachEvent('onload', function(e) { hui.loadHandler(e, null, res); }, false); 
		else if(window.addEventListener)
			window.addEventListener('load', function(e) { hui.loadHandler(e, null, res); }, false);	
		//
		
		return res;
	},
	
	loadHandler:function(e, id, page)
	{
		page.load(e, id, page);
	},
	
	// You can supply another function here so that external frameworks can plug their own events into the page.
	// For example, the jQuery wrapper tiny2_jquery_defaults.js adds a .ready() event to the page that fires on the jQ ready event.
	attachPageToFramework:function(pageinstance)
	{		
	},

	getQueryString:function()
	{
		var res = null;
		var s = window.location.search;
		s = typeof(s) != 'undefined' && s != null && s != '' ? s.substring(1) : null;
		
		if(s)
		{
			res = {};
			
			var a = s.split(/&/);
			var re = /^([^=]+)=(.*)$/;
			for(var i = 0; i < a.length; i++)
				if(re.exec(a[i]))
					res[decodeURIComponent(RegExp.$1)] = RegExp.$2 == '' ? null : decodeURIComponent(RegExp.$2);					
		}		

		return res;
	}
};

function HUI_Page()
{
	// Called when page first set up - NO DOM WILL BE LOADED
	this.init = function(){};
	// Called on document load, so the DOM will be loaded
	this.load = function(){};
}