/**
 * 
 * 
 * 
 * 
 */

var loc = document.location.toString();
loc = loc.substring(0, loc.lastIndexOf("/")+1);

Application = {
	//host : "http://10.18.61.3:8080/dixi-club/",
	host : loc,
	sid : null,
	user : "",
	status : null,
	pidSrc : null,
	node : null,	// contains Node for downloading content
	date:{
		day		: 	new Date().getDate(),
		month	:	new Date().getMonth()+1,
		year	:	new Date().getFullYear(),
		hour	:	new Date().getHours(),
		min		:   new Date().getMinutes(),
		sec		:	new Date().getSeconds()
	},
	
	getFormatedDate:function (date){
		var part = [];
		if(date){
			if (date instanceof Date){
			part[0] = date.getFullYear();
			part[1] = date.getMonth()+1;
			part[2] = date.getDate();
			} else {
				part = date.split("-");
			}	
		} else {
			part[0] = Application.date.year;
			part[1] = Application.date.month;
			part[2] = Application.date.day;
		}

		part[1] = (part[1].length==1)?"0"+part[1]:part[1];
		part[2] = (part[2].length==1)?"0"+part[2]:part[2];
		return part.join("-");
	}
} 
 
 
function v(){
}

/**
 * 
 * ÐÐ»Ð¾Ð±Ð°Ð»ÑÐ½Ð°Ñ ÐºÐ¾Ð»Ð»ÐµÐºÑÐ¸Ñ ÑÐ·Ð»Ð¾Ð² HTML, ÐºÐ¾ÑÐ¾ÑÑÐµ Ð°ÑÑÐ¾ÑÐ¸Ð¸ÑÐ¾Ð²Ð°Ð½Ñ Ñ Ð¾Ð±ÑÐµÐºÑÐ°Ð¼Ð¸
 * Ð´Ð¾ÑÑÑÐ¿ - $elemets.{Ð·Ð½Ð°ÑÐµÐ½Ð¸Ðµ Ð°ÑÑÐ¸Ð±ÑÑÐ°, ÐºÐ¾ÑÐ¾ÑÑÐ¹ Ð±ÑÐ» Ð¸ÑÐ¿Ð¾Ð»ÑÐ·Ð¾Ð²Ð°Ð½ Ð¿ÑÐ¸ Ð°ÑÑÐ¾ÑÐ¸Ð°ÑÐ¸Ð¸}
 * Ð°ÑÑÐ¸Ð±ÑÑ Ð¿Ð¾ ÑÐ¼Ð¾Ð»ÑÐ°Ð½Ð¸Ñ - js
 *  
 */

var $elements={};


/**
 * 
 * @member Object
 * ÐÑÐ¿Ð¾Ð»ÑÐ·ÑÐµÑÑÑ Ð´Ð»Ñ Ð¾ÑÐ»Ð°Ð´ÐºÐ¸
 * @param prefix - ÑÐµÐºÑÑÐ¾Ð²Ð°Ñ Ð¼ÐµÑÐºÐ°
 * @return String - Ð¿ÐµÑÐµÑÐµÐ½Ñ ÑÐ²Ð¾Ð¹ÑÑÐ² Ð¾Ð±ÑÐµÐºÑÐ° Ð¸ Ð¸Ñ Ð·Ð½Ð°ÑÐµÐ½Ð¸Ð¹
 * Ð¸ÑÐ¿Ð¾Ð»ÑÐ·ÑÐµÑÑÑ Ð½Ð°Ð¿ÑÐ¸Ð¼ÐµÑ ÑÐ°Ðº alert(o.$s("Before insert >>/n")); 
 * 
 */
 
Object.prototype.$s = function(prefix){
 	var result = [];
		for(current  in this){
			var prop = current+"\t:\t";
			prop+=( typeof(this[current]) == "function" ) ? "function(){ ... }" : this[current];
	    	result.push(prop);
	 	}
	return ((prefix) ? prefix : "")+"\n"+result.join(",\n");
};
 
Object.prototype.$append = function(source){
	 if (!source || source == null) return;
		 for (current in source){
			this[current] = source[current];
	 	}
	 return this;	
};

Object.prototype.$r = function(){
	 var res =[];
	 for(var current in this){
 			if (typeof(this[current]) != "function" ){
 			res.push(current+"="+encodeURIComponent(this[current]));
 		}
 	}
	 return res.join("&");
};

Object.prototype.$propertiesCount =  function(){
	 var res =0;
	 for(var current in this) res++
	 return res;
}

Object.$wrap = function(obj){
	var o = new Object();
	obj.$s = o.$s;
	obj.$r = o.$r;
	obj.$append = o.$append;
}


String.prototype.$eval = function(){
	try{
		    if (window.execScript)return window.execScript(this.toString());
		    else return window.eval(this.toString());
	    }catch(e){
	        var msg = (e.message) ? e.message : e.description;
	        var n = (e.number) ? e.number : e.lineNumber;
	        alert("ERROR IN SCRIPT\n"+msg+"\n"+n);
	    }
}

