//
// Azure Gaming Widget Functions
//
function isValidElement(name)
{
	var widget = document.getElementById(name);
	if(!widget)
		return false;
	else
		return true;
}

function hideWidget(name)
{
	var widget = document.getElementById(name);
	if(!widget) return false;
	
	widget.style.visibility = 'hidden';
	widget.style.display = 'none';
	
	return true;
}

function unhideWidget(name)
{
	var widget = document.getElementById(name);
	if(!widget) return false;
	
	widget.style.visibility = 'visible';
	widget.style.display = 'block';
	
	return true;
}

//
// JsWidgetChanger
//
function jsWidgetChanger(current, next)
{
	this.changeWidget = function ()
	{
		if(isValidElement(this.current) && isValidElement(this.next))
		{
			if(!hideWidget(this.current) || !unhideWidget(this.next))
			{
				unhideWidget(this.current);
				hideWidget(this.next);
			}
		}
		
		return;
	}
	
	//variables
	this.current = current;
	this.next = next;
}

//
// JsScroller
//
// Written by Nathan Faubion: http://n-son.com
// Use this or edit how you want, just give me
// some credit!
//
// Change Log:
//
// May 5, 2008 - Michael Sadlon
//   Changed o argument for easier use (constructor and swapContent)
//   by making it the name of the actual div, not the parent object
//   Also did some general code cleanup.
function jsScroller (name, o, w, h)
{
	this._name = name;
	this.content = document.getElementById(o);
	
	//Note: due to IE7 not loading the offsetHeight property until the DOM tree is fully loaded, 
	//I have changed the code to call document.getElementById(o) every time this.startScroll() fires -_-;;
	
	//Private methods
	this._setPos = function (x, y)
	{
		if (x < this.viewableWidth - this.totalWidth) 
			x = this.viewableWidth - this.totalWidth;
		if (x > 0) x = 0;
		if (y < this.viewableHeight - this.totalHeight) 
			y = this.viewableHeight - this.totalHeight;
		if (y > 0) y = 0;
		
		this._x = x;
		this._y = y;
		
		this.content.style.left = this._x + 'px';
		this.content.style.top  = this._y + 'px';
	}
	
	//Public Methods
	this.reset = function ()
	{
		this.content = document.getElementById(o);	//fix for IE7
		
		this.totalHeight = this.content.offsetHeight;
		this.totalWidth	 = this.content.offsetWidth;
		this._x = 0;
		this._y = 0;
		
		this.content.style.left = '0px';
		this.content.style.top  = '0px';
	}
	
	this.scrollBy = function (x, y)
	{
		this._setPos(this._x + x, this._y + y);
	}
	
	this.scrollTo = function (x, y)
	{
		this._setPos(-x, -y);
	}
	
	this.stopScroll = function ()
	{
		if (this.scrollTimer) window.clearInterval(this.scrollTimer);
	}
	
	this.startScroll = function (x, y)
	{
		this.content = document.getElementById(o);	//fix for IE7
		this.totalWidth	 = this.content.offsetWidth;
		this.totalHeight = this.content.offsetHeight;
		
		this.stopScroll();
		this.scrollTimer = window.setInterval(this._name + '.scrollBy(' + x + ', ' + y + ')', 25);
	}
	
	this.swapContent = function (n, w, h)
	{
		o = n;	//fix for IE7
		
		this.content = document.getElementById(n);
		if (w) this.viewableWidth  = w;
		if (h) this.viewableHeight = h;
		this.reset();
	}
	
	//variables
	this.viewableWidth  = w;
	this.viewableHeight = h;
	this.totalWidth	 = this.content.offsetWidth;
	this.totalHeight = this.content.offsetHeight;
	this.scrollTimer = null;
	this.reset();
}

//
// XML HTTP Request
//
var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject('Msxml2.XMLHTTP')},
	function () {return new ActiveXObject('Msxml3.XMLHTTP')},
	function () {return new ActiveXObject('Microsoft.XMLHTTP')}
];

function createXMLHTTPObject()
{
	var xmlhttp = false;
	
	for (var i=0; i < XMLHttpFactories.length; ++i)
	{
		try
		{
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e)
		{
			continue;
		}
		break;
	}
	
	return xmlhttp;
}

function sendRequest(url, callback, postData)
{
	var req = createXMLHTTPObject();
	if (!req) return;
	
	var method = (postData) ? 'POST' : 'GET';
	req.open(method, url, true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		
	req.onreadystatechange = function ()
	{
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304)
		{
			//alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	
	if (req.readyState == 4) return;
	req.send(postData);
}
