/*extern Hilite,Behaviour*/

Hilite.style_name_suffix = false;
Hilite.elementid = "content";

Array.prototype.indexOf = function(x) {
   for (var i = 0; i < this.length; i++) {
      if (this[i] == x) {
         return i;
      }
   }
   return -1;
};
Array.prototype.remove = function(x) {
   var i = this.indexOf(x);
   while (i >= 0) {
      this.splice(i, 1);
      i = this.indexOf(x);
   }
};
Array.prototype.forEach = function(f) {
   var i;
   if (typeof(f) == "function") {
      for (i = 0; i < this.length; i++) {
         f(this[i]);
      }
   }
};
String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/, "");
};

function combineClassNames(currentClasses, addClasses, deleteClasses) {
   if (currentClasses instanceof Array) {
      currentClasses = currentClasses.join(" ");
   }
   if (addClasses instanceof Array) {
      currentClasses += " " + addClasses.join(" ");
   }
   else {
      currentClasses += " " + addClasses;
   }
   
   if (!(deleteClasses instanceof Array)) {
      deleteClasses = String(deleteClasses).split(" ");
   }
   
   var newClasses = [], i;
   currentClasses = currentClasses.split(" ");
   currentClasses.forEach(function(x) {
      var c = x.trim();
      if (c.length > 0 && newClasses.indexOf(c) == -1) {
         newClasses[newClasses.length] = c;
      }
   });
   deleteClasses.forEach(function(x) {
      newClasses.remove(x);
   });
   return newClasses.join(" ");         
}

function openImageWindow(url) {
   window.open(url, "showimg", "height=400,width=400,dependent=yes,directories=no,location=no,manubar=no,personalbar=no,resizable=yes,scrollbars=no,status=no,toolbar=no");
   return false;
}

function listProperties(o) {
   var n = "", val;
   for (var p in o) {
      val = o[p];
      if (!(val instanceof Function)) {
         try {
            n += p + " = " + val + "\n";
         }
         catch(e) {
         }
      }
   }
   return n;
}

// --- Voor de navigatiebalk ---

var NavigationBar = {
   doMoveAction: function(verb) {
      if (window.currentArticle) {
         window.location.href = "/?ref=" + window.currentArticle + "&move=" + verb;
      }
      else {
         window.location.href = "/";
      }
   },
   nextArticle: function() {
      doMoveAction("next");
   },
   prevArticle: function() {
      doMoveAction("prev");
   },
   firstArticle: function() {
      doMoveAction("first");
   },
   lastArticle: function() {
      doMoveAction("last");
   },
   upArticle: function() {
      doMoveAction("up");
   }
};

// --- Voor de footer ---

var Footer = {
   stepFooter: function() {
      var footer = Footer.element;
      footer.style.bottom = footer.step + "px";
      footer.style.opacity = footer.opacity();
      var newstep = footer.stepby();
      if (footer.step != footer.stepmax) {
         footer.step = newstep;
         window.setTimeout(Footer.stepFooter, 30);
      }
      else {
         footer.stepsdone();
      }
   },
   showFooter: function() {
      var footer = Footer.element;
      if (!footer.visible && Footer.canShowHide) {
         Footer.canShowHide = false;
         Footer.button.innerHTML = "Showing...";
         footer.style.display = "block";
         footer.step = -footer.offsetHeight;
         footer.stepmin = footer.step;
         footer.stepmax = 0;
         footer.stepby = function() {
            return (this.step < -4)? this.step + 4: 0;
         };
         footer.stepsdone = function() {
            Footer.canShowHide = true;
            Footer.button.innerHTML = "Hide&nbsp;info";
            this.visible = true;
         };
         footer.opacity = function() {
            if (this.step === 0) {
               return 1;
            }
            else {
               return -(1 - (this.stepmin - this.stepmax) / this.step);
            }
         };
         Footer.stepFooter();
      }
   },
   hideFooter: function() {
      var footer = Footer.element;
      if (footer.visible && Footer.canShowHide) {
         Footer.canShowHide = false;
         Footer.button.innerHTML = "Hiding...";
         footer.step = 0;
         footer.stepmin = footer.step;
         footer.stepmax = -footer.offsetHeight;
         footer.stepby = function() {
            return (this.step > -footer.offsetHeight + 4)? this.step - 4: -footer.offsetHeight;
         };
         footer.stepsdone = function() {
            footer.style.display = "none";
            Footer.canShowHide = true;
            Footer.button.innerHTML = "Show&nbsp;info";
            this.visible = false;
         };
         footer.opacity = function() {
            if (this.step === 0) {
               return 1;
            }
            else {
               return -(1 - (this.stepmax - this.stepmin) / this.step);
            }
         };
         Footer.stepFooter();
      }
   }
};

