var last="";
var typed=0;
var bname;
var xinha_editors = null;

function readmore() {
  document.getElementById('readmore').style.display='none';
  document.getElementById('blogtext').overflow='visible';
  document.getElementById('blogtext').style.height='auto';
  document.getElementById('blogtext').style.marginBottom='.5em';
}

function reply (bloguser,blogid,replyid,replyreplyid,width) {
  xinha_init    = null;
  xinha_config  = null;
  xinha_plugins = null;
  if (width>1) {
    width-=5;
  } else {
    width = 710;
  }
  if (last == replyid+"."+replyreplyid) return;
  var div = document.getElementById(replyid+"."+replyreplyid);
  var area = document.createElement('div');
  area.id = "form"+replyid+"."+replyreplyid;
  if (bloguser=="system" || blogid==4294967294) {
    div.style.width=width+"px";
    area.innerHTML = "<style>iframe{border:none}</style><form id=\"blogpost\" style=\"width:"+width+"px; \" method=\"POST\">" + 
    "<input type=\"hidden\" name=\"bloguser\" value=\""+bloguser+"\">"+
    "<input type=\"hidden\" name=\"entry\" value=\""+blogid+"\">"+
    "<input type=\"hidden\" name=\"action\" value=\"newreply\">"+
    "<input type=\"hidden\" name=\"replyid\" value=\""+replyid+"\">"+
    "<input type=\"hidden\" name=\"replyreplyid\" value=\""+replyreplyid+"\">"+
    "<input style=\"margin-top:1em; position:relative; left:3px; width:"+width+"px;\" id=\"newsubject\" type=\"text\" name=\"subject\" >"+
    "<br><textarea id=\"box"+replyid+"."+replyreplyid+"\" onchange=\"typed=1;\" style=\"width:"+width+"px;height:179px\" name=\"text\">"+
    "</textarea><input type=\"hidden\" name=\"blogmarkup\" id=\"blogmarkup\">"+
    "<input type=\"button\" value=\"Reply\"  onclick=\"post(this)\">"+
    "<input type=\"button\" value=\"Cancel\" onclick=\"cancel()\">"+
    "</form><div class=\"roundedCornerSpacer\">&nbsp;</div><br>";
  } else if (replyid==0) {
    width=630;
    area.innerHTML = "<style>iframe{border:none}</style><form id=\"blogpost\" style=\"width:"+width+"px;padding-left:12px; \" method=\"POST\">" + 
    "<input type=\"hidden\" name=\"bloguser\" value=\""+bloguser+"\">"+
    "<input type=\"hidden\" name=\"entry\" value=\""+blogid+"\">"+
    "<input type=\"hidden\" name=\"action\" value=\"newreply\">"+
    "<input type=\"hidden\" name=\"replyid\" value=\""+replyid+"\">"+
    "<input type=\"hidden\" name=\"replyreplyid\" value=\""+replyreplyid+"\">"+
    "<input style=\"margin-top:1em; left:0; width:"+width+"px;\" id=\"newsubject\" type=\"text\" name=\"subject\" >"+
    "<br><textarea id=\"box"+replyid+"."+replyreplyid+"\" onchange=\"typed=1;\" style=\"width:"+width+"px;;height:179px\" name=\"text\">"+
    "</textarea><input type=\"hidden\" name=\"blogmarkup\" id=\"blogmarkup\">"+
    "<input type=\"button\" value=\"Reply\"  onclick=\"post(this)\">"+
    "<input type=\"button\" value=\"Cancel\" onclick=\"cancel()\">"+
    "</form><div class=\"roundedCornerSpacer\">&nbsp;</div><br>";
  } else {
    div.style.width=width+"px";
    area.innerHTML = "<style>iframe{border:none}</style><form id=\"blogpost\"  method=\"POST\">" + 
    "<input type=\"hidden\" name=\"bloguser\" value=\""+bloguser+"\">"+
    "<input type=\"hidden\" name=\"entry\" value=\""+blogid+"\">"+
    "<input type=\"hidden\" name=\"action\" value=\"newreply\">"+
    "<input type=\"hidden\" name=\"replyid\" value=\""+replyid+"\">"+
    "<input type=\"hidden\" name=\"replyreplyid\" value=\""+replyreplyid+"\">"+
    "<input style=\"margin-top:1em; width:"+width+"px;\" id=\"newsubject\" type=\"text\" name=\"subject\" >"+
    "<textarea id=\"box"+replyid+"."+replyreplyid+"\" onchange=\"typed=1;\" style=\"width:"+width+"px;height:150px\" name=\"text\">"+
    "</textarea><input type=\"hidden\" name=\"blogmarkup\" id=\"blogmarkup\">"+
    "<input type=\"button\" value=\"Reply\"  onclick=\"post(this)\">"+
    "<input type=\"button\" value=\"Cancel\" onclick=\"cancel()\">"+
    "</form>";
  }
  if (last!=replyid+"."+replyreplyid) {
    if (last!="") {
      if (typed==1) {
        if (confirm("Erase your other reply?")) {
          var theform = document.getElementById("form"+last);
          if (theform)  theform.parentNode.removeChild(theform);
        } else {
          return;
        }
      } else {
          var theform = document.getElementById("form"+last);
          if (theform)  theform.parentNode.removeChild(theform);
      }
    }
    div.appendChild(area);
    var oldsub = '';
    if (document.getElementById("subject"+replyid+"."+replyreplyid))
      oldsub = document.getElementById("subject"+replyid+"."+replyreplyid).innerHTML;
    if ((replyid+"."+replyreplyid=='0.0') || (!oldsub.match(/^Re:/))) {
      oldsub = "Re: " + oldsub;
    }
    document.getElementById("newsubject").value=oldsub;
  } 
  if (replyid==0 && document.body.scrollTop) document.body.scrollTop=0;
  bname = "box"+replyid+"."+replyreplyid;
  var box = document.getElementById(bname);
  box.focus();
  last = replyid+"."+replyreplyid;

  if (typeof HTMLArea != 'undefined' && HTMLArea != null) {

     xinha_init = xinha_init ? xinha_init : function()
    {
             xinha_plugins = xinha_plugins ? xinha_plugins :
      [
       'InsertUser',
       'FindReplace',
       'GetHtml',
       'InsertSmiley'
      ];

      if(!HTMLArea.loadPlugins(xinha_plugins, xinha_init)) return;

      xinha_editors = xinha_editors ? xinha_editors :
      [
        'blog'
      ];

        var xinha_config;

       xinha_config = xinha_config ? xinha_config() : new HTMLArea.Config();

       xinha_config.statusBar = false;
       xinha_config.baseHref = window.location.protocol+'//'+window.location.hostname+'/';
       xinha_config.stripBaseHref = false;
       xinha_config.stripSelfNamedAnchors = false;
       xinha_config.pageStyle = "p {margin:0} .msuser{font-family:verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif;font-size:72%;cursor:pointer;text-decoration:none;display:inline;background:url("+window.location.protocol+'//'+window.location.hostname+"/img/userinfo.gif) no-repeat 0% -1px;padding-left:20px;}";
       xinha_config.mozParaHandler = 'built-in';
       xinha_config.flowToolbars = false;
       xinha_config.activateByDefault = true;
       xinha_config.focusByDefault = true;
       xinha_config.toolbar =
          [
            ["fontname","bold","italic","underline","strikethrough"],   
            ["separator","forecolor","hilitecolor","textindicator"],
            ["separator","subscript","superscript"],
            ["separator","justifyleft","justifycenter","justifyright","justifyfull"],
            ["linebreak","insertorderedlist","insertunorderedlist","outdent","indent"],
            ["separator","createlink","insertimage"],
            ["separator","undo","redo","print"], (HTMLArea.is_gecko ? [] : ["cut","copy","paste"]),
            ["separator","showhelp","htmlmode"]
          ];

      xinha_editors   = HTMLArea.makeEditors([bname], xinha_config, xinha_plugins);

      HTMLArea.startEditors(xinha_editors);
      xinha_editors[bname].focusEditor();
    }
    xinha_init();

  }

}

