var Class = {create: function() {return function() {this.initialize.apply(this, arguments);}}}

Function.prototype.bind = function(object) {var __method = this;return function() {__method.apply(object,arguments);}}

Array.prototype.each = function(func){for(var i=0; i<this.length; i++) func(this[i], i);}

function $(obj) { return document.getElementById(obj); }

function $$(n,ntag,nclass) {
	var nres = new Array();
	if (n == null) return;
	var els = $(n).getElementsByTagName(ntag);
	var pattern = new RegExp("(^|\\s)"+nclass+"(\\s|$)");
  if (!nclass) {
    for (i = 0; i < els.length; i++) nres[i] = els[i];
  } else {
    for (i = 0, j = 0; i < els.length; i++) {
      if (pattern.test(els[i].className)) {
        nres[j] = els[i];
        j++;
      }
    }
  }
	return nres;
}

function toWiki(html) {
  html = html.replace(/<p>\s*&nbsp;\s*<\/p>/gi,'');
  html = html.replace(/<p>(.*?)<\/p>/gi,"[nl]$1[nl]"); 
  html = html.replace(/<(\/?)i>/gi,"<$1em>");
  html = html.replace(/<(\/?)cite>/gi,"<$1em>");
  html = html.replace(/<em>(.*?)<\/em>/gi,"_$1_");
  html = html.replace(/<(\/?)b>/gi,"<$1strong>");
  html = html.replace(/<strong>(.*?)<\/strong>/gi,"*$1*");
  html = html.replace(/<blockquote>(.*?)<\/blockquote>/gi,"[nl]??$1??[nl]");
  html = html.replace(/<ul>(.*?)<\/ul>/gi, function($1){
    $1 = $1.replace(/<br>/gi, '[br]');
    return $1.replace(/(<\/li>)/gi,'[nl]').replace(/<li>/gi, '* ').replace(/<ul>(.*?)<\/ul>/gi, '[nl]$1');
  });
  html = html.replace(/<ol>(.*?)<\/ol>/gi, function($1){
    $1 = $1.replace(/<br>/gi, '[br]');
    return $1.replace(/(<\/li>)/gi,'[nl]').replace(/<li>/gi, '# ').replace(/<ol>(.*?)<\/ol>/gi, '[nl]$1');
  });
  html = html.replace(/<a\s[^>]*href="([^"]*?)"[^>]*>(.*?)<\/a>/gi, '[[$1|$2]]');
  html = html.replace(/<h1>(.*?)<\/h1>/gi,"[nl]! $1[nl]");
  html = html.replace(/<h2>(.*?)<\/h2>/gi,"[nl]!! $1[nl]");
  html = html.replace(/<h3>(.*?)<\/h3>/gi,"[nl]!!! $1[nl]");
  html = html.replace(/<h4>(.*?)<\/h4>/gi,"[nl]!!!! $1[nl]");
  html = html.replace(/<h5>(.*?)<\/h5>/gi,"[nl]!!!!! $1[nl]");
  html = html.replace(/<h6>(.*?)<\/h6>/gi,"[nl]!!!!!! $1[nl]");
  html = html.replace(/<br><br>\s*/gi, '[nl][nl]');
  html = html.replace(/<br>\s*/gi, '');
  html = html.replace(/<(\/?).*?>/gi,'');  // Remove extra tags
  html = html.replace(/^\s*/gi, '');
  html = html.replace(/\s*$/gi, '');
  html = html.replace(/\[nl\]\s*/gi, "\n");
  html = html.replace(/\[br\]\s*/gi, "\\\\\n");
  return html;
}

function lsCmds(n,nend) {
  if (!nend) nend = 0;
  na = n.getElementsByTagName('a');
  nCmds = new Array(na.length + nend);
  for (i = 0; i < na.length+nend; i++) 
    nCmds[i] = na[i];
  return nCmds;
}

function mkXReq() {
  if (window.XMLHttpRequest) {
    xreq = new XMLHttpRequest();
    xreq.overrideMimeType('text/html');
  }
  else if (window.ActiveXObject) {
    var aVer = [ "MSXML2.XMLHttp.5.0",
      "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
      "MSXML2.XMLHttp","Microsoft.XMLHttp"];
    for (var i = 0; i < aVer.length; i++) {
      try { xreq = new ActiveXObject(aVer[i]); } 
      catch (e) {}
    }
  }
  if (!xreq) {
    alert('We had a little problem to save your edit.');
    return false;
  }
  return xreq;
}

function mke(ntag,nid,nclass,nhtml){
  var n = document.createElement(ntag);
  n.id = nid;
  n.className = nclass;
  n.innerHTML = nhtml;
  return n;
}

function ins(fifrm,fidoc,cmd,options){
  if (!document.all) fifrm.contentDocument.execCommand(cmd, false, options);
  else fidoc.execCommand(cmd, false, options);
  fifrm.contentWindow.focus();
}

function mkb(bclass,btitle,btxt) {
  return "<li><a class="+bclass+" href=javascript:; title='"+btitle+"'><span>"+btxt+"</span></a></li>";
}

SelfClean = Class.create();
SelfClean.prototype = {
	initialize: function(ninput) {
		this.nin = $(ninput);
		this.ntxt = this.nin.value;
		this.nin.onfocus = function(){ this.nin.value = '' }.bind(this);
		this.nin.onblur = function(){ this.nin.value = this.ntxt }.bind(this);
	}
}