Behaviour.addLoadEvent(function() {
   var ie = (navigator.userAgent.indexOf("MSIE") >= 0 && navigator.userAgent.indexOf("Opera") == -1);
   var initFooter = function() {
      var footer = document.getElementById("footer");
      if (!ie) {
         footer.style.bottom = "-" + footer.offsetHeight + "px";
      }
      footer.visible = false;
      Footer.element = footer;
      Footer.button = document.getElementById("minifooter");
      Footer.canShowHide = true;
   };   
   var createPageFooter = function () {
      function appendLink(div, text, title, onclick) {
         var a = document.createElement("a");
         a.href = "#";
         a.title = title;
         a.onclick = onclick;
         a.appendChild(document.createTextNode(text));
         div.appendChild(document.createTextNode("["));
         div.appendChild(a);
         div.appendChild(document.createTextNode("] "));
         return div;
      }
      
      var footer = document.createElement("div");
      footer.id = "pagefooter";
      footer.appendChild(document.getElementById("modified"));
      appendLink(footer, "Printer-friendly version", "Show the printer-friendly version of this page", function() {
         alert("This page is already printer-friendly! You can print this page without problems...");
         window.print();
         return false;
      });
      appendLink(footer, "E-mail this page", "E-mail this page to a friend or whomever you want", function() {
         alert("If everything goes well, this will start your e-mail client.");
         var subject = escape("I found this on Thany.org!"),
             body = escape("Click this link to see what I mean:\n\nhttp://www.thany.org/article/" + Number(window.currentArticle) + "/" + window.encodedTitle);
         window.location.href = "mailto:?subject=" + subject + "&body=" + body;
         return false;
      });
      /*
      appendLink(footer, "This page as PDF", "Download this page as a PDF file", function() {
         window.location.href = "index.pdf.asp?id=" + window.currentArticle;
         return false;
      });
      */
      
      document.getElementById("page").appendChild(footer);
   };   
   var createPrintHeader = function() {
      var appendImage = function(div, id, src) {
         var img = document.createElement("img");
         img.id = id;
         img.src = "images/print/" + src + ".png";
         div.appendChild(img);
      };      
      var header = document.createElement("div");
      header.id = "printheader";
      appendImage(header, "line_left_from_logo_print", "line-left-from-logo");
      appendImage(header, "line_right_from_logo_print", "line-right-from-logo");
      appendImage(header, "url_print", "url-in-line");
      appendImage(header, "logo_print", "logo");
      var currentheader = document.getElementById("header");
      currentheader.parentNode.insertBefore(header, currentheader.nextSibling);
   };
   document.findPosX = function(obj) {
      var curleft = 0;
      while (obj.parentNode) {
         curleft += obj.offsetLeft;
         obj = obj.parentNode;
      }
      if (window.opera) {
         curleft -= window.pageXOffset;
      }
      return curleft;
   };
   document.findPosY = function(obj) {
      var curtop = 0;
      while (obj.parentNode) {
         curtop += obj.offsetTop;
         obj = obj.parentNode;
      }
      if (window.opera) {
         curtop -= window.pageYOffset;
      }
      return curtop;
   };
   document.findPos = function(obj) {
      return {x: this.findPosX(obj), y: this.findPosY(obj)};
   };
   var inputs = cssQuery("input[type=text],input[type=password],select,textarea");
   if (inputs.length > 0) {
      inputs[0].focus();
   }
   initFooter();
   createPageFooter();
   createPrintHeader();
});

