var GetChaturl = "/a_get_chat_data.php";
var SendChaturl = "/a_send_chat_data.php";
var lastID = -1; //initial value will be replaced by the latest known id

function pause_it(){
$('pause_id').value='1';
}
function play_it(){
$('pause_id').value='0';
}
//initiates the first data query
function receiveChatText(mem_del,no_comment_count,lastIDoverride) {
	var tee_id = $('tee_id').value;
	var chat_page=	$('chat_page').value;
	var lastID=	$('lastID').value;
	var pauseit =$('pause_id').value;
	var creator_uid =$('creator_uid').value;
if (lastIDoverride){
var lastID=1;
}
	if (pauseit==0){	
		if (httpReceiveChat.readyState == 4 || httpReceiveChat.readyState == 0) {
		httpReceiveChat.open("GET",GetChaturl + '?tee_id=' + tee_id + '&lastID=' + lastID + '&chat_page=' + chat_page + '&md='+mem_del+'&creator_uid='+creator_uid+'&r='+Math.floor(Math.random() * 500000), true);
		httpReceiveChat.onreadystatechange = function() {
		handlehHttpReceiveChat(no_comment_count);
		}; 
		httpReceiveChat.send(null);
			if ( typeof( window[ 'update_chat_id' ] ) != "undefined" ) {
				clearTimeout(update_chat_id);
			} 
		}else{
		setTimeout('receiveChatText();',12000);
		}

	}
}

//deals with the servers' reply to requesting new content
function handlehHttpReceiveChat(no_comment_count) {
var chat_page=	$('chat_page').value;
  if (httpReceiveChat.readyState == 4) {
    result = httpReceiveChat.responseText.split('--*'); //the fields are seperated by ---
    if (result.length > 2)
	{
	    for(i=result.length-1;i >=1;i=i-8) 
		{ 
			insertNewComment(result[i-8],result[i-7],result[i-6],result[i-5],result[i-4],result[i-3],result[i-2],result[i-1]); 
	    	$('new_comment_id'+result[i-8]).style.display='none';
			new Effect.Appear('new_comment_id'+result[i-8]);
			if (!no_comment_count)
			{
				$('comment_count').innerHTML++;
			}
		}
	    lastID = result[0];
		$('lastID').value=lastID;
    }
    update_chat_id = setTimeout('receiveChatText();',12000); //executes the next data query in 4 seconds
}
	if (chat_page>1)
	{
		$('pause_id').value=1;
	}else{
		$('pause_id').value=0;
	}
}

function insertNewComment(liID,liDate,liName,liAvatar_small,liRate,liText,liFans,liCreator) {
	insertO = document.getElementById("chat_ul");
	var tee_id = $('tee_id').value;
	oLi = document.createElement('li');
	
	oLi.setAttribute('id','new_comment_id'+liID);


		comment_box = document.createElement('DIV');
		comment_box.setAttribute('class','comment_box');
		comment_box.setAttribute('className','comment_box'); //for IE's sake

		
		avatarhref= document.createElement('a');
		avatarhref.setAttribute('href', '/'+liName);
		comment_box.appendChild(avatarhref);
	

		img = document.createElement('IMG');
		img.setAttribute('class','avatar');
		img.setAttribute('className','avatar'); //for IE's sake		
		img.src = liAvatar_small;
		avatarhref.appendChild(img);	

	
		comment_post = document.createElement('DIV');
		comment_post.setAttribute('class','comment_post');
		comment_post.setAttribute('className','comment_post'); //for IE's sake

		comment_byline = document.createElement('DIV');
		comment_byline.setAttribute('class','byline');
		comment_byline.setAttribute('className','byline'); //for IE's sake

		author_name = document.createElement('SPAN');
		author_name.setAttribute('class','author');
		author_name.setAttribute('className','author');
		author_name.setAttribute('id','author_'+liID);

		comment_byline.appendChild(author_name);
		
		
		comment_text = document.createElement('DIV');
		comment_text.setAttribute('class','bluey');
		comment_text.setAttribute('className','bluey');
		comment_text.setAttribute('id','text_'+liID);
	
		
		comment_post.appendChild(comment_byline);
		comment_post.appendChild(comment_text);



		oLi.appendChild(comment_box);
		comment_box.appendChild(comment_post);


		insertO.insertBefore(oLi, insertO.firstChild);
		$('text_'+liID).innerHTML=liText;

		var top_right_button=' <a href="#" rel="/a_delconfirm.php?uname='+liName+'&com_id='+liID+'" onClick="return hs.htmlExpand(this, { contentId: \'delcom_id\', objectType: \'ajax\' } )" class="x" title="Delete or Flag this comment"></a></div>';
		$('author_'+liID).innerHTML='<div class="top_right">'+top_right_button+'</div>'+liName+' '+liCreator+' '+liDate+' '+liRate;
}	




