// Copyright 2006 The Cornerstone Framework Project
// $Header: /home/cvsroot/site4/content/cornerstone/init.js,v 1.15 2011-08-17 06:22:34 svca Exp $

csf = {};

// ------
// logger

csf.Logger = function(level)
{
	this.level = level;
};

csf.Logger.DEBUG = 4;
csf.Logger.INFO = 3;
csf.Logger.WARN = 2;
csf.Logger.ERROR = 1;
csf.Logger.ID = 'csf-logger';

csf.Logger.prototype.debug = function(message)
{
	this.log(csf.Logger.DEBUG, message);
};

csf.Logger.prototype.info = function(message)
{
	this.log(csf.Logger.INFO, message);
};

csf.Logger.prototype.warn = function(message)
{
	this.log(csf.Logger.WARN, message);
};

csf.Logger.prototype.error = function(message)
{
	this.log(csf.Logger.ERROR, message);
};

csf.Logger.prototype.dumpStack = function()
{
	try
	{
		throw new Error('stack');
	}
	catch(e)
	{
		this.error(e.stack || e);
	}
};

csf.Logger.prototype.log = function(level, message)
{
	if (this.level >= level)
	{
		if (window.console && typeof(console.log) == 'function')
		{
			// FireBug
			console.log(message);
		}
		else if (window.opera)
		{
			// Opera
			opera.postError(message);
		}
		else
		{
			var loggerDiv = document.getElementById(csf.Logger.ID);
			if (!loggerDiv)
			{
				loggerDiv = document.createElement('div');
				loggerDiv.id = this.ID;
				var head = document.getElementsByTagName('head')[0];
				head.appendChild(loggerDiv);
			}
			var entry = document.createElement('div');
			entry.innerHTML = message;
			loggerDiv.appendChild(entry);
		}
	}
};

// -------
// XmlHttp

csf.XmlHttp = function()
{
	this.request = false;

	// IE
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with
	// old IE versions and security blocked creation of the objects.
	try
	{
		this.request = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			this.request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			request = false;
		}
	}
	@end @*/

	// Mozilla
	if (!this.request && typeof(XMLHttpRequest) != 'undefined')
	{
		this.request = new XMLHttpRequest();
	}
};

csf.XmlHttp.create = function()
{
	return new csf.XmlHttp();
};

csf.XmlHttp.prototype.requestSync = function(method, url, body, continuation, params, nocache)
{
	if (nocache && isIE())
	{
		url += (url.indexOf('?') > 0 ? '&' : '?') +
			'__nocache__=' + getUniqueNumber(1000000);
	}
	this.request.open(method, url, false);

	if (nocache && !isIE())
	{
		this.request.setRequestHeader('Cache-Control', 'no-cache');
	}
	this.request.send(body);

	continuation.call(this, params);
};

csf.XmlHttp.prototype.requestAsync = function(method, url, body, continuation, params, nocache)
{
	if (nocache && isIE())
	{
		url += (url.indexOf('?') > 0 ? '&' : '?') +
			'__nocache__=' + getUniqueNumber(1000000);
	}
	this.request.open(method, url, true);

	var xmlHttp = this;
	this.request.onreadystatechange = function()
	{
		if (xmlHttp.request.readyState == 4)
		{
			continuation.call(xmlHttp, params);
			xmlHttp.hideProgressBar();
		}
	}

	this.showProgressBar();

	if (nocache && !isIE())
	{
		this.request.setRequestHeader('Cache-Control', 'no-cache');
	}
	this.request.send(body);
};

PROGRESS_BAR_ID = 'progressBar';
PROGRESS_BAR_HTML = '<div id="' + PROGRESS_BAR_ID + '">Loading ...</div>';
document.write(PROGRESS_BAR_HTML);
progressBarLevel = 0;

csf.XmlHttp.prototype.showProgressBar = function()
{
	if (progressBarLevel++ == 0)
	{
		var windowSize = getWindowSize();
		var windowScroll = getWindowScroll();

		var progressBar = document.getElementById(PROGRESS_BAR_ID);
		// progressBar.style.left = windowSize.width / 2 + windowScroll.x + 'px';
		progressBar.style.left = 925 / 2 + windowScroll.x + 'px';
		progressBar.style.top = windowSize.height / 2 + windowScroll.y + 'px';
		progressBar.style.display = '';
		document.body.style.cursor = 'progress';
	}
};

csf.XmlHttp.prototype.hideProgressBar = function()
{
	if (--progressBarLevel == 0)
	{
		var progressBar = document.getElementById(PROGRESS_BAR_ID);
		progressBar.style.display = 'none';
		document.body.style.cursor = 'default';
	}
};

// browser detection

var IEBrowser = 'Microsoft Internet Explorer' ;
var IE = 'IE' ;

getBrowserName = function()
{
	var ret = '' ;
	var name = navigator.appName ;
	if (name == IEBrowser)
	{	
		ret = IE ;
	}
	return ret ;
};

isIE = function()
{
	return navigator.appName == IEBrowser;
};

isWebKit = function()
{
	return navigator.userAgent.indexOf('WebKit') >= 0;
};

isFirefox4Plus = function()
{
	for (var version = 4; version <= 10; version++) {
		if (navigator.userAgent.indexOf('Firefox/' + version) >= 0) {
			return true;
		}
	}
	return false;
};

// ----
// init

/**
 * Initializes CSF.
 * loggerLevel {Number} Logger level of CSF logger
 */
csf.init = function(loggerLevel)
{
	this.logger = new csf.Logger(loggerLevel);

};

/**
 * arguments {Array[String]} Array of script URLs to load
 */
csf.load = function()
{
	for (var i = 0; i < arguments.length; i++)
	{
		var scriptPath = arguments[i];
		if (!this.load.loaded[scriptPath])
		{
			// load if not loaded yet
			this.loadScriptWithPath(scriptPath);
			this.load.loaded[scriptPath] = true;
		}
	}
};

csf.load.loaded = {};

csf.loadScriptWithPath = function(scriptPath)
{
	if (isIE() || isWebKit() || isFirefox4Plus())
	{
		var xmlHttp = csf.XmlHttp.create();
		xmlHttp.requestSync('GET', scriptPath, null, this.executeContent);
	}
	else
	{
		var script = document.createElement('script');
		script.src = scriptPath;
		document.firstChild.firstChild.appendChild(script);
	}

//	csf.logger.debug('csf.loadScriptWithPath: ' + scriptPath);
};

csf.loadScriptWithSource = function(source)
{
	if (isIE())
	{
		eval(source);
	}
	else if (isFirefox4Plus())
	{
		var script = document.createElement('script');
		script.text = source;
		document.head.appendChild(script);
	}
	else
	{
		var script = document.createElement('script');
		script.text = source;
		document.firstChild.firstChild.appendChild(script);
	}
};

csf.executeScriptTag = function(scriptTag)
{
	if (scriptTag.type.indexOf('javascript') >= 0)
	{
		if (scriptTag.src != null && scriptTag.src.length > 0)
		{
			// included script
			var url = scriptTag.src;
			var xmlHttp = csf.XmlHttp.create();
			xmlHttp.requestSync('GET', url, null, this.executeContent);
		}
		else
		{
			// inline script
			var script = scriptTag.innerHTML;
			eval(script);
		}
	}
};

csf.executeContent = function()
{
	var script = this.request.responseText;
	eval(script);
};

