// Get the <script> tag that loaded this file.
//alert("start cbEnable.js");
var cbEnabler = new Object();
cbEnabler.scripts = document.getElementsByTagName('script');
//alert("cbEnabler.scripts:" + cbEnabler.scripts);
cbEnabler.currentScriptTag = cbEnabler.scripts[cbEnabler.scripts.length - 1];
cbEnabler.splitLib = false;
cbEnabler.src = cbEnabler.currentScriptTag.src;
//alert("cbEnabler.src:" + cbEnabler.src + ":");


/**
  Get the param value in the src query string, with the given name.
  src must be set up first.
  @param name the name of the query param
  @return the value of the query param, null if not there.
*/
cbEnabler.getParam = function(name){
// use a regular expression
// need to match:
// any number of chars -> .*
// then a ? -> \?
// optionally followed by any number of chars and then & -> (?:.*&)?
// then the passed in name -> name var
// then = -> \=
// value is the following chars not & -> ([^&]*)
// terminated optionally by an &  -> &?
//first, unencode the string
    var decodedSrc = decodeURI(cbEnabler.src);
    var regex = new RegExp(".*\\?(?:.*&)?" + name + "\\=([^&]*)&?" );
    var result = regex.exec(decodedSrc);
    if (result == null){
        return null;
    }             
    return result[1];
};

/**
  Get the host site e.g.,  "protocol://host.name.com/"
  that is, everything up to the first single /
*/
cbEnabler.getHostSite = function(){
    var decodedSrc = decodeURI(cbEnabler.src);
    var regex = new RegExp("(.*\\/)[^\\/]");
    var result = regex.exec(decodedSrc);
    if (result == null){
        return null;
    }             
    return result[1];
    
};
//get the params
cbEnabler.params = new Object();
cbEnabler.params.site = cbEnabler.getParam("site"); 
cbEnabler.params.project = cbEnabler.getParam("project");
cbEnabler.params.environment = cbEnabler.getParam("environment");
cbEnabler.params.jslib = cbEnabler.getParam("cblib");
cbEnabler.params.sceneGroup = cbEnabler.getParam("scenegroup");
cbEnabler.params.splitLib = cbEnabler.getParam("splitlib");

// split the site at the dots, reverse the pieces and use that as the path
// the the customers content
//cbEnabler.sitePieces = cbEnabler.site.split(".");
//alert("cbEnabler.sitePieces.length" + cbEnabler.sitePieces.length);
//cbEnabler.sitePath = "sites/"
//for (var i = cbEnabler.sitePieces.length - 1; i >= 0; i--){
//    cbEnabler.sitePath += cbEnabler.sitePieces[i] + "/";
//} 


//alert ("cbEnabler.site:" + cbEnabler.site );
//alert ("cbEnabler.sitePath:" + cbEnabler.sitePath);
//alert ("cbEnabler.project:" + cbEnabler.project );
//alert ("cbEnabler.params.environment:" + cbEnabler.params.environment );
//alert ("cbEnabler.jslib:" + cbEnabler.jslib );
//alert ("cbEnabler.sceneGroup:" + cbEnabler.sceneGroup );
//alert("end cbEnable.js")

cbEnabler.load = function(uri){
    var scriptTag = "<scr" + "ipt language='JavaScript1.2' type='text/javascript' src='" 
    + uri + "'";
    scriptTag += "></scr" + "ipt>";
//    alert(scriptTag);
    document.write(scriptTag);
}

/**
Set up the site, project, sceneGroup, environment and jslib based
on params and defaults (if params value null, then use defaults value)
*/
cbEnabler.finalizeSettings = function(){
    //site, project and sceneGroup cannot have defaults, so 
    // they are set directly from the params.
    this.site = this.params.site;
    this.project = this.params.project;  
    this.sceneGroup = this.params.sceneGroup;
//    alert("projectDefaults:before ifs")
    //environment and cbEnable can have defaults, so use if params are null
    if (this.params.environment == null){
        this.environment = this.defaults.environment; 
    } else {
        this.environment = this.params.environment; 
    }
    
    if (this.params.jslib == null){
        this.jslib = this.defaults.jslib; 
    } else {
        this.jslib = this.params.jslib; 
    }
    
     if (this.params.splitLib != null){
        this.splitLib = this.params.splitLib; 
    }
//    alert("projectDefaults:after ifs")
};
// now load everything up

cbEnabler.jslibUrisPreLoad = [
   "cb/javascript/core/IE5Support.js"
  ,"cb/javascript/core/gateway/Exception.js"
  ,"cb/javascript/core/gateway/FlashTag.js"
  ,"cb/javascript/core/gateway/FlashSerializer.js"
  ,"cb/javascript/core/gateway/FlashProxy.js"
  ,"cb/javascript/core/CodeBaby.js"
  ,"cb/javascript/core/CodeBabyDrag.js"
  ,"cb/javascript/core/CBCookie.js"
  ,"cb/javascript/core/InitCodeBaby.js"
];

cbEnabler.singleLib = "cb/javascript/core/cblib.js"

cbEnabler.adScripts = function(){
//    alert("cbEnabler.adScripts start");
    
    this.finalizeSettings();    
    this.jslibPath = this.hostSite + "shared/cblib/" 
    + this.jslib + "/";
    
    if(this.splitLib != "true") { 
    
      this.load( this.jslibPath + this.singleLib );
    
    } else {  
      
      for (var i = 0; i < this.jslibUrisPreLoad.length; i++){
        this.load(this.jslibPath + this.jslibUrisPreLoad[i]);
      }
      
    }
    
    this.hostSitePath =this.hostSite + "sites/" + this.site + "/" + this.project 
    + "/" + this.environment + "/";
    // now the site uris
    this.siteUriPath = this.hostSite + "sites/" + this.site + "/" + this.project 
    + "/" + this.environment + "/cb/javascript/";
    this.load(this.siteUriPath + "scenegroups/" + this.sceneGroup + ".js");
    this.load(this.siteUriPath + this.project + ".js");

}; 
//alert("cbEnabler.js end");


//load the project defaults for environment and jslib
// use window.location.protocol to match http or https protocol of calling site.
//cbJsLibScript.hostSite = window.location.protocol + "//content.codebaby.com.s3.amazonaws.com/";
cbEnabler.hostSite = cbEnabler.getHostSite();
cbEnabler.sitePath = cbEnabler.hostSite + "sites/";
cbEnabler.load(cbEnabler.sitePath + cbEnabler.params.site 
  + "/" + cbEnabler.params.project + "/projectDefaults.js");

