//-----------------------------------------
//              ShoutOut!
//        www.invisionmint.com
//       Copyright © 2007 - 2008
//-----------------------------------------

function shoutout()
{
	this.inited = false;
	this.sid_editting = null;
	this.sid_text = null;
	this.in_refresh = false;
	this.in_idle = false;
	this.activated = true;
	this.clickmiss = null;
	this.refreshing = false;
	this.e_g_shouts = null;
	this.textarea_cols = null;
	this.textarea_rows = null;
	this.in_history = false;
	this.color_but_text = "Color";
	this.color = "#000000";
	this.timeoutobj = null;
	this.sorting = "newtoold";
	this.idlemode = 1;
	this.lang = Array();
	this.cmd = null;
	this.privto = null;
}

shoutout.prototype.init = function()
{
	shoutout.inited = true;
	but = my_getbyid('sb_colorselector');
	shoutout.color_but_text = but.value;
	
	if(shoutout.sorting == "oldtonew")
	{
		shoutout.e_g_shouts.parentNode.scrollTop = shoutout.e_g_shouts.parentNode.scrollHeight;
	}
	shoutout.cmd = new shoutout.commands();
	shoutout.cleartext();
	


	var def_color = my_getcookie("shoutbox_color");
	if(def_color && def_color != "")
	{
		but = my_getbyid('sb_colortochangeto');
		but.value = shoutout.color_but_text + ": "+def_color;
		but.style.color = def_color;
		shoutout.color = def_color;
	}
	
		
}

shoutout.prototype.commands = function()
{
	this.msg = "eval.shoutout.towhotopm();";
}

shoutout.prototype.cleartext = function()
{
	var textarea = my_getbyid('new-shout');
	textarea.value = "";
}

shoutout.prototype.addtotext = function(str, menuclose)
{
	
	if(menuclose)
	{
		ipsmenu.close();
	}
	
	var textarea = my_getbyid('new-shout');
	
	if(str.substring(0, 5) == "eval.")
	{
		eval(str.substring(5));
	}
	else
	{
		textarea.value += str;
	}

}

shoutout.prototype.towhotopm = function()
{
	shoutout.privto = prompt(shoutout.lang['whotosendto'], "");
	if(shoutout.privto == "" || shoutout.privto == null)
	{
		return;
	}
	shoutout.cleartext();
	shoutout.addtotext("/pm \""+shoutout.privto+ "\" ");
	alert(shoutout.lang['thanksnowentermsg']);
	my_getbyid('new-shout').focus();
}

shoutout.prototype.edit = function(id)
{
	var fields = Array();
	var msg = my_getbyid('the-shout-'+id);
	
	msg.innerHTML = '';
	var textarea = document.createElement('textarea');
	textarea.cols = shoutout.textarea_cols;
	textarea.row = shoutout.textarea_rows;
	textarea.id = "edit-Shout";
	msg.appendChild(textarea);
	textarea.innerHTML = my_getbyid('raw-shout-'+id).innerHTML;
	
	shoutout.sid_editting = id;
	shoutout.sid_text = textarea.innerHTML;
	
	textarea.onblur = shoutout.save;	
}

