/*
 * Copyright 2008 Jeffery Palm.
 */

var TESTING = false;
var oldSpan;

function main() {

  // Sanity check
  if (!(""+document.location).match(/.*reddit.com.*/)) {
    alert("This isn't a reddit page.  Returning....");
    return;
  }

  // Maybe we've already done this
  if (document.getElementById("_menu")) {
    alert("You've already run this.  Returning...");
    return;
  }

  // Find all the links
  var ids = [];
  var as = document.getElementsByTagName("a");
  for (var i=0; i<as.length; i++) {
    var a = as[i];
    if (a.id && a.id.match(/title_*/)) {
      var id = a.id.replace(/title_/,"");
      ids.push(id);
    }
  }

  // A link is title_<id>, a score is score_<id>
  // Find the links and scores
  var entries = [];
  for (var i=0; i<ids.length; i++) {
    var id = ids[i];
    var a = document.getElementById("title_" + id);
    if (!a) continue;
    var score = document.getElementById("score_" + id);
    if (!score) continue;
    var src = score.innerHTML;
    src = src.replace(/^\s+/,"");
    src = src.replace(/\s+$/,"");
    src = src.replace(/\s*points/,"");
    var e = {
      href: a.href,
      innerHTML: a.innerHTML,
      score: parseInt(src)
    };
    entries.push(e);
  }

  // Save the width and height, b/c these could disappear once
  // we remove all the body nodes
  var width = document.body.clientWidth;
  var height = document.body.clientHeight;

  // By nice to reddit and put the ad here, too
  var ad = document.getElementById("ad-frame");

  // Remove all the nodes
  while (document.body.childNodes.length > 0) {
    document.body.removeChild(document.body.firstChild);
  }

  // Sort the entries by score
  entries = entries.sort(function(a,b) {return b.score - a.score;});

  // Create the menu at the top left
  var menu = $n("div",document.body);
  var target = $n("iframe",document.body);
  var styleLeft = 10;
  var styleTop = 10;
  var styleWidth = 300;
  menu.id = "_menu";
  menu.style.position = "absolute";
  menu.style.left = styleLeft + "px";
  menu.style.top = styleTop + "px";
  menu.style.maxWidth = styleWidth + "px";

  // The first link to click, so we can automatically call it
  var firstTarget = 0;
  for (var i=0; i<entries.length; i++) {
    var e = entries[i];
    var s = $n("span",menu);
    s.style.fontSize = "8pt";
    var a = $n("a",s);
    a.innerHTML = e.innerHTML;
    if (a.innerHTML.length > 40) {
      a.innerHTML = a.innerHTML.substring(0,40) + "...";
    }
    
    // Make sure the links don't get 'visited' colors
    a.href = "#_" + Math.floor(Math.random() * 1000);
    var f = newTarget(s,target,e.href);
    if (!firstTarget) firstTarget = f;
    a.onclick = f;
    var txt = $t(" (" + e.score + ")",s);
    $n("br",menu);
  }

  // Try to insert the ad, if we don't have one, put a little message
  if (ad) {
    var space = $n("div",menu);
    space.style.paddingTop = "20px";
    menu.appendChild(ad);
  } else {
    ad = $n("menu");
    ad.style.paddingTop = "10px";
    ad.innerHTML = "No ad... I tried";
  }

  // Create the iframe in which to open the articles
  target.id = "_target";
  try {target.border = "0";} catch (e) {} // could be undefined
  try {target.frameborder = "0";} catch (e) {} // could be undefined
  target.style.border = "1px solid #aeaeae";
  target.style.position = "absolute";
  target.style.left = (styleLeft + styleWidth) + "px";
  target.style.top = "10px";
  var targetWidth = width - styleWidth - 30;
  var targetHeight = height - 20;
  target.width = targetWidth;
  target.style.width = targetWidth;
  target.height = targetHeight;
  target.style.height = targetHeight;
  
  // Activate the first link
  firstTarget(null);
  
}

function newTarget(_span,_target,_href) {
  var span = _span;
  var href = _href;
  var target = _target;
  return function(e) {
    target.src = href;
    title = href;
    if (oldSpan) {
      oldSpan.style.fontWeight = "normal";
    }
    span.style.fontWeight = "bold";
    oldSpan = span;
  };
}

function $n(tag,on) {
  var n = document.createElement(tag);
  if (on) on.appendChild(n);
  return n;
}

function $t(txt,on) {
  var n = document.createTextNode(txt);
  if (on) on.appendChild(n);
  return n;
}

try {main();} catch (e) {if (TESTING) alert(e);}

