// Ajax request class
function AjaxRequest(requestfile)
{
	this.phpfile = requestfile;
}

AjaxRequest.prototype.phpfile;

/*
	Sends ajax request to the server using GET http request
*/
AjaxRequest.prototype.requestGET=function(areaId, parameters)
{
	if (areaId != null && areaId != '')
	{
		document.getElementById(areaId).innerHTML = '<font size="1" face="Verdana,Arial,sans-serif,system">Loading...</font>';
	}
	var xmlHttp = this.getAjax();
	if (xmlHttp != null)
	{
		// Prepare the request URL
		var file = this.phpfile;
		if (parameters.length > 0)
		{
			file += "?";
			for (i = 0; i < parameters.length; i++)
			{
				file += parameters[i];
				if (i < parameters.length-1)
				{
					file += "&";
				}
			}
		}
		
		// Set the response handler
		var xmlhttpObject = xmlHttp;
		xmlHttp.onreadystatechange=function()
		{
			if (xmlhttpObject.readyState == 4)
			{
				if (areaId != null && areaId != '')
				{
					document.getElementById(areaId).innerHTML = xmlhttpObject.responseText;
				}
			}
		}
		
		// Send the actual request
		xmlHttp.open("GET", file, true);
		xmlHttp.send(null);
	}
}

/*
	Sends ajax request to the server using GET http request with a callback function
*/
AjaxRequest.prototype.requestGET=function(areaId, parameters, callback)
{
	var xmlHttp = this.getAjax();
	if (xmlHttp != null)
	{
		// Prepare the request URL
		var file = this.phpfile;
		if (parameters.length > 0)
		{
			file += "?";
			for (i = 0; i < parameters.length; i++)
			{
				file += parameters[i];
				if (i < parameters.length-1)
				{
					file += "&";
				}
			}
		}
		
		// Set the response handler
		var xmlhttpObject = xmlHttp;
		xmlHttp.onreadystatechange=function()
		{
			if (xmlhttpObject.readyState == 4)
			{
				if (areaId != null && areaId != '')
				{
					document.getElementById(areaId).innerHTML = xmlhttpObject.responseText;
					eval(callback);
					// callback();
				}
			}
		}
		
		// Send the actual request
		xmlHttp.open("GET", file, true);
		xmlHttp.send(null);
	}
}

/*
	Sends ajax request to the server using POST http request
*/
/*AjaxRequest.prototype.requestPOST=function(areaId, parameters)
{
	var xmlHttp = this.getAjax();
	if (xmlHttp != null)
	{
		var xmlhttpObject = xmlHttp;
		xmlHttp.onreadystatechange=function()
		{
			if (xmlhttpObject.readyState == 4)
			{
				document.getElementById(areaId).innerHTML = xmlhttpObject.responseText;
			}
		}
		var param = '';
		for (i = 0; i < parameters.length; i++)
		{
			param += parameters[i];
			if (i < parameters.length-1)
			{
				param += "&;
			}
		}
		xmlHttp.open("POST", this.phpfile, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", param.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(param);
	}
}*/

/*
	The basic function to get the XML HTTP object for AJAX operations
*/
AjaxRequest.prototype.getAjax=function()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				alert("Browser does not support AJAX. Get to the 90's man!!");
			}
		}
	}
	return xmlHttp;
}

var SpexLab =
{
	mediaPlayerName: 'media_player',
	loadPage:function(page_id)
	{
		this.showLoader();
		(new AjaxRequest("gateway.php")).requestGET('content_area', new Array('pageid='+page_id), 'SpexLab.hideLoader()');
	},
	showLoader:function()
	{
		document.getElementById('loader_layer').style.visibility = "visible";
	},
	hideLoader:function()
	{
		document.getElementById('loader_layer').style.visibility = "hidden";
	},
	playMedia:function(media_url)
	{
		var media_player = document.getElementById(this.mediaPlayerName);
		if (media_player)
		{
			var player = '<object height="16" width="98%">';
			player += '<param name="kioskmode" value="true" />';
			player += '<param name="src" value="'+media_url+'" />';
			player += '<param name="autoplay" value="true" />';
			player += '<param name="controller" value="true" />';
			player += '<embed height="16" src="'+media_url+'" type="video/quicktime" width="98%" controller="true" autoplay="true" kioskmode="true" />';
			player += '</object>';
			player += '<a href="javascript:SpexLab.hideMedia()">Hide Player</a>';
	
			document.getElementById(this.mediaPlayerName).innerHTML = player;
		}
	},
	hideMedia:function()
	{
		document.getElementById(this.mediaPlayerName).innerHTML = '';
	}
}