String.prototype.$eq=function(template){
	
	if (this.length != template.length){
		return false
	}
	
	for(var i=0; i<this.length;i++){
		if (this[i]!=template[i]){
			return false
		}
	}
	return true;
}

String.prototype.$trim=function(l,str){
	l = (l) ? l:15;
	str = (str) ? str:"...";
	
	if (this.length > l+str.length){
		return this.substring(0,l)+str;
	}
	return this.toString();
}

String.prototype.$toText=function(){
	var text = ["&","<",">","\"","\r\n","\r\n"];
	var html = ["&amp;","&lt;","&gt;","&quot;","<br/>","<br>"];
	var res = this.toString();
	for(var i=0;i<html.length;i++){
		res = res.split(html[i]).join(text[i]);
	}
	return res;
}

String.prototype.$toHtml=function(){
	var text = ["&","<",">","\"","\r\n","\n","\r"];
	var html = ["&amp;","&lt;","&gt;","&quot;","<br/>","<br/>","<br/>"];
	var res = this.toString();
	for(var i=0;i<html.length;i++){
		res = res.split(text[i]).join(html[i]);
	}
	return res;
}


Array.prototype.contains = function (item){
	for(var i=0; i<this.length;i++){
		if ( this[i] == item) return true;
	}
	return false;		
}
 
Array.prototype.$indexOf = function(item){
	for(var i=0; i<this.length;i++){
		if ( this[i] == item) return i;
	}
	return -1;		
}

Array.prototype.remove = function (item){
	var i = this.$indexOf(item);
	if (i == -1) return;
	this.splice(i,1);
}  

/**
 * 
 * @param obj 	-
 * @param node 	-
 * @param key 	-
 * 
 */

associate = function (obj, node, key) {
		      key = ( key )? key : "js"; 
		      if (node.nodeType == 1) {
		            var val  = node.getAttribute(key);
		            if ( val ) {
		            	val = '$' + val;
		            	if(obj[val] == null){
		            		obj[ val ] = node;	
		            	} else {
							if ( obj[val] instanceof Array){
								obj[val].push(node);
							}else{
								var tmp = obj[val];
								obj[val] = [];
								obj[val].push(tmp);
								obj[val].push(node);
							}			            		

		            	}
		              } 
		        }      
		      var ch = node.childNodes;
		      for ( var i = 0; i < ch.length; i++)
		        associate (obj, ch[i], key);
		    };

/**
 * @class timer
 */
timer = {
	delay	:	3000,
	start  	:	function(handler) {
			      this.stop();
			      this.handle = window.setInterval(handler, this.delay);
    },
	stop 	:	function() {
		          window.clearInterval(this.handle);
		          clearTimeout(this.handle);
		          delete this.handle;
	}
}

var agent = navigator.userAgent.toLowerCase();
var appver = parseInt(navigator.appVersion);




HttpRequest = {
					create : function(){
						    if(window.XMLHttpRequest){
						    	var result = new XMLHttpRequest();
						      	return result
						    }else
						      if(window.ActiveXObject){
						        	return new ActiveXObject(this._activeXName());
						      }
						    throw new Error("Your browser does not support XML HTTP Requests");
						},
					
					_activeXName : function(){
						    var servers = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
						    var o;
						    for(var i = 0; i < servers.length; i++){
						        try{
						            o = new ActiveXObject(servers[i] + ".XmlHttp");
						            return servers[i] + ".XmlHttp";
						        }catch(ex){};
						    }
						    throw new Error("Could not find an installed XML parser");
					  }
				}


Request = {
	create: function (url,param,handler,target){
		return {"url":url,"param":param,"handler":handler,"target":target}	
	}
}


$getUrl = function (path,params){
	if (params){
		return ServerStub.composeUrl( Application.host+path) + params.$r()+"&"+{r:Math.random()}.$r();
	}else{
		return ServerStub.composeUrl( Application.host+path) +{r:Math.random()}.$r();
	}
}



