//AJAX HELPER
//AUTHOR: Michael Yudin
//PURPOSE: Assist in smooth ajax operations, as well as portability.
//REQUIRES: Currently requires the prototype library.
//CREATED: April, 2009.


var AjaxHelper = function()
{
	// AJAX AREA: Periodical Updaters
	// PURPOSE: Assist in maintaining multiple periodical updaters
	
	this.interval_updaters = new Array(); //keep track of different ajax updaters
	
	// add new
	this.intervalUpdate = function(interval_name, ajax_options)
	{
		this.interval_updaters[interval_name] = new Ajax.PeriodicalUpdater(ajax_options);
		return 1;
	}
	
	// pause an already created updater
	this.pauseInterval = function(interval_name)
	{
		if (this.interval_updaters[interval_name] == undefined)
			return 0;
			
		this.interval_updaters[interval_name].stop();
		return 1;
	}

	// resume an already created updater
	this.startInterval = function(interval_name)
	{
		if (this.interval_updaters[interval_name] == undefined)
			return 0;
			
		this.interval_updaters[interval_name].start();
		return 1;
	}
	
	
	// AJAX AREA: Area Updaters
	// PURPOSE: Assist in smooth ajax area/div updates
	
	this.area_updaters = new Array(); //holds the request data for each updater
	
	// add new
	this.areaUpdater = function(updater_id, target, url, ajax_options)
	{
		
		// we want to make sure there is not already a request pending
		// for this updater
		if (this.area_updaters[updater_id] != undefined)
			this.areaUpdaterAbort(updater_id);
			
		//add the request to the queue
		this.area_updaters[updater_id] = new Ajax.Updater(target, url, ajax_options);
		
		return 1;
	}
	
	// abort a request
	this.areaUpdaterAbort = function(updater_id)
	{
		if (this.area_updaters[updater_id] == undefined)
			return 0;
		
		// make sure we dont call an onComplete or onSuccess when aborting
		this.area_updaters[updater_id].transport.onreadystatechange = Prototype.emptyFunction;
		this.area_updaters[updater_id].transport.abort();
		
		return 1;
	}
	
	// AJAX AREA: Ajax Request
	// PURPOSE: Assist in smooth ajax request
	
	this.ajax_requests = new Array(); //holds the request data for each updater
	
	// add new
	this.doRequest = function(updater_id, url, parameters, status_msg)
	{
		if ( $('request_update') != undefined )
			$('request_update').hide();
		
		// we want to make sure there is not already a request pending
		// for this updater
		if (this.ajax_requests[updater_id] != undefined)
			this.areaUpdaterAbort(updater_id);
			
		//add the request to the queue
		this.ajax_requests[updater_id] = 
			new Ajax.Request(url, {
			  parameters: parameters,
			  method: 'get',
   			  onLoading:function(){$('top_indicator').show();},  
			  onSuccess: function(transport) {
				$('request_update').innerHTML = status_msg;
				$('request_update').show();
				$('top_indicator').hide();
				},
			  onFailure:function(){$('top_indicator').hide();}
			}
			);

		
		return 1;
	}
	
	// abort a request
	this.requestAbort = function(updater_id)
	{
		if (this.ajax_requests[updater_id] == undefined)
			return 0;
		
		// make sure we dont call an onComplete or onSuccess when aborting
		this.ajax_requests[updater_id].transport.onreadystatechange = Prototype.emptyFunction;
		this.ajax_requests[updater_id].transport.abort();
		
		return 1;
	}
}