/* 
	Talcho.com integrated room info
	
	Usage:
	

*/
_talchoPopup=null;
function _openTalchoPopup(url){
	
	if(_talchoPopup && !_talchoPopup.closed){
		_talchoPopup.focus();
	}else{
		_talchoPopup=window.open(url,'chat','location=0,status=0,scrollbars=0,width=600,height=500');
		_talchoPopup.moveTo(40,40);
		_talchoPopup.focus();
	}
}

function TalchoRoomInfo(){

	this.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));
	}
	this.setCookie=function(name, value, expiresSecond){
		var today = new Date();
		var expire = new Date();
		expire.setTime(today.getTime() + expiresSecond);
		document.cookie = name+"="+escape(value)+ ";expires="+expire.toGMTString()+";path=/";
	}

	var self = this;
	this.roomInfoHtml="Currently there are <b>%i</b> users online in our chat";
	this.noOneIsOnlineHtml="There is no users in our chat. You can be the first one there! ;)";
	this.enterChatText="Enter chat";
	this.popupText="(Open popup)";
	
	this.lng="si";
	this.network=(document.domain).replace("www.", "");
	
	this.chatUrl="";
	this.popupUrl="";
	this.roomInfoUrl="";
	
	this.chatOfflineHtml="Chat is currently offline. Sorry :(";

	this.nickname="";
	this.userSessionValue="";

	
	this.timeInterval=15;
	
	
	
	this.authenticate=function(nickname,userSessionValue){
		if( nickname.length!=0 && userSessionValue.length!=0){
			this.nickname=nickname;
			this.userSessionValue=userSessionValue;
		}
	}

	
	this.build = function(){
		document.write("<div id=\"talcho_room_info_container\"></div>");
		this.roomInfoUrl = "http://"+document.domain+"/talcho/talcho.php?action=get_content&content=room_info&network="+self.network;
		var url=this.roomInfoUrl;
		
		var cookieValue=this.getCookie("talcho_count_users");
		if(cookieValue!=null && cookieValue!="-1"){
			this.onResponse({"count":cookieValue});
		}else{
			
			self.loadJSON(url+"&"+new Date().getTime()); // no caching
			
		}

		setTimeout(function(){
			self.loadJSON(url+"&"+new Date().getTime());
		}, this.timeInterval*1000);  // TODO: load only if mouse is moveing
		this.popupUrl='http://'+this.network+'/talcho/chat.php?network='+this.network;
		
	}
	this.loadJSON=function(url)
	{
	if (window.XMLHttpRequest)
	  {
	  xmlhttp=new XMLHttpRequest()
	  xmlhttp.onreadystatechange=function (){
			xmlhttpChange();
	  }
	  xmlhttp.open("GET",url,true)
	  xmlhttp.send(null);
	 
	}
	// code for IE
	else if (window.ActiveXObject)
	  {
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		if (xmlhttp)
		{
		xmlhttp.onreadystatechange=function (){
			xmlhttpChange();
		}
	  xmlhttp.open("GET",url,true)
	  xmlhttp.send(null)
		}
	  }
	}

	xmlhttpChange=function()
	{
		
	
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
	  {
	  // if "OK"
	  if (xmlhttp.status==200)
		{
			
			response = xmlhttp.responseText;
			if(response.charAt(0)!="{"){
				//alert("Wrong response: "+response);
			}else{
				self.onResponse( eval('('+response+')') ); // converto to JSON
			}
		}
	  else{ 
		this.setCookie("talcho_count_users", JSON.count,-1);
	  }
	  }
	}
	this.onResponse=function(JSON){
		var placeholder = document.getElementById("talcho_room_info_container");
		var chatContent='';
		if(JSON.count>0){
			chatContent=this.roomInfoHtml.replace(/%i/g, JSON.count);
		}else{
			chatContent=this.noOneIsOnlineHtml;
		}
		
		this.setCookie("talcho_count_users", JSON.count,this.timeInterval*1000+10);
		
		if(this.chatUrl==""){
			placeholder.innerHTML='<div>'+chatContent +'</div><a href="'+this.popupUrl+'" onclick="_openTalchoPopup(\''+this.popupUrl+'\'); return false;">'+this.enterChatText+'</a>';
		}else{
			placeholder.innerHTML='<div>'+chatContent +'</div><a href="'+this.chatUrl+'">'+this.enterChatText+'</a> <a href="#" onclick="_openTalchoPopup(\''+this.popupUrl+'\'); return false;">'+this.popupText+'</a>';
		}
		
		
		var url = this.roomInfoUrl;
		
		setTimeout(function(){
			self.loadJSON(url+"&"+new Date().getTime());
		}, this.timeInterval*1000);  // TODO: load only if mouse is moveing
		//this.popupUrl='http://framework.local/'+this.lng+'/integrated/?network='+this.network;
		
	}

	
}


