//NOTE: This object requires the jQuery library and will crash without it


/*********************************/
/* Panel Object                  */
/*********************************/

//Panel is an object that knows how to display and hide itself
function Panel(name, location, target)
{
	//Define variables
	this.name = name;
	this.location = location;
	this.visible = false;
	
	//Target is where the ajax will be sent to fill the panel.  This does NOT have to be equivalent to name
	//Without a target specified, the default will be name
	if(target == null)
		this.target = this.name;
	else
		this.target = target;
	
	//Define functions
	this.display = display;
	this.hide = hide;
	this.switchDisplay = switchDisplay;
	this.setVisible = setVisible;
	this.ajax = ajax;
}

/*********************************/
/* Panel functions               */
/*********************************/

//Overload of display to set speed
function display(speed)
{
	if(speed == null)
		speed = "slow";
	jQuery(this.name).slideDown(speed);
	this.visible = true;
}

//Use jQuery to slide the given div out of view
function hide(speed)
{
	if(speed == null)
		speed = "slow";
	jQuery(this.name).slideUp(speed);
	this.visible = false;
}

//Use jQuery to show or hide the given panel depending on visibility
function switchDisplay(speed)
{
	if(this.visible)
	{
		this.hide(speed);
	}
	else
	{
		this.display(speed);
	}
}

//Use jQuery to set height to auto for panels and bring panels to front
function setVisible()
{
	jQuery(this.name).css('height', 'auto');
	jQuery(this.name).css('z-index', '1');
}

//Use jQuery to ajax the contents into the target location
function ajax($)
{
	var localLocation = this.location;
	var localTarget = this.target;
	
	//Check that the location exists
	if(localLocation == '')
		return;

	//Autoload the location using Ajax
	$.ajax({
				url: localLocation,
				async: true,
				dataType: 'html',
				error: function(ajaxRequest)
				{
					jQuery(localTarget).html('Error fetching data.<br />Server Response: ' + ajaxRequest.responseText);
				},
				success: function(content)
				{
					jQuery(localTarget).html(content);
				}
			});
}
