/*************************************************************************************************\
 * core.js
 *	  Updated 07/2005
 *    Core facilities required by any other modules
 *																								
 * DynLite 3.1 library
 *   Lite,fast, and powerful DHTML cross-platform library.						
 *   Copyright(C) Trung Ngo, 2001-2005. All right reserved.
 *
 *	Variables:
 *	----------
 *		navName	:	'ie' or 'ns' or ...
 *		navVer	:	int
 *		isOldNS	:	Netscape prior 6.0
 *		isNS4	:	Netscape 4 ?
 *		isNS6	:	Netscape 6 ?
 *		isIE	:	Internet Explore?
 *		isIE4	:	Internet Explore 4?
 *		isIE5	:	Internet Explore 5?
 *		isDOM	:	support DOM?
 *		isFilter:	support filters ?
 *
 *  SUMMARY
 *	========
 *
 *	Functions:
 *	----------
 *
 *    function dynliteOnLoad()
 *    function registerDynliteLibOnloadHandler()
 *
 *    function registerDynliteLibOnloadHandler(module)
 *
 *    function registerLoadedModule(module)
 *    function requireModule(module)
 *
 *************************************************************************************************/

var isDebugEnabled = true;

var loadedModules = new Array();
var onLoadCallbacks = new Array();
var navName = (navigator.appName.indexOf("Netscape")>=0)? "ns" : navigator.appName.indexOf("Internet Explorer">=0)? "ie" : appName;
var navVer = parseInt(navigator.appVersion);	

//everything is ok except for netscape version prior ver 6
//assume that all others surport DOM
var isOldNS = (document.layers);			
var isNS4 = ((navName=="ns") && (navVer == 4));

//"Gecko" is used to allow Mozilla browsers as well ass the Netscape 6 release version
var isGecko = (navigator.userAgent.indexOf("Gecko") >0)? 1:0;		

// notes: safari is developped on top of gecko -> isGecko = true & isNS6 = true also
var isSafari = (navigator.userAgent.indexOf("Safari") >0)? 1:0;			


var isNS6 = (isGecko && (navVer==5));
var isIE = (navName=="ie") && (navVer>=4);
var isIE4 = isIE && (navVer == 4);
var isIE5 = (navigator.userAgent.indexOf("MSIE 5.5") > 0 | 
			navigator.userAgent.indexOf("MSIE 6.0") > 0)? 1:0; 

var isDOM = (document.getElementById);
var isFilter = (isIE);



var Dynlite = {
  Version:  '4.0',
  
  RootPath: 'js/',
  
  Dependencies : [
    ['ajax.js',         'dynlite.js'],
    ['calendar.js',     'dynlite.js', 'datetime.js', 'drag.js'],
    ['clip.js',         'dynlite.js'],
    ['common.js',       'dynlite.js'],
    ['control.js',      'dynlite.js', 'drag.js'],
    ['cookies.js',      'common.js'],
    ['dataentry.js'],
    ['datagrid.js'],
    ['datetime.js',     'dynlite.js'],
    ['drag.js',         'dynlite.js', 'effect.js'],
    ['debug.js',        'dynlite.js'],
    ['dynlite.js'],
    ['effect.js',       'dynlite.js'],
    ['event.js'],
    ['mics.js'],
    ['swap.js',         'dynlite.js'],
    ['tooltip.js', 'event.js', 'effect.js'],
    ['vietunihelper.js', 'event.js'],
    ['wddx.js']
  ],

  ImportedModules: new Array(),
  
  
  initialize: function() {
    var scriptTags = document.getElementsByTagName("script");
    // search for <script ... src=".../dynlite/*.js"
    // if found, update the RootPath variable
    for(var i=0; i < scriptTags.length; i++) {
      if(scriptTags[i].src && scriptTags[i].src.match(/core\.js(\?.*)?$/)) {
        this.RootPath = scriptTags[i].src.replace(/core\.js(\?.*)?$/,'');
        break;
      }
    }
    
    /*
    if (isDebugEnabled)
      this.importModule('debug.js'); */
  },
  
  
  /**
   * bruteforce to load a module
   */
  load: function(module) {
    var s = module.toLowerCase(); 
    document.write('<script type="text/javascript" src="' + Dynlite.RootPath + s + '"></script>');    
  },
  
  
  
  /**
   * import
   * example:
   *    Dynlite.importModule('common.js');
   */
  importModule: function(module) {
    var s = module.toLowerCase(); 
    var imports = this.ImportedModules;
    
    // check if the module is already imported
    for (var i = 0; i < imports.length; i++)
      if (imports[i] == s)  // the module has beed imported, do nothing
        return;
    
    imports.push(s);
    
    var dep = this.Dependencies;
    for (var i = 0; i < dep.length; i++) {
      var modules = dep[i];
      if (modules[0] == s) {
        // found the dependency setting for module
        for (var j = 1; j < modules.length; j++)
            this.importModule(modules[j]);

        this.load(s);
        return;
      }
    }
    
    alert('could not import module ' + module);
  }
};


Dynlite.initialize();




/*************************************************************************************************\
 *	private function dynliteLibOnLoad()
 *      this function must be called to set up the enviroment
\*************************************************************************************************/
function dynliteLibOnLoad() {
  var func;
  while (func = onLoadCallbacks.pop()) {
    // run all registered callback functions
    func();
  }
  
  //if (isDebugEnabled) {
    // alert('dynliteOnLoad() has been called succesfully!');
  // }
}


/*************************************************************************************************\
 *  public function registerDynliteLibOnloadHandler()
 *    this function should be called anywhere in the html body to
 *    register dynliteLibOnLoad() as the handler for body.onload event
\*************************************************************************************************/
function registerDynliteLibOnloadHandler() {
  if (isIE)
    window.attachEvent('onload', dynliteLibOnLoad);
    
  else if (isGecko)
    window.addEventListener('load', dynliteLibOnLoad, false);
    
  else
    alert("ERROR: cannot register dynliteOnLoad() handler!" +
          "Please use <body onload=''> option.");
}



/*************************************************************************************************\
 *	public function registerLoadedModule(module)
 *  
\*************************************************************************************************/
function registerLoadedModule(module) {
  var s = module.toLowerCase(); 

  /*
  if (moduleLoaded(s))
      // report error, the module is already loaded
      alert('ERROR: the module ' + module + ' has been loaded more than once.');
  */
  // push the current module to the structure
  loadedModules.push(s);
  
  // if (isDebugEnabled) {
  //  alert('module ' + module + ' has been loaded succesfully!');
  // }
}

function registerOnLoadCallBack(func) {
  onLoadCallbacks.push(func);
}



/*************************************************************************************************\
 *	private function moduleLoaded(module)
 *    determine if a module has been loaded
 *************************************************************************************************/
function moduleLoaded(module) {
  var s = module.toLowerCase(); 

  // detect if the module is already in loaded
  var i;
  for (i = 0; i < loadedModules.length; i++) {
    if (loadedModules[i] == s)
      return true;
  }
  
  return false;
}


/*************************************************************************************************\
 *	public function requireModule(module)
 *    this function is used to verify if a required module has been loaded
 *    if the required module has not been loaded, show error message
 *************************************************************************************************/
function requireModule(module) {
  var s = module.toLowerCase(); 

  if (!moduleLoaded(s)) {
    // alert('fail to load:' + s);
    // dont report the error to allow cross referencing
  }
}



/*************************************************************************************************/
// register itself
// register the dynlite lib

registerDynliteLibOnloadHandler();
registerLoadedModule("core.js");
