// Google Analytics variables.
_uacct = "UA-301461-1";
urchinTracker();


// Fall-back function for Opera which (until now -- August 20th, 2007)
// does not support the "href" attribute on <link>-tags.
function gotolink(element) {
 this.location = element.href;
}


// Other javascript from here.
window.onload = function() {

 // Set the window title to reflect on various pages of the weblog.
 if ((/\?id=/.test(document.location) == true) && (document.getElementsByTagName("h2").length > 0)) {
  var urlval = document.location.toString();
  var startidstr = (/\?id=Entry/.test(urlval))?(urlval.indexOf("?id=Entry")+9):(urlval.indexOf("?id=")+4);
  var endidstr = (/&/.test(urlval)?urlval.indexOf("&"):(/#/.test(urlval)?urlval.indexOf("#"):urlval.length))
  var entryid = urlval.slice(startidstr, endidstr);
  var topic = document.getElementsByTagName("h2")[0].firstChild.nodeValue;
  // These's still a bug with this if the topic contains other HTML tags (e.g. highlighted topics due to searches).
  document.title = document.title+" - Entry #"+entryid+": "+topic;
 } else if (/\?random/.test(document.location) == true) {
  var entryid = document.getElementsByTagName("var")[0].firstChild.nodeValue;
  var topic = document.getElementsByTagName("h2")[0].firstChild.nodeValue;
  document.title = document.title+" - Random entry #"+entryid+": "+topic;
 } else if (/\?show=all/.test(document.location) == true) {
  document.title = document.title+" - Showing all entries";
 } else if (/\?search/.test(document.location) == true) {
  document.title = document.title+" - Search results for \""+document.getElementById("search").value+"\"";
 } else if (/\?admin&edit=/.test(document.location) == true) {
  var entryid = document.getElementsByName("id")[0].value;
  var topic = document.getElementById("subject").value;
  document.title = document.title+" - Editing entry #"+entryid+": "+topic;
 } else if (/\?admin/.test(document.location) == true) {
  document.title = document.title+" - Create a new entry";
 }


 // If "highlight=" is present in the URL (thus highlighting is used); continue.
 if(/highlight=/.test(document.location) == true) {
  // Create a new div-element in the document (henceforth called "divhigh").
  divhigh = document.createElement("div");

  // Set attributes for divhigh.
  divhigh.setAttribute("id", "delhighlighting");
  divhigh.setAttribute("title", "Remove highlighting from the entry.");
  divhigh.setAttribute("onclick", "javascript:delhigh()");
  divhigh.setAttribute("onmouseover", "javascript:divhigh.style.textDecoration='none';divhigh.style.color='#C00'");
  divhigh.setAttribute("onmouseout", "javascript:divhigh.style.textDecoration='underline';divhigh.style.color='#600'");

  // Set CSS styling for divhigh.
  divhigh.style.display        = "block";
  divhigh.style.textAlign      = "center";
  divhigh.style.background     = "#EEB url('bg_topic.png') repeat-x"; // Old: #CC9
  divhigh.style.color          = "#600";
  divhigh.style.fontSize       = "18px";
  divhigh.style.height         = "28px";
  divhigh.style.width          = "450px";
  divhigh.style.marginLeft     = "77px";
  divhigh.style.marginBottom   = "30px";
  divhigh.style.padding        = "2px";
  divhigh.style.paddingTop     = "7px";
  divhigh.style.border         = "1px solid #330";
  divhigh.style.textDecoration = "underline";
  divhigh.style.cursor         = "pointer";
  // (I know setting styles like this is generally slower than invoking "className",
  // but I want to keep these CSS contained instead of adding it to the CSS file.)

  // Write text to divhigh.
  divhigh.appendChild(document.createTextNode("Remove Highlighting"));

  // Create a new variable "blogbody" pointing to the div-element with an id of "body".
  blogbody = document.getElementById("body");

  // Append divhigh to blogbody.
  blogbody.appendChild(divhigh);

  // For each div-element in blogbody...
  for (var i = 0; i < blogbody.getElementsByTagName("div").length; i++) {
   // ...which has a "class" attribute which contains "entry"...
   if (/entry/.test(blogbody.getElementsByTagName("div")[i].getAttribute("class")) == true) {
    // ...create a new variable "blogentry" pointing to that div-element.
    blogentry = blogbody.getElementsByTagName("div")[i];
    // In case of multiple div-elements with a class containing "entry", stop (break out of this loop) at the first match:
    break;
   }
  }

  // Place divhigh before the weblog entry.
  blogbody.insertBefore(divhigh, blogentry);
 }
}


// The function which removes the highlighting from the highlighted word(s).
function delhigh() {
 // Get all the span-elements on the page.
 var highspans = document.getElementsByTagName("span");

 // For each span-element on the page...
 for (var i = 0; i < highspans.length; i++) {
  // ...if the span-element has a "class" attribute which equals "highlight"...
  if (highspans[i].getAttribute("class") == "highlight") {
   // ...change the CSS styling of those span-elements to parent defaults (and remove padding).
   highspans[i].style.backgroundColor = "inherit";
   highspans[i].style.color           = "inherit";
   highspans[i].style.fontWeight      = "inherit";
   highspans[i].style.padding         = "0px";
  }
 }

 // And lastly; remove divhigh from the page (since it's no longer needed).
 blogbody.removeChild(divhigh);

 // Reminder: it may be better to make divhigh toggle highlighting instead of removing..?
}
