var ajaxLastAction ='';

function startChat(){
	livechat = new chat();
	livechat.start();
	livechat.online(1000);
}

function chat() {
  lastid = 0;
  refreshtime = 2000;
  canRefresh = true;
  chatArea = document.getElementById('chatChat');
  chatArea.className = browserStyleClass;
  showHide('chatChat');
  chatOnlineArea = document.getElementById('chatOnline');
  chatOnlineArea.className = browserStyleClass;
  showHide('chatOnline');
  inputContainerArea = document.getElementById('chatInput');
  inputContainerArea.className = browserStyleClass;
  showHide('chatInput');
  starterArea = document.getElementById('chatStarter');
  showHide('chatStarter');
  inputArea = document.getElementById('chatInputLine');
  inputArea.focus();

  inputArea.onkeypress = function(e){
	if (typeof e == "undefined"){
		e = window.event;
	}
	var val = e.keyCode;
	if(val == 13){
	  	canRefresh = false;
		ajaxCall('../ajax.php?act=post_chat&post='+encodeURIComponent(inputArea.value)+'&lastId='+lastid);
		inputArea.value='';
	}
  }
  
  this.setlastid = function(newid){
  	if(lastid==newid){
	  	refreshtime = refreshtime*2;
	  	if(refreshtime>10000){refreshtime=10000;} 
  	}else{
	  	refreshtime = Math.round(refreshtime/2);
	  	if(refreshtime<2000){refreshtime=2000;}
  	}
 	lastid = newid;
  }
  
  
  this.setonline = function(newtext){
  	chatOnlineArea.innerHTML = newtext;
  }

  this.add = function(newtext){
  	chatArea.innerHTML = newtext+chatArea.innerHTML;
  }
  
  this.start = function(){
	ajaxCall('../ajax.php?act=initiate_chat');
  }

  this.online = function(when){
  	if(when!=null && when>0){
  		setTimeout("ajaxCall('../ajax.php?act=get_chat_online')", when);
  	}
  }

  this.refresh = function(){
	if(canRefresh){
	  	setTimeout("ajaxCall('../ajax.php?act=get_chat&lastId="+lastid+"')", refreshtime);
	  	canRefresh = false;
	}else{
		setTimeout("delayedrefresh()", refreshtime);
	}
  }
}

function delayedrefresh(){
	livechat.refresh();
}

function ajaxCall(target)
{
	ajaxTarget=target;
	getChatData();
}

function getChatData()
{
	var url = ajaxTarget;
    // branch for native XMLHttpRequest object
	// FF and Opera and others
    if (window.XMLHttpRequest) {
		var reqc = new XMLHttpRequest();
        reqc.onreadystatechange = function(){
        	if (reqc.readyState==4){
            	if (reqc.status==200){
    	        	processChatReqChange(reqc);
				}
				else{
					alert("There was a problem retrieving data from the server:\n" + reqc.statusText);
					return false;
				}
			}
        }
        reqc.open("GET", url, true);
        reqc.send(null);
    // branch for IE/Windows ActiveX version
	// IE
    } else if (window.ActiveXObject) {
        isIE = true;
        var reqc = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqc) {
            reqc.onreadystatechange = function(){
        		if (reqc.readyState==4){
            		if (reqc.status==200){
    	       	 	processChatReqChange(reqc);
					}
					else{
						canRefresh = true;
						livechat.refresh();
						alert("There was a problem retrieving data from the server:\n" + reqc.statusText);
						return false;
					}
				}
			}
            reqc.open("GET", url, true);
            reqc.send();
        }
    }
}

function processChatReqChange(reqc)
{
	ajaxReturn(reqc.responseText);
}

function ajaxReturn(text){
	var fileLines=text.split("\n");
	var ajaxLastAction = fileLines[0].replace(/\r|\n|\r\n/g, "");
	//livechat.add(refreshtime+"<br>");
	switch(ajaxLastAction)
	{
		case "initiate_chat":
			canRefresh = true;
			fileLines=text.split("\n");
			livechat.setlastid(fileLines[1].replace(/\r|\n|\r\n/g, ""));
			for(i=2; i<fileLines.length; i++){
				livechat.add(fileLines[i]);
			}
			livechat.refresh();
		break;
		case "get_chat":
			canRefresh = true;
			newlastid=fileLines[1].replace(/\r|\n|\r\n/g, "");
			if(newlastid>lastid){
				livechat.setlastid(newlastid);
				for(i=2; i<fileLines.length; i++){
					livechat.add(fileLines[i]);
				}
			}
			livechat.refresh();
		break;
		case "post_chat":
			canRefresh = true;
			newlastid=fileLines[1].replace(/\r|\n|\r\n/g, "");
			if(newlastid>lastid){
				livechat.setlastid(newlastid);
				for(i=2; i<fileLines.length; i++){
					livechat.add(fileLines[i]);
				}
			}
		break;
		case "get_chat_online":
			tempOnline ='';
			for(i=2; i<fileLines.length; i++){
				tempOnline += fileLines[i];
			}
			livechat.setonline(tempOnline);
			livechat.online(30000);
		break;
		default:
		break;
	}
	/*var sh = chatArea.scrollHeight;
	var oh = chatArea.offsetHeight;
	//alert(sh+"-"+oh);
	if (sh > 0) // all but Explorer Mac
	{
		//if(chatArea.scrollTop!=chatArea.scrollHeight)
		chatArea.scrollTop = chatArea.scrollHeight;
	}
	else // Explorer Mac;
	     //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		chatArea.scrollTop = chatArea.offsetHeight;
	}*/
}

function showHide(layerId)
{
	DOM = (document.getElementById); // IE 5+ and NS 6+ 
	IE4 = (document.all); // IE 4 
	NS4 = (document.layers); // NS 4
	var state = ""; 

	if (DOM) { 
		state = document.getElementById(layerId).style.display;
		if(state == "none" && state != null) {
			document.getElementById(layerId).style.display = "";
		}
		else {
			document.getElementById(layerId).style.display = "none";
		}
	} 
	else if (IE4) { 
		state = document.all.layerId.style.display;
		if(state == "none" && state != null) {
			document.all.layerId.style.display = "";
		}
		else {
			document.all.layerId.style.display = "none";
		}
	} 
	else if (NS4) { 
		state = document.layers[layerId].style.display;
		if(state == "none" && state != null) {
			document.layers[layerId].style.display = "";
		}
		else {
			document.layers[layerId].style.display = "none";
		}
	} 	

}