/*------------------------------------------
|  Core Lib For use with : Chatter - RAC 
|			 (c) kM0ti0n 2005
|
|	web : http://km0ti0n.blunted.co.uk
|  mail : km0ti0n@gmail.com
|  date : 22/01/2006
--------------------------------------------*/


/*Bass Class*/

function Base(){ 
	this.Controls			= (new Controls()).instanceOf();
}

Base.prototype						= new Object();	
Base.prototype.constructor			= Base;
Base.prototype.ClassName			= "Base";
Base.prototype.CSSClass				= "";
Base.prototype.Parent				= null;	
Base.prototype.ParentElement		= null;	
Base.prototype.MultiChildElements	= false;	
Base.prototype.TopElement			= null;	
Base.prototype.IdCounter			= 0;
Base.prototype.Events				= new Array();
Base.prototype.Init					= function (){};
Base.prototype.ToString				= function () {	return this.ClassName;	}
Base.prototype.toString				= function () { return this.ToString();	}
Base.prototype.Dependant			= [];



Base.prototype.PreInit			= function ()
{
	//document.body.onselectstart = function (  ){return false;}	
	this.CSSClass = this.ClassName;
	for( var i = 0; i < this.Events.length; i++)
	{
		this.AddEvent( this.Events[i] );
	}
	this.Base = this.prototype;
}

// Creates and Appends a new element of type tagName to parent
Base.prototype.NewObj = function (tagName, props, parent, text, doc )
{
	var o;
	if (!doc){ doc = document}
	o = doc.createElement(tagName)
	if (props) for (var p in props) o[p]=props[p];
	if (parent) parent.appendChild(o);
	if (text) o.appendChild(doc.createTextNode(text));
	// Adding FindXY functionality to ALL new Objs
	o.FindXY = function ()
	{
		var obj = this, x=0,y=0;
		while (obj!=null)
		{
			x	+=	obj.offsetLeft-obj.scrollLeft;
			y	+=	obj.offsetTop-obj.scrollTop;
			obj	=	obj.offsetParent;
		}
		return {x:x,y:y};
	}
	return o;
}

// Generates a Unique ID for an Element
Base.prototype.NewId = function (cString)
{ 
	if( !cString ) { cString = this.ToString(); }
	this.IdCounter++;
	return cString + this.IdCounter;
}

// Creates a New Event Caller & Handler
Base.prototype.AddEvent = function (cEvent)
{
	var self = this;
	this[cEvent.substring(2)] = new Event(self, cEvent);
	
	var lNew	= true;
	for( var i = 0; i < this.Events.length; i++)
	{
		if( this.Events[i] == cEvent  )
		{
			lNew = false;
		}
	}
	if ( lNew )
	{
		this.Events[this.Events.length] = cEvent;
	}
	self = null;
}

// Clears all Events and remover circular refferences to parent objects;
Base.prototype.Dispose = function ()
{
	this.PreDispose();
	// Insert Custom Class Dispose code here;
	this.PostDispose();
}

Base.prototype.PreDispose = function ()
{
	for( var i = 0; i < this.Events.length; i++)
	{
		this[this.Events[i]] = null;
		this[this.Events[i].substring(2)] = null;
	}
	for( var i = 0; i < this.Controls.length; i++)
	{
		this.Controls[i].Dispose();
		this.Controls[i] = null;
	}
	
}
Base.prototype.PostDispose = function ()
{
	if(this.ParentElement && !this.MultiChildElements)
	{	
		this.ParentElement.innerHTML = "";
	}
	else if( this.TopElement )
	{	
		this.ParentElement.removeChild( this.TopElement );
	}

	this.Parent			= null;	
	this.ParentElement	= null;	
}


// A collection of all the Objects that are Added to the App
function Controls()
{
	this.instanceOf = function ()
	{
		var self = new Array();
		self.Add = function ( oControl )
		{
			if( oControl )
			{	 
				this[this.length] = oControl;
				
				oControl.PreInit();
				oControl.Init();
			}
		}
		return self;
	}
} 

