var myUsers=new Array(); 
var myBoxes=new Array();
var myChat=new Array();
var lastEvent=new Array();
var bufferdCount=new Array();  
var sendText;
var sendTo;
var sendFrom=-1;
myBoxes[0]=null;      
myBoxes[1]=null;
myBoxes[2]=null;
myBoxes[3]=null;
function GetID(){
if(sendFrom==-1)
$.ajax({
  url: 'chat/UserID.php?cash='+Math.random(),async: false,
 success: function(data) {
  sendFrom=data;
 }
});	
}

function ChatClose(windowID){
		document.getElementById('pop'+windowID+'text').innerHTML='';
if(windowID==1){
myUsers[myBoxes[0]]=null;
myBoxes[0]=null;	
}else if(windowID==2){
myUsers[myBoxes[1]]=null;	
myBoxes[1]=null;}
else if(windowID==3){
myUsers[myBoxes[2]]=null;	
myBoxes[2]=null;}	
else if(windowID==4){
myUsers[myBoxes[3]]=null;	
myBoxes[3]=null;}
$("#pop"+windowID+"text").animate({height:"0",opacity:.1},400,function(){
	$("#pop"+windowID).css('visibility', 'hidden');
});	
}

function Sendmsg (text,boxid){
//document.getElementById('started').innerHTML=text;	
if(myChat[myBoxes[boxid]]==null){
        myChat[myBoxes[boxid]]='<br><span style="color:#00C;"><strong>ME</strong></span><br>'+text;
}else{
      if(lastEvent[myBoxes[boxid]]==2)
	   myChat[myBoxes[boxid]]=myChat[myBoxes[boxid]]+'<br><span style="color:#00C;"><strong>ME</strong></span><br>'+text;
	   else
	   myChat[myBoxes[boxid]]=myChat[myBoxes[boxid]]+'<br>'+text;
}
document.getElementById('pop'+(boxid+1)+'text').innerHTML=myChat[myBoxes[boxid]];
lastEvent[myBoxes[boxid]]=1;
var objDiv = document.getElementById('pop'+(boxid+1)+'text');
objDiv.scrollTop = objDiv.scrollHeight;
sendText=text;
sendTo=myBoxes[boxid];
}
function StartChat(userID,UserName){
	bufferdCount[userID]=null;
if (myUsers[userID]==null){
myUsers[userID]=userID;
if(myBoxes[0]==null){
myBoxes[0]=myUsers[userID];
if(myChat[userID]!=null)
	document.getElementById('pop1text').innerHTML=myChat[userID];
// open window with animation 
	$("#pop1").css('visibility', 'visible');
$("#pop1text").animate({height:"160",opacity:1},400,function(){	
});
document.getElementById('pop1lable').innerHTML=UserName;
}else if(myBoxes[1]==null){
myBoxes[1]=myUsers[userID];
if(myChat[userID]!=null)
	document.getElementById('pop2text').innerHTML=myChat[userID];
// open window with animation 
	$("#pop2").css('visibility', 'visible');
$("#pop2text").animate({height:"160",opacity:1},400,function(){	
});
document.getElementById('pop2lable').innerHTML=UserName;
}else if(myBoxes[2]==null){
myBoxes[2]=myUsers[userID];
if(myChat[userID]!=null)
	document.getElementById('pop3text').innerHTML=myChat[userID];
// open window with animation 
	$("#pop3").css('visibility', 'visible');
$("#pop3text").animate({height:"160",opacity:1},400,function(){	
});
document.getElementById('pop3lable').innerHTML=UserName;
}else if(myBoxes[3]==null){
myBoxes[3]=myUsers[userID];
if(myChat[userID]!=null)
	document.getElementById('pop4text').innerHTML=myChat[userID];
// open window with animation 
	$("#pop4").css('visibility', 'visible');
$("#pop4text").animate({height:"160",opacity:1},400,function(){	
});
document.getElementById('pop4lable').innerHTML=UserName;
}else{
// no more boxes to chat 
$("#dialog").animate({top:"300",opacity:1},100,function(){
$("#dialog").animate({opacity:1},1000,function(){
$("#dialog").animate({top:"1000",opacity:0.1},4000)
});	
});	
myUsers[userID]=null;}
}	
}

var flag=0;
function chatpostion(){
 var viewportwidth;
 var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }
	
document.getElementById('chat').style.top=(viewportheight-55)+'px';
if(sendFrom>=0){
	document.getElementById('chat').style.visibility='visible';}
}
window.onresize = function(event) { 
	chatpostion();	
}
function hideopp (){
	$("#UsersContent").animate({opacity:.1},500);
	chatpostion();	
}