function cancel() {
  if (typed==1) {
    if (confirm("This will erase what you wrote.")) {
      var theform = document.getElementById("form"+last);
      if (theform)  theform.parentNode.removeChild(theform);
    } else {
      return;
    }
  } else {
    var theform = document.getElementById("form"+last);
    if (theform)  theform.parentNode.removeChild(theform);
  }
  last="";
  typed=0;
}
function duhleet (bloguser,blogid,replyid,replyreplyid) {
  var div = document.getElementById(replyid+"."+replyreplyid);
  div.style.border = "2px dashed red";
  div.style.background = "yellow";
  if (confirm("Deleting this comment will also delete any replies to it.")) {
    var gform = document.getElementById('gform');
    gform.action.value = "deletereply";
    gform.replyreplyid.value = replyreplyid;
    gform.replyid.value = replyid;
    gform.entry.value = blogid;
    gform.start.value = getQueryVariable('start');
    gform.submit();
  } else {
    div.style.border = "";
    div.style.background = "";
  }
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return 0;
}

function togglepic() {
  var size = getCookie('size');
  if (size == "1") {
    size = "2";
  } else {
    size = "1";
  }
  var today = new Date();
  var expire = new Date();
  expire.setTime(today.getTime() + 3600000*24*200);
  setCookie("size",size, expire, "/comments", window.location.hostname)
  setTimeout("document.location.href=document.location.href",500);
}


