


var RELAC = {
  domain: "re.la",
  id: "alternet",
  kErrorRegisteredNeeded: 1125,
  kCommentClickDelay: 1700,  // 1.7s delay between clicks on the comment form for them to register
  kMaxCommentSize: 1500,   // Maximum comment length, kept low enough to not hit GET limits in IE
  user_val: null,
  relay_loaded: null, // set by re.la.main.js
  ready: false, // set by re.la.main.js
  autoload: false,
  relaySession: null
};


    RELAC.user_val = {};
    RELAC.user_val.id = 'None';
    RELAC.user_val.username = '';
    RELAC.user_val.email = '';
    RELAC.user_val.state = 'NotUser';
    RELAC.user_val.loggedIn = false;



/*var relayBootstrap =*/ (function () {
  var head, script, i;

  head = document.getElementsByTagName("head")[0];

  // see http://docs.jquery.com/Events/ready#fn
  // We also want to load static css before kicking things off...
  var internal_css = "http://re.la/static/CACHE/css/60db098a4a52.css";

  script = document.createElement("link");
  script.rel = "stylesheet";
  script.href = internal_css;
  script.type = "text/css";
  script.media = "screen, projection";
  head.appendChild(script);

  head = document.getElementsByTagName("head")[0];

  var external_scripts = [
    "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"
  ];

  for (i=0; i<external_scripts.length; ++i) {
    script = document.createElement("script");
    script.src = external_scripts[i];
    head.appendChild(script);
  }

  var internal_js = "http://re.la/static/CACHE/js/3cf26b3e0454.js";
  
  script = document.createElement("script");
  script.src = internal_js;
  head.appendChild(script);
  
  return true;

})();

// This should be wired to 'email' icon of the host page.
var relayThis = function (node, url) {
    // on large host page (e.g. alternet) need to test
    // if this file (defining RELAC) is even loaded yet;
    // relay_loaded is set once re.la.main is init and active.
    
    // If a relay instance already exists, then destroy it. If not, create one.
    Relay.ui.embeddedShareElement && Relay.$(Relay.ui.embeddedShareElement).length > 0 ? createRelay = false : createRelay = true;
    
    var destroyThenCreate = false;

    if (node !== undefined) {
      if (createRelay) {
        // Count the number of relayThis links and relayShareDivs
        // Use the relayShareDiv that has the same index as the selected node
        var links = Relay.$("a[onclick*='relayThis']");
        var relayShareDivs = Relay.$(Relay.lib.getLoaderArgs().share);
        for (var j=0; j<links.length; j++ ) {
          if (links[j] === node) {
            var relayShareDiv = Relay.$(relayShareDivs[j]);
            !relayShareDiv.attr("id") ? relayShareDiv.attr("id","relayShareDiv") : null;
          }
        }
        
        //// Crawl the DOM looking for nearest share div and add id=relayShareDiv
        // parents = $(node).parents();
        //for (var i=0; i<parents.length; i++) {
        //  var shareNode = $(parents[i]).find(Relay.lib.getLoaderArgs().share);
        //  if (shareNode.length === 1) {
        //    console.debug(parents[i])
        //    !shareNode.attr("id") ? shareNode.attr("id","relayShareDiv") : null;
        //    break;
        //  }
        //  else if (shareNode.length > 1) {
        //    // Argh, found more than one
        //    
        //    
        //  }
        //}
      }
      else {
        // Check if this parent node is the same as the parent node of the
        // open relay instance. If they are different, create a relay instance
        // for the new node after we've destroyed this one.
        destroyThenCreate = Relay.ui.parentNode !== node;
      }
      Relay.ui.parentNode = node;
    }
    else {
      createRelay ? Relay.$(Relay.lib.getLoaderArgs().share+":first").attr("id","relayShareDiv") : null;
    }

    createRelay ? Relay.createRelay(url) : null;
    
    if (RELAC && RELAC.relay_loaded === true &&
        Relay.lib.requestQueue && Relay.lib.requestQueue.length === 0) {
        Relay.ui.toggleEmbedded();
    }
    
    if (!createRelay) {
      Relay.$(Relay.ui.embeddedShareElement).empty().attr("id", null);
      RELAC.relay_loaded = false;
      destroyThenCreate ? relayThis(node,url) : null;
    }
};
