/*!
    @brief Cake Specific Javascript helpers
    @author Jon Parrott
    @date 5/5/2010
    Provides a couple of useful javascript functions/varaibles for use with cake.
    The variables are usually set in the layout via the element cakejs
*/

var Cake = {
    
    //! @brief Base URL for the application, usually set automatically by the cakejs.ctp element.
    urlbase: "/",
    
    //! @brief produces a url similar to $html->url. Useful for from-root urls.
    url: function( path ){
        return Cake.urlbase + (path.replace(/^\//, ""));
    }

};

/*!
 Provides dynamic synchronous loading of js ( for ajax responses )
 from: http://stackoverflow.com/questions/593419/how-do-you-dynamically-load-a-javascript-file-from-different-domain
*/
var Script = {
  _loadedScripts: [], // Array to store loaded scripts.
  load: function(script){
    // include script only once
    if (this._loadedScripts.indexOf(script) != -1){
      return false;
    }
    // request file synchronous
    var code = new Ajax.Request(script, {
      asynchronous: false, method: "GET",
      evalJS: false, evalJSON: false
    }).transport.responseText;
    // eval code on global level
    if (Prototype.Browser.IE) {
      window.execScript(code);
    } else if (Prototype.Browser.WebKit){
      $$("head").first().insert(Object.extend(
        new Element("script", {type: "text/javascript"}), {text: code}
      ));
    } else {
      window.eval(code);
    }
    // remember included script
    this._loadedScripts.push(script);
    
    return true;
  }
};

