/**
 * rDom Lite - based on rDom v0.5
 *
 * Functions:
 * ~~~~~~~~~~
 * Object bindToObj(el,obj) - binds a function to an object
 * bool   addEvent(el,ev,f,bindObj[optional]) - adds an event handler to an element
 * bool   cssClassAdd(el,c)   - adds a css class
 * bool   cssClassCheck(el,c) - check whether a class is in an lement
 * bool   setInitHandler(obj) - Sets a handler for the onload event of window
 * String trim(s) - not dom related but used to trim a string
 * stopEvent (e)	- stops an event bubbling
 * $			 - an alias to document.getElementById()
 */
var rDomLite = {	
	bindToObj : function (f, obj) {
		return function() {f.apply(obj, arguments)};
	},
	
	addEvent : function (el,ev,f,bindObj)
	{
		if (typeof el == 'string') {
			el = document.getElementById(el);
		}
		if(el === null || typeof el == 'undefined') return false;
		var func = f;
		
		if (typeof bindObj != 'undefined' 
		    && typeof func.apply == 'function') {
				func = this.bindToObj(f, bindObj);
		}

		if (el.attachEvent) {
			el.attachEvent('on' + ev, func);
		} else if (el.addEventListener) {
			el.addEventListener(ev, func, false);
		} else {
		
			el['on' + ev] = func;
		}
		return true;
	},
	
	
	trim : function (s)
	{
		while (s.substring(0,1) == ' ')
			s = s.substring(1, s.length - 1);
		while (s.substring(s.length - 1, 1) == ' ')
			s = s.substring(0, s.length - 1);
			
		return s;
	},
	
	cssClassAdd : function (el,c) {
		if (!el) return false;
		if (!this.cssClassCheck(el,c)) {
			if (el.className.length) 
				el.className += (' ' + c);
			else
				el.className = c;
		}
		return true;
	},
	
	cssClassCheck : function (el,c) {
		if (!el || !el.className) return;
		var regex = new RegExp("\\b" + c +"\\b");
		return regex.test(el.className);
	},
	
	setInitHandler : function(obj) {
		this.addEvent(window, 'load', obj.init, obj);
	},
		
	stopEvent : function (e)
	{
		if (!e && !window.event) return;
		if (!e) e = window.event;
		(e.stopPropagation) ? e.stopPropagation() : (e.stopBubble = true);
		(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
	},
	
	$ : function(s) {
		return document.getElementById(s);
	},
	
		/**
	* Creates a DOM on load handler, allows you to modify the DOM
	* when its finished loading (even if other elements weren't loaded yet)
	* Based on Solutions by: Dean Edwards/Matthias Miller/John Resig
	*
	* URL: http://dean.edwards.name/weblog/2006/06/again/
	*
	* Konqueror and Multiple Loaders via MSIE support added by Max Raskin (24.06.07).
	*
	* Browsers Tested with : IE6, IE7, FireFox 2.0/Win, FireFox 1.5/Linux, Konqueror 3.5.2/Linux
	*
	* Function Assembled by: Max Raskin
	*
	* @param Function func 			  - callback function to call when DOM's loaded.
	* @param Object[optional] bindObj - an object to bind the function to
	*
	* @return bool - true if succeeded setting a DOM event listener, false if
	*				 reverted to regular onload listener
	*/
	setDOMLoadHandler : function(f, bindObj) {
		var func = f;
		
		// Bind to an object if specified
		if (typeof bindObj != 'undefined' 
		    && typeof func.apply == 'function') {
				func = this.bindToObj(f, bindObj);
		}
		/* for Mozilla/Opera9 */
		if (document.addEventListener 
			&& !/WebKit/i.test(navigator.userAgent)
			&& !/Konqueror/i.test(navigator.userAgent)) {
					
		    document.addEventListener("DOMContentLoaded", func, false);
		    return true;
		}
		
		/* for Internet Explorer */
		/*@cc_on @*/
		/*@if (@_win32)
			var num = 1;
			while (document.getElementById("__ie_onload" + num)) {
				num++;
			}
			
		    document.write("<script id=__ie_onload" + num + " defer src=javascript:void(0)><\/script>");
		    var script = document.getElementById("__ie_onload" + num);
		    script.onreadystatechange = function() {
		        if (this.readyState == "complete") {
		        
		            func(); // call the onload handler   
		        }
		    };
		    return true;
		  
		/*@end @*/
		
		
		
		/* for Safari/Konqueror */
		if (/WebKit/i.test(navigator.userAgent) 
			|| /Konqueror/i.test(navigator.userAgent)) {
		    var _timer = setInterval(function() {
		        if (/loaded|complete/.test(document.readyState)) {
		        	// call the onload handler
		            var initfunc = function(event,_timer) {
		            	clearInterval(_timer);
		            	func(event);
		            }
		            initfunc(event,_timer);
		        }
		    }, 10);
		    return true;
		}
		
		// In case all above failed, use regular load event handler
		this.addEvent(window, 'load', func);
		return false;
	}
	
};



