var data = new Date();
var http_request = Array();
var http_requestp = Array();
var interval = Array();
function go(id){
return document.getElementById(id);
}

function closePopup(){
		go("popup_bg").style.display='none';
		go("chat_info").style.display='none';
}
var tb_sound_newmsg="http://projekt-img.cint.pl/tb/sound/newmsg.wav";
function playsound(soundfile){
if (document.all && document.getElementById){
document.getElementById("tb_sound").src="" //reset first in case of problems
document.getElementById("tb_sound").src=soundfile
}
}
function show(id){
var obj=go(id);
if(obj.style.display=="none") obj.style.display="";
}
function show2(id,idi){
var obj=go(id);
var obji=go(idi);
if(obj.style.display=="none") obj.style.display="";
else obji.style.display='';
}

function showhide(id){
var obj=go(id);
if(obj.style.display=="none") obj.style.display="";
else obj.style.display='none';
}

function hide(id){
var obj=go(id);
if(obj.style.display=="") obj.style.display="none";
}



function newmsg(id,name){
var friends=go('tb_friends');
var friends_msg=go('tb_friends_msg');
var friends_msg_name=go('tb_friends_msg_name');
var friends_msg_id=go('tb_friends_msg_id');
friends.style.display='';
friends_msg.style.display='';
friends_msg_name.innerHTML=name;
friends_msg_id.value=name;
}

function scrollt(id){
var div=go(id);
div.scrollTop=div.scrollHeight;
}

function isInt (i) {
return (i % 1) == 0;
} 


function tb_hide(id){
if(id=='1'){
hide('tb_main');
show('tb_hide');
}
else{
hide('tb_hide');
show('tb_main');
}
eraseCookie('tb_hide');
createCookie('tb_hide',id,7);

}