function post() {
  form = document.getElementById('blogpost');
  if (typeof xinha_editors != 'undefined' && xinha_editors != null) {
    if (!HTMLArea.is_opera)
     form.blogmarkup.value =  nobreaks(xinha_editors[bname].getHTML());
  }
  form.submit();
}

function nobreaks(h) {
  return h.replace(/[\r\n]/g," ");
}

function openedit() {
  var editd = document.getElementById('editd');
  editd.style.display='block';
  var editstyle = document.getElementById('editstyle');
  editstyle.style.display='none';
  var editarea = document.getElementById('editarea');
  editarea.value = document.getElementById('pcontent').innerHTML;
  contenteditor();
}

function openstyle() {
  var editstyle = document.getElementById('editstyle');
  editstyle.style.display='block';
  var editd = document.getElementById('editd');
  editd.style.display='none';
}

function cancelwiki() {
  var editstyle = document.getElementById('editstyle');
  editstyle.style.display='none';
  var editd = document.getElementById('editd');
  editd.style.display='none';
}

function getMarkup() {
  var markup;
  if (typeof xinha_editors != 'undefined' && xinha_editors != null && xinha_editors['editarea'] != null) {
    if (!HTMLArea.is_opera)
     markup =  xinha_editors['editarea'].getHTML();
  } else {
     markup = document.getElementById('editarea').value;
  }
  return markup;
}

function preview() {
  var iform = document.getElementById('iform');
  iform.action.value = "previewwiki";
  iform.param.value = getMarkup();
  iform.submit();
}

function savehtml(i) {
  var editarea = document.getElementById('editarea');
  var comments  = document.getElementById('htmlcomments');
  if (comments.value.length==0) {
    alert("Please describe your changes for the record.");
    return;
  }
  var iform = document.getElementById('iform');
  iform.action.value = "savewikihtml";
  comments.value = comments.value.replace(/:/g,"");
  iform.param.value = i+":"+version+":C"+comments.value+":"+getMarkup();
  iform.submit();
}
 
function savecss(i) {
  var stylearea = document.getElementById('stylearea');
  var comments  = document.getElementById('csscomments');
  if (comments.value.length==0) {
    alert("Please describe your changes for the record.");
    return;
  }
  var iform = document.getElementById('iform');
  iform.action.value = "savewikistyle";
  comments.value = comments.value.replace(/:/g,"");
  iform.param.value = i+":"+version+":S"+comments.value+":"+stylearea.value;
  iform.submit();
}


function previewstyle() {
  var stylearea = document.getElementById('stylearea');
  var iform = document.getElementById('iform');
  iform.action.value = "previewwikistyle";
  iform.param.value = stylearea.value;
  iform.submit();
}
 
