﻿function ScriptLoader()
{
    // The root element.
    var _rootElement = CreateRootElement('script','ascript');   
    //The script source.
    var _callback = null;
    // The script source.
    var _scriptSource = null;
    // True if the script is loaded.
    var _loaded = false;
    
    // Create the root element.
    function CreateRootElement(tag,id)
    {
        if ( tag == null )
        {
            return null;
        }
        if ( id == null )
        {
            return;
        }
        var instanceNum = 0;
        var element = document.getElementById(id);
        while ( element != null )
        {
            instanceNum++;
            element = document.getElementById(id + instanceNum.toString());
            if ( element == null )
            {
                id = id + instanceNum.toString();
                break;
            }
        }
        document.write('<' + tag + ' id="' + id + '"></' + tag + '>'); 
        return document.getElementById(id);
    }
    
    // Add to the load event
    function addLoadEvent(func) { 
      var oldonload = window.onload; 
      if (typeof oldonload != 'function') { 
        window.onload = func; 
      } else { 
        window.onload = function() { 
          oldonload(); 
          func(); 
        } 
      } 
    }
    
    // Load the script.
    function LoadScript()
    {
        var _scriptLoad = function()
        {
            if ( _loaded ) { return; }
            _loaded = true;
            if ( _callback != null )
            {
                try
                {
                    _callback();
                }
                catch(ex) {}
            }
        };
        _rootElement.type = "text/javascript";
        _rootElement.onload = _scriptLoad;
        _rootElement.onreadystatechange = _scriptLoad;
        _rootElement.src = _scriptSource;
    }
    // Read the query string.
    function ReadQueryString()
    {
        var _queryString = {};
        var _parentRoot = _rootElement.previousSibling;
        if ( !_parentRoot || !_parentRoot.src )
        {
            return _queryString;
        }
        var _index = _parentRoot.src.indexOf('?',1); 
        if ( _index < 1 )
        {
            return _queryString;
        }
        
        
        var _query = _parentRoot.src.substring(_index+1);
        var _items = _query.split('&');
        var _keyValue;
        
        for ( var i = 0; i < _items.length; i++ )
        {
            var _keyValue = _items[i].split('=');
            _queryString[_keyValue[0]] = _keyValue[1];
        }
        return _queryString;
        
    }

    ScriptLoader.prototype.getScriptElement = function()
    {
        return _rootElement;
    }
        
    ScriptLoader.prototype.Init = function(url,callback)
    {
             
        if ( !url )
        {     
            var queryString = ReadQueryString();
            if ( queryString['url'] )
            {
                _scriptSource=unescape(queryString['url']);
            }
        }else {_scriptSource=url;}             
        _callback = callback;
        addLoadEvent(LoadScript);       
    }
}