/*--------------------------------------------------------*\
| PLEASE DO NOT MAKE CHANGES TO THIS FILE OR THEY WILL BE
| OVER WRITTEN ON EACH UPDATE
\*--------------------------------------------------------*/

// Setup Variables
var _shoutDoc = null;
var _shoutDiv = null;
var _shoutRRate = 2000;
var _shoutRRateMax = 5000;
var _shoutLastPost = null;
var _shoutLastUpdate = null;
var _shoutScrollTop = 0;
var _shoutApp = '';
var _shoutLastID = 0;
var _shoutSize = 0;
var _shoutScrollTop = 0;
//var _shoutColor = true;
//var _shoutLength = 0;

// Start Shoutbox
function _shoutStart(shout,append/*,lastID*/) {
	_shoutDoc = document.getElementById(shout);
	_shoutScrollBottom();
	_shoutLastPost = new Date().getTime();
	_shoutApp = append;
	_shoutRRate = _shoutRRateMax;
	_shoutLastID = _shoutGetLastID(_shoutDoc.innerHTML);
	_shoutSize = 0;
	if(_shoutDoc.scrollHeight<=0)
		window.onload = _shoutScrollBottom;
	setTimeout('_shoutGetContent()',_shoutRRateMax);
}

// Scroll Bottom
function _shoutScrollBottom() {
	_shoutDoc.scrollTop = _shoutDoc.scrollHeight;
}

// Get Content
function _shoutGetContent(setRefresh,setScroll) {
	_shoutAjax('/shout/shout'+_shoutApp+'.html?'+new Date().getTime(),'_shoutContent(xmlHttp.responseText,'+new Date().getTime()+','+setRefresh+','+setScroll+');');
}

// Shout Content
function _shoutContent(content,timeFired,setRefresh,setScroll) {
	// Don't Overwrite Updated Posts
	if(timeFired>=_shoutLastPost)
		_shoutUpdate(content,setScroll);
	
	// Set New Rate
	_shoutRRate += (Math.ceil((new Date().getTime()-_shoutLastPost)*.005));
	_shoutRRate = Math.min(_shoutRRateMax,Math.max(2000,_shoutRRate),_shoutRRate);
	if(setRefresh!==false)
		setTimeout('_shoutGetContent()',_shoutRRate);
}

// Shout Post
function _shoutPost(nameID,nameDefault,msgID,msgDefault,btn) {
	// Load
	var msgText = document.getElementById(msgID);
	var nameText = document.getElementById(nameID);

	// Disable Btn
	btn.disabled = true;
	msgText.blur();
	msgText.disabled = true;

	// Your Name
	nameText.value = _shoutTrim(nameText.value);
	if(nameText.value==nameDefault || nameText.value=='')
	{
		alert('Please enter your name.');
		btn.disabled = false;
		msgText.disabled = false;
		nameText.value = '';
		nameText.focus();
		return;
	}
	
	// Your Message
	msgText.value = _shoutTrim(msgText.value);
	if(msgDefault!='')
	{
		var msgRep = new RegExp('^'+msgDefault,'i');
		msgText.value = msgText.value.replace(msgRep,'');
	}
	if(msgText.value==msgDefault || msgText.value=='')
	{
		alert('Please enter your message.');
		btn.disabled = false;
		msgText.disabled = false;
		msgText.value = '';
		msgText.focus();
		return;
	}

	// Post
	msgVal = msgText.value;
	_shoutAjax('/shout/post.php?name='+escape(nameText.value)+'&msg='+escape(msgVal)+'&nocache='+new Date().getTime(),'_shoutPosted(xmlHttp.responseText,\''+btn.id+'\',\''+msgID+'\',\''+msgDefault+'\');');
	_shoutLastPost = new Date().getTime();
}

// Shout Posted
function _shoutPosted(content,btnID,msgID,msgDefault) {
	// Enable Posts
	var msgText = document.getElementById(msgID);
	msgText.disabled = false;
	document.getElementById(btnID).disabled = false;

	// Check Error
	if(content.match(/^ERROR=/))
	{
		content = content.replace(/ERROR=/,'');
		alert(content);
		return;
	}

	// Update Content
	msgText.value = '';
	msgText.focus();
	_shoutUpdate(content);
}

