if (!("console" in window)) window.console = {};

$A(["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"]).each(function(m){
  if (!window.console[m]) { window.console[m] = function(){} }
});

var BaseClass = function(){
  var myself            = this;
  var initFunctions     = [];
  var preInitFunctions  = [];
  var postInitFunctions = [];
  var preInitStarted  = false;
  var initStarted     = false;
  var postInitStarted = false;
  
  Object.extend(
    this, {
      addInitFunc : function(f) {
        if (preInitStarted) f(); else initFunctions.push(f);
      },
      addPreInitFunc : function(f) {
        if (initStarted) f(); else preInitFunctions.push(f);
      },
      addPostInitFunc : function(f) {
        if (postInitStarted) f(); else postInitFunctions.push(f);
      },
      init : function(){
        var i;
        preInitStarted = true;
        for (i=0; i< preInitFunctions.length; i++) {
          preInitFunctions[i]();
        }
        initStarted = true;
        for (i=0; i< initFunctions.length; i++) {
          initFunctions[i]();
        }
        postInitStarted = true;
        for (i=0; i< postInitFunctions.length; i++) {
          postInitFunctions[i]();
        }
      },
      addOnloadFunction: function(element, callback){
        if (callback) {
          var firer = function(){ 
            if (callback._fired) return;
            callback._fired = true; 
            callback(); 
          }

          element.onload = firer;
          Event.observe(element, 'load', firer);
          Event.observe(element, 'readystatechange', function(){ 
            if ((this.readyState == 'loaded') || (this.readyState == 'complete')) firer() 
          });
        }
      },
      requireJS : function(srcURL, callback) {
        myself.addInitFunc(function(){
          // console.log('loading js '+srcURL);
          try {

            if (document.createElementNS) {
              var newScript = document.createElementNS("http://www.w3.org/1999/xhtml","html:script");
            } else {
              var newScript = document.createElement("script");
            }

            myself.addOnloadFunction(newScript, callback);
            
            newScript.setAttribute("type", "text/javascript");

            newScript.setAttribute("src", srcURL);
            document.getElementsByTagName("head")[0].appendChild(newScript);
          } catch (e) {
            // if inserting via DOM fails:  brute force approach 
            document.write('<script type="text/javascript" src="'+srcURL+'"></script>'); 
            if (callback) callback();
          } 
        });
      },
      requireCSS : function(srcURL, callback) {
        myself.addInitFunc(function(){
          // console.log('loading css '+srcURL);
          try {

            if (document.createElementNS) {
              var newSheet = document.createElementNS("http://www.w3.org/1999/xhtml","html:link");
            } else {
              var newSheet = document.createElement("link");
            }
            
            // ACHTUNG: funktioniert momentan NUR im IE und OPERA (verkehrte Welt?)
            myself.addOnloadFunction(newSheet, callback);

            newSheet.setAttribute("rel",  "stylesheet");
            newSheet.setAttribute("type", "text/css");

            newSheet.setAttribute("href", srcURL);
            document.getElementsByTagName("head")[0].appendChild(newSheet);
          } catch (e) {
            // if inserting via DOM fails:  brute force approach 
            document.write('<link type="text/javascript" src="'+srcURL+'"></script>'); 
            if (callback) callback();
          } 
        });
      }

    }
  );
  
  Event.observe(window, 'load', myself.init);

  document.write = function(what) {
    // console.log('no write '+what);
  }
}
Base = new BaseClass();

// extensions for other classes
Date.prototype.format = function(){
  var hours   = this.getUTCHours();
  var minutes = this.getUTCMinutes();
  var seconds = this.getUTCSeconds();
 
  if (hours   < 10) hours   = "0"+hours;
  if (minutes < 10) minutes = "0"+minutes;
  if (seconds < 10) seconds = "0"+seconds;
  return hours + ":" + minutes + ":" + seconds;
}

// Längenbeschränker
function check_length(source, target, maxChars) {
  source = $(source);
  var len = source.value.length;

  if (len > maxChars) {
    source.value = source.value.substr(0, maxChars);
    len = maxChars;
  }
  $(target).innerHTML = maxChars - len;
}

