//
//                Copyright (c) 2004 Smartlink Corp.
//                       All rights reserved.
//

//////////////////////////////////////////////////////////////////////////////
//
// ContentCtrl
//

function newContentCtrl (listener) {
   return new ContentCtrl (listener);
}

function ContentCtrl (listener) {
   this.translCtrl = new TranslCtrl (document.getElementById ("translCtrl"), listener);
   this.listCtrl   = new ListCtrl   (document.getElementById ("listCtrl"), listener);
   this.errMsgCtrl = document.getElementById ("errMsg");
   
   this.selectView   = contentSelectView;
   this.setContent   = contentSetContent;
   this.clearContent = contentClearContent;
   this.setItems     = contentSetItems;
   this.setError     = contentSetError;
}

function contentSelectView (view) {
   switch (view) {
      case "html":
         hideControl (this.errMsgCtrl);
         this.listCtrl.hide ();
         this.translCtrl.show ();
         break;
      case "list":
         hideControl (this.errMsgCtrl);
         this.translCtrl.hide ();
         this.listCtrl.show ();
         break;
      case "error":
         this.translCtrl.hide ();
         this.listCtrl.hide ();
         showControl (this.errMsgCtrl);
         break;
   }
}

function contentSetContent (data) {
   this.selectView ("html");
   this.translCtrl.setContent (data);
}

function contentClearContent () {
   this.selectView ("html");
   this.translCtrl.clearContent ();
}

function contentSetItems (className, items, selItem) {
   this.selectView ("list");
   this.listCtrl.setItems (className, items, selItem);
}

function contentSetError (text) {
   this.selectView ("error");
   this.errMsgCtrl.innerHTML = text;
}

//////////////////////////////////////////////////////////////////////////////
//
// ListCtrl
//

function ListCtrl (ctrl, listener) {
   this.show = function () { showControl (this.ctrl) };
   this.hide = function () { hideControl (this.ctrl) };
   this.listener = listener;
   this.ctrl = ctrl;
   this.ctrl.ref = this;
   this.ctrl.onclick = listOnClick;
   this.ctrl.ondblclick = listOnDblClick;
   this.setItems = listSetItems;
}

function listSetItems (className, items, selItem) {
   this.ctrl.innerHTML = "";
   if (items == null)
      return;
   
   var doc = document;
   for (var i = 0; i < items.length; ++i) {
      var li = doc.createElement ("LI");
      li.innerHTML = htmlEncode (items [i]);
      if (className)
         li.className = className;
      if (i == selItem)
         li.className = "selected";
      this.ctrl.appendChild (li);
   }
   if (selItem) {
      var liSel = this.ctrl.childNodes [selItem];
      scrollToElement (liSel);
   }
//li.innerHTML = "<iframe frameborder='0' style='width:0px;height:0px;visibility:hidden;' src='links.html'></iframe>";
}

function listOnClick (e) {
   if (!e)
      e = window.event;

   var liSel = (e.srcElement ? e.srcElement : e.target);
   if (liSel.tagName != "LI")
      return;

   var listCtrl = this.ref;
   if (liSel.className == "link") {
      listCtrl.listener.onLookup (liSel.innerHTML, "", "");
      return;
   }
   
   liSel.className = "selected";
   for (var i = 0; i < listCtrl.ctrl.childNodes.length; ++i) {
      var li = listCtrl.ctrl.childNodes [i];
      if (li.className == "selected" && li != liSel)
         li.className = "";
   }
}

function listOnDblClick (e) {
   if (!e)
      e = window.event;
   var li = (e.srcElement ? e.srcElement : e.target);

//////////////////////////////////////////////////////////////////// FIREFOX /////////////////////////////
   if (!browser.ie)
       li.innerText = window.getSelection ().toString ();
//////////////////////////////////////////////////////////////////// FIREFOX /////////////////////////////

   if (li.tagName != "LI")
      return;
      
   this.ref.listener.onLookup (li.innerText);
}

//////////////////////////////////////////////////////////////////////////////
//
// TranslCtrl
//

