var serverList = new Array();
var currentListPosition = 0;

$(document).ready(function()
	{
	$('ul').serializelist();
	});

window.onload = function()
	{
	preloadImages();
	//getGoing();
	}

function showDiv(showElement)
	{
	document.getElementById(showElement).style.display = 'block';
	}
function hideDiv(hideElement)
	{
	document.getElementById(hideElement).style.display = 'none';
	}

function verticalOnly(item)
	{
	item.toolManDragGroup.verticalOnly()
	}

function speak(id, what)
	{
	var element = document.getElementById(id);
	element.innerHTML = 'Clicked ' + what;
	}

$(document).ready(function() 
	{
	$("form").submit(function(e) 
		{
		if (e.originalEvent.explicitOriginalTarget.id == "shoutBoxSubmit") 
			{
			//we need to send the text via JS and then stop the form submit.
			addShoutBox();
			callShoutBox();
			e.preventDefault();
			}
		});
	return;
	});

function addShoutBox()
	{
	//function to send new shoutbox entries via JS	
	var sMessage = $('#newShout').val();
	
	//$.post("/ajaxserver.php?sid=" + sessionID, { "function": "userAddShoutbox", xmlData: sMessage},function(data){updateShoutBox(data);});
	
	$('#newShout').val('');
	}

function callShoutBox()
	{
	$.post("/ajaxserver.php", { "function": "populateShoutbox"},function(data){updateShoutBox(data);});
	}

function updateShoutBox(data)
	{
	//function to re-populate the shoutbox (can be setup on a timed event to reload...
	$("#shoutBox-Lines").html(data);
	}

function submitCommand(sCommand)
	{
	var iCommand = document.getElementById('COMMAND');
	var iSubmit = document.getElementById('CUSTOMCOMMAND');
	if(iCommand != null)
		{
		iCommand.value = sCommand;
		if(iSubmit != null)
			{
			iSubmit.submit();
			}
		else
			{
			alert("no form :'(");
			}
		}
	else
		{
		alert("No command field");
		}
	}

function serverstatus() 
		{
		submitCommand("status");
		}
function maplist()
		{
		submitCommand("maps *");
		}
function changemap()
		{		
		var iMapName = document.getElementById("MAPNAME");
		
		submitCommand("map " + iMapName.value);
		}
function restart()
		{
		submitCommand("restart");		
                }
function kickid()
		{
		
		var iKickID = document.getElementById("KICKID");
		var iReason = document.getElementById("KICKREASON");
		
		submitCommand("kickid " + iKickID.value + " " + "\"" + iReason.value + "\"");
                }
function say()
		{
		var iSay = document.getElementById("SAY");
		
		submitCommand("say " + "\"" + iSay.value + "\"");
		}
function search()
		{
		var iSearch = document.getElementById("SEARCH");
		
		submitCommand("cvarlist " + iSearch.value);
		}
// Get the HTTP Object

function getHTTPObject()
	{
	if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else
		{
		alert("Your browser does not support AJAX.");
		return null;
		}
	}
// Change the value of the outputText field
function setOutput(serverid)
	{
	if(httpObject.readyState == 4)
		{
		var responseTxt = httpObject.responseText;

		var updateTxt = "Unknown response(" + responseTxt + ")";
		switch(responseTxt)
			{
			case "getstate() = on":
				{
				updateTxt = "Server powered up";
				break;
				}
			case "getstate() = off":
				{
				updateTxt = "Server powered down";
				break;
				}
			case "reset() = 1":
				{
				updateTxt = "Server restarting";
				break;
				}
			case "stop() = 1":
				{
				updateTxt = "Server powering down";
				break;
				}
			case "start() = 1":
				{
				updateTxt = "Server powering up";
				break;
				}
			}

		document.getElementById('outputText_'+serverid).innerHTML = updateTxt;
		if(serverList.length > 0)
			{
			if(currentListPosition < serverList.length)
				{
				currentListPosition += 1;
				doWork('getstate',serverList[currentListPosition]);
				}
			}
		}
	}

function doWork(option,serverid)
	{
	httpObject = getHTTPObject();
	if (httpObject != null)
		{
		document.getElementById('outputText_'+serverid).innerHTML = "Running";
		httpObject.open("GET", "vmcom.php?serverid="+serverid+"&option="
		+option, true);
		httpObject.send(null);
		//httpObject.onreadystatechange = setOutput;
		httpObject.onreadystatechange = function() { setOutput(serverid) };
		}
	}
function getAllStatus()
	{
	//this function will loop through the above array and (should) return ALL server status via javascript
	doWork('getstate',serverList[currentListPosition]);
	}
function find_username(url)
	{
	popup(url, 760, 570, '_usersearch');
	return false;
	}
function popup(url, width, height, name)
	{
	if (!name)
		{
		name = '_popup';
		}

	window.open(url.replace(/&amp;/g, '&'), name, 'height=' + height + ',resizable=yes,scrollbars=yes, width=' + width);
	return false;
	}

function insert_user(user)
	{
	opener.document.forms['select_user'].username.value = ( opener.document.forms['select_user'].username.value.length && opener.document.forms['select_user'].username.type == "textarea" ) ? opener.document.forms['select_user'].username.value + "\n" + user : user;
	}

function insert_marked(users)
	{
	if (typeof(users.length) == "undefined")
		{
		if (users.checked)
			{
			insert_user(users.value);
			}
		}
	else if (users.length > 0)
		{
		for (i = 0; i < users.length; i++)
			{
			if (users[i].checked)
				{
				insert_user(users[i].value);
				}
			}
		}

	self.close();
	}

function insert_single(user)
	{
	opener.document.forms['select_user'].username.value = user;
	self.close();
	}

function ajaxSort(tableName,orderField,listID)
	{
	var dataArray = 'tablename=' + tableName +'&orderfield=' + orderField + '&' + $('#' + listID).sortable('serialize');

	$.ajax({
	   type: "POST",
	   url: "ajaxserver.php?functionname=sort",
	   data: dataArray,
	   success: function(msg)
			{
			$('#ajaxReturn').html(msg);
			}
		});
	}	