// Shout Update
function _shoutUpdate(content,setScroll)
{
	// Has EOF
	if(!content.match(/ENDOFFILE$/))
		return;
	content = _shoutTrim(content.replace(/ENDOFFILE$/,''));

	// Set Scroll
	if(setScroll!==false)
		_shoutScrollTop = _shoutDoc.scrollTop;

	// Get Size Info
	var sizeBefore = content.indexOf('<!---- SEPARATOR '+_shoutLastID+' ---->');
	if(sizeBefore!==-1)
		sizeBefore += ('<!---- SEPARATOR '+_shoutLastID+' ---->').length;
	var sizeTotal = content.length;

	// Same Size, Same ID
	if(_shoutSize==sizeBefore && _shoutSize==sizeTotal)
		return;

	// Missing Content (Reload)
	else if(_shoutSize==0 || sizeBefore==-1 || _shoutSize>sizeBefore)
	{
		//_shoutLastID = 0;
		//_shoutColor = true;
		_shoutSize = 0;
		_shoutDoc.innerHTML = '';
	//	alert(_shoutSize);
		//alert(sizeBefore);
	}
	
	// By Last ID
	else if(sizeBefore!==-1)
	{
		// Get New Content
		var content = _shoutTrim(content.substr(sizeBefore));
	
		// New Content
		/*
		if(content!=null && content!='')
		{
			// Fix Colors
			var isEven = _shoutGetLength(content)%2==0;
			if(
				(!isEven && !_shoutColor) ||
				(isEven && _shoutColor))
				{
					content = content.replace(/shoutBg1/g,'shoutBg3');
					content = content.replace(/shoutBg2/g,'shoutBg1');
					content = content.replace(/shoutBg3/g,'shoutBg2');
				}
			if(isEven)
				_shoutColor = _shoutColor?false:true;
		}
		*/
	}

	// Create New
	var newDiv = document.createElement('div');
	newDiv.innerHTML = content;

	// Append New
	_shoutDoc.appendChild(newDiv);

	// If 200px away keep position
	_shoutDoc.scrollTop = _shoutDoc.scrollHeight-(_shoutDoc.offsetHeight+_shoutScrollTop)>=200?_shoutScrollTop:_shoutDoc.scrollHeight;

	// New ID
	_shoutLastID = _shoutGetLastID(_shoutDoc.innerHTML);

	// New Length
	_shoutSize = sizeTotal;

	_shoutLastUpdate = new Date().getTime();
}

// Shout Delete
function _shoutDelete(msgid) {
	_shoutRefresh(false);
	_shoutAjax('/shout/delete.php?id='+msgid,'_shoutDeleted(xmlHttp.responseText);');
}

// Shout Deleted
function _shoutDeleted(content) {
	// Check Error
	if(content.match(/^ERROR=/))
	{
		content = content.replace(/ERROR=/,'');
		alert(content);
		return;
	}

	// Update Content
	_shoutUpdate(content,false);
}

// Shout Ban
function _shoutBan(msgid) {
	_shoutRefresh(false);
	_shoutAjax('/shout/delete.php?id='+msgid+'&ban=1','_shoutBanned(xmlHttp.responseText);');
}

// Shout Banned
function _shoutBanned(content) {
	// Check Error
	if(content.match(/^ERROR=/))
	{
		content = content.replace(/ERROR=/,'');
		alert(content);
		return;
	}

	// Update Content
	_shoutUpdate(content,false);
}

// Shout Refresh
function _shoutRefresh(getContent)
{
	//_shoutColor = true;
	_shoutScrollTop = _shoutDoc.scrollTop;
	_shoutLastID = 0;
	_shoutDoc.innerHTML = '<div style="padding:4px;text-align:center">REFRESHING...</div>';
	if(getContent!==false)
		_shoutGetContent(false,false);
}

// Shout Get Length
/*
function _shoutGetLength(content)
{
	var matches = content.match(new RegExp('shoutBg[1-2]','g'));
	return matches==null?0:matches.length;
}
*/

// Shout Get Last ID
function _shoutGetLastID(content)
{
	// '+_shoutLastID+' ---->
	var lastIndex = content.lastIndexOf('<!---- SEPARATOR');
	if(lastIndex>=0)
	{
		var newID = content.substr(lastIndex).match(/<!---- SEPARATOR [0-9]+ ---->/);
		if(newID!=null)
			return parseInt(newID.toString().replace(/<!---- SEPARATOR /,'').replace(/ ---->/,''));
		else
			return '';
	}
	else
		return '';
}

// Shout Prefill
function _shoutPrefill(txt)
{
	document.getElementById('shoutboxMsg').value = '@'+txt+' - ';
	if(document.getElementById('shoutboxMsg').type=='text')
		document.getElementById('shoutboxMsg').focus();
}

// Shout Ajax
function _shoutAjax(url,codeSuccess)
{
	var xmlHttp;
	try
	{
  	xmlHttp = new XMLHttpRequest();
	}
	catch(e)
  {
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
			eval(codeSuccess);
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function _shoutTrim(str) {
	return str.replace(/^\s+|\s+$/g,'');
}