/**
 * Load specified js modules and run load handlers
 *
 */
function BxScriptLoader()
{
	$this = this;	
	this.loadScript = null;
	this.loadAfterXml = null;
	this.scriptsCounter = 0;
	this.scriptsCount = 0;
	this.scripts = null;
	this.afterScriptsArrayLoading = null;
}




/*
*	execute script.onload method
*/
BxScriptLoader.prototype._afterLoading = function()
{
	if(this.loadScript && typeof(this.loadScript.onload) == 'function')
		this.loadScript.onload();
		
	if(this.scriptsCount == 0)
		this.loadScript = null;
}


/*
*	indicates that script is loaded successfully
*	tries to execute script.onload function
*/
BxScriptLoader.prototype.scriptLoaded = function()
{
	//---opera only*******if((/opera/i).test(navigator.userAgent) )
	if(/MSIE/.test(navigator.userAgent))	//all ie group
		this._afterLoading();
}


/**
 * Set script that will be loaded
 * 
 * @param	string	script			 full url of the script
 */
BxScriptLoader.prototype.setScript = function(script)
{
	this.loadScript = script;
}













/**
 * Set script for running after loading xml data from server
 *
 * @param	string	script
 */
BxScriptLoader.prototype.setAfterXml = function(script)
{
	this.loadAfterXml = script;
}


/**
 * Run script after loading xml data from server
 *
 * @param	string	script
 */
BxScriptLoader.prototype.runAfterXml = function(script)
{
	if(typeof(this.loadAfterXml) == 'function')
		this.loadAfterXml();
		
	this.loadAfterXml = null;
}








/**
 * Common method for js scripts loading
 *
 * @param	string	href
 * @param	bool	isArray
 * @param	string	path
 */
BxScriptLoader.prototype.runScript = function(href, isArray, path)
{
	this.loadScript = document.createElement('script');
	
	if(isArray == true)
	{
		this.loadScript.onload = function()
		{
			$this.scriptsCounter++;
			if($this.scriptsCounter < $this.scriptsCount)
			{
				$this.runScript('', true, path);
			}
			else
			{
				for(var i = 0; i < $this.scriptsCount; i++)
				{
					var s = "g" + $this.scripts[i] + " = new " + $this.scripts[i] + "();";
					try
					{
						eval(s);
					}
					catch(e)
					{
						
					}
				}
				
				this.scripts = null;
				this.scriptsCount = 0;
				this.scriptsCounter = 0;
				
				if($this.afterScriptsArrayLoading != null)
					$this.afterScriptsArrayLoading();
			}
		}
		
		href = path + this.scripts[this.scriptsCounter] + '.js';
	}
	
	document.body.appendChild(this.loadScript);
	this.loadScript.type = "text/javascript";
	
	if(this.loadScript.setAttribute)
		this.loadScript.setAttribute('src', href);
	else
		this.loadScript.src = href;
}


/**
 * Runs script array
 *
 * @param	array		arrayScripts
 * @param	function	loading handler
 * @param	string		path
 */
BxScriptLoader.prototype.runScriptArray = function(arrayScripts, h, path)
{
	this.scriptsCounter = 0;
	this.scriptsCount = arrayScripts.length;
	this.scripts = arrayScripts;
	
	if(typeof(h) == "function")
		this.afterScriptsArrayLoading = h;
	else
		this.afterScriptsArrayLoading = null;
	
	this.runScript('', true, path);
}


/*
*	create the BxLoader
*/
var gScriptLoader = null;
gScriptLoader = new BxScriptLoader();