/**********************************************
 * Element():
 *
 * Create a DOM element with attributes.
 **********************************************/
	
	function Element(name, atts, styles)
	{
		var _el = document.createElement(name);
		
		for(var i in atts)
			if(typeof atts[i]=='function')
				_el[i] = atts[i];
			else
				_el.setAttribute(i, _el[i] = atts[i]);
		
		for(var i in styles) _el.style[i] = styles[i];
		
		return _el;
	}
	
/**********************************************
 * Text():
 *
 * Create a DOM text element.
 **********************************************/
	
	function Text(text)
	{
		return document.createTextNode(text);
	}
	
/**********************************************
 * parseQuery():
 *
 * parse the query into a collection, key referenced.
 **********************************************/
	
	function parseQuery()
	{
		if(!location.search) return {};
		
		var queryStr = location.search.substr(1).split('&'),
			queryArr = {};
		for(var i = 0; i < queryStr.length; i++)
		{
			queryStr[i] = queryStr[i].split('=');
			queryArr[queryStr[i][0]] = queryStr[i][1] || '';
		}
		
		return queryArr;
	}
	
/**********************************************
 * parseCookie():
 *
 * parse the cookies into a collection, key referenced.
 **********************************************/
	
	function parseCookie()
	{
		var cookieArr = {};
		
		if(document.cookie.length)
		{
		    var cookieStr = document.cookie.split(/;\s*/);
    		
		    for(var i = 0; i < cookieStr.length; i++)
		    {
			    cookieStr[i] = cookieStr[i].split('=');
			    cookieArr[cookieStr[i][0]] = cookieStr[i][1] || '';
		    }
		}
		
		cookieArr.__setValue = function(key, val, exp) {
			cookieArr[key] = val;
			document.cookie = 
				key + '=' + val + '; expires=' + 
				new Date(new Date().getTime()+(exp || 1800000)).toUTCString()
				+ '; path=/';
		};
		
		return cookieArr;
	}
	
/**********************************************
 * truncateHandlers():
 *
 * Truncate all handlers in the node tree.
 **********************************************/
	
	function truncateHandlers(node)
	{
		//====================================================
		// In IE, even the garbage collector itself is a junk,
		// it didn't collect event handlers of trashed nodes.
		//====================================================
		for(var evtName in node)
			if(/on.*/i.exec(evtName) && typeof node.evtName == 'function')
				node.evtName = null;
		
		for(var i=0;i<node.childNodes.length;i++)
			truncateHandlers(node.childNodes[i]);
	}
	
/**********************************************
 * truncateChildren():
 *
 * Truncate all child nodes in the node tree.
 **********************************************/
	
	function truncateChildren(node)
	{
		for(var i=node.childNodes.length;i>0;i--)
			node.removeChild( node.childNodes[0] );
	}

/**********************************************
 * toggle():
 *
 * Toggle visibility of element(s).
 **********************************************/
	
	function toggle(element) {
		if( element.length ) {
			for(var i=0; i<element.length; i++)
				toggle( element[i] );
			return;
		}
		
		if( typeof element == 'string' )
			element = $(elements[i]);
		
		with( element.style ) display = 
			( System.GetStyle(element, 'display') == 'block'? 'none' : 'block' );
	}
	
/**********************************************
 * hide():
 *
 * Hide element(s).
 **********************************************/
	
	function hide(element) {
		if( element.length ) {
			for(var i=0; i<element.length; i++)
				hide( element[i] );
			return;
		}
		
		if( typeof element == 'string' )
			element = $(elements[i]);
		
		with( element.style ) display = 'none';
	}
	
/**********************************************
 * show():
 *
 * Show element(s).
 **********************************************/
	
	function show(element) {
		if( element.length ) {
			for(var i=0; i<element.length; i++)
				show( element[i] );
			return;
		}
		
		if( typeof element == 'string' )
			element = $(elements[i]);
		
		with( element.style ) display = 'block';
	}

//========================================================================
//.. Pre-define constants -:
//------------------------------------------------------------------------
	
	/* Query String & Cookies */
	var $_GET		= parseQuery();
	var $_COOKIE	= parseCookie();
	
//========================================================================
//.. Main System Object -:
//------------------------------------------------------------------------
	
var System = {
	
		GetStyle : function(node, style)
		{
			var win = document.defaultView || window;
			
			return ( win.getComputedStyle && win.getComputedStyle(node, null).getPropertyValue
				(style.replace(/[A-Z]/g, function(match) { return '-' + match.toLowerCase() })) ) || 
				( node.currentStyle || node.style )[style];
		}, 
		
		GetStyles : function(node, styles)
		{
			for(var style in styles)
				styles[style] = this.GetStyle(node, style);
			
			return styles;
		}, 
		
		HasClassName : function(node, className)
		{
		    if(typeof node.className != 'string') return;
		    
		    var tmp = node.className.split(' ');
		    
		    return tmp.indexOf(className) >= 0;
		},
		
		AddClassName : function(node, className)
		{
		    if(typeof node.className != 'string') return;
		    
		    var tmp = node.className.split(' ');
		    
		    if(tmp.indexOf(className) < 0)
		        tmp.push(className);
		    
		    node.className = tmp.join(' ');
		},
		
		RemoveClassName : function(node, className)
		{
		    if(typeof node.className != 'string') return;
		    
		    var tmp = node.className.split(' ');
		    
		    tmp.remove(className);
		    
		    node.className = tmp.join(' ');
		}
		
	//===========================================
};
