


 /*
  * Toucan Import System
  *
  * This script will allow javascript moudles to be loaded
  * on-demand. It will automatically try and load a js with the
  * same name as the current running file with an js extension.
  *
  * NOTE: This script must be called import.js, renaming this will cause the
  *       script to fail to load modules.
  */
  var toucan = new function() {

      this._loadedModules = new Array();
      this._loadedScripts = new Array();

      /* Imports a javascript module */
      this.jsImport = function(src) {
        if(!this._loadedModules.inArray(src)) {
          this._loadedModules[this._loadedModules.length] = src;
        }
      }

      /* Loads all the sript in the _loadedModules array */
      this.performLoad = function() {

        var i, j, combinedUrl = '';

        if(this._loadedModules.length > 0) {

          /* Combines all the script into one HTTP request. Uses combine.php */
          for(i = 0, j = this._loadedModules.length; i < j; i++) {
            combinedUrl += this._loadedModules[i] + '.js,';
          }
          combinedUrl = combinedUrl.substr(0, combinedUrl.length-1);
          this.load(this.getPath() + combinedUrl);

          this._loadedModules = new Array();
        }
      }

      /* Loads a javascript module */
      this.load = function(src, callback) {
        if(!this._loadedScripts.inArray(src)) {
          this.domLoad(src, callback);
          this._loadedScripts[this._loadedScripts.length] = src;
        }
        else {

          if(typeof callback == 'function') {
            callback();
          }
        }
      }

      /* Loads the javascript using DOM */
      this.domLoad = function(src, callback) {
        var scriptObj       = document.createElement("SCRIPT");
        scriptObj.type      = "text/JavaScript";
        scriptObj.src       = src;

        if(typeof callback == 'function') {
          scriptObj.onload = callback;
        }

        document.getElementsByTagName("head")[0].appendChild(scriptObj);
     }

      /* Loads javascript using XHTTP */
      this.ajaxLoad = function(src) {
        var xmlHttp;
        if(window.XMLHttpRequest) {

          try {
            xmlHttp = new XMLHttpRequest();
          }
          catch(e) {
            xmlHttp = false;
          }
        }
        else if(window.ActiveXObject) {

          // IE 6 (Non native support)
          try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
          }
          catch(e) {
            try {
              xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {
              xmlHttp = false;
            }
          }
        }

        if(xmlHttp) {
          xmlHttp.open("GET", src, false);
          xmlHttp.send(null);

          if(xmlHttp.status == 200) {
            var scriptObj       = document.createElement("SCRIPT");
            scriptObj.type      = "text/JavaScript";
            scriptObj.innerHTML = xmlHttp.responseText;
            document.getElementsByTagName("head")[0].appendChild(scriptObj);
          }
          else {
            alert("Failed to load module:\n'" + src + "'");
          }
        }
        else {
          alert("Load failed, XMLHTTP not supported by your browser.");
        }
      }

      /* Returns the path of this script. */
      this.getPath = function() {
        var scripts = document.getElementsByTagName("SCRIPT");
        var path    = "";

        for(var i = 0, j = scripts.length; i < j; i++) {
          var pos  = scripts[i].src.indexOf("import.js");
          if(scripts[i] && pos > -1) {
            path = scripts[i].src.substr(0, pos);
            return path
          }
        }

        return "./";
      }

      /* Try and load the default script */
      var path     = this.getPath();
      var win      = window.location.href;
      var fileName = win.substring(win.lastIndexOf('/')+1, win.lastIndexOf('.'));
      if(fileName.lastIndexOf('/') == fileName.length-1) {
        fileName = "index";
      }
      var defUrl = path + '../' + fileName + '.js';
      this.domLoad(defUrl);
  }

  Array.prototype.inArray = function(value, searchKeys)
  // Returns true if the passed value is found in the
  // array.  Returns false if it is not.
  {
    if(searchKeys){

        // Search keys
        for(stuff in this) {
        // Matches identical (===), not just similar (==).
        if (stuff === value) {
         return true;
        }
       }
    }
    else{
        for(stuff in this) {
        // Matches identical (===), not just similar (==).
        if (this[stuff] === value) {
         return true;
        }
       }
    }
   return false;
  }

