if (Object.isUndefined(NVB)) { var NVB = { } }

NVB.files = Class.create({
	initialize: function(options) {
    this.scripts_to_load = options.scripts || new Array();
		this.onload_events = options.onload || new Array();
		this.css_to_load = options.css || new Array();
		this.google = options.google_analytics || false;
    this.path = options.path || '/www/js/';
		this.css_path = options.css_path || '/www/css/';

		this.loaded = 0;
		this.head = $$('head')[0];
		this.console = false;
    this.loadType = document.addEventListener ? true : false;
    this.events = {
			script_loaded: this.script_loaded.bind(this),
			readyStateChange: this.readyStateChange.bind(this),
			google_loaded: this.google_loaded.bind(this)
		}
    if ( !this.loadType ) document.observe("script:loaded", this.events.script_loaded);
		if(this.css_to_load.length > 0)this.addCss();
		if(this.google){
      document.observe("script:google_loaded", this.events.google_loaded);
      this.addGoogle();
    }
		this.loadScript();
	},
	loadScript: function(){
      var src = this.scripts_to_load.shift();
      var jsEl = new Element('script', { type: 'text/javascript', src: this.path+src});
      var id = jsEl.identify();
      if ( this.loadType ) {
        jsEl.onload = this.events.script_loaded
      }else{
        jsEl.observe("readystatechange", this.events.readyStateChange);
      }
      this.head.insert(jsEl);
	},
	script_loaded: function(event) {
		this.loaded++;
		if(!this.scripts_to_load.length){
			document.stopObserving("script:loaded", this.events.script_loaded)
			delete this.addScripts;
			delete this.addCss;
			delete this.scripts_to_load;
			delete this.css_to_load;
			delete this.loaded;
      
      if(Object.isArray(this.onload_events)){
  			this.onload_events.each(
  				function(s){
  					if(Object.isFunction(s))s();
  					else eval(s);
  				}
  			);
  		}else if(Object.isFunction(this.onload_events)){
  			this.onload_events();
  		}
		}else{
      this.loadScript();
    }
	},
  readyStateChange:function(evt){
    if (/loaded|complete/.test(evt.element().readyState)) {
      document.fire("script:loaded");
    }
  },
	addCss: function(){
		this.css_to_load.each( function(s){
			var cssEl = new Element('link', { type: 'text/css', rel: 'stylesheet', href: this.css_path+s, media:'screen' });
			this.head.appendChild(cssEl);
		}.bind(this));
	},
  addGoogle: function(){
      var src = "http://www.google-analytics.com/ga.js";
      var jsEl = new Element('script', { type: 'text/javascript', src: src});
      var id = jsEl.identify();
      if ( this.loadType ) {
        jsEl.onload = this.events.google_loaded
      }else{
        jsEl.observe("readystatechange", function(evt){
          if (/loaded|complete/.test(evt.element().readyState)) {
            document.fire("script:google_loaded");
          }
        });
      }
      this.head.insert(jsEl);
	},
  google_loaded: function(event){
    var pageTracker = _gat._getTracker(this.google);
    pageTracker._initData();
    pageTracker._trackPageview();
		document.stopObserving("script:google_loaded", this.events.google_loaded)
  }
});