// entrypoint function
//
function $_(e)
{
	return obj = new $_obj(e);
}



/*
Constructor

Parameters:
	mixed - either a DOM object, or the id of a DOM object
*/
$_obj = function (e)
{
	this._ = null;
	if (e)
	{
		if (typeof(e) == "string")
		{
			var re = document.getElementById( e );
			if (re != null  &&  typeof(re.appendChild) != "undefined" ) {
				this._ = re;
			}
		}
		else if (typeof(e.appendChild) != "undefined")
			this._ = e;
	}
	
	
	this.utils =
	{
		cleanNumber : function(str, dec)
		{
			str = str.toString().replace(",",".") - 0;
			if (isNaN(str))
				str = "0";
			
			if (dec>0)
			{
				str = str.toString();
				if (str.lastIndexOf(".")==-1)
					str += ".";

				while (str.lastIndexOf(".") >= str.length-dec)
					str += new String("0");
			}
			else if (dec === 0)
				Math.round(str);

			return str.replace(".",",");
		}
	};
};



/*
Method: childs

Parameters:
	n - Integer - index

Returns:
	mixed - DOMCore instance if single, childNodes array if plural
*/
$_obj.prototype.childs = function (n)
{
	var ele = this._;
	if (!ele)
		return false;
	
	var c = this._.childNodes.length;
	if (!c)
		return false;
	else if (n!=null && n<c)
		return new $_obj(this._.childNodes[n]);
	else if (c==1)
		return new $_obj(this._.childNodes[0]);
	else
		return this._.childNodes;
}


/*
Method: position

Returns:
	Object - {x:Integer, y:Integer}
*/
$_obj.prototype.position = function ()
{
	var obj = this._;
	
	if (obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft;
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			curleft += obj.x;
		
		
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop;
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curtop += obj.y;

		return {x:curleft, y:curtop};
	}
	else
	{
		return null;
	}
};



/*
Method: empty
Removes all child nodes
*/
$_obj.prototype.empty = function ()
{
	var ele = this._;
	if (!ele)
		return false;
	
	while (ele.childNodes.length)
		ele.removeChild( ele.childNodes[0] );

	return this;
};



/*
Method: appendContent
*/
$_obj.prototype.appendContent = function ( cont, dom )
{
	var ele = this._;
	if (!ele)
		return false;
	
	if (typeof(cont) == "string" && dom)
		ele.appendChild( document.createTextNode(cont) );
	else if (typeof(cont) == "string")
		ele.innerHTML = cont;
	else if (typeof(cont) == "object")
		ele.appendChild( cont );
	
	return this;
};



// replace content
//
$_obj.prototype.replaceContent = function ( cont, dom )
{
	this.empty();
	this.appendContent(cont,dom);
	
	return this;
};



// get parent
//
$_obj.prototype.parent =  function ()
{
	return new $_obj(this._.parentNode);
};



// show
//
$_obj.prototype.show = function ()
{
	if (this._)
		this._.style['display'] = "block";
	
	return this;
};



// hide
//
$_obj.prototype.hide = function ()
{
	if (this._)
		this._.style['display'] = "none";
	
	return this;
};



// appendElement
//
$_obj.prototype.appendElement = function ( type, attr, cont, dom )
{
	var ele = this._;
	if (!ele)
		return false;
	
	var ne = document.createElement( type );
	if (!ne)
		return false;

	for (var a in attr)
		ne[a] = attr[a];
	
	if (typeof(cont) == "string" && dom)
		ne.appendContent( document.createTextNode(cont) );
	else if (typeof(cont) == "string")
		ne.innerHTML += cont;
	else if (typeof(cont) == "object")
		ne.appendContent.appendChild( cont );
	
	this.appendContent(ne);
	
	return this;
};



// ajax methods

$_obj.prototype.ajax = function ()
{
	var a = {
		req : null,
		result : "",

		request : function (query, onwait, ondata, onerror)
		{
			this.query = query;
			this.cb_wait = onwait;
			this.cb_data = ondata;
			this.cb_error = onerror;
			this.req = this.getAjax();

			var nocacheuri = this.query + "&ts=" + new Date().getTime();
			var r = this.req;
			var doneWait = false;
			var p = this;

			r.open( "GET", nocacheuri, true );

			r.onreadystatechange = function ()
			{
				/*
				0 = uninitialized
				1 = loading
				2 = loaded
				3 = interactive
				4 = complete
				*/
				
				
				if (r.readyState < 4)
				{
					if (!doneWait)
					{
						p.onWait( r );
						doneWait = true;
					}
				}
				else
				{
					switch (r.status)
					{
						case 200: // "OK"
							p.onData( r );
							break;

						case 304: // "Not modified" - should happen because we are avoiding the browser cache

							break;

						default:
							p.onError( r );
					}
				}
			};

			r.send( null );
		},

		getAjax : function ()
		{
			var oXHR;

			// Returns a new Ajax object (cross-browser) if available, false if not.
			try
			{  
				oXHR = new ActiveXObject('Msxml2.XMLHTTP');
			}
			catch (e)
			{
				try
				{
					oXHR = new ActiveXObject('Microsoft.XMLHTTP');
				}
				catch (e2)
				{
					try
					{
						oXHR = new XMLHttpRequest();
					}
					catch (e3)
					{
						oXHR = false;
					}
				}
			}

			return oXHR;
		},

		onWait : function (oXHR)
		{
			this.cb_wait( this );
		},

		onData : function (oXHR)
		{
			this.result = this.req.responseText;
			this.cb_data( this );
		},

		onError : function (oXHR)
		{
			this.cb_error( this );
		}
	};
	
	
	return a;
};




