var IE = false;
var focus_chat = false;
var serv_load = 1;
var wait_time = 5000;
var chat_up = false;
var PMNotify = true;
var full_chat = 0;
if (navigator.appName.indexOf('Microsoft') > -1) IE = true;
function initXMLHttpClient(){
	var xmlhttp;
	try{
		xmlhttp = new XMLHttpRequest();
	} catch (e){
		var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
		var success = false;
		for (var i=0;i < XMLHTTP_IDS.length && !success; i++){
			try{
				xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
				success = true;
			} catch (e){}
		};
		if (!success){
			xmlhttp = null;
		};
	};
	return xmlhttp;
}
function sendRequest(code, add_data) {
	if (document.hasFocus()) {
		fullurl = ajax_url;
		if (!IE){
			n = new Date();
			n = n.getTime();
			fullurl +='?r=' + n;
		};
		params = 'code=' + code + add_data + '&mk=' + mk;
		var httpreq = initXMLHttpClient();
		if (httpreq == null) return;
	
		httpreq.open('POST', fullurl, true);
		httpreq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	
		httpreq.onreadystatechange = function() {
			if (httpreq.readyState == 4 && httpreq.status == 200){
				var code = httpreq.responseText.substring(0,2);
				var data = httpreq.responseText.substring(2,httpreq.responseText.length);
				if (code == '01') checkStatus(data);
				if (code == '03' && data == 200) {
					document.getElementById('chat_reply').value = '';
					sendRequest('01', '&full_chat=' + full_chat);
				};
			};
		};
		httpreq.send(params);
	}

}
function fadeIn(id){
	document.getElementById(id).style.display = 'block';
	if (!IE){
		document.getElementById(id).style.opacity = 0;
		for(i=1;i<=10;i++){
			setTimeout('document.getElementById(\'' + id + '\').style.opacity = ' + (i/10), i*50);
		};
	};
}
function fadeInFaded(id){
	document.getElementById(id).style.display = 'block';
	if (!IE){
		document.getElementById(id).style.opacity = 0;
		for(i=1;i<=10;i++){
			setTimeout('document.getElementById(\'' + id + '\').style.opacity = ' + (i/15), i*50);
		};
	};
}
function fadeOut(id) {
	if (!IE) {
		for(i=1;i<=10;i++) {
			setTimeout('document.getElementById(\'' + id + '\').style.opacity = ' + (1-i/10), i*50);
			if (i==10) setTimeout('document.getElementById(\'' + id + '\').style.display = \'none\'', i*50);
		};
	};
	if (IE) document.getElementById(id).style.display = 'none';
}
function checkStatus(data) {
	newPM = data.substring(0, data.indexOf(';'));
	data = data.substring(data.indexOf(';')+1, data.length);
	serv_load = data.substring(0, data.indexOf(';'));
	chatmsgs = data.substring(data.indexOf(';')+1, data.length);
	if (chatmsgs.length > 0 && chat_up == false) {
		chat_up = true;
		fadeInFaded('chat_box');
		document.getElementById('chat_box_button').style.display='none';
	}
	if (chat_up == true) {
		document.getElementById('chat_area').innerHTML = chatmsgs;
		document.getElementById('chat_area').scrollTop = 1000;
	}
	if (user_logged_in == '1') {
		if (parseInt(newPM) == 0) {
			img_src = img_url + 'no_new_pm.gif';
			document.getElementById('sidebar_messages').innerHTML = 'View my messages';
		}
		if (parseInt(newPM) > 0) {
			img_src = img_url + 'new_pm.gif';
			if (PMNotify == true && display_popups == '1') {
				msg = 'You have ' + newPM + ' new Private Message';
				if (newPM > 1) msg = msg + 's';
				document.getElementById('message_popup').innerHTML = '<div id="message_popup_inner">' + msg + '.<br /><a href="#" onclick="togglePopups()">[Stop these popups]</a></div>';
				fadeIn('message_popup');
				setTimeout('fadeOut(\'message_popup\')', 5000);
				PMNotify = false;			
			}
		document.getElementById('sidebar_messages').innerHTML = 'View my messages (' + newPM + ')';
		}
		document.getElementById('pm_img').src = img_src;
	}
	load_src = 'load_low.png';
	if (parseFloat(serv_load) > 1) load_src = 'load_medium.png';
	if (parseFloat(serv_load) > 2) load_src = 'load_high.png';
	document.getElementById('load_indicator').src = site_url + 'images/' + load_src;
	updateWaitTime();
	setTimeout('updateStatus()', wait_time);
}
function togglePopups() {
	sendRequest('02', '');
	fadeOut('message_popup');
	display_popups = 0;
}
function sendMsg() {
	msg = document.getElementById('chat_reply').value;
	if(msg.length > 0) sendRequest('03', '&msg=' + msg);
}
function maxChat() {
	full_chat = 1;
	sendRequest('01', '&full_chat=' + full_chat);
	wind = document.getElementById('chat_box');
	wind.style.left = '10%';
	wind.style.width = '79%';
	wind.style.height = '75%';
	wind.style.opacity = '1';
	wind.onmouseover = null;
	wind.onmouseout = null;
	document.getElementById('chat_area').style.height = '84%';
	document.getElementById('chat_area').style.fontSize = '1em';
	if (user_logged_in == '1') {
		document.getElementById('chat_reply').style.width = '93%';
		document.getElementById('chat_box_button').style.display = 'none';
	}

	document.getElementById('chat_links').innerHTML = '<a href="#" onclick="fadeOut(\'chat_box\');fadeInFaded(\'chat_box_button\')">[close chat window]</a> <a href="#" onclick="minChat()">[minimise chat window]</a>';

}
function minChat() {
	full_chat = 0;
	sendRequest('01', '&full_chat=' + full_chat);
	wind = document.getElementById('chat_box');
	wind.style.left = '';
	wind.style.right = '0px';
	wind.style.width = '20%';
	wind.style.height = '128px';
	wind.style.opacity = '0.75';
	wind.onmouseover = null;
	wind.onmouseout = null;

	document.getElementById('chat_area').style.height = '50px';
	document.getElementById('chat_area').style.fontSize = '0.7em';
	document.getElementById('chat_links').innerHTML = '<a href="#" onclick="maxChat()">[maximise]</a>&nbsp;&nbsp;<a href="#" onclick="fadeOut(\'chat_box\');fadeInFaded(\'chat_box_button\')">[close]</a>';
	if (user_logged_in == '1') {
		document.getElementById('chat_reply').style.width = '74.5%';
	}
}
function updateWaitTime() {
	wait_time = serv_load * serv_load * 1000 * 2;
	if (wait_time < 1000) wait_time = 1000;
}
function updateStatus() {
//	document.getElementById('chat_area').innerHTML = 'Updating...';
	sendRequest('01', '&full_chat=' + full_chat);
}
function startUp() {
	sendRequest('01', '&full_chat=' + full_chat);
	setTimeout('updateStatus()', wait_time);
}
startUp();


function keyHandler(evt) {
        evt = (evt) ? evt : ((window.event) ? window.event : "")
        if (evt) {
            // process event here
            if (evt.keyCode==13 || evt.which==13) {
			if (focus_chat) sendMsg(); 
		}
	}
}
document.onkeypress = keyHandler;
