
function reverse(s) {
 crypt = "";
 for(i = s.length-1; i >= 0; i--) {
  crypt += s[i]; 
 }
 return crypt;
}

function email(anchor) {
 document.write("<a href=\"mailto:"+reverse("gro.rebutrebu@yddubyddups")+"\">"+anchor+"</a>")
}

Date.daysOfWeek = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
Date.monthsOfYear = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

Date.friendly = function(secsSinceEpoch) {
  var now = (new Date()).getTime() / 1000;
  var diff = now - secsSinceEpoch;
  if (diff < 0) {
    return "???"
  } else if (diff < 60) {
    return plural(Math.floor(diff)," second") + " ago ";
  } else if (diff < 3600) {
    return plural(Math.floor(diff / 60)," minute") + " ago ";
  } else if (diff < 86400) {
    return plural(Math.floor(diff / 3600)," hour") + " ago ";
  } else if (diff < (7*86400)) {
    return plural(Math.floor(diff / 86400)," day") + " ago ";
  } else if (diff < (14*86400)) {
    var weeks = Math.floor(diff / (7*86400));
    diff = diff - (weeks*7*86400);
    var days = Math.floor(diff / 86400);
    var str = plural(weeks," week");
    if (days > 0) {
        str = str + " and " + plural(days, " day");
    }
    return str + " ago ";
  } else {
    var date = new Date(secsSinceEpoch * 1000);
    now = new Date();
    if (date.getFullYear() == now.getFullYear()) {
      // within current year
      return Date.daysOfWeek[date.getDay()] + ", " + Date.monthsOfYear[date.getMonth()] + " " + date.getDate() + " ";
    } else {
      return Date.daysOfWeek[date.getDay()] + ", " + Date.monthsOfYear[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear() + " ";
    }
  }
}

function plural(value, str) {
  if (value > 1) {
    return value + str + "s"
  } else {
    return value + str
  }
}

// Post data type - holds information about each post on the site.
function Post(id, author, timestamp, title, url, tags) {
  this.id = id;
  this.author = author;
  this.timestamp = timestamp;
  this.title = title;
  this.url = url;
  this.tags = tags;
}

// Post methods

Post.prototype.write_tags = function(root, suffix) {
 if (this.tags && this.tags.length > 0) {
   for (var i = 0; i < this.tags.length; i++) {
     document.write("<a href=\"" + root + this.tags[i] + suffix +"\" rel=\"tag\">");
     document.write(this.tags[i]);
     document.write("</a> ");
   }
 }
}

/* Adapted from Highlight Current Link, Kevin Cannon, www.multiblah.com */
// TODO: write a general writeByLine() function.
// Use compatible DOM navigation API.
function writeFriendlyDates() {
  for (var i = 0; i < document.links.length; i++) {
    var link = document.links[i];
    if (link.className && link.className == "permalink") {
      // Remove leading 'I' placed so that the id validates as XHTML.
      alert(link.id.substr(1));
      var post = p.posts_byid[link.id.substr(1)];
      if (post) {
        link.innerHTML = Date.friendly(post.timestamp);
      }
    }
  }
}


// Post database - holds information about all the posts on the site.
function PostDB() {
  // list of tags
  this.tags_list = new Array();
  // chronological list of posts (newest first)
  this.posts_list = new Array();
  // maps blogger id -> Post
  this.posts_byid = new Object();
  // maps tag -> list of Posts (newest first)
  this.posts_bytag = new Object();
}

// create a new Post object and add it to the post database.
// we use the list to create a doubly linked list of Posts.
// we also add the post to two indices: posts_byid and posts_bytag.
PostDB.prototype.ap = function(id, author, timestamp, title, url, tags) {
  new_post = new Post(id, author, timestamp, title, url, tags);
  if (this.posts_list.length > 0) {
    next_post = this.posts_list[this.posts_list.length-1];
    next_post.prev = new_post;
    new_post.next = next_post;
  }
  this.posts_list.push(new_post);
  // add to index objects.
  this.posts_byid[id] = new_post;
  if (new_post.tags) {
    for (var i = 0; i < new_post.tags.length; i++) {
      tag = new_post.tags[i];
      if (!this.posts_bytag[tag]) {
        this.posts_bytag[tag] = new Array();
        this.tags_list.push(tag);
      } 
      this.posts_bytag[tag].push(new_post);
    }
  }
}

PostDB.prototype.write_tags = function(prefix, suffix, recent) {
 if (recent > this.posts_list.length) {
   recent = this.posts_list.length;
 }
 var recent_ts = this.posts_list[recent-1].timestamp;
 this.tags_list.sort();
 document.writeln("<p>");
 for (var i = 0; i < this.tags_list.length; i++) {
   var tag = this.tags_list[i];
   var posts = this.posts_bytag[tag];
   document.write("<a href=\"");
   document.write(prefix + tag + suffix);
   document.write("\" rel=\"tag\">");
   if (posts[0].timestamp >= recent_ts) {
     document.write("<b>"+tag+"</b>");
   } else {
     document.write(tag);
   }
//   document.write(" (" + posts.length + ")");
   document.write("</a>");
   if (i != this.tags_list.length-1) {
     if (i % 3 != 2) {		     
       document.write(" &sdot; ");
     } else {
       document.write("<br/>");
     }
   }
 }
 document.writeln("</p>");
}

PostDB.prototype.write_posts = function(begin, end, maxsize) {
  if (end > this.posts_list.length-1) {
    end = this.posts_list.length;
  }
  document.write("<ul>");
  for (var i = begin; i <= end; i++) {
    var post = this.posts_list[i];
    document.write("<li><a href=\"");
    document.write(post.url);
    document.write("\" title=\"" + post.title + "\">");
    document.write(post.title);
    document.write("</a></li>");
  }
  document.write("</ul>");
}

/** Adapted from Kevin Cannon, www.multiblah.com */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof oldonload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(writeFriendlyDates);

