
var chat_req_timer;
(function() {
	
	// initialize some variables
	var users = document.getElementById('chat_with'); // temporary?
	var btn = document.getElementById('request_chat');
	var reqs = document.getElementById('chat_requests');
	var chat_boxes = document.getElementById('chat_boxes');
	var req = new XMLHttpRequest();
	var refresh_secs = 5000;
	var last_req = 0; //Math.ceil((new Date()).getTime()/1000);
	var active_chats = {};
	
	if(!req) {
		return null;
	}
	
	// if there are any incoming chat requests, open up
	// some mini chat windows
	function notifyOfRequests() {
		if(req && req.readyState == 4 && req.status == 200) {
			
			// we've got some requests!
			if(req.responseText !== '') {
				if(req.responseText.length > 4) {
					if(req) {
						res = eval("("+req.responseText+")");
						
						if(confirm('A user would like to instant message you. Would you like to chat?')) {
							window.open('ezc/start.php');
								// "Instant Message Window [id:" + Math.round(100*Math.random()) + Math.round(100*Math.random()) + "]",
								// "width=400,height=400,top=100,left=100,scrollbars=yes,resizable=yes,toolbar=no,directories=no,status=no,menubar=no");
						}

						for(var i = 0; i < res.length; i++) {
							// modified
						}
					}
				} else {
					// alert(req.responseText);
				}
			}
			
			// tell it to go again
			chat_req_timer = window.setTimeout(function(){checkForRequests();},refresh_secs);
		}
	}
	
	// check for incoming chat requests
	function checkForRequests() {
		if(window.chat_req_timer) {
			clearTimeout(chat_req_timer);
		}
		
		if(req) {
			// modified
			req.open("GET", 'ezc/chat/responder.php?act=get_requests&t='+last_req, true);
			req.onreadystatechange = notifyOfRequests;
			req.send("");
			//last_req = Math.ceil((new Date()).getTime()/1000);
		}
	}
	
	// get it all going
	if(req) {
		chat_req_timer = window.setTimeout(function(){checkForRequests();},0);
	}
})();