//stores a new comment on the server
function sendComment() {
		var tee_id = $('tee_id').value;
		var chat_input_id_small = $('chat_input_id_small').value;
		var chat_input_id_big = $('chat_input_id_big').value;
		$('chat_input_id_small').value = '';
		$('chat_input_id_big').value = '';
		if (chat_input_id_small){
		var	chat_input_id = chat_input_id_small;
		}else{
		var	chat_input_id = chat_input_id_big;
		}
	var currentChatText = chat_input_id;
	var currentChatText = currentChatText.replace(/\+/g,'*amus15*');
	var currentChatText = encodeURIComponent(currentChatText);

	if (currentChatText != '' & (httpSendChat.readyState == 4 || httpSendChat.readyState == 0)) {
		disable_form(0,'chatForm','post_comment_button1');
		disable_form(0,'chatForm','post_comment_button2');
		$('working').style.display='block';
		$('working').innerHTML='<img src="/i/stat/s_paus.gif">';
	var chat_page=	$('chat_page').value;
	if (chat_page>1){
	turn_page(1);
	}	
		param = 'chat_input_id=' + currentChatText + '&tee_id=' + tee_id + '&r='+Math.floor(Math.random() * 100000);	
		httpSendChat.open("POST", SendChaturl, true);
		httpSendChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  	httpSendChat.onreadystatechange = handlehHttpSendChat;
  	httpSendChat.send(param);
	document.forms['chatForm'].elements['chat_input_id_small'].focus();
	} else {
	//	setTimeout('sendComment();',1);
	}
}

function save_fan(rand_id)
{
	var randomnumber=Math.round(Math.random()*100000);
	var fanof_uid  = $('fanof_uid'+rand_id).value;
	var fan_msg_id = encodeURIComponent($('fan_msg_id'+rand_id).value);
	var myAjax = new Ajax.Request('/a_fan.php',{
		method: 'post', parameters: 'save=1&fanof_uid='+fanof_uid+'&fan_msg_id='+fan_msg_id+'&rand_id='+rand_id+'&r='+randomnumber}
	);	
}
function remove_fan(rand_id,liID){
	var randomnumber=Math.round(Math.random()*100000);
	var fanof_uid =$('fanof_uid'+rand_id).value;
	var myAjax = new Ajax.Request('/a_fan.php',{
		method: 'post', parameters: 'fanof_uid='+fanof_uid+'&del=1&r='+randomnumber}
	);	
}
function add_fan(fan_count,liID){
var new_fan_count=fan_count+1;
$('fan_id'+liID).innerHTML=new_fan_count;
}
function delete_comment(com_id){	
var randomnumber=Math.floor(Math.random() * 100000);
new Effect.Fade('new_comment_id'+com_id);
new Effect.SlideUp('new_comment_id'+com_id);
var myAjax = new Ajax.Request('/a_delconfirm.php',{method: 'post', parameters: 'com_id='+com_id+'&del=1&r='+randomnumber});	
$('comment_count').innerHTML--;
}
function report_comment(com_id,reason,closethis){	
	if (reason){
		var randomnumber=Math.floor(Math.random() * 100000);
		new Effect.Fade('new_comment_id'+com_id);
		new Effect.SlideUp('new_comment_id'+com_id);
		var myAjax = new Ajax.Request('/a_delconfirm.php',{method: 'post', parameters: 'com_id='+com_id+'&reason='+encodeURIComponent(reason)+'&del=1&r='+randomnumber});	
		$('comment_count').innerHTML--;
		return hs.close(closethis);
	}
}

function report_comment(com_id,reason,closethis){	
	if (reason){
		var randomnumber=Math.floor(Math.random() * 100000);
		new Effect.Fade('new_comment_id'+com_id);
		new Effect.SlideUp('new_comment_id'+com_id);
		var myAjax = new Ajax.Request('/a_delconfirm.php',{method: 'post', parameters: 'com_id='+com_id+'&reason='+encodeURIComponent(reason)+'&del=1&r='+randomnumber});	
		$('comment_count').innerHTML--;
		return hs.close(closethis);
	}
}


function turn_page(page){
	var chat_page=	$('chat_page').value;
	$('page_'+chat_page+'_top').className='';
	$('page_'+page+'_top').className='here';
	$('page_'+chat_page+'_top').innerHTML=chat_page;
	$('page_'+page+'_top').innerHTML=page;

	$('page_'+chat_page+'_bot').className='';
	$('page_'+page+'_bot').className='here';
	$('page_'+chat_page+'_bot').innerHTML=chat_page;
	$('page_'+page+'_bot').innerHTML=page;

	if (page==1){
	$('lastID').value='1';
	}
		pause_it();
		$('chat_ul').innerHTML='';
		$('chat_page').value=page;
		play_it();
		receiveChatText('',1);

		return false;

}
//deals with the servers' reply to sending a comment
function handlehHttpSendChat() {
  if (httpSendChat.readyState == 4) {
	disable_form(1,'chatForm','post_comment_button1');
	disable_form(1,'chatForm','post_comment_button2');
	Effect.Fade('working');
	receiveChatText(1); //refreshes the chat after a new comment has been added (this makes it more responsive)
  }
}



//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


// initiates the two objects for sending and receiving data
var httpReceiveChat = getHTTPObject();
var httpSendChat = getHTTPObject();
