var cache=Math.random();
var ini=false;
var tmp_obj=new Object();
var chat_obj=Array();
var ar_chat=Array();
var tmp_obj_index=Array();
var tmp_obj_focus=false;
var chat_refresh=false;

window.onunload = function(){remove_chat();};

function call(query){
if(chat_refresh==true){
cache++;
var el=document.getElementById("call");
if(el.lastChild)el.removeChild(el.lastChild);
var obj=document.createElement("SCRIPT");
obj.setAttribute('src',query+'&c='+cache);
el.appendChild(obj);
}else{cache++;
var el=document.getElementById("call");
if(el.lastChild)el.removeChild(el.lastChild);
var obj=document.createElement("SCRIPT");
obj.setAttribute('src',query+'&c='+cache);
el.appendChild(obj);	
}
}

function ini_chat(text1,text2){
tmp_obj_focus=true;
chat_refresh=true;
if(!document.getElementById('tmp_obj')){set_avatar(text2);}
else{reset_chat(text1);}
tmp_obj_focus=false;
}

function set_avatar(text2){
var obj=document.getElementById('call_chat');
obj.innerHTML=text2;

var top_pos=obj.offsetTop;
var left_pos=obj.offsetLeft;

var obj=obj.parentNode;
var el=document.createElement('DIV');
el.style.zIndex='100';
el.style.position='absolute';
el.style.top=top_pos+'px';
el.style.left=left_pos+'px';
el.style.width='2.9em';
el.style.backgroundColor='#FFFFFF';
el.style.paddingTop='0.5em';
el.style.border='solid 1px gray';
el.style.textAlign='center';

el.setAttribute('id','ini_div');
obj.appendChild(el);
obj=obj.lastChild;
for(i=1;i<7;i++){
el=document.createElement('IMG');
el.setAttribute('src','avatar/avatar'+i+'.gif');
el.onclick=function(){set_chat(this);};
el.style.padding='0.5em';
el.style.marginBottom='0.3em';
obj.appendChild(el);
el=document.createElement('BR');
obj.appendChild(el);

}}

function set_chat(img){
var img_src=img.src;
img.parentNode.parentNode.removeChild(img.parentNode);		
var obj=document.body;
var el=document.createElement('IMG');
el.onclick=function(){set_new_message();};
el.setAttribute('src',img_src);
tmp_obj=document.createElement('DIV');
tmp_obj.setAttribute('id','tmp_obj');
tmp_obj.onmousedown=function(){set_focus(0);};
tmp_obj.onmouseup=function(){set_focus(1);};
tmp_obj.topvalue=300;
tmp_obj.leftvalue=500;
tmp_obj.style.top="300px";
tmp_obj.style.left="500px";
tmp_obj.className="chatter";
tmp_obj.appendChild(el);
create_textarea();
obj.appendChild(tmp_obj);
call('ini_chat.php?ini=false&img='+ (img.src.substring(img.src.lastIndexOf('\/')+1)));


}

function refresh_chat(ini,message){
if(ini!=false && chat_refresh==true)call('ini_chat.php?&ini='+ini);
}

function create_textarea(){
var el=document.createElement('DIV');
tmp_obj.appendChild(el);
el=document.createElement('TEXTAREA');
el.setAttribute('cols','20');
el.setAttribute('scrollbar','no');
el.style.overflow='hidden';	
tmp_obj.lastChild.appendChild(el);
}

function set_new_message(){
if(tmp_obj.lastChild.firstChild.value){
var message=tmp_obj.lastChild.firstChild.value;
tmp_obj.removeChild(tmp_obj.lastChild);
var img=(tmp_obj.firstChild.src.substring(tmp_obj.firstChild.src.lastIndexOf('\/')+1));
var el=document.createElement('DIV');
el.className='message';
el.setAttribute('id','message');
tmp_obj.appendChild(el);
el=document.createTextNode(message);
tmp_obj.lastChild.appendChild(el);
var regEx=/&/gi;
message=message.replace(regEx,'`~6^');
call('ini_chat.php?&ini='+ini+'&message='+message+'&t='+tmp_obj.topvalue+'&l='+tmp_obj.leftvalue+'&img='+img);
}else if(tmp_obj.lastChild.id=='message'){
tmp_obj.removeChild(tmp_obj.lastChild);	
create_textarea();	
}}

function create_chat_obj(chat_obj_index, img_path){
var obj=document.body;
var el=document.createElement('IMG');
el.setAttribute('src','avatar/'+img_path);
chat_obj[chat_obj_index]=new Object(document.createElement('DIV'));
chat_obj[chat_obj_index].className="chatter";
chat_obj[chat_obj_index].appendChild(el);
obj.appendChild(chat_obj[chat_obj_index]);	
tmp_obj_index.push(chat_obj_index);
}

function set_chatter_position(chat_obj_index,left,top){
chat_obj[chat_obj_index].topvalue=top;
chat_obj[chat_obj_index].leftvalue=left;
chat_obj[chat_obj_index].style.top=top+'px';
chat_obj[chat_obj_index].style.left=left+'px';
}

function move_it(e){
	if(ini!=false && tmp_obj_focus==false && chat_refresh==true){	
	var mouseX = (window.Event) ? e.pageX : event.clientX;
	var mouseY = (window.Event) ? e.pageY : event.clientY;
	
	tmp_obj.topvalue=mouseY-10;
	tmp_obj.leftvalue=mouseX-50;
	tmp_obj.style.left=tmp_obj.leftvalue+'px';
	if(navigator.userAgent.indexOf('MSIE')>0)tmp_obj.topvalue+=document.body.scrollTop;
	tmp_obj.style.top=tmp_obj.topvalue+'px';
	if(tmp_obj.lastChild.firstChild.value){set_new_message();
	}else{ call('ini_chat.php?&ini='+ini+'&message=1&t='+tmp_obj.topvalue+'&l='+tmp_obj.leftvalue+'&img='+(tmp_obj.firstChild.src.substring(tmp_obj.firstChild.src.lastIndexOf('\/')+1)));}
}}

function set_focus(v){
if(v==0)tmp_obj_focus=true;
else	tmp_obj_focus=false;
}

function reset_chat(text){
	var x;
	var tmp_ini=ini;
	ini=false;
for (i=0;i<tmp_obj_index.length;i++){
	x=tmp_obj_index[i];	
document.body.removeChild(chat_obj[x]);}
document.body.removeChild(tmp_obj);
chat_obj=Array();
tmp_obj=new Object();
tmp_obj_index=new Array();
tmp_obj_focus=false;
ar_chat=new Array();
call('ini_chat.php?&ini='+tmp_ini+'&rm=1&text='+text);	
}

function remove_chat(){

if(ini!=false){
call('ini_chat.php?&ini='+ini+'&rm=1');	
}}

function check_for_logouts(ar){
ar.sort();
var e=2;
var obj=new Object();var x;
for (i=0;i<tmp_obj_index.length;i++){
x=tmp_obj_index[i];
if(ar[e]!=x){
	if(chat_obj[x])
document.body.removeChild(chat_obj[x]);
chat_obj.splice(x,1);
ar_chat.splice(x,1);
tmp_obj_index.splice(i,1);
}else{e++;
}}}	