function doRevert(comments) {
  comments = comments.value;
  if (comments.length<1) {
    MyAppAlert("<b>Error:</b> You must specify a reason");
    return;
  } 
  comments.replace(/:/g,"");
  var iform = document.getElementById('iform');
  iform.action.value = "revertwiki";
  iform.param.value = window.p1+":"+window.p2+":R"+window.p2+"R"+comments;
  iform.submit();
}

function revert(page,version) {
    window.p1 = page;
    window.p2 = version;
    alertPrompt = new jt_AppPrompt(jt_AppAlert.Warning,doRevert);
    alertPrompt.setTitle("Revert");
    alertPrompt.askUser("<b>WARNING</b>: This will erase all changes back to version "+version+"!<br><br>Reason: ","");
    alertPrompt.moveTo(-1, -1);
    alertPrompt.show();
    alertPrompt.moveTo(-1, -1);
}

function contenteditor() {
  xinha_init    = null;
  xinha_config  = null;
  xinha_plugins = null;
  
  if (typeof HTMLArea != 'undefined' && HTMLArea != null ) {

    if (typeof xinha_editors != 'undefined' && xinha_editors != null && xinha_editors['editarea'] != null) return;
    if (getCookie('htmlmode')=="1") return;

     xinha_init = xinha_init ? xinha_init : function()
    {
             xinha_plugins = xinha_plugins ? xinha_plugins :
      [
       'InsertUser',
       'FindReplace',
       'GetHtml',
       'InsertSmiley'
      ];

      if(!HTMLArea.loadPlugins(xinha_plugins, xinha_init)) return;

      xinha_editors = xinha_editors ? xinha_editors :
      [
        'blog'
      ];

        var xinha_config;

       xinha_config = xinha_config ? xinha_config() : new HTMLArea.Config();
       xinha_config.statusBar = false;
       //xinha_config.width  = '702px';
       xinha_config.baseHref = window.location.protocol+'//'+window.location.hostname+'/';
       xinha_config.stripBaseHref = false;
       xinha_config.stripSelfNamedAnchors = false;
       var mycss = "";//document.getElementById("styleme").innerHTML.replace(/\r|\n/g," ");
       //mycss = mycss.replace(/\#pcontent/g,"body");
       xinha_config.pageStyle = "html{font-family:verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif;font-size:72%;margin:0} p {margin:0} h1{font-family:trebuchet ms,arial,helvetica,sans-serif;margin-bottom:0px;margin-top:.5em;font-size:170%;letter-spacing:0.4em;} h2,fieldset.recentactivity legend{font-family:trebuchet ms,arial,helvetica,sans-serif;font-size:130%;font-weight:bold;margin-bottom:.2em;margin-top:1em;} .msuser{font-family:verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif;font-size:72%;cursor:pointer;text-decoration:none;display:inline;background:url("+window.location.protocol+'//'+window.location.hostname+'/'+"userinfo.gif) no-repeat 0% -1px;padding-left:20px;} " + mycss;
       
       xinha_config.mozParaHandler = 'built-in';
       xinha_config.flowToolbars = false;
       xinha_config.activateByDefault = true;
       xinha_config.focusByDefault = false;
       xinha_config.toolbar =
          [
            ["fontname","bold","italic","underline","strikethrough"],   
            ["separator","forecolor","hilitecolor","textindicator"],
            ["separator","subscript","superscript"],
            ["separator","justifyleft","justifycenter","justifyright","justifyfull"],
            ["linebreak","insertorderedlist","insertunorderedlist","outdent","indent"],
            ["separator","createlink","insertimage"],
            ["separator","undo","redo","print"], (HTMLArea.is_gecko ? [] : ["cut","copy","paste"]),
            ["separator","showhelp","htmlmode"]
          ];

      xinha_editors   = HTMLArea.makeEditors(['editarea'], xinha_config, xinha_plugins);
      HTMLArea.startEditors(xinha_editors);
      //window.xinha = xinha_editors['editarea'];
      //setTimeout("window.xinha.setHTML(document.getElementById('pcontent').innerHTML);",1);
    }
    xinha_init();
    
  }

}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}