/* 
 * Gaia Ajax Widgets, an Ajax Widget Library for ASP.NET 2.0
 * Copyright (C) 2007  Frost Innovation AS
 * All rights reserved.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as 
 * published by the Free Software Foundation.
 * 
 * Or if you have purchased the "Professional Edition" from Frost Innovation
 * it is distributed to you in person under the Gaia Commercial License
 * which makes it possible for you to create closed source applications
 * and still be able to distribute those applications without creating 
 * restrictions for your source code.
 * 
 * Unless you're developing GPL software you should and probably legally
 * have to purchase a license of the Gaia Commercial License since otherwise
 * you might be forced to GPL license your derived works.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should find a copy of both the GPL version 2 and the 
 * Gaia Commercial License on disc where you have extracted these files.
 * If not visit http://www.gnu.org for the GPL version or 
 * http://ajaxwidgets.com for the Gaia Commercial License.
 * 
 * Breaking the terms of whatever license you're entitled to use will cause
 * prosecution by Frost Innovation AS. If possible we will demand to settle any 
 * law suits at a court in Norway.
 * 
 */



/*
        Helper for including JavaScript files DYNAMICALLY...!!
 */
 
var javaScriptFilesToWaitFor = 0;

function _checkIfAllFilesAreLoaded(checkFunction){
  var finished = false;
  try {
    var splits = checkFunction.split('.');
    var idx;
    for( idx = 0; idx < splits.length; ++idx ){
      var tmpChecker = '';
      for( var idx2 = 0; idx2 <= idx; ++idx2){
        if( idx2 != 0 )
          tmpChecker += '.';
        tmpChecker += splits[idx2];
      }
      if( eval('typeof window.' + tmpChecker) == 'undefined' )
        break;
    }
    if( idx == splits.length ) {
      finished = true;
    }
  } catch(err) {
    ; // Silently passing ince this is probably a "namespaced" type where the earlier parts of the "namespace" is undefined too...!!
  }
  if( finished ){
    // Decrement the number of files to wait for...
    javaScriptFilesToWaitFor -= 1;
  } else {
    // Wait 1/10 of second and try again...!!
    setTimeout(function(){
      _checkIfAllFilesAreLoaded(checkFunction);
    }, 100);
  }
}

$incJs = function(script, typeToWaitFor){
  var exists = false;
  var els = document.getElementsByTagName('script');
  for( var x = 0; x < els.length; ++x ){
    if( els[x].src.indexOf(script) != -1 ){
      exists = true;
      break;
    }
  }
  if( !exists ){
    javaScriptFilesToWaitFor += 1;
    var xJFile = document.createElement('script');
    xJFile.type='text/javascript';
    xJFile.src=script;
    document.getElementsByTagName('head')[0].appendChild(xJFile);

    // Waiting for script to load...
    _checkIfAllFilesAreLoaded(typeToWaitFor);
  }
}




