<!--
/*
This little snippet of code provides support for a TS3 monitor applet. The TS server data must be provided formatted as HTML by an external page, which gets loaded 
through an XMLHttp call (nowadays they call it Ajax it's trendier). 
*/

// Objective: Creates a TSMonitor class object.
// Arguments: - serverIP, string; TS3 server IP.
//			  - serverPort, integer; TS server voice port (UDP). Defaults to 9987.
//			  - queryPort, integer; TS server query port (TCP). Defaults to 
function TSMonitor(serverIP, serverPort, queryPort, frameLayer) 
{
	this.serverIP = serverIP;
	this.serverPort = serverPort;	// TS3 server voice port (UDP)
	this.queryPort = queryPort;		// TS3 server query port (TCP)
	this.scriptPathName = "ts3monitor.asp";
	this.curSkinPath = "skins/default/"
	this.monitorOn = false;
	this.timeout = 15000;
	this.outputLayer = null;
	this.frameLayer = frameLayer;

	this.handleResponse = function(httpRequest)
	{
		switch (httpRequest.readyState)
		{
			case 0:
				break;
			case 1:
				break;
			case 2:
				break;
			case 3:
				if (this.outputLayer)
				{
					if (this.outputLayer.innerText != "Loading...")
					{
						// Stores the scrollTop position to restore it when data are received, and displays a "loading" message:
						this.outputLayer.tempScroll = this.outputLayer.scrollTop;
						this.outputLayer.innerText = "Loading...";
					}
				}
				break;

			case 4:
				if (this.outputLayer)
				{
					//var nScrollTop = this.outputLayer.scrollTop;
					this.outputLayer.innerHTML = httpRequest.responseText;
					if (this.outputLayer.tempScroll) this.outputLayer.scrollTop = this.outputLayer.tempScroll;
				}
		}
	}

	this.hide = function()
	{
		if (this.frameLayer) this.frameLayer.style.display = "none";
	}

	this.refresh = function()
	{
		//alert(this.serverIP + " is alive " + this.monitorOn);
		if (this.monitorOn)
		{
			makeRequest(this.scriptPathName + "?ip=" + this.serverIP + "&sp=" + this.serverPort + "&qp=" + this.queryPort + "&action=get", "GET", "", this);
			var obj = this;
			setTimeout(function () { obj.refresh(); }, obj.timeout);
		}
		
	}

	this.loadPos = function()
	{
		// Check that the pcpcookielib is referenced:
		if (cookieObject)
		{
			// Get the layer coords from the cookie and set the layer to the given coords:
			oCookie = new cookieObject("ts_monitor", 365, "/", "x", "y");
			if (oCookie.found)
			{
				this.frameLayer.style.left = oCookie.get("x");
				this.frameLayer.style.top = oCookie.get("y");
			}
			
		}
	}

	this.savePos = function()
	{
		// Check that the pcpcookielib is referenced:
		if (cookieObject)
		{
			// Store the layer coords:
			oCookie = new cookieObject("ts_monitor", 365, "/", "x", "y");
			oCookie.put("x", this.frameLayer.style.left);
			oCookie.put("y", this.frameLayer.style.top);
			oCookie.write();
		}
	}

	

	this.setSkin = function(newSkinPath)
	{
		if (newSkinPath) this.curSkinPath = newSkinPath;

		if (this.frameLayer)
		{
			updateSkin(this.frameLayer, this.curSkinPath);
		}
	}

	this.show = function()
	{
		if (this.frameLayer) this.frameLayer.style.display = "block";
	}

	function updateSkin(oItem, skinPath)
	{
		if (oItem.style)
			{
				if (oItem.style.backgroundImage)
				{
					if (oItem.style.backgroundImage.indexOf("bg.") > 0)
						oItem.style.backgroundImage = "url('" + skinPath + "bg.png')";
					else
						oItem.style.backgroundImage = "url('" + skinPath + "bg2.png')";
				}
			}
			if (oItem.tagName == "IMG")
			{
				oItem.src = skinPath + "btn_close.png";
			}
			if (oItem.childNodes)
			{
				for (var i=0; i < oItem.childNodes.length; i++)
					updateSkin(oItem.childNodes[i], skinPath);
			}
	}

}


-->