(function() {
   var rules = {
      'code': function(el) {
         var br = el.nextSibling;
         if (br && br.tagName.toLowerCase() == "br") {
            br.parentNode.removeChild(br);
         }
      },
      'a': function(el) {
         var rot13 = function(input) {
            var coding = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMabcdefghijklmnopqrstuvwxyzabcdefghijklm", text = "", ch, pos;
            for (var i = 0; i < input.length; i++) {
               ch = input.charAt(i);
               pos = coding.indexOf(ch);
               if (pos > -1) {
                  ch = coding.charAt(pos + 13);
               }
               text += ch;
            }
            return text;
         };
         var unrot13 = function() {
            if (this.href.indexOf("mailto:") !== 0) {
               this.href = this.rot13(this.href);
            }
         };   
         
         el.rot13 = rot13;
         el.unrot13 = unrot13;
         if (el.rel == "internal" || el.href.substr(0, 7) == "mailto:" || el.href.substr(0, 7) == el.rot13("mailto:") || el.href.replace(/(http|https|ftp):\/\/(.+?)\/.*/gi, "$2").indexOf(window.location.host) >= 0) {
            el.target = "";
         }
         else if (el.name.length === 0 && el.href.substr(0, 11).toLowerCase() != "javascript" + ":") {
            el.target = "_blank";
            el.rel = "external";
         }
      },
      '#content li': function(el) {
         var span = document.createElement("span");
         span.className = "litext";
         span.innerHTML = el.innerHTML;
         while (el.childNodes.length > 0) {
            el.removeChild(el.firstChild);
         }
         el.appendChild(span);
      },
      '#content table[class=""]': function(el) {
         el.cellSpacing = 0;
         el.cellPadding = 0;
         el.className = "datatable";
      },
      '#content h2, #content h3': function(el) {
         var prev = el.previousSibling;
         if (prev.tagName && prev.tagName.toUpperCase() == "H1") {
            return;
         }
         var clear = document.createElement("div");
         clear.className = "clear";
         clear.appendChild(document.createTextNode("\u00a0"));
         el.parentNode.insertBefore(clear, el);
      },
      '#content img': function(el) {
         if (el.className == "icon") {
            return;
         }
         var regexSimpleBold = /\*(\S+(.+?)\S+)\*/g, regexSimpleItalic = /\/(\S+(.+?)\S+)\//g;
         var simpleMarkup = function(s) {
            s = s.replace(/&/g, "&amp;");
            s = s.replace(/</g, "&lt;");
            s = s.replace(/>/g, "&gt;");
            s = s.replace(/"/g, "&quot;");
            s = s.replace(regexSimpleBold, "<strong>$1</strong>");
            s = s.replace(regexSimpleItalic, "<em>$1</em>");
            return s;
         };
         var hideSimpleMarkup = function(s) {
            s = s.replace(regexSimpleBold, "$1");
            s = s.replace(regexSimpleItalic, "$1");
            return s;
         };
         
         var outerDiv = document.createElement("div"), nextClear = false;
         outerDiv.className = "image";
         if (el.align == "right" || el.style.cssFloat == "right") {
            outerDiv.style.cssFloat = "right";
            outerDiv.style.marginLeft = "4px";
         }
         else {
            outerDiv.style.cssFloat = "left";
            outerDiv.style.marginRight = "4px";
         }
         
         var innerDiv = document.createElement("div"), innerText = document.createElement("span");
         innerText.innerHTML = simpleMarkup(el.alt);
         el.alt = hideSimpleMarkup(el.alt);
         el.border = 0;
         el.align = "";
         el.style.cssFloat = "";
         if (el.title.length === 0) {
            el.title = el.alt;
         }
         if (el.parentNode.tagName.toUpperCase() == "A") {
            el = el.parentNode;
         }
         
         var insertPos = (el.nextSibling? el.nextSibling: null), insertParent = el.parentNode;
         innerDiv.style.width = "auto";
         innerDiv.appendChild(el);
         innerDiv.appendChild(innerText);
         outerDiv.appendChild(innerDiv);
         if (insertPos === null) {
            insertParent.appendChild(outerDiv);
         }
         else {
            insertParent.insertBefore(outerDiv, insertPos);
         }
      },
      "#minifooter": function(el) {
         el.onclick = function() {
            if (Footer.element.visible) {
               Footer.hideFooter();
            }
            else {
               Footer.showFooter();
            }
         };
      },
      "form[name=search]": function(el) {
         el.onsubmit = function() {
            var keywords = cssQuery("input[name=keywords]");
            if (keywords.length > 0 && keywords[0].value.trim() == "") {
               alert("You didn't enter a search request yet.");
               keywords[0].focus();
               return false;
            }
         };
      }
   };
   Behaviour.register(rules);
})();