/**

	Title : REDScript

	Ver : 0.1

	Author : RED

	Email : nosteel@gmail.com

	Modified : 2008. 10. 21  15:08:02

	Manual :

	Comment :

**/


/* AJAX
AJAX({
	sync : false, //Default
	method : "GET", //Default
	url : u,
	arequest : "abc=1234567890", //Default null

	target : "aaa" //
		OR
	rfunction : function (q) {document.getElementById(t).innerHTML = q;}
	waiting : function () {}
});
//*/
//document.domain = "pandora.tv";
var browser = function () {
	var agent = navigator.userAgent.toLowerCase();
	return (agent.indexOf("msie") != -1 && agent.indexOf("opera") == -1 ? "IE" : (agent.indexOf("opera") != -1 ? "OP" : (agent.indexOf("chrome") != -1 ? "CR" : (agent.indexOf("safari") != -1 ? "SF" : (agent.indexOf("firefox") != -1 ? "FF" : "ETC")))));
}

function AJAX(Obj) {
	this.aParam = Obj;
	if (typeof this.aParam.sync != "boolean") {this.aParam.sync=false;}

	if (typeof this.aParam.method != "string") { this.aParam.method="GET"; }
	else if (this.aParam.method.toUpperCase() != "POST") { this.aParam.method="GET"; }


	this.setXMLHTTP();

	if (this.aParam.debug=="on") {
		alert(this.aParam.method);
		alert(this.aParam.url);
		alert(this.aParam.sync);
		alert(this.aParam.arequest);
		alert(this.aParam.rType);
	}
	this.XMLHTTP.open(this.aParam.method, this.aParam.url, this.aParam.sync);
	this.XMLHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	if ((this.aParam.arequest!=null || this.aParam.arequest!="") && this.aParam.method.toUpperCase()=="POST") { this.XMLHTTP.send(this.aParam.arequest);}
	else { this.XMLHTTP.send( null );}

	if (this.aParam.sync) {
		var This = this;
		this.XMLHTTP.onreadystatechange = function() {This.setCollback();}
	} else {
		this.setCollback();
	}
}

AJAX.prototype = {
	setXMLHTTP : function(){
		try {
			if ( window.XMLHttpRequest ) {
				this.XMLHTTP = new XMLHttpRequest();
				if (this.XMLHTTP.readyState == null ) {
					this.XMLHTTP.readyState = 1;
					this.XMLHTTP.addEventListener( "load", function () { this.XMLHTTP.readyState = 4; if ( typeof this.XMLHTTP.onreadystatechange == "function" ) { this.XMLHTTP.onreadystatechange(); } }, false);
				}
			} else { this.XMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );}
		} catch (e) {alert('No browser');}
	},
	setCollback : function(){
		if ( this.XMLHTTP.readyState == 4 ) {

			if ( this.XMLHTTP.status == 200 || this.XMLHTTP.status == 304) {

				var ret = (this.aParam.rType=="xml")? this.XMLHTTP.responseXML : this.XMLHTTP.responseText;
				this.Collback(ret);
			} else {
				var ret = (this.aParam.rType=="xml")? this.XMLHTTP.responseXML : this.XMLHTTP.responseText;
				this.Collback(ret);
			}
		}else {
			if ( typeof this.aParam.waiting == "function" && this.aParam.wait=="") { this.aParam.waiting();this.aParam.wait = "on"; }
		}
	},
	Collback : function (ret){
		if (this.aParam.target) { document.getElementById(this.aParam.target).innerHTML = ret;}
		else if (typeof this.aParam.rfunction == "function") {this.aParam.rfunction(ret);}
		else {this.setData = ret;}
	},
	setWait : function(){}

}


// cookie

var oCookie = {
	set : function (sName, sValue, expireSeconds) {
		var sDomain = ".pandora.tv";
		var todayDate = new Date();
		var ExpireTime = new Date(todayDate.getTime()+expireSeconds*1000);
		document.cookie = sName + "=" + sValue + ";" + ((expireSeconds) ? "expires=" + ExpireTime.toGMTString() + ";" : "") + "domain=" + sDomain + ";path=/;";
	},

	// get cookie value
	get : function (sName) {
		var aCookie = document.cookie.split("; ");
		for (var i=0; i < aCookie.length; i++) {
			var aCrumb = aCookie[i].split("=");
			if (sName == aCrumb[0]) {
				return aCrumb[1];
			}
		}
		return null;
	},

	// delete cookie
	destroy : function (sName) {
		this.set(sName, "", 0);
	},

	// update cookie
	update : function (expireSeconds) {
		var CookieList = document.cookie.split("; ");
		for(var i=0;i<CookieList.length;i++) {
			var Cookies = CookieList[i].split("=");
			if(Cookies[0] == "SavedID") {
				continue;
			}
			this.set(Cookies[0], Cookies[1], expireSeconds);
		}
	}
};


