/*
 * (c) 2009.02.12 by martin vit
 */
var mld;

function trim(str) {
  var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
  for(var i=0; i<str.length; i++) {
    if(whitespace.indexOf(str.charAt(i)) === -1) { 
      str = str.substring(i); 
      break;
    }
  }
  for(var i=str.length-1; i>=0; i--) {
    if(whitespace.indexOf(str.charAt(i)) === -1) {
      str = str.substring(0, i+1);
      break;
    }
  }
  return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

//----------------------------------------------------------------------
function Cookie() {
  this.setCookie = Cookie_setCookie;
  this.getCookie = Cookie_getCookie;
  this.getCookieByIdx = Cookie_getCookieByIdx;
}

function Cookie_setCookie(name, value) {
  var tmp_expires = new Date();
  tmp_expires.setTime(tmp_expires.getTime() + (365 * 24 * 3600 * 1000));
  
  var argv = this.setCookie.arguments;
  var argc = this.setCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : tmp_expires;
  var path = (argc > 3) ? argv[3] : '/';
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;

  var expr = name + "=" + escape (value) + 
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");  
  
  document.cookie = expr;
}

function Cookie_getCookieByIdx(idx)
{
  var endstr = document.cookie.indexOf (";", idx);
  if(endstr == -1) endstr = document.cookie.length;
  var s = document.cookie.substring(idx, endstr);
  return unescape(s);
}

function Cookie_getCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  
  while(i < clen)
  {
    var j = i + alen;
    if(document.cookie.substring(i, j) == arg) return this.getCookieByIdx(j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

//----------------------------------------------------------------------
function MLD(document, parent, docPrefix) {
  if(!docPrefix) docPrefix = './';
  //attribs...
  this.cookie = new Cookie();
  this.document = document;
  this.parent = parent;
  this.docPrefix = docPrefix;
  this.form = document.vcb_form;
  this.elements = null;
  this.logicMap = null;
  this.lastVcb = null;
  this.dictId = 'en';
  this.dirId = 0;
  this.trn_window = 0;
  this.lastTrnVcb = null;
  this.lastTrnDictdir = null;
  
  //methods...
  this.mapHTMLElements = MLD_mapHTMLElements;
  this.createLogicMap = MLD_createLogicMap;
  this.setJs = MLD_setJs;
  this.getDictionaryLabel = MLD_getDictionaryLabel;
  this.getDictListIdx = MLD_getDictListIdx;
  this.getElement = MLD_getElement;
  this.setDictionary = MLD_setDictionary;
  this.setDictionaries = MLD_setDictionaries;
  this.highlightLabel = MLD_highlightLabel;
  this.selectDictList = MLD_selectDictList;
  this.turnDictionary = MLD_turnDictionary;
  this.onDictDirChange = MLD_onDictDirChange;
  this.onLinesChange = MLD_onLinesChange;
  this.setDictionaryId = MLD_setDictionaryId;
  this.setDirectionId = MLD_setDirectionId;
  this.setFocus = MLD_setFocus;
  this.setVocable = MLD_setVocable;
  this.onSubmit = MLD_onSubmit;
  this.submit = MLD_submit;
  this.onKeyUp = MLD_onKeyUp;
  this.onKeyDown = MLD_onKeyDown;
  this.setLines = MLD_setLines;
  this.setDictDir = MLD_setDictDir;
  this.setCookies = MLD_setCookies;
  this.getCookies = MLD_getCookies;
  this.onTrnWindow = MLD_onTrnWindow;
  this.isEqualWithLast = MLD_isEqualWithLast;
  this.shiftKey = false;
  
  //init...
  this.mapHTMLElements();
  this.createLogicMap()
}

function MLD_mapHTMLElements() {
  var names = [
    ['lbl_en','lbl_en'],['lbl_ge','lbl_ge'],['lbl_fr','lbl_fr'],['lbl_it','lbl_it'], 
    ['lbl_sp','lbl_sp'],['lbl_ru','lbl_ru'],['lbl_la','lbl_la'],['lbl_eo','lbl_eo'], 
    ['lbl_cz','lbl_cz'],['cz_sep','cz_sep'], 
    ['vcb','inp_vcb'],['dictdir','cb_dictdir'],['lines','cb_lines'],['js','inp_js']
  ];
  
  this.elements = new Array();
  for(var i=0; i<names.length; i++) {
    this.elements[names[i][0]] = this.document.getElementById( names[i][1] );
  }
}

function MLD_createLogicMap() {
  this.logicMap = {
    labelLeft: {
      1: 'esperantsk&#253;',
      2: 'polsk&#253;'
    },     
    labelRight: {
      1: '&#269;esk&#253;',
      2: 'slovensk&#253;'
    }, 
    ids: {
      encz: { lbl: 'lbl_en', lblLeftIdx: 0, lblRightIdx: 1, idx: 1 },
      gecz: { lbl: 'lbl_ge', lblLeftIdx: 0, lblRightIdx: 1, idx: 5 },
      frcz: { lbl: 'lbl_fr', lblLeftIdx: 0, lblRightIdx: 1, idx: 9 },
      itcz: { lbl: 'lbl_it', lblLeftIdx: 0, lblRightIdx: 1, idx: 13 },
      spcz: { lbl: 'lbl_sp', lblLeftIdx: 0, lblRightIdx: 1, idx: 17 },
      rucz: { lbl: 'lbl_ru', lblLeftIdx: 0, lblRightIdx: 1, idx: 21 },
      lacz: { lbl: 'lbl_la', lblLeftIdx: 0, lblRightIdx: 1, idx: 25 },
      eocz: { lbl: 'lbl_eo', lblLeftIdx: 1, lblRightIdx: 1, idx: 29 },
      eosk: { lbl: 'lbl_eo', lblLeftIdx: 1, lblRightIdx: 2, idx: 33 },
      plcz: { lbl: 'lbl_eo', lblLeftIdx: 2, lblRightIdx: 1, idx: 37 }
    },
    dictdir: {
      'encz.en' : { dictId:'encz', dirId:0 },
      'encz.cz' : { dictId:'encz', dirId:1 },
      'gecz.ge' : { dictId:'gecz', dirId:0 },
      'gecz.cz' : { dictId:'gecz', dirId:1 },
      'frcz.fr' : { dictId:'frcz', dirId:0 },
      'frcz.cz' : { dictId:'frcz', dirId:1 },
      'itcz.it' : { dictId:'itcz', dirId:0 },
      'itcz.cz' : { dictId:'itcz', dirId:1 },
      'spcz.sp' : { dictId:'spcz', dirId:0 },
      'spcz.cz' : { dictId:'spcz', dirId:1 },
      'rucz.ru' : { dictId:'rucz', dirId:0 },
      'rucz.cz' : { dictId:'rucz', dirId:1 },
      'lacz.la' : { dictId:'lacz', dirId:0 },
      'lacz.cz' : { dictId:'lacz', dirId:1 },
      'eocz.eo' : { dictId:'eocz', dirId:0 },
      'eocz.cz' : { dictId:'eocz', dirId:1 },
      'eosk.eo' : { dictId:'eosk', dirId:0 },
      'eosk.sk' : { dictId:'eosk', dirId:1 },
      'plcz.pl' : { dictId:'plcz', dirId:0 },
      'plcz.cz' : { dictId:'plcz', dirId:1 }
    },
    //---
    labels: ['lbl_en','lbl_ge','lbl_fr','lbl_it','lbl_sp','lbl_ru','lbl_la','lbl_eo']
  };
  
  //reverse mapping...
  //idx -> [lbl, dictId, dirId]
  this.logicMap['idxs'] = new Array();
  for(var id in this.logicMap['ids']) {
    var rec = this.logicMap['ids'][id];
    this.logicMap['idxs'][rec.idx] = { lbl:rec.lbl, dictId: id, dirId: 0 };
    this.logicMap['idxs'][rec.idx + 1] = { lbl:rec.lbl, dictId: id,  dirId: 1 };
  }
}

function MLD_setJs() {
  var elem = this.getElement('js');
  eval('elem.value = "1"');
}

function MLD_getDictionaryLabel(dictId) {
  return this.logicMap['ids'][dictId].lbl;
}

function MLD_getElement(name) {
  var elem = this.elements[name];
  return elem;
}

function MLD_getDictListIdx(dictId, dirId) {
  if(!dictId) dictId = this.dictId;
  if(!dirId) dirId = this.dirId;
  var idx = this.logicMap['ids'][dictId].idx;
  return idx + dirId;
}

//---
function MLD_setDictionaryId(dictId) {
  this.dictId = dictId;
  this.lastVcb = null;
  
  var rec = this.logicMap['ids'][dictId];
  if(rec) {
    var lbl = this.logicMap['labelRight'][rec.lblRightIdx];
    var elem = this.getElement('lbl_cz');
    eval('elem.innerHTML = lbl');
    if(rec.lblLeftIdx > 0) {
      lbl = this.logicMap['labelLeft'][rec.lblLeftIdx];
      var elem = this.getElement(rec.lbl);
      eval('elem.innerHTML = lbl');
    }
  }
}

function MLD_setDirectionId(dirId) {
  this.dirId = dirId;
  var elem = this.getElement('cz_sep');
  var s = (dirId == 0) ? '&gt;&gt;' : '&lt;&lt;';
  eval('elem.innerHTML = s');
  this.lastVcb = null;
}

//---
//low level...
function MLD_highlightLabel(dictId) {
  //reset...
  var labels = this.logicMap['labels'];
  for(var i=0; i<labels.length; i++) {
    var elem = this.getElement(labels[i]);
    eval("elem.style.fontWeight = 'normal'");
  }
  //highlight...
  var lbl = this.getDictionaryLabel(dictId);
  var elem = this.getElement(lbl);
  eval("elem.style.fontWeight = 'bold'");
}

function MLD_selectDictList(dictId, dirId) {
  if(!dictId) dictId = this.dictId;
  if(!dirId) dirId = this.dirId;
  var idx = this.getDictListIdx(dictId, dirId);
  var list = this.getElement('dictdir');
  list.selectedIndex = idx;
}

//---
//logic...
function MLD_setDictionaries(dictIds, dirId, force) {
  var dictId;
  for(var i=dictIds.length-1; i>=0; i--) {
    dictId = dictIds[i];
    if(this.dictId == dictId) break;
  }
  this.setDictionary(dictId, dirId, force);
}

function MLD_setDictionary(dictId, dirId, force) {
  if(force == undefined) force = false;
  if(dirId == undefined) dirId = this.dirId;
  
  if((this.dictId != dictId) || (force)) {
    if(!dictId) dictId = this.dictId;
    this.setDictionaryId(dictId);
    this.setDirectionId(dirId);
    this.highlightLabel(dictId);
    this.selectDictList(dictId, dirId);
  }
  else {
    this.turnDictionary(dictId, dirId);
  }
}

function MLD_turnDictionary(dictId, dirId) {
  if(!dictId) dictId = this.dictId;
  if(!dirId) dirId = this.dirId;
  
  this.setDictionaryId(dictId);
  dirId = (dirId + 1) % 2;
  this.setDirectionId(dirId);
  this.selectDictList(dictId, dirId);
}

function MLD_onDictDirChange() {
  var list = this.getElement('dictdir');
  var idx = list.selectedIndex;
  var val = list[idx].value;
  
  if(val == '+') {  idx++; list.selectedIndex = idx; }
  else if(val == '-') { idx--; list.selectedIndex = idx; }
  
  var rec = this.logicMap['idxs'][idx];
  if((this.dictId != rec.dictId) || (this.dirId != rec.dirId)) {
    this.setDictionaryId(rec.dictId);
    this.setDirectionId(rec.dirId);
    this.highlightLabel(rec.dictId);
    this.selectDictList(rec.dictId, rec.dirId);
  }
}

function MLD_onLinesChange() {
  var list = this.getElement('lines');
  var idx = list.selectedIndex;
  var val = list[idx].value;
  
  if(val == '+') {  idx++; list.selectedIndex = idx; }
  else if(val == '-') { idx--; list.selectedIndex = idx; }  
}

function MLD_onSubmit(e) {
  this.setJs();
  this.setVocable();
  this.setFocus(true);
  if(this.shiftKey) {
    this.shiftKey = false;
    this.turnDictionary();
  }
}

function MLD_submit(e) {
  this.setJs();
  eval("this.form.submit()");
}

function MLD_onKeyDown(e) {
  e = window.event || e;
  //16:shift
  if(e.keyCode == 16) this.shiftKey = true;
}

function MLD_onKeyUp(e) {
  e = window.event || e;
  
  if(e.keyCode == 16) this.shiftKey = false;

  //27:esc | 33:page up | 34:page down
  if(((e.keyCode == 27) ||(e.keyCode == 33) || (e.keyCode == 34))) {
    this.turnDictionary();
    this.setFocus(false);
    this.submit();
    return;
  }
  
  //ctrl + 37:left | 38:up | 39:right | 40:down
  if((e.ctrlKey) && ((e.keyCode == 38) || (e.keyCode == 40))) {
    this.turnDictionary();
    this.setFocus(false);
    return;
  }
  
  //alt + 1 | 2 | 3 | 4 | 5 | 6 | 7
  /*
  if(e.altKey) {
    if(e.keyCode == 49) setDictionary(['encz']);
    else if(e.keyCode == 50) setDictionary(['gecz']);
    else if(e.keyCode == 51) setDictionary(['frcz']);
    else if(e.keyCode == 52) setDictionary(['itcz']);
    else if(e.keyCode == 53) setDictionary(['spcz']);
    else if(e.keyCode == 54) setDictionary(['rucz']);
    else if(e.keyCode == 55) setDictionary(['lacz']);
    else if(e.keyCode == 56) setDictionary(['eocz','eosk','plcz']);
    return;
  }
  */
}

function MLD_onTrnWindow() {
  this.trn_window = (this.trn_window + 1) % 2;
  if(this.trn_window) {
    eval("this.parent.output.location = this.docPrefix + 'output_editor.html'");
  }
  else {
    eval("this.parent.output.location = this.docPrefix + 'output.html'");
  }
}

//---
//set...
function MLD_setFocus(flgSelect) {
  var elem = this.getElement('vcb');
  if(flgSelect) eval('elem.select()');
  eval('elem.focus()');
}

function MLD_setVocable(vcb) {
  var update = false;
  var elem = this.getElement('vcb');
  
  if(!vcb) eval('vcb = elem.value');
  else { if(decodeURI) vcb = decodeURI(vcb); update = true; }

  var vcb_trim = trim(vcb);
  if(vcb_trim != vcb) update = true;
  vcb = vcb_trim;
  
  if((vcb.length > 1) && (this.lastVcb == vcb)) {
    vcb = vcb.substring(0, vcb.length - 1);
    update = true;
  }
  this.lastVcb = vcb;
  if(update) eval('elem.value = vcb');
  
  return vcb;
}

function MLD_setLines(line) {
  var list = this.getElement('lines');
  
  var idx = -1;
  var len;
  eval('len = list.length');
  for(var i=0; i<len; i++) {
    var val;
    eval('val = list[i].value');
    if(line == val) { idx = i; break; }
  }
  if(idx >= 0) eval('list.selectedIndex = idx');
}

function MLD_setDictDir(dictdir) {
  if(!dictdir) return;
  var rec = this.logicMap['dictdir'][dictdir];
  this.setDictionary(rec.dictId, rec.dirId, true);
}

function MLD_isEqualWithLast(lastTrnVcb, lastTrnDictdir) {
  var equal = false;
  
  if((this.lastTrnVcb == lastTrnVcb) && (this.lastTrnDictdir == lastTrnDictdir)) equal = true;
  this.lastTrnVcb = lastTrnVcb;
  this.lastTrnDictdir = lastTrnDictdir;
  return equal;
}

//---
//cookies...
function MLD_setCookies() {
  //dictdir...
  var list = this.getElement('dictdir');
  var idx, val;
  eval('idx = list.selectedIndex');
  eval('val = list[idx].value');
  if(val) this.cookie.setCookie('dictdir', val);
  
  //lines...
  list = this.getElement('lines');
  eval('idx = list.selectedIndex');
  eval('val = list[idx].value');
  if(val) this.cookie.setCookie('lines', val);
}

function MLD_getCookies() {
  var dictdir = this.cookie.getCookie('dictdir');
  if(dictdir) this.setDictDir(dictdir);
  var lines = this.cookie.getCookie('lines');
  if(lines) this.setLines(lines);
}

//----------------------------------------------------------------------
//static handlers...
function handleOnKeyUp(e) {
  mld.onKeyUp(e);
}

function handleOnKeyDown(e) {
  mld.onKeyDown(e);
}

function setDictionary(dictIds) {
  mld.setDictionaries(dictIds);
  mld.setCookies();
  mld.setFocus(true);
  mld.submit();
}

function turnDictionary() {
  mld.turnDictionary();
  mld.setCookies();
  mld.setFocus(true);
  mld.submit();
}

function handleOnDictdirChange() {
  mld.onDictDirChange();
  mld.setCookies();
  mld.setFocus(true);
  mld.submit();
}

function handleOnLinesChange() {
  mld.onLinesChange();
  mld.setCookies();
  mld.setFocus(true);
  mld.submit();
}

function handleOnSubmit() {
  mld.setCookies();
  mld.onSubmit();
}

function translateWindow() {
  mld.onTrnWindow();
}

function translate(vcb,dictdir) {
  var flg = mld.isEqualWithLast(vcb,dictdir);
  if(flg) vcb = null;
  mld.setVocable(vcb);
  mld.setDictDir(dictdir);
  mld.setFocus(true);
  mld.submit();
}

function retranslate() {
  mld.setFocus(true);
  mld.submit();
}
