/*
  Author: Moises Beltran
*/
/*
  Cpo Commerce Inc. Base Class
  Provides interface to allow inclusion of other files. Should contain general code
*/
var Cpo = Base.extend({
  constructor: function(sitename) {
    //hmm.. not much goes here
  },
  base_path: false,
  version: "0.001",
  copyright: "&#169; Cpo Commerce 2008",
  sitename: "cporotarytools",//this holds the sitename
  set_basepath: function(){
    var scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
      var src = scripts[i].getAttribute("src");
      if (!src) continue;
      var m = src.match(/cpoBase\.js/i);
      if (m) {
        this.base_path = src.substring(0, m.index);
 
        break;
      }
    }
  },
  plugin: function(file){ 
    if(!this.base_path) this.set_basepath();
    return this.base_path+'/plugins/'+file+'.js';
  },
  poe: false,
  //OH! Prototype. Exclude all methods added by Array.Proto
  PrototypeObjectExtensions: function(){
    poe = ''
    for (var i in Array.prototype) poe += i+'|';
    this.poe = new RegExp('('+poe+'null|_)')
  },
  //TODO: If prototype library is included, we need to do some hackery
  //      In order to get this to work properly. Prototype includes tons
  //      Of other methods for Array.prototype
  parse_opts: function(orig_opts){
    if(this.poe == false) this.PrototypeObjectExtensions();
    opts = '';
    for (var i in orig_opts) {
      if(i.match(this.poe) == null) opts += i+'="'+orig_opts[i]+'"';
    }
    return opts;
  },
  load_script: function(url){
    opts = new Array(); 
    url = url.replace(/(\/\/)+/g,"/");
    url = url.replace(":/","://");
    opts['language'] = 'javascript';
    opts['type'] = 'text/javascript';
    opts['src'] = url;
//     alert(url);
    document.write('<script '+this.parse_opts(opts)+'></script>');
    return true;
  },
  require: function(fn,name) {
    try { eval(fn);} catch(e) {cpo.include(name);}
  },
  include: function(includes){
    incs = includes.split(",");
    for(var i=0;i<incs.length;i++){
      this.load_script(this.plugin(incs[i])); //assume relative to plugins
    }
  }

});
cpo = new Cpo();
//will check for jquery. if not will load from plugins
// cpo.require('jQuery','jquery');
// include main modules // ?

//cpo.require('ShipOnDay','ship_on_day');
//cpo.require('CookieJar','cookie_jar')
//cpo.require('ShopCart','shop_cart');

//init all other plugins
// Each file should include its own plugins to avoid clutter
// use following to check for needed plugins: 
// cpo.require('UtcTime','utc_time')
// cpo.include('utc_time');
// cpo.include('ship_on_day');

