/**
 * Contains the Javascript class Sitebox.
 *
 * @copyright		2000-2006 CARE Internet Services B.V. All Rights Reserved
 * @internal		$Workfile: Sitebox.js$
 * @internal		$Date: 03-08-07 08:19:15 AM$
 * @author			$Author: robert$
 * @version			$Revision: 1$
 *
 * $NoKeyword$
 */

/**
 * Sitebox class with basic static functions.
 */
Sitebox = {
	includeJavascriptOnce: function(url)
	{
		var loadedScripts = document.getElementsByTagName('script');
		for (var i = 0; i < loadedScripts.length; i++)
		{
			if (loadedScripts[i].src && getNodeAttribute(loadedScripts[i], 'src').indexOf(url) == 0)
			{
				return false;
			}
		}
		script = createDOM('SCRIPT', {'src': url, 'type': 'text/javascript'});
		appendChildNodes(document.getElementsByTagName('head')[0], script);
		return true;
	}, // function includeJavascriptOnce

	includeStylesheetOnce: function(url)
	{
		var loadedStylesheets = document.getElementsByTagName('link');
		for (var i = 0; i < loadedStylesheets.length; i++)
		{
			if (loadedStylesheets[i].rel == 'stylesheet' && loadedStylesheets[i].href && getNodeAttribute(loadedStylesheets[i], 'href').indexOf(url) == 0)
			{
				return false;
			}
		}
		stylesheet = createDOM('LINK', {'href': url, 'type': 'text/css', 'rel': 'stylesheet'});
		appendChildNodes(document.getElementsByTagName('head')[0], stylesheet);
		return true;
	}, // function includeStylesheetOnce

	showProgressIndicator: function(msg)
	{
		if (!$('sb-progress-indicator'))
		{
			attributes = {
				'id': 'sb-progress-indicator',
				'style': {
					'zIndex': '1000000000',
					'position': 'absolute',
					'left': '50%',
					'top': '50%',
					'margin': '-28px 0 0 -105px',
					'width': '200px',
					'border': '2px solid #000',
					'padding': '10px 10px 10px 10px',
					'font': 'bold 11pt sans-serif',
					'background': '#FFF'}};
			indicator = DIV(attributes, IMG({'src': sbFrameworkUrl + '/images/Sitebox/ProgressIndicator/indicator.gif', 'style': {'marginRight': '5px', 'verticalAlign': 'text-top'}}), msg);
			appendChildNodes(currentDocument().body, indicator);
		}
		else
		{
			this.updateProgressIndicator(msg);
		}
	}, // function showProgressIndicator

	updateProgressIndicator: function(msg)
	{
		if ($('sb-progress-indicator'))
		{
			$('sb-progress-indicator').childNodes[1].nodeValue = msg;
		}
	}, // function updateProgressIndicator

	hideProgressIndicator: function()
	{
		if ($('sb-progress-indicator'))
		{
			removeElement($('sb-progress-indicator'));
		}
	}, // function hideProgressIndicator

	callMaxTries: 100,
	callInterval: 10,
	callCounter: new Array(),
	call: function(f, id)
	{
		if (id == undefined)
		{
			var id = this.callCounter.length;
			this.logDebug('new function call id: ' + id);
			this.callCounter[id] = 0;
		}
		try
		{
			f();
		}
		catch(e)
		{
			/* Only retry if it's a ReferenceError. */
			if (e instanceof Error || e instanceof ReferenceError || e instanceof TypeError)
			{
				if (this.callCounter[id] < this.callMaxTries)
				{
					window.setTimeout('Sitebox.call(' + f + ', ' + id + ');', this.callInterval);
					this.callCounter[id] += 1;
					this.logDebug('function call ' + id + ' failed: ' + this.callCounter[id]);
				}
				else
				{
					this.logDebug('function call ' + id + ' aborted: ' + this.callCounter[id] + ' tries.');
					/* Rethrow exeption, and let the browser/log panel catch it. */
					throw e;
				}
			}
			else
			{
				/* If it's any other type of exception, rethrow it
				 * and let the browser/log panel catch it.
				 */
				throw e;
			}
		}
	}, // function call

	/**
	 * Static shortcut function for logging an emergency to the debug panel.
	 */
	logEmerg: function(message)
	{
		if (sbDebugPanel)
		{
			sbDebugPanel.log(message, 'emerg', toISOTimestamp(new Date()));
		}
	}, // function logEmerg

	/**
	 * Static shortcut function for logging an alert to the debug panel.
	 */
	logAlert: function(message)
	{
		if (sbDebugPanel)
		{
			sbDebugPanel.log(message, 'alert', toISOTimestamp(new Date()));
		}
	}, // function logAlert

	/**
	 * Static shortcut function for logging a critical message to the debug panel.
	 */
	logCrit: function(message)
	{
		if (sbDebugPanel)
		{
			sbDebugPanel.log(message, 'crit', toISOTimestamp(new Date()));
		}
	}, // function logCrit

	/**
	 * Static shortcut function for logging an error message to the debug panel.
	 */
	logError: function(message)
	{
		if (sbDebugPanel)
		{
			sbDebugPanel.log(message, 'error', toISOTimestamp(new Date()));
		}
	}, // function logError

	/**
	 * Static shortcut function for logging a warning message to the debug panel.
	 */
	logWarning: function(message)
	{
		if (sbDebugPanel)
		{
			sbDebugPanel.log(message, 'warning', toISOTimestamp(new Date()));
		}
	}, // function logWarning

	/**
	 * Static shortcut function for logging a notice to the debug panel.
	 */
	logNotice: function(message)
	{
		if (sbDebugPanel)
		{
			sbDebugPanel.log(message, 'notice', toISOTimestamp(new Date()));
		}
	}, // function logNotice

	/**
	 * Static shortcut function for logging an info message to the debug panel.
	 */
	logInfo: function(message)
	{
		if (sbDebugPanel)
		{
			sbDebugPanel.log(message, 'info', toISOTimestamp(new Date()));
		}
	}, // function logInfo

	/**
	 * Static shortcut function for logging a debug message to the debug panel.
	 */
	logDebug: function(message)
	{
		if (sbDebugPanel)
		{
			sbDebugPanel.log(message, 'debug', toISOTimestamp(new Date()));
		}
	} // function logDebug
};

/**
 * Prevents javascript errors if no debug panel available
 */
var sbDebugPanel = false;