JSLoader = new Object();

JSLoader.scripts = new Array();
JSLoader.scripts.push();

JSLoader.callbacks = new Array();

JSLoader.loadScripts = function() {
  for (var i = 0; i < arguments.length; i++) {
    if (typeof(arguments[i]) == "string") {
      if (!this.isScriptLoaded(arguments[i])) {
        this.scripts.push([arguments[i], false]);
      }
    } else {
      this.callbacks.push(arguments[i]);
    }
  }
  this.loadUnloaded();
}

JSLoader.loadUnloaded = function() {

  var allLoaded = true;
  for (var i = 0; i < this.scripts.length; i++) {
    if (!this.scripts[i][1]) {
      this.loadScript(this.scripts[i][0], 0);
      allLoaded = false;
    }
  }
  if (allLoaded) {
    for (var i = 0; i < this.callbacks.length; i++) {
      this.callbacks[i].call();
    }
    this.callbacks = new Array();
  }  
}
  
JSLoader.loadScript = function(path, tries) {
  var http = getHTTPRequest();
  
  http.open("GET", path+"?bn="+window.buildNumber, true);
  
  http.onreadystatechange = function() {
    if(http.readyState == 4) {
      http.onreadystatechange = function () {};
      JSLoader.handleScriptLoad(http, path);
    }
  };
    
  try {
    http.send(null);
  } catch(e) {
    alert(e.message);
  }
}

JSLoader.handleScriptLoad = function(http, path) {
  try {
    if (http.status == "200") {
      data = http.responseText;
      globalEval(data);
      setTimeout("JSLoader.scriptLoaded('" + path + "');", 10);
//    } else if (http.tries < 2) {
//      http.tries++;
//      http.send(null);
    }
  } catch(e) {}  
}

JSLoader.scriptLoaded = function(path) {
  var stillLoading = false;
  for (var i = 0; i < this.scripts.length; i++) {
    if (this.scripts[i][0] == path) {
      this.scripts[i][1] = true;
    }
    if (!this.scripts[i][1]) {
      stillLoading = true;
    }
  }    
  if (!stillLoading) {
    for (var i = 0; i < this.callbacks.length; i++) {
      this.callbacks[i].call();
    }
    this.callbacks = new Array();
  }
}

JSLoader.isScriptLoaded = function(path) {
  for (var i = 0; i < this.scripts.length; i++) {
    if (this.scripts[i][0] == path) {
      return true;
    }
  }
  return false;
}

function globalEval(js) {
//  var scriptTag = document.getElementById('loadScript');
  var head = document.getElementsByTagName("head")[0];

//  if(scriptTag) head.removeChild(scriptTag);

  script = document.createElement('script');
  script.type = 'text/javascript';
//  script.id = 'loadScript';
  script.defer = true;

  //js = js.replace("\"<", "\"<\"+\"");
  
  if ((null == script.canHaveChildren || script.canHaveChildren) && SystemConfig.getType() != TYPE_SAFARI) {
    script.innerHTML = js;
    
  } else {
    script.text = js;
  } 

   head.appendChild(script);
//  js = js.replace(/\\/g, "\\\\");
//  js = js.replace(/'/g, "\\'");
//  js = js.replace(/\r/g, "\\r");
//  js = js.replace(/\n/g, "\\n");
  
//  setTimeout("eval('"+js+"');", 0);
}

var msxmlNames = [ "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ];
function getHTTPRequest() {
  /* Mozilla XMLHttpRequest */
  try {
    return new XMLHttpRequest();
  } catch(e) {}

  /* Microsoft MSXML ActiveX */
  for (var i=0;i < msxmlNames.length; i++) {
    try {
      return new ActiveXObject(msxmlNames[i]);
    } catch (e) {}
  }
}