ServerStub = {
	defaultMethod : "GET",
	
	status:	null, 
	
	xmlHttp 	: 	null,
	
	target		:	null,
	
	handler		:	null,
	
	source		:	null,
	
	lock		:	false,
	
	queue		:	[],
	
	composeUrl 	: function (url){
	    var result="";
	    if (url.indexOf("?")==(url.length-1))
	      result = url;
	    else if (url.indexOf("?")>0 && url.lastIndexOf("&")!=(url.length-1))
	      result = url+"&";
	    else if (url.indexOf("?")>0 && url.lastIndexOf("&")==(url.length-1))
	      result = url;
	    else if (url.indexOf("?")<=0) 
	      result = url+"?";	      
		return result;
	},
	
	_onreadystatechange : function(){
		try {
		    if(this.xmlHttp.readyState == 4){
		    	
		    	if(this.xmlHttp.status == 408){
					location.href = location.href;		    		
		    	}		    	
		    	this.source = this.xmlHttp.responseText;
		    	if ( this.target != null && this.handler != null){
		    		this.handler.call(this.target,this.source, this.xmlHttp.status);
		    	}else if (this.handler != null){
		    		this.handler.call(this,this.source,this.xmlHttp.status);
		    	}
//		    	alert(4);
		    	this.handler = null;
		    	this.target = null;
		    	this.xmlHttp.abort();
		    	this.lock = false;
		    	this._sendAsync();
		    }
		} catch ( e ) {
		    location.href = location.href;
		} 
	    
	},

	_send: function(request){
		this.lock = true;
		request.param = (request.param)?request.param:{};
		var query = this.composeUrl( request.url )+ ((request.param)?request.param.$r()+"&":"")+{r:Math.random()}.$r()+"&host="+Application.host;
		this.xmlHttp = HttpRequest.create();
		if (request.method.toUpperCase() == "GET"){
			this.xmlHttp.open("GET",query,false);
		    try {
		    	this.xmlHttp.send(null);
		   }catch (e){
		     	alert(query+" not found \nor server don't response");
		   }
		}else{
			var query = this.composeUrl( request.url )+{r:Math.random()}.$r()+"&host="+Application.host;
			this.xmlHttp.open("POST",query,false);
			this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
			try {
		    	this.xmlHttp.send(request.param.$r());
		   }catch (e){
		     	alert(query+" not found \nor server don't response");
		   }
		} 
	    this.source = this.xmlHttp.responseText;
	    ServerStub.status = this.xmlHttp.status;
	    this.lock = false;
	    return this.source;
	},
	
	_sendAsync: function (request){
//		alert(request.$s());
		if (this.lock){
			this.queue.push(request);
			return
		}		
		if (!request ||request == null){
			if(	this.queue.length>0	){
				this._sendAsync(this.queue.shift())
				return
			}else{
				return
			}
		}
		this.lock = true;
		request.param = (request.param)?request.param:{};
	    this.handler = (request.handler)?request.handler:this.handler;
		this.target = (request.target)?request.target:this.target;
		this.xmlHttp = HttpRequest.create();
		this.xmlHttp.onreadystatechange = this.__onreadystatechange;	    
	    if (request.method.toUpperCase() == "GET"){
			var query = this.composeUrl( request.url )+ request.param.$r()+"&"+{r:Math.random()}.$r()+"&host="+Application.host;
		    this.xmlHttp.open("GET",query,true);
		    this.xmlHttp.send(null);
	    }else{
	    	var query = this.composeUrl( request.url )+{r:Math.random()}.$r()+"&host="+Application.host;
			this.xmlHttp.open("POST",query,true);
			this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
			this.xmlHttp.send(request.param.$r());
		}
	},

	get : function(request){
		if (this.lock) return null;
		request.method = "GET";
			return this._send(request);
		}, 
 
	 	getAsync : function(request){
   				request.method = "GET";
			this._sendAsync(request);
	},
	
	post	:	function(request){
		if (this.lock) return null;
		request.method = "POST";
		return this._send(request);
	},
	
	postAsync	:	function(request){
		request.method = "POST";
		this._sendAsync(request);
	},
	
	send	:	function(request){
		if (this.lock) return null;
		request.method = ServerStub.defaultMethod;
		return this._send(request);
	},
	
	sendAsync	:	function(request){
		request.method = ServerStub.defaultMethod;
		this._sendAsync(request);
	}
}

ServerStub.__onreadystatechange = 	function(){ServerStub._onreadystatechange()};


$include = function(url){
	ServerStub.send({"url":url}).$eval();
}


$loadFragment = function(url){
	return ServerStub.send({"url":url});	
}

window.childFrames = {};
window.onLoadChildFrame = function(frameName,response){}

$BreakLineSeparator = /\s*\r\n\s*|\s*\n\r\s*|\s*\r\s*|\s*\n\s*/g;

setLocationHash = function (obj){
	if (obj && obj != null){
		location.hash = "#"+obj.$r();
	}else{
		location.hash = "#";
	}
	//alert("!");
	//history.back();
}	

getLocationHash = function (){
	var hash = location.hash;
	if (hash.indexOf("#")==0){
		hash = hash.substring(1,hash.length);
	}
	if(hash.length == 0){
		return {}
	}
	var params = hash.split("&");
	var result = {};
	for (var i=0; i<params.length; i++){
		var tmp = params[i].split("=");
		result[tmp[0]] = tmp[1];
	}
	return result;
}	

//getLocationHash();
//setLocationHash({});

go = function(url){
	if(url == null || url.indexOf('#')<0)return;
	url = url.substring(url.indexOf('#'));
//	alert(url);
	location.hash = url;
	processHash();
}