function fade(io){
  n = $$('wikitext','div','sample');
  if (io == 1) for (i=0; i<this.n.length; i++) { n[i].className += ' fade'; }
  if (io == 0) for (i=0; i<this.n.length; i++) { n[i].className = n[i].className.replace(/\sfade/,''); }
}

Ed = Class.create();
Ed.prototype = {
  initialize: function(nClass,nTxtTag,css,css2) {
    this.frmStyle = "<link rel='stylesheet' type='text/css' href='"+css+"' />";
    this.scCss = "<link rel='stylesheet' type='text/css' href='"+css2+"' />";
    this.ei = '';
    this.inCmt = 0;
    urlp = location.href.split('/');
    urlt = urlp[urlp.length-2]+'-'+urlp[urlp.length-1].replace(/[#|\?].*/,'');
    urlpp = urlp[urlp.length-2].split('-');
    urlc = urlpp[0]+'-'+urlp[urlp.length-1].replace(/[#|\?].*/,'');
    this.url = location.href.replace(/(\.org)\/.*$/gi,'$1/Translations/') + urlt +'?action=edit';
    this.pName = 'Translations'+'.'+urlt;
    this.cn = 'Comments.' + urlc;
    this.pg = urlpp[urlpp.length-1];
    this.urlc = location.href.replace(/(\.org)\/.*$/gi,'$1/Comments/') + urlc;
    this.nm = '';
    this.n = $$('wikitext','span','marker');
    this.n.each(function(ni,i) {
      ni.tv = ni.parentNode.getElementsByTagName('div')[0]; 
      ni.ov = ni.parentNode.getElementsByTagName('div')[1];
      ni.b = ni.getElementsByTagName('a')[0];
      ni.nbsc = ni.getElementsByTagName('em')[0].innerHTML;
      ni.b.href = 'javascript:;'
      ni.b.onclick = function() {
        this.ei = i;
        fade(1);
        this.close(i);
        ni.ov.className += ' up';
        ni.className += ' off';
        if (!this.rte) { this.mkRte(i); } else this.mv(i);
        $('rec').className = ''; 
        $('rec').innerHTML = "Save";
        $('hint').innerHTML = "&gt; Contribute! Add or refine the translation.";
      }.bind(this);
    }.bind(this));
  },
  close: function(ii) {
    for (i=0; i<this.n.length; i++) { 
      this.n[i].className = (!this.n[i].className.match(/off/)) ? this.n[i].className : this.n[i].className.replace(/\soff/,'');
      this.n[i].ov.className = (!this.n[i].ov.className.match(/up/)) ? this.n[i].ov.className : this.n[i].ov.className.replace(/\sup/,'');
    }
    if (this.rte) this.rte.className = 'off';
    if (this.scifrm) {
      this.inCmt = 1;
      this.tog();
    }
  },
  mkRte: function(ii) {
    this.rte = document.createElement('div');
    this.rte.id = 'rte';
    this.n[ii].parentNode.appendChild(this.rte);
    hint = mke('p','hint','',"&gt; Contribute! Add or refine the translation.");
    this.rte.appendChild(hint);
    
    this.cmds = mke('ul','','',mkb('bold','bold','Bold') + mkb('italic','italic','Italic') + mkb('formatblock-h2','Big Section Header','H2') + mkb('formatblock-h3','Small Section Header','H3') + mkb('createlink','Insert Link','A') + mkb('unlink','Remove Link','Unlink') + mkb('insertunorderedlist','Insert bullet list','UL') + mkb('insertorderedlist','Insert ordered list','OL') + mkb('indent','Quote/Indent','Blockquote') + mkb('removeFormat','Remove All Formating','Unformat') + "<li id='scCmt'></li><li id='close'>Cancel</li><li id='rec'>Save</li>");
    this.rte.appendChild(this.cmds);
    
    this.mkEd(ii);
    
    nm = mke('p','auth','',"<input id='authnm' value='&rarr; Add your name' />");
    this.rte.appendChild(nm);
    $('authnm').onclick = function(){ 
      $('authnm').value = ''; 
      $('authnm').className = 'stress'; 
    }
    
    $('close').onclick = function(){
      this.close();
      fade(0);
    }.bind(this);
    $('rec').onclick = this.update.bind(this);
    this.b = lsCmds(this.cmds,-1);
    this.b.each(function(bi) {
      bi.onclick = function() {
        bcmd = bi.className;
        if (bcmd == 'createlink') {
          lnurl = prompt('Please enter a URL:', 'http://');
      		if (lnurl !=null && lnurl != '') {
            if (this.inCmt == 0) ins(this.ifrm, this.idoc, bcmd, lnurl);
            else ins(this.scifrm, this.scidoc, bcmd, lnurl);
      		}
        } else if (bcmd.match(/formatblock/)) {
          bh = bcmd.replace(/formatblock-/gi,'');
          if (this.inCmt == 0) ins(this.ifrm, this.idoc, 'formatblock', '<'+bh+'>');
          else ins(this.scifrm, this.scidoc, 'formatblock', '<'+bh+'>');
        } else {
          if (this.inCmt == 0) ins(this.ifrm, this.idoc, bcmd, null);
          else ins(this.scifrm, this.scidoc, bcmd, null);
        }
      }.bind(this);
    }.bind(this));
  },
  tog: function(){
    if (this.inCmt == 0) {
      if ($('addSc')) $('addSc').className = 'onCmt';
      $('hint').innerHTML = "&gt; You'll be the first to annotate this section.";
      $('rec').innerHTML = "Post";
      this.ifrm.className = 'off';
      if (!this.scifrm) this.mkCEd();
      this.scifrm.className = '';
      this.scSync();
      this.inCmt = 1;
    } else {
      if ($('addSc')) $('addSc').className = '';
      $('hint').innerHTML = "&gt; Contribute! Add or refine the translation.";
      $('rec').innerHTML = "Save";
      this.scifrm.className = 'off';
      this.ifrm.className = '';
      this.inCmt = 0;
    }
  },
  mkEd: function(ii){
    this.ifrm = document.createElement('iframe');
    this.ifrm.id = 'rtefrm';
    this.ifrm.name = 'rtefrm';
    this.rte.appendChild(this.ifrm);
    this.idoc = frames[this.ifrm.name].document;
    this.sync(ii);
  },
  mkCEd: function(){
    this.scifrm = document.createElement('iframe');
    this.scifrm.id = 'scifrm';
    this.scifrm.name = 'scifrm';
    this.rte.insertBefore(this.scifrm, this.ifrm);
    this.rte.insertBefore(this.scifrm, $('auth'));
    this.scidoc = frames[this.scifrm.name].document;
  },
  mv: function(ii) {
    this.n[ii].parentNode.appendChild(this.rte);
    this.rte.className = '';
    if (this.nm != '') $('authnm').value = this.nm;
    this.sync(ii);
  },
  sync: function(ii) {
    if (this.n[ii].nbsc == 0) {
      $('scCmt').innerHTML = "<strong id='addSc'><span>Add note</span></strong>";
      $('addSc').title = 'Add a note for this section';
      $('addSc').onclick = this.tog.bind(this);
    } else {
      $('scCmt').innerHTML = "<a href='"+this.urlc+"' id='sc'>"+this.n[ii].nbsc+"</a>";
    }
    
    this.ifrm.style.height = this.n[ii].ov.offsetHeight+24+'px';
    if (!document.all) {
      this.ifrm.contentDocument.designMode = 'on';
      this.ifrm.contentDocument.open();
      this.ifrm.contentDocument.write(this.frmStyle + this.n[ii].tv.innerHTML);
      this.ifrm.contentDocument.close();
      this.ifrm.contentDocument.execCommand('styleWithCSS', false, false);
    } else {
      this.idoc.designMode = 'on';
      this.idoc.open();
      this.idoc.write(this.frmStyle + this.n[ii].tv.innerHTML);
      this.idoc.close();
    }
    this.ifrm.contentWindow.focus();
  },
  scSync: function() {
    if (!document.all) {
      this.scifrm.contentDocument.designMode = 'on';
      this.scifrm.contentDocument.open();
      this.scifrm.contentDocument.write(this.scCss);
      this.scifrm.contentDocument.close();
      this.scifrm.contentDocument.execCommand('styleWithCSS', false, false);
    } else {
      this.scidoc.designMode = 'on';
      this.scidoc.open();
      this.scidoc.write(this.scCss);
      this.scidoc.close();
    }
    this.scifrm.contentWindow.focus();
  },
  update: function() {
    if (/name/.test($('authnm').value)) {
      $('authnm').className = 'stress';
      return;
    } else this.nm = $('authnm').value;
    
    if (this.inCmt == 0) {
      idoc = (!document.all) ? this.ifrm.contentDocument.body.innerHTML : this.idoc.body.innerHTML;
      etxt = toWiki(idoc.replace(/\n/g,'')).replace(/\n*$/gi,'').replace(/^\n*/gi,'') + "\n";
      $('rec').innerHTML = 'Saving...';
      $('rec').className = 'busy';
      edata = "action=edit"
        + "&n=" + this.pName
        + "&text=" + encodeURI(etxt)
        + "&author=" + encodeURI(this.nm)
        + "&s=" + (this.ei + 1)
        + "&sum=" + this.n.length
        + "&live=1&post=1";
      this.xreq = mkXReq();
      this.xreq.onreadystatechange = function() { 
        if (this.xreq.readyState == 4) { if (this.xreq.status == 200) {
          this.n[this.ei].tv.innerHTML = (!document.all) ? this.ifrm.contentDocument.body.innerHTML : this.idoc.body.innerHTML;
          this.close();
          fade(0);
        } else { alert('There was a problem with the request.'); } }
      }.bind(this);
      this.xreq.open('POST', this.url, true);
      this.xreq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      this.xreq.send(edata);
    } 
    else {
      scidoc = (!document.all) ? this.scifrm.contentDocument.body.innerHTML : this.scidoc.body.innerHTML;
      etxt = toWiki(scidoc.replace(/\n/g,'')).replace(/\n*$/gi,'').replace(/^\n*/gi,'') + "\n";
      $('rec').innerHTML = 'Posting...';
      $('rec').className = 'busy';
      edata = "action=edit"
        + "&n=" + this.cn
        + "&text=" + encodeURI(etxt)
        + "&author=" + encodeURI(this.nm)
        + "&s=0"  // Comment on top
        + "&pos=p" + this.pg + "s" + (this.ei+1)
        + "&cmt=1&post=1";
      this.xreq = mkXReq();
      this.xreq.onreadystatechange = function() { 
        if (this.xreq.readyState == 4) { if (this.xreq.status == 200) {
          this.n[this.ei].nbsc = '1';
          this.close();
          fade(0);
        } else { alert('There was a problem with the request.'); } }
      }.bind(this);
      this.xreq.open('POST', this.urlc, true);
      this.xreq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      this.xreq.send(edata);
    }
  }
}

PgCmt = Class.create();
PgCmt.prototype = {
  initialize: function(nid,nroot,frmCss){
    if (!$(nid)) return;
    this.n = $(nid);
    this.p = $(nroot);
    this.css = "<link rel='stylesheet' type='text/css' href="+ frmCss +" />";
    urlp = location.href.split('/');
    urlcp = urlp[urlp.length-2].split('-');
    this.url = location.href.replace(/(\.org)\/.*$/gi,'$1/Comments/') + urlcp[0] + '-' + urlp[urlp.length-1];
    this.cp = 'Comments.' + urlcp[0] + '-' + urlp[urlp.length-1];
    this.pg = urlcp[urlcp.length-1];
    this.n.onclick = function(){
      if (!this.rte || this.rte.className == 'off') fade(1);
      else fade(0);
      if (!this.rte) this.mkRte();
      else this.rte.className = (this.rte.className == '') ? 'off' : '';
    }.bind(this);
  },
  close: function(){
    if (this.rte) this.rte.className = 'off';
  },
  mkRte: function(){
    this.rte = mke('div','pgCmtRte','','');
    this.p.appendChild(this.rte);
    this.cmds = mke('ul','','',mkb('bold','bold','Bold') + mkb('italic','italic','Italic') + mkb('createlink','Insert Link','A') + mkb('unlink','Remove Link','Unlink') + mkb('insertunorderedlist','Insert bullet list','UL') + mkb('insertorderedlist','Insert ordered list','OL') + mkb('indent','Quote/Indent','Blockquote') + mkb('removeFormat','Remove All Formating','Unformat') + "<li id='pgRec'>Save</li><li id='pgClose'>Cancel</li>");
    this.rte.appendChild(this.cmds);
    
    this.cifrm = document.createElement('iframe');
    this.cifrm.id = 'cifrm';
    this.cifrm.name = 'cifrm';
    this.rte.appendChild(this.cifrm);
    this.cidoc = frames[this.cifrm.name].document;
    if (!document.all) {
      this.cifrm.contentDocument.designMode = 'on';
      this.cifrm.contentDocument.open();
      this.cifrm.contentDocument.write(this.css);
      this.cifrm.contentDocument.close();
      this.cifrm.contentDocument.execCommand('styleWithCSS', false, false);
    } else {
      this.cidoc.designMode = 'on';
      this.cidoc.open();
      this.cidoc.write(this.css);
      this.cidoc.close();
    }
    
    this.auth = mke('p','','',"<input id='cmtAuth' value='' />");
    this.rte.appendChild(this.auth);
    $('cmtAuth').value = ($('authnm')) ? $('authnm').value : 'Your name';
    if ($('cmtAuth').value == 'Your name') 
      $('cmtAuth').onclick = function(){$('cmtAuth').value == ''}.bind(this);
    this.set();
  },
  set: function(){
    this.b = lsCmds(this.cmds);
    this.b.each(function(bi) {
      bi.onclick = function() {
        bcmd = bi.className;
        if (bcmd == 'createlink') {
          lnurl = prompt('Please enter a URL:', 'http://');
      		if (lnurl !=null && lnurl != '') ins(this.cifrm, this.cidoc, bcmd, lnurl);
        } else {
          ins(this.cifrm, this.cidoc, bcmd, null);
        }
      }.bind(this);
    }.bind(this));
    $('pgClose').onclick = function(){
      this.close();
      fade(0);
    }.bind(this);
    $('pgRec').onclick = this.update.bind(this);
  },
  update: function(){
    cAuth = $('cmtAuth').value;
    if (cAuth == '' || /name/.test(cAuth)) {
      $('cmtAuth').className = 'stress';
      return;
    }
    idoc = (!document.all) ? this.cifrm.contentDocument.body.innerHTML : this.cidoc.body.innerHTML;
    etxt = toWiki(idoc.replace(/\n/g,'')).replace(/\n*$/gi,'').replace(/^\n*/gi,'') + "\n";
    
    $('pgRec').innerHTML = 'Saving...';
    $('pgRec').className = 'busy';
    edata = "action=edit"
      + "&n=" + this.cp
      + "&text=" + encodeURI(etxt)
      + "&author=" + encodeURI(cAuth)
      + "&s=0"  // Comment on top
      + "&pos=p" + this.pg + "s0"
      + "&cmt=1&post=1";

    this.xreq = mkXReq();
    this.xreq.onreadystatechange = function() { 
      if (this.xreq.readyState == 4) { if (this.xreq.status == 200) {
        this.n.innerHTML = "1 note";
        this.n.id = 'pgCmt';
        this.n.title = '';
        this.href = this.url;
        this.close();
        alert(this.xreq.responseText);
        fade(0);
      } else { alert('There was a problem with the request.'); } }
    }.bind(this);
    
    this.xreq.open('POST', this.url, true);
    this.xreq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    this.xreq.send(edata);
  }
}

window.onload = function() {
  lnTog = new Toggle('lnSelected', 0, ['lnSwitch']);
  css = 'http://traduwiki.org/pub/skins/greylady/includes/ifrm.css';
  css2 = 'http://traduwiki.org/pub/skins/greylady/includes/ifrm2.css';
  new Ed('marker','sample',css,css2);
  new PgCmt('addPgCmt','monitor',css2);
}