var ShoutReq;
var nRefresh = 30000;   // now (temporarily?) irrelevant. See below.
var shout_channelcat=0;
var shout_channelid=0;
var shout_guildid=0;
var shout_timer;
var shout_styleid=0;

function ShoutBox(guildid,channelcat,channelid,shout_text,url_base,styleid)
{

  if (window.XMLHttpRequest) { // Non-IE browsers
    ShoutReq = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    ShoutReq = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    // TODO: This browser does not support Ajax
  }

  ShoutReq.onreadystatechange = processStateChangeShout;

	// start = 'http://192.168.10.179:3080';
	// if (url_base) {
	// 	start = url_base;
	// }
	// console.log(start);
  // shoutstr = start + '/GuildShout.php?timestamp=' + new Date().getTime() +'&guildid='+guildid+'&cat=' + channelcat + '&id=' + channelid + '&shout='+escape(shout_text);

  shoutstr = '/GuildShout.php?shout_styleid=' + styleid + '&timestamp=' + new Date().getTime() +'&guildid='+guildid+'&cat=' + channelcat + '&id=' + channelid + '&shout='+escape(shout_text);

  ShoutReq.open('GET', shoutstr, true);
  ShoutReq.send(null);

  return false;

}

function processStateChangeShout()
{
  var ShoutCell = document.getElementById('ShoutBox');
  if(ShoutReq.readyState==4) {  // Complete
    if(ShoutReq.status==200) {  // OK
      ShoutCell.innerHTML = ShoutReq.responseText;
    } else {
      // console.log("Problem: " +ShoutReq.statusText);
    }
  }
}

/* Guild shouts */

function doShout(guildid)
{
	e = document.getElementById('shout_text');
	ShoutBox(guildid,0,0,e.value,'',shout_styleid);
	e.value = '';
}

function doShoutRefresh(guildid)
{

	ShoutBox(guildid,0,0,"",'',shout_styleid);

    // Removing this to prevent hammering of server
	//shout_timer = setTimeout("doShoutRefresh(" + guildid + ");",nRefresh);

}

/* General-purpose shouts (any channel) */

function redrawShout()
{
	setShoutChannel(shout_guildid,shout_channelcat,shout_channelid);
}

function setShoutChannel(guildid,channelcat,channelid,styleid)
{
	shout_guildid = guildid;
	shout_channelcat = channelcat;
	shout_channelid = channelid;

	shout_styleid = (styleid == null) ? 0 : styleid;

	clearTimeout(shout_timer);
	doGenShoutRefresh();

}

function setShoutChannelStr(str)
{

	var channel_array = str.split(',');
	setShoutChannel(channel_array[0],channel_array[1],channel_array[2]);

}

function doGenShout()
{
	e = document.getElementById('shout_text');
	ShoutBox(shout_guildid,shout_channelcat,shout_channelid,e.value,'',shout_styleid);
	e.value = '';
}

function doGenShoutRefresh()
{

	ShoutBox(shout_guildid,shout_channelcat,shout_channelid,"",'',shout_styleid);

    // Removing this to prevent hammering of server
	//setTimeout("doGenShoutRefresh();",nRefresh);
}