function loadchat(){
if(sendFrom>=0){
	document.getElementById('chat').style.visibility='visible';
$.ajax({
  url: 'chat/chat.php?sendFrom='+sendFrom+'&sendTo='+sendTo+'&sendText='+encodeURI(sendText)+'&cash='+Math.random(),async: false,
 success: function(data) {
   // $('#content').html(data);
sendText='';
var str=new Array();
str=data.split('|');
for (var i=0;i<str.length;i++){
str[i]=str[i].split(',');
  }
for (var i=0;i<str[0].length-1;i++){
str[0][i]=str[0][i].split(':');
}
  
	 // read messages
	 for (var i=1;i<str.length;i++){
  //  htmler=htmler+' sender:   '+str[i][0]+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text : '+str[i][1]+'<br>';
	if(myChat[str[i][0]]==null){
        myChat[str[i][0]]='<br><span style="color:#00C;"><strong>'+str[i][2]+'</strong></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+str[i][3]+'<br>'+str[i][1];}
	else{
		if(lastEvent[str[i][0]]==2)
        myChat[str[i][0]]=myChat[str[i][0]]+'<br>'+str[i][1];
		else
        myChat[str[i][0]]=myChat[str[i][0]]+'<br><span style="color:#00C;"><strong>'+str[i][2]+'</strong></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+str[i][3]+'<br>'+str[i][1];
		}
lastEvent[str[i][0]]=2;// 2 mean last action is recive	
	// sender session is in box 1
	if(myBoxes[0]==str[i][0]){
		bufferdCount[str[i][0]]=-1;
	document.getElementById('pop1text').innerHTML=myChat[str[i][0]];
	var objDiv = document.getElementById("pop1text");
objDiv.scrollTop = objDiv.scrollHeight;
	}
	// sender session is in box 2
	if(myBoxes[1]==str[i][0]){
	bufferdCount[str[i][0]]=-1;
	document.getElementById('pop2text').innerHTML=myChat[str[i][0]];
	var objDiv = document.getElementById("pop2text");
objDiv.scrollTop = objDiv.scrollHeight;
	}
	// sender session is in box 3
	if(myBoxes[2]==str[i][0]){
	bufferdCount[str[i][0]]=-1;
	document.getElementById('pop3text').innerHTML=myChat[str[i][0]];
	var objDiv = document.getElementById("pop3text");
objDiv.scrollTop = objDiv.scrollHeight;
	}
	// sender session is in box 4
	if(myBoxes[3]==str[i][0]){
     bufferdCount[str[i][0]]=-1;
	document.getElementById('pop4text').innerHTML=myChat[str[i][0]];
	var objDiv = document.getElementById("pop4text");
objDiv.scrollTop = objDiv.scrollHeight;
	}
	if(bufferdCount[str[i][0]]==null)
	bufferdCount[str[i][0]]=1;
    else if(bufferdCount[str[i][0]]>-1)
	bufferdCount[str[i][0]]++;
	
	var htmler='<ul  dir="ltr">';
 if(str[0][0][1]==-1)
 htmler="&nbsp;&nbsp;No Users Online&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br><br>";
 else{	 
  for (var i=0;i<str[0].length-1;i++){
    //htmler=htmler+' name:   '+str[0][i][0]+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id : '+str[0][i][1]+'<br>';
	if(bufferdCount[str[0][i][1]]>0){
	htmler=htmler+'<li style="background-color:#91e4df">&nbsp;&nbsp;<a href="javascript:StartChat('+str[0][i][1]+',\''+str[0][i][0]+'\');"><img src="chat/no_profile.jpg" alt="'+str[0][i][0]+'" width="25" height="25" hspace="0" vspace="0" border="0" align="absmiddle"/></a>&nbsp;<a href="javascript:StartChat('+str[0][i][1]+',\''+str[0][i][0]+'\');">'+str[0][i][0]+'&nbsp;&nbsp;&nbsp;&nbsp;'+bufferdCount[str[0][i][1]]+'</a>&nbsp;</li>';
	}
	else{
	htmler=htmler+'<li>&nbsp;&nbsp;<a href="javascript:StartChat('+str[0][i][1]+',\''+str[0][i][0]+'\');"><img src="chat/no_profile.jpg" alt="'+str[0][i][0]+'" width="25" height="25" hspace="0" vspace="0" border="0" align="absmiddle"/></a>&nbsp;<a href="javascript:StartChat('+str[0][i][1]+',\''+str[0][i][0]+'\');">'+str[0][i][0]+'</a>&nbsp;</li>';
		
	}
	}
	htmler=htmler+'</ul>';}
	$('#UsersContent').html(htmler);
	 
	for (var i=0;i<str[0].length-1;i++) 
	 	if(bufferdCount[str[0][i][1]]>0)
	 showChat();
	}
	
	}
});	

if(flag==1){
document.getElementById('UsersContent').innerHTML=document.getElementById('UsersContent').innerHTML;
document.getElementById('chatUsers').style.visibility='visible';
document.getElementById('chatUsers').style.top =(-26-document.getElementById('UsersContent').clientHeight)+'px';
$("#UsersContent").animate({opacity:1},500);
//$('.backanimate').animate({backgroundColor: '#fff' }, 10000);

}else
{
//setTimeout('loadchat()', 5000);
}	
setTimeout('loadchat();', 3000);
}else{
	document.getElementById('chat').style.visibility='hidden';

}
}
function showChat(){
document.getElementById('chatUsers').style.visibility='visible';
document.getElementById('chatUsers').style.top =(-26-document.getElementById('UsersContent').clientHeight)+'px';
$("#UsersContent").animate({opacity:1},500);
}
function chatTo(){
	GetID();
if(flag==0){
flag=1;
loadchat();
}else{
flag=0;	
document.getElementById('chatUsers').style.visibility='hidden';
$("#UsersContent").animate({opacity:.1},500);

}
}
