/*-- $Workfile: script.js $ --------------------------------------------
This javascript module implements a wrapper of the SCRIPT HTMLElement.
Version    : $Header: /Leuzinger/filmarchiv-leuzinger/scripts/dom/script.js 5     19.08.06 9:42 Hartwig $
             $NoKeywords: $
Application: ECMAScript DOM Utilities
Platform   : XHTML1, ECMA2, DOM1, IE6, FF1
Description: This ECMAScript module implements a wrapper for the SCRIPT HTMLElement.
             It makes sure an attribute readyState is available and set correctly.
             It must be loaded before any other module using its Import function.
						 N.B.: "import" is a Javascript keyword reserved for future use.
						 Do not confuse it with "Import"!
------------------------------------------------------------------------
Copyright  : Enter AG, Zurich, 2006
----------------------------------------------------------------------*/

/*----------------------------------------------------------------------
bDebug (public, global)
  debug variable can be turned on locally in order to make display()
  visible.
----------------------------------------------------------------------*/
var bDebug = false;

/*----------------------------------------------------------------------
display (public, global)
  conditional alert for trivial debugging
----------------------------------------------------------------------*/
function display(s)
{
  if (bDebug)
    alert(s);
} /* display */

display("loading /scripts/dom/script.js");

/*----------------------------------------------------------------------
Abstract Script class has an empty constructor
----------------------------------------------------------------------*/
function Script()
{
} /* Script */

/*----------------------------------------------------------------------
global constants
----------------------------------------------------------------------*/
Script.sSCRIPTS_ROOT = "/scripts/";
Script.sSCRIPT_SRC = "dom/script.js";
	
/*----------------------------------------------------------------------
findScript searches for script elements with the given source suffix
(private, global)
----------------------------------------------------------------------*/
Script.findScript = function(sSrcSuffix)
{
  scriptFound = null;
  var ascript = self.document.getElementsByTagName("script");
	for (var i = 0; (scriptFound == null) && (i < ascript.length); i++)
	{
	  var scriptElement = ascript[i];
		var sSrcValue = scriptElement.getAttribute("src");
		if (sSrcValue)
		{
			if (sSrcValue.substr(sSrcValue.length - sSrcSuffix.length,sSrcSuffix.length) == sSrcSuffix)
				scriptFound = scriptElement;
		}
	}
	return scriptFound;
} /* findScript */

/*----------------------------------------------------------------------
getScriptSrc converts a script path relative to script root "scripts"
into a relative URL to be used as src.
----------------------------------------------------------------------*/
Script.getScriptSrc = function(sScriptPath)
{
  display("getScriptSrc("+sScriptPath+")");
  var sScriptSrc = sScriptPath;
  /* first search for suffix "/scripts/dom/script.js" */
	var scriptScript = Script.findScript(Script.sSCRIPTS_ROOT+Script.sSCRIPT_SRC);
	var sSrc = scriptScript.getAttribute("src");
	/* determine root from it */
	var sScriptRoot = sSrc.substr(0,sSrc.length - Script.sSCRIPT_SRC.length);
	/* append sScriptPath */
	sScriptSrc = sScriptRoot + sScriptPath;
	/* return script URL */
  display("return: "+sScriptSrc);
	return sScriptSrc;
} /* getScriptSrc */

/*----------------------------------------------------------------------
signalLoad is called at the end of each script
----------------------------------------------------------------------*/
Script.signalLoad = function(sScriptPath)
{
	display("Script.signalLoad");
  /* script target (browser selection) */
	var scriptElement = Script.findScript(sScriptPath);
	display("loaded: "+scriptElement.getAttribute("src"));
  /* readyState property */
	if (scriptElement.readyState == "uninitialized")
	  scriptElement.readyState = "loaded";
} /* signalLoad */

/*----------------------------------------------------------------------
isReady checks readyState and isReady of imported scripts (private, global)
----------------------------------------------------------------------*/
Script.isReady = function()
{
	display("isReady");
  var bReady = true;
	display("readyState: "+this.readyState);
	if ((this.readyState == "loaded") || (this.readyState == "interactive") || (this.readyState == "complete"))
	{
	  var i;
		for (i = 0; bReady && (i < this.ascriptImported.length); i++)
		{
		  if (!this.ascriptImported[i].isReady())
			  bReady = false;
		}
	}
	else
	  bReady = false;
	return bReady;
} /* isReady */

/*----------------------------------------------------------------------
Import (public, global) 
  "static class" method for creating a new script, inserting it
  in the current window's document and loading it from the source.
----------------------------------------------------------------------*/
Script.Import = function(sScriptPath,scriptParent)
{
  display("Script.Import("+sScriptPath+","+scriptParent+")");
	/* prevent importing same script twice */
	var scriptElement = Script.findScript(Script.sSCRIPTS_ROOT+sScriptPath);
	if (scriptElement == null)
	{
	  /* create the script element */
    scriptElement = self.document.createElement("script");
		/* type is always "text/javascript" */
		scriptElement.setAttribute("type","text/javascript");
		/* charset is always iso8859-1 for our scripts */
		scriptElement.charset="ISO-8859-1";
		/* define property readyState, unless it is there already */
		if (scriptElement.readyState == null)
		  scriptElement.readyState = "uninitialized";
		else
		  display("readyState is defined!");
  	display("readyState: "+scriptElement.readyState);
    /* imported scripts (empty array) */
		scriptElement.ascriptImported = new Array();
		/* define "isReady()" method */
		scriptElement.isReady = Script.isReady;
		/* source file name */
    scriptElement.setAttribute("src",Script.getScriptSrc(sScriptPath));
		/* insert new script element into head of document to start loading it */
		var head = self.document.getElementsByTagName("head")[0];
		head.appendChild(scriptElement);
		display("script inserted");
	}
	else
    display(sScriptPath+" is already imported!");
	/* add new script element to parent's collection of imported scripts */
	if (scriptParent)
	  scriptParent.ascriptImported[scriptParent.ascriptImported.length] = scriptElement;
	display("return: "+scriptElement);
	return scriptElement;
} /* Script.Import */

/*----------------------------------------------------------------------
LoadExternal (public, global) 
  "static class" method for loading anchors marked with rel="external"
	as if they were marked target="_blank"
	(which is not part of XHTML 1.1 (Strict))
----------------------------------------------------------------------*/
Script.LoadExternal = function()
{
 if (!self.document.getElementsByTagName) return;
 var anchors = self.document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
} /* Script.LoadExternal */

self.onload = Script.LoadExternal;

/*--End of file-------------------------------------------------------*/