function tb_sendmsg(){
var submit=go('tb_friends_msg_submit');
var topic=go('tb_friends_msg_topic');
var msg=go('tb_friends_msg_msg');
submit.disabled='disabled';
var poststr="c=1&topic=" + encodeURI( go("tb_friends_msg_topic").value )+"&tresc="+encodeURI( go("tb_friends_msg_msg").value)+"&adresat="+encodeURI( go("tb_friends_msg_id").value);
var res4=tb_post('/send/msg//?', poststr,'');
msg.value='';
topic.value='';
submit.disabled='';
var friends=go('tb_friends');
var friends_msg=go('tb_friends_msg');
friends.style.display='';
friends_msg.style.display='none';
}

   function tb_post(url, parameters,id) {
      http_requestp[id] = false;
      if (window.XMLHttpRequest) {
         http_requestp[id] = new XMLHttpRequest();
         if (http_requestp[id].overrideMimeType) {
            http_requestp[id].overrideMimeType('text/html; charset=ISO-8859-2');
         }
      } else if (window.ActiveXObject) {
         try {
            http_requestp[id] = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_requestp[id] = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_requestp[id]) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_requestp[id].onreadystatechange = (function(i){
			return function(){
				tb_alert_post(i);
			}})(id);
      http_requestp[id].open('POST', url, true);
      http_requestp[id].setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=ISO-8859-2");
      http_requestp[id].setRequestHeader("Content-length", parameters.length);
      http_requestp[id].setRequestHeader("Connection", "close");
      http_requestp[id].send(parameters);
      
   }

   function tb_alert_post(id) {
      if (http_requestp[id].readyState == 4) {
         if (http_requestp[id].status == 200) {
            res3 = http_requestp[id].responseText;
            go('chat_inf').innerHTML=res3+'<br /><a href="javascript:void(0);" onclick="closePopup();">Ok</a>';
            go("popup_bg").style.opacity="0.5";
			go("popup_bg").style.display='';
			go("chat_info").style.display='';
			http_requestp[id]=false;
         } else {
            //alert('There was a problem with the request.');
            //alert(http_request.status);
         }
      }
   }
function priv_schema(id,name){
return '<div id="tb_action_priv_id_'+id+'">'+name+'</div>';
}

function showhidechat(id,name){
var ch=go('tb_action_priv_'+id+'_view');
if(ch.style.display==''){
ch.style.display='none';


}
else{
tb_chat(id,'',name,'');
}

}

function tb_addids(id){
var ids=go('tb_action_priv_ids');
var idi;
idi=ids.innerHTML.split('|');
var ok=1;
for(var x=0;x<=idi.length;x++){
if(idi[x]==id) ok=0;
}
if(ok==1) ids.innerHTML=ids.innerHTML+'|'+id;
eraseCookie('priv_ids');
var priv_ids=go('tb_action_priv_ids');
createCookie('priv_ids',priv_ids.innerHTML,7);
}

function tb_delids(id){
var ids=go('tb_action_priv_ids');
var idi;
idi=ids.innerHTML.split('|');
var nids='';
for(var x=0;x<=idi.length;x++){
if(idi[x]!='' && idi[x]!='undefined' && idi[x]!=undefined && isInt(idi[x])){
if(idi[x]==id) {}
else{
nids=nids+'|'+idi[x];
}
}
}
ids.innerHTML=nids;
eraseCookie('priv_ids');
var priv_ids=go('tb_action_priv_ids');
createCookie('priv_ids',nids,7);
}

function closechat(id){
var mainDivElement = go("tb_action_priv");
var element=go('tb_action_priv_'+id);
mainDivElement.removeChild(element);
clearInterval(interval[id]);
tb_delids(id);
}

function tb_chat(id,lang,name,hidden){
var priv=go('tb_action_priv');
var priv_id=go('tb_action_priv_'+id);
var priv_ids=go('tb_action_priv_ids');
var cr=0;
if(priv_id==undefined && priv_ids.innerHTML.split('|').length>=11){
            go('chat_inf').innerHTML='Maximum of chat window is 10<br /><a href="javascript:void(0);" onclick="closePopup();">Ok</a>';
            go("popup_bg").style.opacity="0.5";
			go("popup_bg").style.display='';
			go("chat_info").style.display='';
}
else{
if(priv_id==undefined){
cr=1;
var mainDivElement = go("tb_action_priv");
var ndivi = document.createElement("div");
ndivi.setAttribute("id", "tb_action_priv_"+id);
ndivi.setAttribute("class", "tb_action_priv");
ndivi.setAttribute("style","background-color:none;");
ndivi.style.backgroundImage="url(http://www.projekt-img.cint.pl/tb/images/fhaj.gif)";
ndivi.className="tb_action_priv";
mainDivElement.appendChild(ndivi);
tb_addids(id);
priv_id=go('tb_action_priv_'+id);
priv_id.innerHTML='<a href="javascript:void(0);" onclick="showhidechat(\''+id+'\',\''+name+'\');">'+name+'</a> <div class="tb_closeb"><a href="javascript:void(0);" onclick="closechat(\''+id+'\');">x</a></div>';

var mainDivElementi = go("tb_action_priv_"+id);
var ndivii = document.createElement("div");
ndivii.setAttribute("id", "tb_action_priv_"+id+"_view");
ndivii.setAttribute("class", "tb_action_priv_view");
ndivii.className="tb_action_priv_view";
ndivii.setAttribute("onmouseover", "tb_read('"+id+"');");
mainDivElementi.appendChild(ndivii);



var priv_id_view=go('tb_action_priv_'+id+'_view');
//priv_id_view.innerHTML=name+' <a href="javascript:void(0);" onclick="hide(\'tb_action_priv_'+id+'_view\');">X</a>';
priv_id_view.innerHTML=' <b>'+name+'</b> <div class="tb_close"><a href="javascript:void(0);" onclick="hide(\'tb_action_priv_'+id+'_view\');" style="font-weight:bolder;">_</a>  <a href="javascript:void(0);" onclick="closechat(\''+id+'\');" style="font-weight:bolder;">X</a></div>';


var mainDivElementii = go("tb_action_priv_"+id+"_view");
var ndivy = document.createElement("div");
ndivy.setAttribute("id", "tb_action_priv_"+id+"_msg");
ndivy.setAttribute("class", "tb_action_priv_msg");
ndivy.className="tb_action_priv_msg";
mainDivElementii.appendChild(ndivy);
go('tb_action_priv_'+id+'_msg').innerHTML='Loading...';
var mainDivElementiii = go("tb_action_priv_"+id+"_view");
var ndivyy = document.createElement("div");
ndivyy.setAttribute("id", "tb_action_priv_"+id+"_tmp");
ndivyy.setAttribute("class", "tb_action_priv_tmp");
ndivyy.className="tb_action_priv_tmp";
mainDivElementiii.appendChild(ndivyy);
ndivyy.nodeValue='';

priv_id_view.innerHTML=priv_id_view.innerHTML+'<form method="post" name="tb_action_priv_'+id+'_form" action="javascript:void(0);" onsubmit="tb_sendpriv('+id+');return false;" style="margin-top:0px;margin-bottom:0px;"><input type="hidden" name="id" id="tb_action_priv_form_id_'+id+'" value="'+id+'" /><input type="text" class="tb_input" id="tb_action_priv_form_msg_'+id+'" name="msg" size="38" /><input class="tb_input" type="submit" name="submit" value="&gt;&gt;" /><br></form>';
tb_readpriv("+id+",'');
interval[id]=setInterval("tb_readpriv("+id+",'')",5000);
}
else{
tb_readpriv(id,'1');
}

var action_priv_id=go('tb_action_priv_'+id);

action_priv_id.style.display='';
var privs=priv_ids.innerHTML.split('|');
var tobj;
var x;
if(privs.length>0){
for(x=0;x<=privs.length;x++){

if(privs[x]!=null && go('tb_action_priv_'+privs[x]+'_view')!='undefined' && (privs[x]!=id && hidden=='') && privs[x]>0){
//go('tb_action_priv_'+privs[x]).style.backgroundImage="url(http://www.projekt-img.cint.pl/tb/images/fhaj.gif)";
go('tb_action_priv_'+privs[x]+'_view').style.display='none';
}
}
}
if(hidden==''){
//go('tb_action_priv_'+id).style.backgroundImage="url(http://www.projekt-img.cint.pl/tb/images/fhaj.gif)";
go('tb_action_priv_'+id+'_view').style.display='';
go('tb_action_priv_'+id+'').style.display='';
go('tb_action_priv_'+id).style.backgroundImage="url(http://www.projekt-img.cint.pl/tb/images/fhaj.gif)";
go('tb_friends').style.display='none';
go('tb_action_priv_form_msg_'+id).focus();
scrollt('tb_action_priv_'+id+'_msg');
}
else{
	if(cr==1) go('tb_action_priv_'+id+'_view').style.display='none';
}
}
}

function tb_read(id){
var dod2 = data.getMinutes()+''+data.getSeconds()+''+data.getMilliseconds();
tb_makePOSTRequest('/chat/markread/'+id+'/'+dod2+'/?','',id);
}

function tb_readpriv(id,blok){
var check=go('tb_action_priv_'+id);
if(check!=undefined){
if(check.style.display==''){
var dod2 = data.getMinutes()+''+data.getSeconds()+''+data.getMilliseconds();
tb_makeRequest('/chat/readprivate/'+id+'/'+dod2+'/?','',id);
if(blok==''){
}
}
}
}


function tb_sendpriv(id,msg){
var msg=go('tb_action_priv_form_msg_'+id);
var view = go('tb_action_priv_'+id+'_msg');
if(msg.value!=""){
//view.innerHTML=view.innerHTML+'<div class="tb_me"><span class="tb_date">data:</span>' +msg.value+'</div>';
var poststr = "text=" + encodeURI( msg.value );
msg.value='';
var dod2 = data.getMinutes()+''+data.getSeconds()+''+data.getMilliseconds();
tb_makePOSTRequest('/chat/postprivate/'+id+'/'+dod2+'/?', poststr,id);
tb_read(id);
tb_readpriv('"+id+"',1);
}
}

function tb_save_status(){
var desc=go('tb_status_form_desc');
var status1=go('tb_status_form_radio1').checked;
var status2=go('tb_status_form_radio2').checked;
var status3=go('tb_status_form_radio3').checked;
var status4=go('tb_status_form_radio4').checked;
var status=0;
if(status1!='') status=1;
if(status2!='') status=2;
if(status3!='') status=3;
if(status4!='') status=4;
var submit=go('tb_status_form_submit');
submit.disabled='disabled';
var poststr="c=1&tb=1&fop="+encodeURI(desc.value)+"&status="+status;
var res4=tb_post('/send/opis/?', poststr,'');
//tb_makePOSTRequest('/send/opis/?', poststr,'');
submit.disabled='';
//var res=go('tb_status_result');
//res.innerHTML=res4;

}




   function tb_makeRequest(url, parameters,id) {
      http_request[id] = false;
      if (window.XMLHttpRequest) { 
         http_request[id] = new XMLHttpRequest();
         if (http_request[id].overrideMimeType) {
            http_request[id].overrideMimeType('text/html; charset=ISO-8859-2');
         }
      } else if (window.ActiveXObject) {
         try {
            http_request[id] = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request[id] = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request[id]) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
	http_request[id].onreadystatechange = (function(i){
			return function(){
				tb_alertContents(i);
			}})(id);
      http_request[id].open('GET', url + parameters, true);
      http_request[id].send(null);
      
   }

   function tb_makePOSTRequest(url, parameters,id) {
      http_requestp[id] = false;
      if (window.XMLHttpRequest) {
         http_requestp[id] = new XMLHttpRequest();
         if (http_requestp[id].overrideMimeType) {
            http_requestp[id].overrideMimeType('text/html; charset=ISO-8859-2');
         }
      } else if (window.ActiveXObject) {
         try {
            http_requestp[id] = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_requestp[id] = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_requestp[id]) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
	http_requestp[id].onreadystatechange = (function(i){
			return function(){
				tb_alertContents2(i);
			}})(id);
      http_requestp[id].open('POST', url, true);
      http_requestp[id].setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=ISO-8859-2");
      http_requestp[id].setRequestHeader("Content-length", parameters.length);
      http_requestp[id].setRequestHeader("Connection", "close");
      http_requestp[id].send(parameters);
   }

   function tb_alertContents(id) {
      if (http_request[id].readyState == 4) {
         if (http_request[id].status == 200) {
            res = http_request[id].responseText;
            var field=go('tb_action_priv_'+id+'_msg');
            var tmp=go('tb_action_priv_'+id+'_tmp');
            tmp.innerHTML=res;
           if(tmp.innerHTML!=field.innerHTML && res!=''){
           	go('tb_action_priv_'+id+'_msg').innerHTML=res;
           	if(go('tb_action_priv_'+id+'_view').style.display=='none' && field.innerHTML!='Loading...'){
           	go('tb_action_priv_'+id+'').style.backgroundImage="url(http://www.projekt-img.cint.pl/tb/images/fhaj2.gif)";
           	//playsound(newmsg);
           	}
           	scrollt('tb_action_priv_'+id+'_msg');  
           }
           http_request[id]=false;
         } else {
            //alert('There was a problem with the request.');
            //alert(http_request.status);
         }
      }
   }

function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}

   function tb_alertContents2(id) {
      if (http_requestp[id].readyState == 4) {
         if (http_requestp[id].status == 200) {
            res2 = http_requestp[id].responseText;      
            http_requestp[id]=false;
         } else {
            //alert('There was a problem with the request.');
            //alert(http_request.status);
         }
      }
   }
   
   function tb_getPOST(id) {
      var poststr = "text=" + encodeURI( document.getElementById("hidden").value );
            var dod2 = data.getMinutes()+''+data.getSeconds()+''+data.getMilliseconds();
      tb_makePOSTRequest('/chat/postprivate/'+id+'/'+dod2+'/?', poststr);
   }
   
   function tb_get(obj) {
      var getstr = "?";
      for (i=0; i<obj.childNodes.length; i++) {
         if (obj.childNodes[i].tagName == "INPUT") {
            if (obj.childNodes[i].type == "text") {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
            if (obj.childNodes[i].type == "checkbox") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               } else {
                  getstr += obj.childNodes[i].name + "=&";
               }
            }
            if (obj.childNodes[i].type == "radio") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               }
            }
         }   
         if (obj.childNodes[i].tagName == "SELECT") {
            var sel = obj.childNodes[i];
            getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
         }
         
      }
                              var dod2 = data.getMinutes()+''+data.getSeconds()+''+data.getMilliseconds();
      tb_makeRequest('/chat/readprivate/'+id+'/'+dod2+'/?', getstr);
   }


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function tb_init(){
var tmain=go('tb_main');
var cook=readCookie('tb_hide');
if(cook=='' || cook=='0'){
tb_hide('0');
}
else{
tb_hide('1');
}
/*
if(tmain!='undefined'){
tmain.style.display='';
}
else setTimeout('tb_init()',100);
*/
onlineMain();
setInterval("onlineMain()", 10000);
}
function tb_test(){
var cook=go('tb_hide').style.display;
if(cook=='none'){
hide('tb_main');
show('tb_main');
}
else{
hide('tb_hide');
show('tb_hide');
}
onlineMain();

}
window.onscroll=tb_test;


   function makeRequestMain(url, parameters,par2,id) {
      http_request[id] = false;
      if (window.XMLHttpRequest) { 
         http_request[id] = new XMLHttpRequest();
         if (http_request[id].overrideMimeType) {
            http_request[id].overrideMimeType('text/html; charset=ISO-8859-2');
         }
      } else if (window.ActiveXObject) {
         try {
            http_request[id] = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request[id] = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request[id]) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request[id].onreadystatechange = (function(i){
			return function(){
				tb_alertContentsMain(i);
			}})(id);
      http_request[id].open('GET', url + parameters, true);
      http_request[id].send(null);
   }

/*
function stayHome(){ 
if(go('popup_bg').style.display==''){
var nTop=0;
var nTop2=100;
var iV = document.body.scrollTop; 
go('popup_bg').style.top = iV+nTop;
go('chat_info').style.top = iV+nTop2;
}
}
//window.onscroll = stayHome;
//window.onload=stayHome;
*/
   function tb_alertContentsMain(id) {
      if (http_request[id].readyState == 4) {
         if (http_request[id].status == 200) {
            var res1 = http_request[id].responseText;
            if(res1!='' && res1!=undefined && res1!='undefined'){
            var nowy=res1.split('|||',4);
            if(nowy[1]==1){
            //go('panel_friends').innerHTML = '';  
            //go('chat_inf').innerHTML=nowy[2];
            //go("popup_bg").style.opacity="0.5";
			//go("popup_bg").style.display='';
			//go("chat_info").style.display='';
            }
            else {
            if(nowy[2]!='blank||blank' && nowy[2]!='undefined' && nowy[2]!=undefined && nowy[2]!='' && nowy.length>3){
            var chaty=nowy[2].split('||');
            for(var y=1;y<=chaty.length;y++){
            if(chaty[y]!='' && chaty[y]!=' ' && chaty[y]!='undefined' && chaty[y]!=undefined){
            var tmpx=chaty[y].split('|');
            if(tmpx[0]!='' && tmpx[1]!=''){
            tb_chat(tmpx[0],'',tmpx[1],'2');  
            }          
            }
            }
            }
            //go('panel_friends').innerHTML = nowy[3];
            go('tb_friends_list').innerHTML = nowy[3];    
            }
            }  
            http_request[id]=false;      
         } else {
            //alert('There was a problem with the request.');
            //alert(http_request.status);
         }
      }
   }
   
   function getMain(url,par2) {
      var getstr = "?";
      var dod2 = data.getMinutes()+''+data.getSeconds()+''+data.getMilliseconds();
      makeRequestMain(url,'/?'+dod2,par2);
   }
function onlineMain(){
getMain('/send/friends/online','tb_friends_list');
}
