/*
  CodeBaby JavaScript Library
  Copyright (c) 2009 CodeBaby Corp.
  All rights reserved.
  This source code can only be distributed in accordance with the terms of the license agreement entered into
  with CodeBaby.

  Version: 1.2
  $Rev: 5621 $
  $Date: 2009-03-23 17:02:20 -0600 (Mon, 23 Mar 2009) $
  $URL: http://svn.codebaby.com/svn/Development/Hosted/Branches/Verison1.2/Hostsite/cbEnableV1.2.js $
  
  
*/	 

// Create the cbEnabler object which manages the loading of all scripts and files
// needed to run a CodeBaby production
var cbEnabler = new Object();

// Get the <script> tag that loaded this file, and extract the src path of
// the script
cbEnabler.scripts = document.getElementsByTagName('script');
cbEnabler.currentScriptTag = cbEnabler.scripts[cbEnabler.scripts.length - 1];
cbEnabler.src = cbEnabler.currentScriptTag.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){
    // first, unencode the string
    var decodedSrc = decodeURI(cbEnabler.src);
    
    // 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 &  -> &?
    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 /
  
  @return name of the host site
*/
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];
    
};

/**
  Gets site name from the script tag, or default to the value 
  extracted from current URL
  
  @return name of site
*/
cbEnabler.getSite = function() {
    // first, try to get the value from the query strings
    var siteName = cbEnabler.getParam("site");
    if(siteName == null){
        // query string for site is not given, extract the site name from the URL
        siteName = location.host;
    }
    
    return siteName;     
}

/**
  Gets the project name from the script tag. There is no default for this value,
  if it is not passed by query string, then the function must be overridden in
  siteDefaults.js.
  
  @return name of the project
*/
cbEnabler.getProject = function() {
    // first, try to get the value from the query strings
    var projName = cbEnabler.getParam("project");
    if(projName == null){
        // query string for project is not given
    }
    
    return projName;     
}

/**
  Gets the release name from the script tag. There is no default for this value,
  if it is not passed by query string, then the function must be overridden in
  siteDefaults.js.
  
  @return name of the release
*/
cbEnabler.getRelease = function() {
    // first, try to get the value from the query strings
    var relName = cbEnabler.getParam("release");
    if(relName == null){
        // query string for site is not given
    }
    
    return relName;          
}

/**
  Gets the scene group name from the script tag. There is no default for this value,
  if it is not passed by query string, then the function must be overridden in
  siteDefaults.js.
  
  @return name of the scene group
*/
cbEnabler.getSceneGroup = function() {
    // first, try to get the value from the query strings
    var sceneGroupName = cbEnabler.getParam("scenegroup");
    if(sceneGroupName == null){
        // query string for scene group is not given
    }
    
    return sceneGroupName;           
}

/**
  Gets the CBLib version from the script tag, or default to 
  the current version
  
  @return the cblib version used
*/
cbEnabler.getCBLibVersion = function() {
    // first, try to get the value from the query strings
    var cbLibName = cbEnabler.getParam("cblib");
    if(cbLibName == null){
        // query string for site is not given, default to current version
        cbLibName = "1.3";
    }
    if(cbEnabler.getSite() =="www.ppcc.edu"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.liquiddreamspools.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.logitechsqueezebox.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.codebabylive.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.netnanny.com"){
		cbLibName = "1.3.2"; //conflict with IE5Support .js and the YUI libraries; 
	}
	if(cbEnabler.getSite() =="www.netnannyDummy.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.scottishhomeimprovements.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.scottishstainedglass.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.redNolandDemo.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.springsgov.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="WesternUnion"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.phillong.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.philanthrocorp.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="TeknoBubbles"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="DirectEnergy"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.stephencoonts.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="CCCA"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="www.providentfinancial.com"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="MotorNeeke"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="ProFlightLog"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="ChampionWindows"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="DrS"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="WeBelong"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="WHOW"){
		cbLibName = "1.3.1";
	}
	if(cbEnabler.getSite() =="ColoradoGearUp"){
		cbLibName = "1.3.1";
	}
    return cbLibName;   
}

