function checkmessages(){
	setInterval('im(\'check\',0,0)', 2000);
}

function processcheck(ret){
	var JSON = "data="+ret;
	eval(JSON);
	
	if(data.error==true){
		// parse the error here
		// data.reason : shows the error message
	}else{
		
		if(data.newim==true){
			// open an im for each convo
			var i = 0;
			for(i=0;i<data.ims.length;i++){
					// if it's already open, just update the text, if not, open the window
					if(document.getElementById("im_window_"+data.ims[i].cid)){
						//window is open, update text :)	 
						
						display_text(data.ims[i].cid,data.ims[i].name,data.ims[i].display_picture,data.ims[i].user_id,data.ims[i].chat)
						
					}else{
						openim(data.ims[i].cid,data.ims[i].name,data.ims[i].display_picture,data.ims[i].user_id,data.ims[i].chat);
					}
			}
			
		}
		
	}
}

function display_text(cid, name, displaypic, userid, chat){
	
	
	var displaypicture = '';
	var html = '';
	
	if(displaypic){
		displaypicture = '<img src="'+displaypic+'" style="float:right; padding:3px 3px 3px 3px;" />';	
	}
	
	// is the window open?
	if(document.getElementById("im_window_"+cid)){
		// yes it's open, display the chat:)
		document.getElementById("im_window_"+cid).innerHTML = displaypicture+chat;
		
		//scroll to bottom
		var objDiv = document.getElementById("im_window_"+cid);
		objDiv.scrollTop = objDiv.scrollHeight;	
		
	}else{
		// the window is closed, request to open it	
		openim(cid, name, displaypic, userid, chat);
		//scroll to bottom
		var objDiv = document.getElementById("im_window_"+cid);
		objDiv.scrollTop = objDiv.scrollHeight;	
	}
	
}

function openim(cid, name, displaypic, userid, chat){
	
	
	var displaypicture = '';
	var html = '';
	
	if(displaypic){
		displaypicture = '<img src="'+displaypic+'" style="float:right; padding:3px 3px 3px 3px;" />';	
	}
	
 var win = new Window({id: "im_"+cid, className: "alphacube", title: "Chat with "+name, width:350, height:300}); 
 
 html = '<table border="0" cellspacing="0" cellpadding="0" class="im_container" align="center">';
   html += '<tr><td class="im_view_cont" id="im_view_cont_'+cid+'"><div class="im_view" align="left" id="im_window_'+cid+'">'+displaypicture+chat+'</div></td></tr>';
  html += '<tr><td class="im_text" height="26"><input class="im_input" id="im_input_'+cid+'" type="text" onKeydown="im_submit_check('+cid+',this.value,event);"></td></tr></table>';
 
 
  win.getContent().innerHTML = html;
  win.setDestroyOnClose(); 
  win.showCenter();
  win.setConstraint(true, {left:0, right:0, top: 0, bottom:0})
  win.toFront();
   
   //setTimeout('resizeim('+cid+')',2000);
}

function resizeim(cid){

	if(document.getElementById("im_view_cont_"+cid)){
		//alert("height: "+document.getElementById("im_view_cont_"+cid).offsetHeight);
		document.getElementById("im_window_"+cid).offsetHeight = document.getElementById("im_view_cont_"+cid).offsetHeight;
		
		//setTimeout('resizeim('+cid+')',2000);
	}
	
}

function im(act,id, msg){
	
	var ajaxRequest;
	var url = "ajax/im.php?act="+act+"&cid="+id+"&message="+msg;
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//document.getElementById("content").innerHTML = ajaxRequest.responseText;
			
			// process returned value
				processcheck(ajaxRequest.responseText);
			
		}
	}
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null); 
	
}

function im_submit_check(cid,text,evnt){
	if(evnt.keyCode=='13'){
	document.getElementById('im_input_'+cid).value = '';
	im("send",cid,escape(text));
	}
	
}

function new_chat(uid){
	im("new",uid,0);
}

function open_buddylist(){
		if(!document.getElementById("buddylist")){
			 var win = new Window({id: "im_buddylist", className: "alphacube", title: "Buddy List", width:200, height:300}); 
 
 html = '<div id="buddylist"></div>';
 
	  win.getContent().innerHTML = html;
	  win.setDestroyOnClose(); 
	  win.showCenter();
	  win.setConstraint(true, {left:0, right:0, top: 0, bottom:0})
	  win.toFront();
	  updatebuddylist();
	  
	  	setInterval("updatebuddylist()",2000);
		}
}

function updatebuddylist(){
	var ajaxRequest;
	var url = "ajax/im.php?act=buddylist"
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//document.getElementById("content").innerHTML = ajaxRequest.responseText;
			
			// process returned value
				var JSON = "data="+ajaxRequest.responseText;
				eval(JSON);
				
				document.getElementById('buddylist').innerHTML = data.buddylist;
			
		}
	}
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null); 
}