/**
 * QCM javascript library
 * loader based on script.aculo.us (http://script.aculo.us) js loader
 *
 * @namespace QCM library
 * @author Petr Grochál
 */
var QCM = {
  version: '1.0',
  /**
   * @methodOf QCM
   * @param libraryName string; required library url
   */
  require: function(libraryName) {
    try{
      // inserting via DOM fails in Safari 2.0, so brute force approach
      document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
    } catch(e) {
      // for xhtml+xml served content, fall back to DOM methods
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = libraryName;
      document.getElementsByTagName('head')[0].appendChild(script);
    }
  },
  /**
   * @methodOf QCM
   * @throws QCM library requires Prototype and script.aculo.us javascript frameworks if there is not Prototype or Scriptaculous defined
   */
  load: function() {
    if ((typeof Prototype == 'undefined') || (typeof Scriptaculous == 'undefined')) {
      throw('QCM library requires Prototype and script.aculo.us javascript frameworks');
    }

    var js = /qcm\.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(/\?.*load=([a-z,]*)/);
      (includes ? includes[1] : 'qcmclient,qcmtools').split(',').each(function(include) {
        QCM.require(path+include+'.js');
      });
    });
  }
};

QCM.load();