/**
  Gets whether the project is enabled from script tag, or defaults to true
  
  @return true if this project is enabled, false otherwise
*/
cbEnabler.getProjectEnabled = function() {
    // first, try to get the value from the query strings
    var isProjectEnabled = cbEnabler.getParam("projectEnabled");
    if(isProjectEnabled == null){
        isProjectEnabled = true;
    }
    
    return isProjectEnabled;               
}

/**
  Gets whether metrics are enabled from script tag, or defaults to true
  
  @return true if metrics for this project are enabled, false otherwise
*/
cbEnabler.getMetricsEnabled = function() {
    // first, try to get the value from the query strings
    var isMetricsEnabled = cbEnabler.getParam("metrics");
    if(isMetricsEnabled == null){
        isMetricsEnabled = true;
    }
    
    return isMetricsEnabled;         
}

/**
  Gets whether split testing is enabled from the script tag, or defaults 
  to false
  
  @return true if split testing for this project is enabled, false otherwise
*/
cbEnabler.getSplitTestingEnabled = function() {
    // first, try to get the value from the query strings
    var isSplitTestingEnabled = cbEnabler.getParam("splittesting");
    if(isSplitTestingEnabled == null){
        isSplitTestingEnabled = false;
    }
    
    return isSplitTestingEnabled;    
}

/**
  Gets whether javascript debug is enabled from the script tag, or defaults 
  to false
  
  @return true if javascript debugging is enabled, false otherwise
*/
cbEnabler.getJSDebugEnabled = function() {
    // first, try to get the value from the query strings
    var isJSDebugEnabled = cbEnabler.getParam("jsdebug");
    if(isJSDebugEnabled == null){
        isJSDebugEnabled = false;
    }
    
    return isJSDebugEnabled;   
}

/**
  Gets whether actionscript debug is enabled from the script tag, or defaults 
  to false
  
  @return true if actionscript debugging is enabled, false otherwise
*/
cbEnabler.getASDebugEnabled = function() {
    // first, try to get the value from the query strings
    var isASDebugEnabled = cbEnabler.getParam("asdebug");
    if(isASDebugEnabled == null){
        isASDebugEnabled = false;
    }
    
    return isASDebugEnabled;  
}

/**
  Gets if split lib is enabled from script tag, or defaulted to false
  
  @return the parent element for the CodeBaby content
*/
cbEnabler.getSplitLibEnabled = function() {
    // first, try to get the value from the query strings
    var isSplitLibEnabled = cbEnabler.getParam("splitlib");
    if(isSplitLibEnabled == null){
        isSplitLibEnabled = false;
    }
    
    return isSplitLibEnabled;  
}

/**
  Gets uri to the shared CodeBaby Library
  
  @return location of the CBlib files
*/
cbEnabler.getCBLibURI = function() {
    return this.getHostSite() + 
           "shared/cblib/" + 
           this.getCBLibVersion() + "/"
}

/**
  Gets uri to the root project
  
  @return location of the project root
*/
cbEnabler.getProjectURI = function() {
    return this.getHostSite() + "sites/" + 
           this.params.site + "/" + 
           this.getProject() + "/" + 
           this.getRelease() + "/";
}

/**
  Gets uri to the root project javascript
  
  @return location of the project javascript root
*/
cbEnabler.getProjectJSURI = function() {
    return this.getProjectURI() + "cb/javascript/";
}

/**
  Gets the parent element from the script tag, or defaults to the
  <body> element
  
  @return the parent element for the CodeBaby content
*/
cbEnabler.getParentElement = function() {
    // first, try to get the value from the query strings
    var parentId = cbEnabler.getParam("parentid");
    var parentEl = null;
    if(parentId != null){
        // parentId query string is given, get the element it refers to
        parentEl = document.getElementById(parentId);
        
    } else {
        // query string for site is not given, default to the <body> tag
        parentEl = document.body;
    }
    
    return parentEl;              
}

/**
  Gets the X value used to create the codebaby Div from script tag
  or defaults to 0
  
  @return leftmost x-value of the CodeBaby div
*/
cbEnabler.getCodeBabyDivX = function() {
    return 0;
}

/**
  Gets the Y value used to create the codebaby Div from script tag
  or defaults to 0
  
  @return topmost y-value of the CodeBaby div
*/
cbEnabler.getCodeBabyDivY = function() {
    return 0;
}

