/**
 * @author pversai
 */

/**
 * The RDCOM object is the single global object used by YUI Library.  It
 * contains utility function for setting up namespaces, inheritance, and
 * logging.
 * @module rdcom
 * @title  RDCOM Global
 */

if(typeof RDCOM == "undefined"){
	/**
	 * The RDCOM global namespace object
	 * @class RDCOM
	 * @static
	 */
	var RDCOM = {};
};

if(typeof RDCOM.utils == "undefined"){
	RDCOM.utils = {};
};

/**
 * @method include
 * @classDescription includes any file to a page as js.
 * @param {Object} strPath
 */
RDCOM.utils.includeFile = function(strPath){
	var head = document.getElementsByTagName('head').item(0);
	script = document.createElement('script');
	script.src = strPath;
	script.type = 'text/javascript';
	head.appendChild(script);
	return script;
}

/**
 * @method include
 * @classDescription user friendly class import function.
 * @param {Object} strNamespace
 */
RDCOM.utils.include = function(strNamespace){
	var namespaceList = Array();
	namespaceList["Array"] = "/js/lib/rd/Array.js";
	namespaceList["Events"] = "/js/lib/rd/EventRouter.js";
	namespaceList["ValidateForm"] = "/js/lib/offer/validateForm.js";
	namespaceList["Ajax"] = "/js/lib/rd/net.js";
	return RDCOM.utils.includeFile(namespaceList[strNamespace]);
}

/**
 * @method URL
 * @classDescription Manipulates url in the string to get appropreate parameters
 */
RDCOM.utils.URL = function(url){
	this.url = url || window.location.href;
	var params = this.url.split("?");
	this.host = params[0];
	if (params[1]) {
		var queryString = params[1];
		var parameters = queryString.split("&");
		for (var i = 0; i < parameters.length; i++) {
			var params_values = parameters[i].split("=");
			this.parameters[params_values[0]] = params_values[1];
		}
	}
}
RDCOM.utils.URL.prototype = {
	url:new String(),
	parameters:new Array(),
	host:new String(),
	getURL:function (){
		return this.url.href;
	},
	getParameter:function(paramName){
		if (typeof this.parameters[paramName] ==  "undefined")
			return null;
		
		return this.parameters[paramName];
	}, 
	getHost:function(){
		return this.host.split("/")[2];
	}
}

/**
 * Adds event listner object to specifyed element
 * @param {Function} fn
 * @param {Object} elm Element to register the event on
 * @param {String} evType Event type
 * @param {Boolean} useCpature event registration type
 */
RDCOM.utils.addEvent = function(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}
/**
 * Adapts the event element to IE and DOM browsers compatibility
 * @param {Object} e
 */
RDCOM.utils.Event = function(e){
	try{
		var event = e || window.event;
		this.target = event.currentTarget || event.srcElement;
		if (this.target.nodeType == 3) // defeat Safari bug
			this.target = targ.parentNode;
		this.type = event.type;
	} catch (error){}
}


/**
 * Cookies!
 * Static Object that reads and writes cookies
 */

RDCOM.utils.BrowserCookies = {
	read:function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	write:function(name,value,days,domain){
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		
		if(domain !=null && typeof domain != 'undefined'){
			domain = "; domain=" + domain;
		}	    
				
		document.cookie = name+"="+value+expires+domain+"; path=/";
	}
}

/**
 * Wrapper objects
 */
var rdOnloadManager = {
	addListener: function(func){
		RDCOM.utils.addEvent(window, "load", func, false);
	}
}
var rdInclude = RDCOM.utils.includeFile;