/**
*	@fileoverview Simple example that shows how to encapsulate
*	XMLHTTPRequestCalls
*
*	@author Mike Chambers (mesh@adobe.com)
*/

/**
*	Constructor for the class.
*
*	@param {String} dataURL The path to the data that the class
*	will load (OPTIONAL)
*
*	@constructor
*/
function Ajax(containerID, strLoadPage, loadingType)
{
	this._strLoadPage = strLoadPage;
	this._containerID = containerID;
	this._loadingType = loadingType;
	this.load();
}

//where to load the data from
Ajax.prototype._containerID = "";
Ajax.prototype._strLoadPage = "";

//var to hold an instance of the XMLHTTPRequest object
Ajax.prototype._request = undefined;


/**************** Public APIs **********************/

/**
*	Tells the class to load its data and render the results.
*/
Ajax.prototype.load = function()
{
	//get a new XMLHTTPRequest and store it in an instance var.
	this._request = this._getXMLHTTPRequest();

	//set the var so we can scope the callback
	var _this = this;

	//callback will be an anonymous function that calls back into our class
	//this allows the call back in which we handle the response (_onData())
	// to have the correct scope.
	this._request.onreadystatechange = function(){_this._onData()};
	this._request.open("GET", this._strLoadPage, true);
	this._request.send(null);
}

/***************Private Rendering APIs ********************/

//renders the entire widget
Ajax.prototype._render = function(title)
{
	if(this._containerID != null)
	{
	    var content = document.getElementById(this._containerID);
	    content.innerHTML = title;
    }
}

//renders the entire widget
Ajax.prototype._loading = function()
{
	if(this._containerID != null)
	{
	    var content = document.getElementById(this._containerID);
	    if(this._loadingType != null && this._loadingType != undefined && this._loadingType != "")
	    {
        /*
	        if(navigator.appName != "Microsoft Internet Explorer")
	        {
	            if(content.getStyle('position') != 'relative' && content.getStyle('position') != 'absolute')
	            {
	                content.style.position = 'relative';
	            }
    	    }
    	    else
    	    {
	            if(content.style.position != 'relative' && content.style.position != 'absolute')
	            {
	                content.style.position = 'relative';
	            }
    	    }
        */
    	    switch(this._loadingType)
    	    {
    	        default : 
        	        content.innerHTML = '<div style="width:100%; text-align:center;"><img src="/images/presentation/Loading.gif" alt="Loading data"/></div>';    
    	        break;
    	        case 'full' :
    	            content.innerHTML += '<div class="progressPanel"><img src="/images/presentation/loadingCircleImage.gif" alt="Loading data"/></div>';    
    	        break;
    	        case 'none' :
        	    
    	        break;
    	    }
        }
        else
        {
            content.innerHTML = '<div style="width:100%; text-align:center;"><img src="/images/presentation/Loading.gif" alt="Loading data"/></div>';    
        }
    }
}

/***************Private Data Loading Handlers*******************/

//callback for when the data is loaded from the server
Ajax.prototype._onData = function()
{
	if(this._request.readyState == 4)
	{
		if(this._request.status == "200")
		{
			this._render(this._request.responseText);

			//if the onDraw callback has been defined
			//call it to let the listener know
			//that we are done creating the list
			if(this.onDraw != undefined)
			{
				this.onDraw();
			}
		}
		else
		{
			//check if an error callback handler has been defined
			if(this.onError != undefined)
			{
				//pass an object to the callback handler with info
				//about the error
				this.onError({status:this_request.status,
						statusText:this._request.statusText});
			}
		}

		//clean up
		delete this._request;
	}
	else
	{
		this._loading();
	}
}

/***************Private Data Util Functions ********************/

//returns an XMLHTTPRequest instance (based on browser)
Ajax.prototype._getXMLHTTPRequest = function()
{
	var xmlHttp;
	try
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e2)
		{
		}
	}

	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined'))
	{
		xmlHttp = new XMLHttpRequest();
	}

	return xmlHttp;
}