function TranslCtrl (ctrl, listener) {
   this.listener = listener;
   this.ctrl = ctrl;
   this.ctrl.ref = this;
   this.ctrl.ondblclick = translOnDblClick;
   this.show = function () { showControl (this.ctrl) };
   this.hide = function () { hideControl (this.ctrl) };
   var divs = ctrl.getElementsByTagName ("DIV");
   this.translation   = divs [0];
   this.phrases       = divs [1];
   this.htmlNoResults = this.translation.innerHTML;
   this.htmlLoading   = this.phrases.innerHTML;
   this.translation.innerHTML = "";
   this.phrases.innerHTML = "";
   this.phrasesBar = ctrl.getElementsByTagName ("SPAN") [0];
   this.phrasesBar.ref = this;
   this.phrasesBar.setState = phrasesSetState;
   this.phrasesBar.collapse = function () { this.setState ('collapsed') }
   this.phrasesBar.expand = function () { this.setState ('expanded') }
   this.phrasesBar.onclick = phrasesOnClick;
   this.setContent = translSetContent;
   this.clearContent = translClearContent;
   this.setTranslation = translSetTranslation;
   this.setPhrases = translSetPhrases;
   this.expandPhrases = function () { this.phrasesBar.expand () }
   this.onPhrasesToggled = translOnPhrasesToggled;
   this.data = new Object;
}

function translSetContent (data) {
   if (!data) { // Just show "Loading... Please wait"
      this.translation.innerHTML = this.htmlLoading;
      this.phrases.innerHTML = "";
      hideControl (this.phrasesBar);
      return;
   }

   if (!data.dicID)
      return;
      
   if (data.dicID != this.data.dicID || data.lang != this.data.lang)
      this.clearContent ();
   this.data.dicID = data.dicID;
   this.data.lang  = data.lang;
      
   this.setTranslation (data.translation);
   this.setPhrases (data.phrases);

   if (data.phrases && !data.translation)
      this.expandPhrases ();
   if (data.phrases == "" && data.translation == "")
      this.translation.innerHTML = this.htmlNoResults;

}

function translClearContent () {
   hideControl (this.phrasesBar);
   this.translation.innerHTML = "";
   this.phrases.innerHTML = "";
}

function translSetTranslation (html) {
   if (html == null)
      return;
   this.translation.innerHTML = html + "<iframe frameborder='0' style='width:0px;height:0px;visibility:hidden;' src='links.html'></iframe>";
   scrollToElement (this.translation);
}

function translSetPhrases (html) {
   if (html == null) {
      this.phrases.innerHTML = "";
      this.phrasesBar.collapse ();
      showControl (this.phrasesBar);
   }
   else if (html == "") {
      this.phrases.innerHTML = this.htmlNoResults;
      this.phrasesBar.collapse ();
      hideControl (this.phrasesBar);
      hideControl (this.phrases);
   }
   else {
      this.phrases.innerHTML = html + "<iframe frameborder='0' style='width:0px;height:0px;visibility:hidden;' src='links.html'></iframe>";
      showControl (this.phrasesBar);
   }
}

function translOnPhrasesToggled (state) {
   if (state == "expanded") {
      if (!this.phrases.innerHTML) {
         this.phrases.innerHTML = this.htmlLoading;
         this.listener.onTabSelected ("phrases");
      }
   }
}

function translOnDblClick (e) {
   var text;

   if (document.selection) {
      text = document.selection.createRange ().text;

      if (text.indexOf ("\n") >= 0)
         text = text.substr (0, text.indexOf ("\n"));
      document.selection.empty (); // to prevent selection of DIV[@id='translCtrl']
   }
   else if (window.getSelection) {
      text = window.getSelection ().toString ();
//alert("text : " + text);
   }

   if (this.ref && text)
      this.ref.listener.onLookup (text);      
}

function phrasesSetState (state) {
   var imgNode = this.getElementsByTagName ("IMG") [0];
   var newState = state;
   if (state != "collapsed" && state != "expanded") {
      newState = (imgNode.src.indexOf ("collapsed") >= 0
                     ? "expanded" : "collapsed");
   }
   imgNode.src = "images/" + newState + ".gif";
   showControl (this.ref.phrases, newState == "expanded");
   return newState;
}

function phrasesOnClick (e) {
   var newState = this.setState ('toggle');
   if (newState == "expanded")
      scrollToElement (this.firstChild);
   this.ref.onPhrasesToggled (newState);
}

