/*
opendot_includes.js.php

Include the scripts required for opendots

Copyright (c) Gareth Hadfield 2008
*/
/*
includes.js.php

Copyright (c) Gareth Hadfield 2008
*/

if(document.DONE_INCLUDES == undefined){

  if(HTTP_FMD == undefined){
    var HTTP_FMD = "http://gtdesktop.com/opendots/";
  }

  function global_eval(aScript){
    if(aScript != undefined){
      if(window.execScript != undefined){
        window.execScript(aScript);
      }
      else{
        if(IS_SAFARI){
          var aScriptTag = document.createElement("script");
          aScriptTag.type = "text/javascript";
          aScriptTag.innerHTML = "eval(unescape('"+escape(aScript)+"'));";
          document.getElementsByTagName('head')[0].appendChild(aScriptTag);
        }
        else{
          eval.call(window, aScript);
        }

      }
    }
  }
  
  var GLOBAL_INCLUDES = new Array();

  function global_include(aScriptFile){
  	if(!in_array(aScriptFile, GLOBAL_INCLUDES)){
  		GLOBAL_INCLUDES[GLOBAL_INCLUDES.length]=aScriptFile;
	    var aScriptText = load_text(aScriptFile, undefined, false, (OPENDOTS_MODE==OPENDOTS_DISPLAY_MODE)/*todo test*/, false);
  	  global_eval(aScriptText);
  	}
  }

  function add_http(aString){
    // prepend http://host/path to aString if not already http:
    if(aString==undefined){
      aString = "";
    }
    var aPathName = HTTP_FMD;
    if((aString.toLowerCase().substr(0, 5)) != "http:"){
      if(aString.substr(0, 1)=="/"){
        aString = aString.substr(1);
      }
      aString = aPathName + aString;
    }
    return(aString);
  }

  function remove_http(aString){
    // remove the http:// if present
    var aToken = "http://";
    if((aString.toLowerCase().substr(0, aToken.length)) == aToken){
      aString = aString.substr(aToken.length);
    }
    return(aString);
  }

  function file_included(aFile, aFiles, aCompareFunction){
    // return true if the given file is present in the dom
    var result = false;

    if(aFiles != undefined){
      for(var i=0; i<aFiles.length; i++){
        result = aCompareFunction(aFile, aFiles[i]);
        if(result){
          break;
        }
      }
    }

    return(result);
  }

  if(document.head == undefined){
    document.head = document.getElementsByTagName("head")[0];
  }

  function script_included(aScriptFile){
    // return true if the given script is present in the dom
    var aScriptCompare = function(aFile1, aFile2){
      aFile1 = add_http(aFile1).toLowerCase();
      aFile2 = add_http(aFile2.src).toLowerCase();
      return(aFile1 == aFile2);
    };
    var aScripts = document.head.getElementsByTagName("script");
    return(file_included(aScriptFile, aScripts, aScriptCompare));
  }

  function dom_js_include(aScriptFile){
    document.use_dom_includes = true; // the first use of this function signals to always use in the future when calling js_include
    // add the given script to the dom
    var aHead = document.getElementsByTagName("head")[0];
    if(aHead != undefined){
      var aScript = document.createElement("script");

      aScript.language = "javascript";
      aScript.type = "text/javascript";
      aScript.src = aScriptFile;
      aHead.appendChild(aScript);
    }
    return(false);
  }

  //document.aScriptArray = new Array;

  function js_include(aScriptFile){
    aScriptFile = HTTP_FMD + aScriptFile;
    // add the given script to the dom if it is not included already

    if(!script_included(aScriptFile)){

      if(document.use_dom_includes===true){
        dom_js_include(aScriptFile);
      }
      else{
      }
    }
  }

  function link_included(aLinkFile){
    // return true if the given link is present in the dom
    var aLinkCompare = function(aFile1, aFile2){
      aFile1 = add_http(aFile1).toLowerCase();
      aFile2 = add_http(aFile2.href).toLowerCase();
      return(aFile1 == aFile2);
    };
    var aLinks = document.getElementsByTagName("link");
    return(file_included(aLinkFile, aLinks, aLinkCompare));
  }

  function css_included(aCSSFile){
    // return true if the given css link is present in the dom
    return(link_included(aCSSFile));
  }

  function dom_css_include(aCSSFile){
    // add the given CSS file to the dom
    var aHead = document.getElementsByTagName("head")[0];
    if(aHead != undefined){
      var aCSS = document.createElement("link");
      aCSS.rel = "stylesheet";
      aCSS.type = "text/css";
      aCSS.href = aCSSFile;
      aHead.appendChild(aCSS);
    }
  }

  function css_include(aCSSFile){
    aCSSFile = HTTP_FMD + aCSSFile;
    // add the given css to the dom if it is not included already
    if(!css_included(aCSSFile)){
      dom_css_include(aCSSFile);
    }
  }

  document.DONE_INCLUDES = true;
}
/*
END OF includes.js.php
*/
