/*
	Modules:
  		+ ajax
*/

function print_r(obj)
{
	var ret = "object {\r\n";
	for(key in obj){
		ret += "\t" + key + ":" + obj[key] + "\r\n";
	}
	ret += "}";
	return ret;
}

function gettype (obj){
	var type = typeof obj;
	if (type == 'object'){
		if (obj.getDate) return 'object';
		if (obj.split) return 'string';
		return object;
	}
	return type;
}

function clsClient(location) { 
	if(!location) location = "/mods/client/v1/client.php";
	this._construct(location) 
}
(function() { // to create local-scope variables
	var index = 0;
	var items = new Array();		
	var test;
	var _req;
	clsClient.onUpdate = function(){
		items[index].onUpdate();
	}
	clsClient.prototype = {
        _construct: function(location){
			index++;
			_req = new Subsys_JsHttpRequest_Js();
			_req.onreadystatechange = function(){
				if (_req.readyState == 2) {
					clsClient.onUpdate();
				}
			}
			
			this.location = location;
			//this.vars = {};
			//client_id = this.getCookie("client_id");
			//if(false) {	
				//svars = this.getCookie("svars");
			//} else {
				//client_id = '<?=$ClientId?>';
				//svars = '<?=$SVars?>';
			//}
			//this.vars = this._phps.unserialize(this.svars);	
			this.SaveCookie();
			items[index] = this;			
		},
		SaveCookie: function()
		{
			//this.setCookie("client_id", this.client_id);
			//this.setCookie("svars", this.svars);
		},
		Refresh: function()
		{			
			//this.svars = this._phps.serialize(this.vars);
			//this.SaveCookie();	
		},
		onUpdate: function()
		{
		},
		Update: function(q){			
			if(!this.location) return false;
			this.Refresh();
			//var url = this.location+"?cid="+this.client_id+"&svars="+this.svars;
			//_req.open("GET", url, true);
			//_req.send(null);	
			//_req.open("GET", url, true);
			_req.caching = false; 
			_req.open("GET", this.location, true);
			_req.send({cid: this.client_id, vars: q});
		},
		getVar: function(name)
		{			
			for(v in this.vars)
				if(v == name) return this.vars[v];		
			return false;
		},
		getClientFlashDataObj: function(){
			this.Refresh();
			var data = {
				client_id: this.client_id,
				location : this.location,
				svars : this.svars
			}
			s = this._phps.serialize(data);
			s = s.split("\"").join("%22");
			return s;
		},	
		setCookie: function(name, value, expires, path, domain, secure){
			document.cookie= name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		},		
		getCookie: function(name){
			var dc = document.cookie;
			var prefix = name + "=";
			var begin = dc.indexOf("; " + prefix);
			if (begin == -1) {
				begin = dc.indexOf(prefix);
				if (begin != 0) return null;
			} else {
				begin += 2;
			}
			var end = document.cookie.indexOf(";", begin);
			if (end == -1) {
				end = dc.length;
			}
			return unescape(dc.substring(begin + prefix.length, end));	
		},		
		clearCookie: function (name){
			this.setCookie(name,"",-1);
		}		
	}
})();

