var VDALoader = {
  Version: '2.0.0',
  require: function(libraryName) {
    // inserting via DOM fails in Safari 2.0, so brute force approach
    document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
    //$$("head")[0].insert({ bottom: new Element('script', {"type":"text/javascript","src":libraryName}) });
  },
  requireCSS: function(cssName) {
	// inserting via DOM fails in Safari 2.0, so brute force approach
	document.write('<link rel="stylesheet" type="text/css" href="'+cssName+'" />');
	//$$("head")[0].insert({ bottom: new Element('link', {"rel":"stylesheet","type":"text/css","href":cssName}) });	
  }, 
  REQUIRED_PROTOTYPE: '1.6.0.3',
  REQUIRED_SCRIPTACULOUS: '1.8.1',
  modulePath: '../mods/',
  cssPath: '../css/',
  
  load: function() {
    function convertVersionString(versionString) {
      var v = versionString.replace(/_.*|\./g, '');
      v = parseInt(v + '0'.times(4-v.length));
      return versionString.indexOf('_') > -1 ? v-1 : v;
    }

    if((typeof Prototype=='undefined') || (typeof Element == 'undefined') || (typeof Element.Methods=='undefined') ||
        (convertVersionString(Prototype.Version) <  convertVersionString(VDALoader.REQUIRED_PROTOTYPE)))
       throw("VDA requires the Prototype JavaScript framework >= " + VDALoader.REQUIRED_PROTOTYPE);
    
    if(convertVersionString(Scriptaculous.Version) <  convertVersionString(VDALoader.REQUIRED_SCRIPTACULOUS))
    	throw("VDA requires at least script.aculo.us' version " + VDALoader.REQUIRED_SCRIPTACULOUS);
    
    if(typeof Effect=='undefined')
    	throw("VDA requires including script.aculo.us' effects.js library");
    
    //if(typeof Droppables=='undefined')
    //	throw("VDA requires including script.aculo.us' dragdrop.js library");

    var js = /vda_loader.js(\?.*)?$/;
    $$('head script[src]').findAll(function(s) {
      return s.src.match(js);
    }).each(function(s) {
      var path = s.src.replace(js, ''),
      includes = s.src.match(/\?mode=([a-z_]*)/);
      VDALoader.requireCSS(path+VDALoader.cssPath+'vda_style-'+VDALoader.Version+'.css');
      
      VDALoader.require(path+'vda_core-'+VDALoader.Version+'.js'); 
      VDALoader.require(path+"vda_commsmanager_"+(includes ? includes[1] : 'ajax')+'-'+VDALoader.Version+'.js');
      VDALoader.require(path+'vda_ui-'+VDALoader.Version+'.js'); 
      VDALoader.require(path+'vda_session-'+VDALoader.Version+'.js');
      VDALoader.require(path+'vda_sessionmanager-'+VDALoader.Version+'.js');
      VDALoader.require(path+'vda_uimanager-'+VDALoader.Version+'.js');
      VDALoader.require(path+'vda_commsmanager-'+VDALoader.Version+'.js');
      VDALoader.require(path+'vda_window.js');
      
      
      modules = s.src.match(/\?mods=([a-z_,]*)/);
      (modules ? modules[1] : '').split(',').each(
       function(module) { if(module!='') VDALoader.loadModule(module, path) });
      
      VDALoader.require(path+'vda_init-'+(includes ? includes[1] : 'ajax')+'.js');
    });    
  },
  
  loadModule: function(moduleName, path) {
	  switch (moduleName) { 
	   	case 'survey':	   	
	   		if(typeof Ajax.InPlaceEditor == 'undefined')
	   			throw("VDA Survey module requires including script.aculo.us' controls.js library");
	   		VDALoader.requireCSS(path + VDALoader.modulePath+moduleName+'/css/starbox.css');
	   		VDALoader.require(path + VDALoader.modulePath+moduleName+'/js/starbox.js');
	   		VDALoader.requireCSS(path + VDALoader.modulePath+moduleName+'/css/survey_style-1.0.0.css');
	   		VDALoader.require(path + VDALoader.modulePath+moduleName+'/js/vda_survey-1.0.0.js');
	      	break 
	   	case 'extview':
	   		VDALoader.requireCSS(path + VDALoader.modulePath+moduleName+'/css/extview_style-1.0.0.css');
	   		VDALoader.require(path + VDALoader.modulePath+moduleName+'/js/vda_extview-1.0.0.js');
	      	break
	   	case 'history_ui_back':
	   		VDALoader.requireCSS(path + VDALoader.modulePath+'history/css/history_ui_back_style-1.0.0.css');
	   		if(typeof initHistory != "function")
	   			VDALoader.require(path + VDALoader.modulePath+'history/js/vda_history-1.0.0.js');
	   		VDALoader.require(path + VDALoader.modulePath+'history/js/vda_history_ui_back-1.0.0.js');
	      	break
	   	case 'tts':
	   		if(typeof swfobject=='undefined')
	   	    	throw("VDA TTS module requires swfobject library");
	   		VDALoader.requireCSS(path + VDALoader.modulePath+moduleName+'/css/tts_style-1.0.0.css');
	   		VDALoader.require(path + VDALoader.modulePath+moduleName+'/js/vda_tts-1.0.0.js');
	      	break
	   	case 'swfemotion':
	   		if(typeof swfobject=='undefined')
	   	    	throw("VDA SWF-EMOTION module requires swfobject library");
	   		VDALoader.require(path + VDALoader.modulePath+moduleName+'/js/vda_swfemotion-1.0.0.js');
	      	break
	   	default: 
	   		throw("Requested module could not be found: " + moduleName);
	  } 
  }
};

VDALoader.load();
