// only define this stuff once.

if(typeof Widget == 'undefined')
{

(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;


  // The base Class implementation (does nothing)
  this.Class = function(){};
  
  // Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype;
    
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
    
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" && 
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
            
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
            
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);        
            this._super = tmp;
            
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
    
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
    
    // Populate our constructed prototype object
    Class.prototype = prototype;
    
    // Enforce the constructor to be what we expect
    Class.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;
    
    return Class;
  };
})();

var Widget = Class.extend(
{
  autoStart : true,
  baseUrl   : '/widget/',
  widgetName: '',

  init    : function(target, config)
  {
    var tmp;
    eval('tmp = '+this.widgetName+'IniConfig;')
    this.absorbConfig(tmp);
    this.absorbConfig(config);
    this.target = typeof target == 'string'? document.getElementById(target) : target;

  },

  build   : function()
  {
    this.requestTemplate(function(content)
    {
      this.target.innerHTML = content;
    });
  },

  requestTemplate : function(/*required function*/responseHandler, /*optional string*/templateName)
  {
    var request = Widget.getAjaxRequest();

    request.open('GET', this.baseUrl + this.widgetName + '?action=getTemplate&templateName=' + (templateName? templateName : ''));

    var _this = this;
    request.onreadystatechange = function(e)
    {
      if(request.readyState == 4)
        if(request.status == 200)
          responseHandler.apply(_this, [request.responseText]);
        else
          alert("There was an error getting the template.");
    };
    request.send();
  },

  absorbConfig : function(newConfig)
  {
    for(key in newConfig)
      this[key] = newConfig[key];
  }


});

Widget.loadScriptToHead = function(url)
{
  if (document.body == null) 
  {
    try 
    {
      document.write("<script src='" + url + "' type='text/javascript'></script>");
    } 
    catch (e) 
    {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.language = "JavaScript";
      script.src = url;
      document.getElementsByTagName("head")[0].appendChild(script);
    }
  } 
  else 
  {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.language = "JavaScript";
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
  }
};

Widget.loadCssToHead = function(url)
{
  if (document.body == null) 
  {
    try 
    {
      document.write("<link href='" + url + "' type='text/css' media='screen' rel='stylesheet' />");
    } 
    catch (e) 
    {
      var script = document.createElement("link");
      script.type = "text/css";
      script.href = url;
      script.media = 'screen';
      document.getElementsByTagName("head")[0].appendChild(script);
    }
  } 
  else 
  {
    var script = document.createElement("link");
    script.type = "text/css";
    script.media = "screen";
    script.rel = 'stylesheet';
    script.href = url;
    document.getElementsByTagName("head")[0].appendChild(script);
  }
};

Widget.getAjaxRequest = function()
{
  var request = false;
  try {
    request = new XMLHttpRequest();
    } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");
  else
    return request
};


} // if Widget is not defined


