var Commentary = {
	RefreshRate : function(){
		var wait = 3000;
		var rand = 2000;
		return (wait+(Math.random()*rand));
	},
	CommentInt : 0,
	newComments : function(){
		this.CommentInt = setInterval(function(){Commentary.fetchnewComments();},this.RefreshRate());
	},
	fetchnewComments : function(){
		$.ajax({type: "GET",
			url: "xmlcomments2.php?ft=view&req="+req,
			dataType: "text",
			success: function(json){
				eval(json);
				if (JSON.mail.notify){
					document.getElementById("mail").innerHTML="<a href='mail.php' target='_blank' onClick=\"window.open('mail.php','mailphp','scrollbars=yes,resizable=yes,width=550,height=300').focus();$('#mail').hide('slow');return false;\" class='hotmotd'>You have "+JSON.mail.unseen+" new messages!</a>";
					$("#mail").show("slow");
					//setTimeout(function(){$("#mail").hide("slow");},3000);
				}
				if (JSON.commenthead) document.getElementById("commentaryheader").innerHTML = JSON.commenthead;
				if (JSON.commentbody==0){
				}else{
					var commentary = "";
					for (var i in JSON.commentbody){
						commentary += JSON.commentbody[i].debug;
						commentary += JSON.commentbody[i].comment;
					}
					document.getElementById("commentary").innerHTML=commentary;
				}
				if (JSON.commentfoot) document.getElementById("commentaryfooter").innerHTML = JSON.commentfoot;
			},
			error: function(){
				document.getElementById("commentary").innerHTML="Refresh the chat!";
			}
		});
	},
	commentModerate: function(url){
		$.ajax({type: "GET",
			url: url,
			dataType: "text",
			success: function(){Commentary.fetchnewComments()}
		});
	},
	NewPost : function(form){
var form = document.getElementById("commentaryform");
var talkline = form.talkline.value;
var schema = form.schema.value;
var counter = form.counter.value;
var section = form.section.value;
	var insertcommentary = form.insertcommentary.value;
 $.ajax({
   type: "POST",
   url: "xmlcomments2.php?ft=post",
   data: "insertcommentary="+totalEncode(insertcommentary)+"&talkline="+talkline+"&schema="+schema+"&counter="+counter+"&section="+section,
   success: function(){
	Commentary.fetchnewComments();
   }
 });
	form.insertcommentary.value="";
	document.getElementById("charsleftinsertcommentary").innerHTML="";
	document.getElementById("previewtextinsertcommentary").innerHTML="";
	}
}

var commentary = document.getElementById("commentary");
function commentaryrate(){
	var wait = 3000;
	var rand = 2000;
	return (wait+(Math.random()*rand));
}
/*var xmlcomments = {};
function newcommentary(){
	xmlcomments = setInterval("request('xmlcomments2.php?ft=view&req='+req)", commentaryrate());
}*/

/*function newcommentary(){
	xmlcomments = setInterval(function(){$.ajax({
  url: "xmlcomments2.php?ft=view&req="+req,
  cache: false,
  dataType: "html",
  success: function(html){
	if (html==""){}else{$("#commentary").html(html);}
  }
});},commentaryrate());
}
function newcommentary(){
	xmlcomments = setInterval(function(){
	$.ajax({
		type: "GET",
		url: "xmlcomments2.php?ft=view&req="+req,
		dataType: "text",
		success: function(json) {
			eval(json);
			var block = '';
			block += JSON.commenthead;
			for (var i in JSON.commentbody){
				block += JSON.commentbody[i].debug;
				block += JSON.commentbody[i].comment;
			}
			block += JSON.commentfoot;
			if (block==""){}
			else{$("#commentary").html(block);}
		}
	});
},commentaryrate());
}
*/
var block = document.getElementById("commentblock");
var view = document.getElementById("commentview");
function commentblock(){/*
	block.style.position="absolute";
	block.style.display="none";
	block.style.visibility="hidden";
	*/view.style.position="static";
	view.style.display="block";
	view.style.visibility="visible";
	block.parentNode.replaceChild(view,block);
	createCookie("livechat",1,30);
	Commentary.newComments();
	newpost();
}
function newpost(){
	$("form#commentaryform").submit(function(){
		Commentary.NewPost(this);
		return false;
	});
}
function commentview(){/*
	view.style.position="absolute";
	view.style.display="none";
	view.style.visibility="hidden";
	block.style.position="static";
	block.style.display="block";
	block.style.visibility="visible";*/
	view.parentNode.replaceChild(block,view);
	clearInterval(Commentary.CommentInt);
	eraseCookie("livechat");
	$("form#commentaryform").unbind("submit");
}
function requestdel(url){
	Commentary.commentModerate(url);
}
function trim(str){
return str.replace(/^\s\s*/, "").replace(/\s\s*$/, "");}
function totalEncode(str){
var s=escape(trim(str));
s=s.replace(/\+/g,"+");
s=s.replace(/@/g,"@");
s=s.replace(/\//g,"/");
s=s.replace(/\*/g,"*");
return(s);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


$(document).ready(function(){
	$("div#commentview").fadeTo("fast", 0);
});
$("button#enablechat").click(function(){
	$("#enablechat").hide("fast");
	$("#disablechat").show("fast");
	$("div#commentblock").fadeTo("slow", 0);
	setTimeout(function(){commentblock();},1000);
	setTimeout(function(){$("div#commentview").fadeTo("slow", 1.0);},3000);
});
$("button#disablechat").click(function(){
	$("#disablechat").hide("fast");
	$("#enablechat").show("fast");
	$("div#commentview").fadeTo("slow", 0);
	setTimeout(function(){commentview();},1000);
	setTimeout(function(){$("div#commentblock").fadeTo("slow", 1.0);},3000);
});
$(document).ready(function(){
	var input = document.getElementById("inputinsertcommentary");
	if (input) input.parentNode.setAttribute("id","commentaryform");

	Commentary.fetchnewComments();
	var cookie = readCookie("livechat");
	if (cookie==1){
	$("#enablechat").hide("fast");
	$("#disablechat").show("fast");
	commentblock();
	$("div#commentblock").fadeTo("fast",0);
	$("div#commentview").fadeTo("fast",1.0);
	}else{
	$("#disablechat").hide("fast");
	$("#enablechat").show("fast");
	}
	$("#mail").hide("fast");
});