if (!(typeof YUI === 'undefined' || !YUI)) {
  YUI.log("Warning: Quorus will override existing YUI!");
}

(function() {
  //Wrapper-safe way to initialize the QUORUS variable
  if (typeof QUORUS === 'undefined' || !QUORUS) {
    QUORUS = { bootTime: new Date() };
  }
  var Q = QUORUS;

  // Pre-bootstrap functions

  (function () {
  var defaultConfig = {"siteKey":"brooksrunning","publicUrlBase":"http://www.quorus.com","version":"dcdb141","persistBar":false},
      localConfig,
      sessionConfig,
      parse,
      tmp,
      localStorage;

  if(window.localStorage) {
    localStorage = window.localStorage
  } else if(window.globalStorage) {
    //FF 3.0 hack, lacks localStorage, get it from a cookie instead
	 localStorage = {q_config: readCookie('q_config')};
  }
  // Determine which version to load, and change what we load ourselves
  QUORUS.advancedBrowser = !! (window.postMessage && localStorage && window.sessionStorage);

  parse = (function () {
    if (window.JSON)
      return JSON.parse;
    else {
      return function (str) {
        return eval('(' + str + ')');
      }
    }
  })();

  function readCookie (name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i=0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1, c.length);
      if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length));
    }
    return null;
  }
  localConfig   = (QUORUS.advancedBrowser ? (tmp = localStorage.q_config) && parse(tmp) : 
    (tmp = readCookie('q_config')) && parse(tmp)) || {};
  sessionConfig = (QUORUS.advancedBrowser ? (tmp = sessionStorage.q_config) && parse(tmp) :
    (tmp = readCookie('q_config_sess')) && parse(tmp)) || {};

  function getConfig (key) {
    if (key in sessionConfig) return sessionConfig[key];
    if (key in localConfig)   return localConfig[key];
    if (key in defaultConfig) return defaultConfig[key];
  }
  QUORUS.getConfig      = getConfig;
  QUORUS._defaultConfig = defaultConfig;
  QUORUS._sessionConfig = sessionConfig;
  QUORUS._localConfig   = localConfig;
})();


  Q.disable = Q.disable || function() {
    setBodyClass('Disabled');
    Q.use = function () {};
  };

  function setBodyClass (c) {
    if (document.body && document.body.className)
      document.body.className = [document.body.className.replace(/(\s*)quorus[a-z]+/gi, '$1'), 'quorus' + c].join(' ');
  };

  // Bootstrap process
  (function () {
    var callbacks = Q._callbacks || [],
        head = document.getElementsByTagName('head')[0],
        timeout,
        tmp;
    //For when stage2 loads
    Q._callbacks = callbacks;

    function dropScript (url) {
      var script = document.createElement('script');
      // This only works in Firefox
      script.onerror = Q.disable;
      script.src = url;
      script.defer = 'defer';
      setTimeout(function () {
        head.appendChild(script);
      }, 0);
    }

    function dropStyle (url) {
      var styleElem = document.createElement('link');
      styleElem.id = "quorus-styles";
      styleElem.setAttribute('type', 'text/css');
      styleElem.setAttribute('rel', 'stylesheet');
      //TODO: Version this like everything else
      styleElem.setAttribute('href', url);
      head.appendChild(styleElem);
    }

    // Overridden by stage2
    Q.use = function (fn) {
      var a = Array.prototype.slice.call(arguments, 0);
      callbacks.push(a);
    };

    var rootUrl = "http://www.quorus.com/bootstrap/";
    if (tmp = Q.getConfig('envOverride')) {
      tmp = rootUrl + 'override/' + tmp + '/' + Q.getConfig('siteKey') + '.js';
      //This script will callback with the apppropriate URLs
      dropScript(tmp);
      Q._dropScript = dropScript;
      Q._dropStyle  = dropStyle;
    } else {
      tmp = rootUrl + Q.getConfig('version') + '/' + Q.getConfig('siteKey');
      var scriptUrl = tmp + '/stage2.js';
      var styleUrl  = tmp + '/css/embedded.css';

      dropStyle(styleUrl);
      dropScript(scriptUrl);      
    }
  })();
})();
