/*
	This class is used for content management.
	It basically loads content from scripts into the 
	content area.
 */

var Content = new Object ();	// Initialize the Content object.

// Sets this object's work-element.
Content.setElement = function (element)
{
	this.element = element;
}

// Displays a URI's content into the content area.
// Param (uri): The URI to load the content from.
Content.display = function (uri, beforeHTML, afterHTML, dontSaveHistory)
{
	// Save stuff to history
	if (!dontSaveHistory)
	{
		for (var i = Content.historyPosition + 1; i < Content.history.length; delete Content.history [i ++]);
		Content.history [++ Content.historyPosition] = uri;
	}
	
	// Update the current location.
	Content.uri = uri;
	
	if ((typeof Content.request) != 'undefined' && Content.request.readyState != 4) // If there's already a request going on...
		Content.request.abort ();		// Cancel the request.
	
	// Create the new request
	Content.request = new XMLHttpRequest ();
	
	// Open a connection
	Content.request.open ("GET", uri, true);
	
	// Do no-cache hack
	Content.request.setRequestHeader ("If-Modified-Since", new Date (0));
	
	// Set the callback function to when the response state changes.
	Content.request.onreadystatechange = Content.onStateChange;
	Content.beforeHTML = beforeHTML;
	Content.afterHTML = afterHTML;
	
	// Send nothing, this is not a form.
	Content.request.send (null);
	
	// Clear all of the current content.
	Content.clear ();
}

Content.forward = function ()
{
	if (Content.historyPosition < Content.history.length - 1)
		Content.display (Content.history [++ Content.historyPosition], false, false, true);
}

Content.back = function ()
{
	if (Content.historyPosition > 0)
		Content.display (Content.history [-- Content.historyPosition], false, false, true);
}

// Clears the page and shows a loading wait frame.
Content.clear = function ()
{
	Content.clean();
	Content.element.style.backgroundImage		= "url(images/loading.gif)";
	Content.element.style.backgroundRepeat		= "no-repeat";
	Content.element.style.backgroundPosition	= "center center"
}

// Clean the page
Content.clean = function ()
{
	Content.setContent ("");
}

// Sets the content.
Content.setContent = function (content, saveHistory)
{
	Content.element.style.backgroundImage = "none";
	Content.element.innerHTML = content;
	//AutoSizer.update ();
	if (content === "")
		return;
	
	// Run on page load evaluation script
	var elEvals = this.element.getElementsByTagName ("script");

	for (var i=0; i < elEvals.length; i++)
	{
  	var evalString = "";
		if (elEvals[i])
		{
			try
			{
				evalString  = elEvals[i].innerHTML.replace (/&gt;/g, ">");
				evalString  = evalString.replace (/&lt;/g, "<");
				evalString  = evalString.replace (/&amp;/g, "&");
	  
				eval (evalString);
			}
			catch (e)
			{
				alert ("ERROR: " + e.message + "\r\n" + evalString);
			}
		}
	}
}

// Runs when a response state has been changed.
Content.onStateChange = function ()
{
	if (Content.request.readyState == 4)
	{
		var evaluate = Content.request.getResponseHeader ("Evaluate");
		if (evaluate)
			eval (evaluate);
		
		if (Content.request.status == 200 && Content.request.responseText)
			Content.setContent ((Content.beforeHTML ? Content.beforeHTML : "") + Content.request.responseText + (Content.afterHTML ? Content.afterHTML : ""));
		else if (!evaluate)
		{
			var errPre = document.createElement ("DIV");
			errPre.innerHTML = "<strong>שגיאה בטעינת דף</strong><br />\r\nמצב: " + Content.request.status + " " + Content.request.statusText + "<br />URI: " + Content.uri;
			Content.setContent ("");
			Content.element.appendChild (errPre);
		}
	}
}

Content.history = new Array ();
Content.historyPosition = -1;

