/**
 * Common
 * js.Common
 *
 * Common is a group of utilies, functions, prototypes that are used to simplifiy common tasks in javascript.
 * This file should be attached to all pages useing any of the classes contained in these folders.
 */

function Common()/*:void*/
{
	//empty
}
 
var Utils/*:Object*/ = {
	
	//Browser safe way to find out if an object exists
	isEmpty : function(value/*:Object*/)/*:Boolean*/
	{
		if((typeof value != 'undefined' && value != null && value != '' && value != false) || value > 0) return false;
		return true;
	},
	
	isIE : function()/*:Boolean*/
	{
		return (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	},
	
	isWin : function()/*:Boolean*/
	{
		return (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	},
	
	isOpera : function()/*:Boolean*/
	{
		return (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
	},
	
	$ : function(id/*:String*/)/*:HtmlObject*/
	{
		if(document.getElementById)
		{
			return document.getElementById(id);
		}
		else if(document.all)
		{
			return document.all[id];
		}
		return null;
	},
	
	showHideElement : function(id/*:String*/, show/*:Boolean*/)/*:void*/
	{
		var value/*:String*/ = (show) ? 'block' : 'none';
		Utils.setStyle(id, "display", value);
	},
	
	setStyle : function(id/*:String*/, styleName/*:String*/, styleValue/*:String*/)/*:void*/
	{
		var ele/*:HtmlObject*/ = Utils.$(id); 
		ele.style[styleName] = styleValue;
	},
		
	write : function (id/*:String*/, value/*:String*/)/*:void*/
	{
		var ele/*:HtmlObject*/  = Utils.$(id); 
		ele.innerHTML = value;
	},
	
	redirect : function (url/*:String*/)/*:void*/
	{
		if( !Utils.isEmpty(url) && url != "#")
		{
			window.location = url;
		}
	},
	
	encodeQuery : function(value/*:Object*/)/*:String*/
	{
		var tmp/*:Array*/ = new Array();
		for(var key/*:String*/ in value)
		{
			tmp.push(key + '=' + value[key]);
		}
		return (tmp.length > 0)? tmp.join("&") : '';
	},
	
	decodeQuery : function(value/*:String*/)/*:Object*/
	{
		var tmp/*:Array*/ = value.contains('&') ? value.split('&') : [value];
		var obj/*:Object*/ = {};
		var i/*:Number*/ = tmp.length;
		while(i--)
		{
			var a/*:String*/ = tmp[i].toString().split('=');
			var n/*:String*/ = a[0];
			var v/*:String*/ = a[1];
			obj[n] = v;
		}
		return obj;
	},
	
	addUIReference : function(prefix/*:String*/, id/*:String*/)/*:void*/
	{
		var ele = new UIReference(id);
		window[prefix + "$" + id] = ele;
	}
	
}

String.prototype.contains = function(value/*:String*/)/*:Boolean*/
{
	return (this.indexOf(value) != -1);
}


/**********************
 * UIReference Class
 **********************/

function UIReference( targetID/*:String*/ )
{
	this.private.target = Utils.$( targetID );
}
 
UIReference.prototype = {
	
	private : {
		dataType 	: 'UIReference',
		target 		: null,
		children 	: new Array()
	},
	
	addChild : function(value/*:UIComponent*/)/*:void*/
	{
		this.private.children.push(value);
		this.updateDisplayList();
	},
	
	removeChild : function(value/*:UIComponent*/)/*:UIComponent*/
	{
		var i = this.private.children.length;
		var child = null;
		while(i--)
		{
			if(this.private.children[i] == value)
				child = this.private.children.splice(index, 1);
		}
		this.updateDisplayList();
		return child;
	},
	
	updateDisplayList : function()/*:void*/
	{
		//add all children to display
		var html = "";
		for(var i = 0; i < this.private.children.length; i++)
		{
			var child = this.private.children[i];
			if(!Utils.isEmpty(child.toHtmlString))
				html += child.toHtmlString();
			else
				html += child;
		}
		this.private.target.innerHTML = html;
	},
	
	toHtmlString : function()/*:String*/
	{
		this.updateDisplayList();
		return this.private.target.innerHTML
	}
}