// Event Handler Called from Base.AddEvent("onsomeevent")
// Creates Base.onsomeevent and Base.someevent.Fire(oEvent);
function Event(self, cEvent)	{ this.Self = self; this.cEvent = cEvent; }
Event.prototype					= new Object();
Event.prototype.constructor		= Event;
Event.prototype.Self			= null;
Event.prototype.cEvent			= "";

Event.prototype.Fire			= function (oEvent)
{
	if(this.Self[this.cEvent])
	{
		this.Self[this.cEvent](oEvent);
	}
	else
	{	
		if(this.Self.Parent)
		{
			this.Self.Parent.AddEvent( this.cEvent );
			this.Self.Parent[this.cEvent.substring(2)].Fire(oEvent);
		}
		else
		{
			alert("nothing to do");
		}
	}
}

// Renders png correctly in IE and 
//just renders normal img's for FF
function PNG( imgName, props, parent )
{
	var img = document.createElement( "img" )
	if( window.attachEvent && !window.opera)
	{
		img.src = "images/png.gif";
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + imgName + "\", sizingMethod='image');";
	}
	else
	{
		img.src = imgName;
	}
	if (props) for (var p in props) img[p]=props[p];
	img.style.border = "solid 0px";
	return parent.appendChild( img );
}


function AddClass(obj,cName){ KillClass(obj,cName); return obj.className+=(obj.className.length>0?' ':'')+cName; }
function KillClass(obj,cName){ return obj.className=obj.className.replace(new RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),''); }
function HasClass(obj,cName){ return (!obj || !obj.className)?false:(new RegExp("\\b"+cName+"\\b")).test(obj.className) }
function RemoveClass(obj,cName){ KillClass(obj,cName) }


Array.prototype.removeItem=function(el){
	// *** Removes an item from the Array by value (not index)
	for (var i=0;i<this.length;i++) if (this[i]==el) this.splice(i,1);
}

Array.prototype.indexOf=function(el){
	for (var i=0;i<this.length;i++) if (this[i]==el) return i;
	return -1;
}


var __cCurrentCursor = "";
function SetCursor( cCursor )
{
	if( !cCursor )
	{		
		RemoveClass(document.documentElement, __cCurrentCursor);
	}
	else
	{
		__cCurrentCursor = cCursor;
		if( !HasClass(document.documentElement, __cCurrentCursor) )
		{
			AddClass(document.documentElement, __cCurrentCursor);
		}
	}
}

function FindXY(obj)
{
	var x=0,y=0;
	while (obj!=null)
	{
		x	+=	obj.offsetLeft-obj.scrollLeft;
		y	+=	obj.offsetTop-obj.scrollTop;
		obj	=	obj.offsetParent;
	}
	return {x:x,y:y};
}

if (typeof Number.prototype.toFixed!='function' || (.9).toFixed()=='0' || (.007).toFixed(2)=='0.00') Number.prototype.toFixed=function(f){
	if (isNaN(f*=1) || f<0 || f>20) f=0;
	var s='',x=this.valueOf(),m='';
	if (this<0){ s='-'; x*=-1; }
	if (x>=Math.pow(10,21)) m=x.toString();
	else{
		m=Math.round(Math.pow(10,f)*x).toString();
		if (f!=0){
			var k=m.length;
			if (k<=f){
				var z='00000000000000000000'.substring(0,f+1-k);
				m=z+m;
				k=f+1;
			}
			var a = m.substring(0,k-f);
			var b = m.substring(k-f);
			m = a+'.'+b;
		}
	}
	if (m=='0') s='';
	return s+m;
}


function renderDate(el, e)
{
	if(XAPDatePicker)
	{
		var evt = e || event;
		evt.src = evt.target || evt.srcElement
		var oPicker = new XAPDatePicker();
		oPicker.ParentElement = el;
		oPicker.Parent = null;
		oPicker.Event = evt; 
	//	oPicker.Target = el; 
		oPicker.Init();
	}
}


