// ==UserScript==
// @name LJ Bind IP
// @namespace http://hacks.atrus.org/greasemonkey/
// @description Present the "bind cookie to IP address" option on all(?) login forms
// @include http://*.livejournal.com/*
// @include https://www.livejournal.com/login.bml
// ==/UserScript==

// XXX translate?
var expiredesc = "Remember me";
var binddesc = "Bind cookie to IP Address";

if(-1 == document.cookie.indexOf("ljsession=")) {

  // don't attempt LJ changes on user pages
  if((0 == document.location.pathname.indexOf("/~")) ||
     (0 == document.location.pathname.indexOf("/users")) ||
     (0 == document.location.pathname.indexOf("/community")) ||
     ("www.livejournal.com" != document.location.hostname)) {
    var tbody = document.getElementById("lj_more");
    tbody = document.evaluate("./td/table/tbody", tbody,
    null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
    
    var expire = document.createElement("input");
    expire.setAttribute("type", "checkbox");
    var bindip = document.createElement("input");
    bindip.setAttribute("type", "checkbox");
    bindip.checked = true;
    
    var toAdd = [
      {node: bindip, desc: binddesc},
      {node: expire, desc: expiredesc}];
    for(var i = 0; i < toAdd.length; i++) {
      tbody.appendChild(document.createElement("tr"));
      tbody.lastChild.appendChild(document.createElement("td"));
      tbody.lastChild.lastChild.appendChild(document.createElement("label"));
      tbody.lastChild.lastChild.lastChild.appendChild(toAdd[i].node);
      tbody.lastChild.lastChild.lastChild.appendChild(document.createElement("span"));
      tbody.lastChild.lastChild.lastChild.lastChild.style.setProperty("position", "absolute", "");
      tbody.lastChild.lastChild.lastChild.lastChild.appendChild(document.createTextNode(toAdd[i].desc));
    }
    
    var inlined = false;
    function inlineOpts(e) {
      if(inlined) return;
      inlined = true;
      if(document.getElementById("logincheck").checked) {
        var username = document.getElementById("username");
        if(expire.checked) {
          username.value += "!";
        }
        if(bindip.checked) {
          username.value += "<";
        }
      }
    }
    document.getElementsByName("submitpost")[0].addEventListener("mousedown", inlineOpts, true, false);
    // in case they make preview work
    document.getElementsByName("submitpreview")[0].addEventListener("mousedown", inlineOpts, true, false);
    // in case they fix get rid of the button nonsense (maybe they have cross-browser problems?)
    document.getElementById("postform").addEventListener("submit", inlineOpts, true, false);
    
  } else {
    if("/login.bml" == document.location.pathname) {
      var tbody = document.getElementById("login");
      for(var i = 0; i < tbody.childNodes.length; i++) {
        if("DIV" == tbody.childNodes[i].nodeName) {
          tbody = tbody.childNodes[i];
          break;
        }
      }

      tbody = tbody.firstChild.firstChild.firstChild.firstChild.childNodes[1].childNodes[1];
      var lastTr = null;
      for(var i = 0; i < tbody.childNodes.length; i++) {
        if("TR" == tbody.childNodes[i].nodeName) {
          lastTr = tbody.childNodes[i];
        }
      }

      var newTr = document.createElement("tr");
      tbody.insertBefore(newTr, lastTr);
      newTr.innerHTML = "<td></td><td><label><input type='checkbox' name='bindip' value='yes' checked='checked' style='margin-left: 0px; margin-bottom: 0px;'/>Bind to IP address</label></td>"; 
    } else {
      var lb = document.getElementById("LoginBox");
      if(lb) { // it might by dystopia or something
        var label = document.createElement("label");
        label.innerHTML = "<input type='checkbox' name='bindip' value='yes' checked='checked' style='height: 10px; width: 10px;' />" + binddesc.toLowerCase();
        lb.lastChild.lastChild.lastChild.lastChild.lastChild.appendChild(label);
      }
    }
  }
}