function addEvent(func){
// Simon Willison: http://simon.incutio.com/archive/2004/05/26/addLoadEvent
  if (!document.getElementById | !document.getElementsByTagName) return
  var oldonload=window.onload
  if (typeof window.onload != 'function') {window.onload=func}
  else {window.onload=function() {oldonload(); func()}}
}

function stripeCode(){

  // Locate and isolate pre followed by code with a class of codelist
  var pres=document.getElementById('content').getElementsByTagName("pre")

  for (var g=0;g<pres.length;g++){
    if (pres[g].firstChild.className=="codelist"){

      // create a new code element
      var newCode=document.createElement('code')

      // reset variables;

      // even: for odd or even line
      var even=false

      // line content container
      var spanContent=""

      // codeContent= all the code elements content
      var codeContent=pres[g].firstChild.firstChild.nodeValue

      // first character(s) should be the carriage return and or a line feed
      var crLF=codeContent.charAt(0)
      if (codeContent.charCodeAt(1)==10) crLF+=codeContent.charAt(1)

      // now work through the content character at a time
      for (var f=0;f<codeContent.length;f++){

        // test for an "end of line"
        if (codeContent.substr(f,crLF.length)==crLF){

          // check for CR and LF step if required
          if (codeContent.charAt(f+1)==crLF[1]) f++

          // create a new span as the lines container
          var newSpan=document.createElement('span')

          // apply a class
          newSpan.className=even?"odd":"even"

          // check to see if its a comment
          if (spanContent.match("//")){
            newSpan.className="rem"
            even= !even
          }

          // if no content add a space
          if (spanContent==""){
            spanContent=" "
            even= !even
          }

          //add line content
          newSpan.appendChild(document.createTextNode(spanContent))

          // add new line to the new code element
          newCode.appendChild(newSpan)

          // add CR or LF if required
          if (crLF.length==1) newCode.appendChild(document.createTextNode(crLF))

          // flip odd / even boolean
          even= !even

          // reset line container
          spanContent=""
        }

        // otherwise add character to line container
        else spanContent+=codeContent.charAt(f)
      }

      // finaly replace old code element with the freshly created one
      pres[g].replaceChild(newCode,pres[g].firstChild)
    }
  }
}

addEvent(stripeCode)