shoutout.prototype.save = function()
{

	var fields = Array();
	var textarea = my_getbyid('edit-Shout');
	var msg = my_getbyid('the-shout-'+shoutout.sid_editting);
	
	if(textarea.value == shoutout.sid_text) {
		return;
	}
	
	my_getbyid('raw-shout-'+shoutout.sid_editting).innerHTML = textarea.value;
	fields['message'] = textarea.value;
	fields['id'] = shoutout.sid_editting;
	
	req = function()
	{
		if (xmlobj.readystate_ready_and_ok())
		{
			var html = xmlobj.xmlhandler.responseText;
			
			if(html.substring(0, 5) == 'ERROR') {
				eval(html.substring(6));
			} else {
				msg.innerHTML = html; 
				gmsg = my_getbyid('shoutId-'+shoutout.sid_editting);
			}
			
			html = '';
		
		}

	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(req);
	
	var u = ipb_var_base_url+'autocom=shoutbox&code=ajax&cmd=editshout&id='+shoutout.sid_editting;
	
	xmlobj.process(u, 'POST', xmlobj.format_for_post(fields));
}

shoutout.prototype.deleteshout = function(id) {
	req = function()
	{
		if (xmlobj.readystate_ready_and_ok())
		{
			var html = xmlobj.xmlhandler.responseText;
			if(my_getbyid('history-row-'+id)) {
				var row = my_getbyid('history-row-'+id);
				row.parentNode.removeChild(row);
			}
			
			if(my_getbyid('shout-row-'+id)) {
				var row = my_getbyid('shout-row-'+id);
				row.parentNode.removeChild(row);
			}
			
			html = '';
		}

	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(req);
	
	var u = ipb_var_base_url+'autocom=shoutbox&code=ajax&cmd=deleteshout&id='+id;
	
	xmlobj.process(u, 'GET');
}

shoutout.prototype.refresh_ani = function()
{
	toggleview('refresh_button');
}

shoutout.prototype.resize = function(add)
{
	height = parseInt(shoutout.e_g_shouts.parentNode.style.height) ? parseInt(shoutout.e_g_shouts.parentNode.style.height ) : 100;
	
	if(add == "1") {
		// Increase
		nheight = height + 20;
	} else {
		// Decrease
		if(height != "20") {
			nheight = height - 20;
		}
	}
	
	if(nheight < 0) {
		nheight = 0;
	}
	
	shoutout.e_g_shouts.parentNode.style.height = nheight + "px";
	
	my_setcookie('shoutbox_size', nheight, 1);
}

shoutout.prototype.updatecolor = function(obj)
{
	value = obj.value;
	obj.style.color = value;
}

shoutout.prototype.changecolor = function(e)
{
	obj = my_getbyid('sb_color2');
	if(obj.value == '')
	{
		obj = my_getbyid('sb_color1');
	}
	
	value = obj.value;
	but = my_getbyid('sb_colortochangeto');
	
	but.value = shoutout.color_but_text + ": "+value;
	but.style.color = value;
	my_getbyid('new-shout').style.color = value;
	
	shoutout.color = value;
	
	my_setcookie("shoutbox_color", value, 1);
	
	ipsmenu.menu_registered['sb_colorselector'].close();
	ipsmenu.menu_registered['sb_helpbutton'].open(my_getbyid('sb_helpbutton'));
}

shoutout.prototype.cancelcolor = function(e)
{
	but = my_getbyid('sb_colortochangeto');
	
	but.value = shoutout.color_but_text;
	but.style.color = null;
	my_getbyid('new-shout').style.color = null;
	
	shoutout.color = null;
	
	my_setcookie("shoutbox_color", "", 0);
	
	ipsmenu.menu_registered['sb_colorselector'].close();
	ipsmenu.menu_registered['sb_helpbutton'].open(my_getbyid('sb_helpbutton'));
}

shoutout.prototype.setonclick = function() {
	document.onmousemove = shoutout.resetrefresh;
	document.onclick = shoutout.resetrefresh;
	document.onkeydown = shoutout.resetrefresh;
	window.onresize = shoutout.resetrefresh;
}

shoutout.prototype.resetrefresh = function() {
	shoutout.clickmiss = 0;
	
	if(shoutout.in_idle) {
		shoutout.e_g_shouts.parentNode.className = 'row1';
		currentlyRefreshing = false;
		shoutout.runtimeout();
	}
	
	is_idle = false;
	
}

shoutout.prototype.idleani = function() {
	shoutout.e_g_shouts.parentNode.className = 'darkrow3';
	
	my_getbyid('refresh_button').style.display = '';
}

shoutout.prototype.checkclick = function() {
	if(shoutout.clickmiss >= 3) {
		is_idle = true;
		
		shoutout.idleani();
	} else {
		is_idle = false;
	}
}

shoutout.prototype.runtimeout = function() {
	shoutout.refresh();
	shoutout.timeoutobj = setTimeout("shoutout.runtimeout();", shoutout.refreshtime);
}

shoutout.prototype.cleartimeout = function() {
	clearTimeout(shoutout.timeoutobj);
	shoutout.timeoutobj = setTimeout("shoutout.runtimeout();", shoutout.refreshtime);
}

shoutout.prototype.js_inc = function(filename)
{
	var body = document.getElementsByTagName('body').item(0);
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	body.appendChild(script)
}

shoutout.prototype.js_exec = function(scriptx)
{
	var body = document.getElementsByTagName('body').item(0);
	script = document.createElement('script');
	script.type = 'text/javascript';
	script.innerHTML = scriptx;
	body.appendChild(script)
}

shoutout.prototype.refresh = function()
{
	if(shoutout.refreshing) {
		return;
	}
	shoutout.refreshing = true;
	shoutout.cleartimeout();
	
	if(shoutout.idlemode)
	{
		shoutout.checkclick();
		if(shoutout.in_idle) {
			return;
		}
	}
	
	shoutout.clickmiss++;

	if(!shoutout.activated) {
		return;
	}
	
	
	shoutout.refresh_ani();
	
	req = function()
	{
		if (xmlobj.readystate_ready_and_ok())
		{
			var html = xmlobj.xmlhandler.responseText;
			

			if(shoutout.sorting == "oldtonew")
			{
				shoutout.e_g_shouts.innerHTML = shoutout.e_g_shouts.innerHTML + html;
				shoutout.e_g_shouts.parentNode.scrollTop = shoutout.e_g_shouts.parentNode.scrollHeight;
			}
			else
			{
				shoutout.e_g_shouts.innerHTML = html + shoutout.e_g_shouts.innerHTML;
			}
			
			html = '';
			shoutout.refreshing = false;
			shoutout.refresh_ani();
		}

	}
	
	var e = shoutout.e_g_shouts.getElementsByTagName('div');
	var lsid = 0;
	
	for(var i=0;i<e.length;i++){
	
		if(e[i].id.substring(-1,10) == "shout-row-") {
			if(shoutout.sorting != "oldtonew")
			{
				if(lsid == 0) {
					lsid = e[i].id.substring(10);
				}
			}
			else
			{
				lsid = e[i].id.substring(10);
			}
		}
		
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(req);
	
	var u = ipb_var_base_url+'autocom=shoutbox&code=ajax&cmd=showshouts&lastshoutid='+lsid;
	
	xmlobj.process(u, 'GET');	
}


shoutout.prototype.post = function() {
	var fields = Array();
	var textarea = my_getbyid('new-shout');
	var ob = my_getbyid('global-shouts');

	var color = shoutout.color;
	
	
	shoutout.refresh_ani();
	
	fields['message'] = textarea.value;
	if(color != '')
	{
		fields['color'] = color;
	}
	
	if(fields['message'] == '') {
		if(my_getbyid('new-shout').value != '') {
			fields['message'] = my_getbyid('new-shout').value;
		}
	}
	
	if(fields['message'].substring(0, 5) == "/pm {")
	{
		if(shoutout.privto == null)
		{
			fields['privto'] = fields['message'].substring(5);
			pos = fields['privto'].indexOf("}");
			fields['privto'] = fields['privto'].substring(0, pos);
		}
		else
		{
			fields['privto'] = shoutout.privto;
		}
		
		fields['message'] = fields['message'].substring(5);
		fields['message'] = fields['message'].substring(fields['privto'].length + 2);
		shoutout.privto = null;
	}
	
	req = function()
	{
		if (xmlobj.readystate_ready_and_ok())
		{
			var html = xmlobj.xmlhandler.responseText;
			
			
			if(html.substring(0, 5) == 'ERROR') {
				eval(html.substring(6));
			} else {
			

				if(shoutout.sorting == "oldtonew")
				{
					shoutout.e_g_shouts.innerHTML = shoutout.e_g_shouts.innerHTML + html;
					shoutout.e_g_shouts.parentNode.scrollTop = shoutout.e_g_shouts.parentNode.scrollHeight;
				}
				else
				{
					shoutout.e_g_shouts.innerHTML = html + shoutout.e_g_shouts.innerHTML;
				}
			}

			
			
			shoutout.refresh_ani();
			
			html = '';
		}

	}
	
	var e = ob.getElementsByTagName('div');
	var lsid = 0;
	
	for(var i=0;i<e.length;i++){
	
		if(e[i].id.substring(-1,10) == "shout-row-") {
			if(shoutout.sorting != "oldtonew")
			{
				if(lsid == 0) {
					lsid = e[i].id.substring(10);
				}
			}
			else
			{
				lsid = e[i].id.substring(10);
			}
		}
		
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange(req);
	
	var u = ipb_var_base_url+'autocom=shoutbox&code=ajax&cmd=postshout&lastshoutid='+lsid;
	shoutout.cleartext();
	xmlobj.process(u, 'POST', xmlobj.format_for_post(fields));	
}


shoutout.prototype.activate = function() {
	togglecategory('shoutbox', 0);
	shoutout.activated = true;
	
}

shoutout.prototype.deactivate = function() {
	togglecategory('shoutbox', 1);
	shoutout.activated = false;
}

shoutout.prototype.emoticon = function(code, usless, useless2) {
	my_getbyid('new-shout').value += ' '+code + ' ';
}


shoutout.prototype.showemoticons = function() {
	
	window.emoticon = shoutout.emoticon;
	window.open(ipb_var_base_url+'act=legends&CODE=emoticons', 'Emoticons', 'width=250,height=500,resizable=yes,scrollbars=yes');
	return false;
}

shoutout.prototype.bbcode = function() {
	window.open(ipb_var_base_url+'act=legends&CODE=bbcode', 'BBCode', 'width=700,height=500,resizable=yes,scrollbars=yes');
	return false;
}

shoutout.prototype.colormenu = function()
{
	
	ipsmenu.menu_registered['sb_colorselector'].open(my_getbyid('sb_helpbutton'));
	document.onclick = menu_action_close;
}
