Ed = Class.create();
Ed.prototype = {
  initialize: function(nid){
    this.frmStyle = "<link rel='stylesheet' type='text/css' href='http://" + location.host +"/pub/skins/greylady/styles/ifrmSbmt.css' />";
    this.n = $(nid);
    if (!this.rte) this.mkRte();
  },
  mkRte: function(){
    chtml = mkb('bold','Bold','B') + 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');
    this.rte = mke('ul','','cbar',chtml);
    this.n.appendChild(this.rte);

    this.ifrm = document.createElement('iframe');
    this.ifrm.id = this.n.id + '-ifrm';
    this.ifrm.name = this.n.id + '-ifrm';
    this.ifrm.className = 'ifrm';
    this.n.appendChild(this.ifrm);
    this.idoc = frames[this.ifrm.name].document;
    if (!document.all) {
      this.ifrm.contentDocument.designMode = 'on';
      this.ifrm.contentDocument.open();
      this.ifrm.contentDocument.write(this.frmStyle);
      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.idoc.close();
    }

    this.rszr = mke('p','','rszr',"<a href='javascript:;' id='edSmlr' title='Shrink the editor'><span>&ndash;</span></a><a href='javascript:;' id='edBigr' title='Expand the editor'><span>+</span></a>");
    this.n.appendChild(this.rszr);

    this.pimp();
  },
  pimp: function(){
    this.cmds = lsCmds(this.rte);
    this.cmds.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.ifrm, this.idoc, bcmd, lnurl);
        } else if (bcmd.match(/formatblock/)) {
          bh = bcmd.replace(/formatblock-/gi,'');
          ins(this.ifrm, this.idoc, 'formatblock', '<'+bh+'>');
        } else ins(this.ifrm, this.idoc, bcmd, null);
      }.bind(this);
    }.bind(this));

    this.crszr = lsCmds(this.rszr);
    this.crszr[0].onclick = function(){
      this.ifrm.style.height = this.ifrm.offsetHeight - 80 + 'px';
    }.bind(this);
    this.crszr[1].onclick = function(){
      this.ifrm.style.height = this.ifrm.offsetHeight + 80 + 'px';
    }.bind(this);
  },
  doc: function(){
    idoc = (!document.all) ? this.ifrm.contentDocument.body.innerHTML : this.idoc.body.innerHTML;
    return idoc;
  }
}

Menu = Class.create();
Menu.prototype = {
  initialize: function(nid, ndiv){
    this.intog = 0;
    this.nval = '';
    this.n = $(nid);
    this.ndiv = $(ndiv);
    this.itms = $$(this.ndiv.id,'span','');
    this.n.onclick = this.tog.bind(this);
    this.itms.each(function(itm){
      itm.onclick = function(){
        this.n.innerHTML = itm.innerHTML;
        this.nval = itm.className;
        this.tog();
      }.bind(this);
    }.bind(this));
  },
  tog: function(){
    this.ndiv.className = (this.intog == 0) ? 'on' : '';
    this.n.className = (this.intog == 0) ? 'on' : '';
    this.intog = (this.intog == 0) ? 1 : 0;
  }
}

Slides = Class.create();
Slides.prototype = {
  initialize: function(navs){
    this.txt = new Ed('itxt');
    this.dsc = new Ed('idesc');
    this.more = $('go');
    this.less = $('bk');
    this.sbmt = $('sbmt');
    this.f = $("sfrm");
    this.s = [$('s0'), $('s1')];
    this.subs = [$('sub0'), $('sub1')];
    this.ln = new Menu('lbln','lnSel');
    this.cc = new Menu('lblic','licSel');
    this.m = 0;
    this.more.onclick = function(){
      if (this.chk(0) == 1) return;
      itxt = this.txt.doc();
      $('text').value = toWiki(itxt.replace(/\n/g,' ')).replace(/\n*$/gi,'').replace(/^\n*/gi,'') + "\n";
      idsc = this.dsc.doc();
      $('desc').value = toWiki(idsc.replace(/\n/g,' ')).replace(/\n*$/gi,'').replace(/^\n*/gi,'') + "\n";
      this.reach(1);
    }.bind(this);
    this.less.onclick = function(){
      this.reach(0);
    }.bind(this);
    this.sbmt.onclick = function(){
      if (this.chk(1) == 1) return;
      $('lang').value = this.ln.nval;
      $('cc').value = this.cc.nval;
      this.f.submit();
    }.bind(this);
  },
  clear: function(){
    this.more.className = "off";
    this.less.className = "off";
    this.sbmt.className = "off";
    $('msg').innerHTML = "";
    for (i=0; i<this.s.length; i++) {
      this.s[i].className = "off";
      this.subs[i].className = "";
    }
  },
  reach: function(ii){
    this.clear();
    this.subs[ii].className = "big";
    this.s[ii].className = "";
    window.scrollTo(0,0);  // Jump to page top
    if (ii == 1) {
      this.less.className = "";
      this.sbmt.className = "";
    } else this.more.className = "";
  },
  chk: function(ii) {
    if (ii == 0)
      this.m = ($('title').value == '' || this.txt.doc() == '' || this.dsc.doc() == '') ? 1 : 0;
    else
      this.m = ($('owner').value == '' || $('author').value == '' || $('tags').value == '' || this.ln.nval == '' || this.cc.nval == '') ? 1 : 0;
    if (this.m == 1) $('msg').innerHTML = "Sorry but all the fields are required.";
    return this.m;
  }
}

function chkemail(str) {
  send = $('send');
  send.onclick = function(){
    m = 1;
    s = $(str).value.split(",");
    for (i=0; i<s.length; i++) {
      if (!/@/.test(s[i])) m = 0;
    }
    if (m==1) $('sfrm').submit();
    else $('msg').innerHTML = "Verify your email addresses";
  }
}

window.onload = function(){
  if (!$('s2')) new Slides();
  else chkemail('emails');
}