/**
  Gets the Position used to create the codebaby Div from script tag
  or defaults to absolute
  
  @return positioning of the CodeBaby div
*/
cbEnabler.getCodeBabyDivPosition = function() {
    return "absolute";
}

/**
  Creates the Div that holds the codebaby object under the HTML element
  retrieved by cbEnabler.getParentElement
*/
cbEnabler.createCodeBabyDiv = function() {
    var divString = '<div id="CodeBabyDraggable" style="position:';
    divString += this.getCodeBabyDivPosition();
    divString += '; top:';
    divString += this.getCodeBabyDivY();
    divString += '; left:';
    divString += this.getCodeBabyDivX();
    divString += '; z-index:1000"></div>';
    
    var containingEl = this.getParentElement();
    containingEl.innerHTML += divString;
}

/**
  Dynamically loads a Javascript file into the HTML document
*/
cbEnabler.loadScript = function(uri){
    var scriptTag = "<script language='JavaScript1.2' type='text/javascript' src='" + uri + "'></script>";
    document.write(scriptTag);
}

/**
  Dynamically loads a CSS file into the HTML document
*/
cbEnabler.loadCSS = function(css){
    var cssTag = "<link type='text/css' href='" + css + "' rel='stylesheet'></link>";
    document.write(cssTag);
}

/**
  Finalizes the settings for the CodeBaby. Uses default or overridden functions 
  to get all the necessary variables. 
  Creates the CodeBaby div in which the codebaby is displayed.
*/
cbEnabler.finalizeSettings = function(){
 
    // Load each of the variables; These functions can be overridden
    // in siteDefaults.js
    this.params.project = this.getProject();
    this.params.release = this.getRelease();
    this.params.sceneGroup = this.getSceneGroup();
    this.params.projectEnabled = this.getProjectEnabled();
    this.params.metricsEnabled = this.getMetricsEnabled();
    this.params.splitTestingEnabled = this.getSplitTestingEnabled();
    this.params.jsDebugEnabled = this.getJSDebugEnabled();
    this.params.asDebugEnabled = this.getASDebugEnabled();
    this.params.splitLib = this.getSplitLibEnabled(); 
    this.params.parentElement = this.getParentElement();
    this.params.CBLibVersion = this.getCBLibVersion();
    this.params.CBLibURI = this.getCBLibURI();
    this.params.projectURI = this.getProjectURI();
    this.params.projectJSURI = this.getProjectJSURI();
    
    
    // Wirte CodeBaby div to the HTML
    this.createCodeBabyDiv();
};

// List of all .js files needed, if not using the amalgamated cblib.js, used
// in cbEnabler.addScripts()
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/InitCodeBaby.js"
];

// Name of the javascript file that contains all necessary .js files, used
// in cbEnabler.addScripts()
cbEnabler.singleLib = "cb/javascript/core/cblib.js"

/**
    Set up the site, project, sceneGroup, environment and jslib based
    on params and defaults (if params value null, then use defaults value)
*/
cbEnabler.addScripts = function(){
    
    // Setup all necessary variables
    this.finalizeSettings(); 
    
    // If splitlib is not set, use the amalgamated cblis.js file, otherwise
    // use each individual .js file (useful for debugging)
    if(this.params.splitLib != "true") { 
      this.loadScript(this.params.CBLibURI + this.singleLib);
    } else {  
      for (var i = 0; i < this.jslibUrisPreLoad.length; i++){
        this.loadScript(this.params.CBLibURI + this.jslibUrisPreLoad[i]);
      }
    }
    
    // Load the scenegroup and project js files
    this.loadScript(this.params.projectJSURI + "scenegroups/" + this.params.sceneGroup + ".js");
    this.loadScript(this.params.projectJSURI + this.params.project + ".js");

}; 


// Create the params object and get the Site param (the only one we should be
// retrieving before loading siteDefaults.js)
cbEnabler.params = new Object();
cbEnabler.params.site = cbEnabler.getSite(); 

// Load the site defaults
cbEnabler.loadScript(cbEnabler.getHostSite() + 
                     "sites/" +
                     cbEnabler.params.site + "/" +
                     "siteDefaults.js");
