/* Unified script for IE and Mozilla (NS, etc.). This is for OEA
   text frames. */

  var tgtClass = "word"
  var currentSel = null
  /* Make sure the initial index of the cssRule for span.word is 2 */
  var wordRuleIndex = 2
  var wordStyleEnabled = true
  var linkRuleIndex = 1
  var linkStyleEnabled = false

  /* Thanks to Brock Weaver at www.js-examples.com.
      Some adaptation for our purpose (it trims punctuation too). */
  String.prototype.trim = function() {
    var x=this;
    x=x.replace(/^\s*(.*)/, "$1");
    x=x.replace(/(.*?)\s*$/, "$1");
    x=x.replace(/^[\.\?,:;!\"\']*(.*)/, "$1");
    x=x.replace(/(.*?)[\.\?,:;!\"\']*$/, "$1");
    return x;
  }

  /* Install event listeners */
  function init() {
    if (document.addEventListener) {
      document.addEventListener("click", clickEvent, false)
      document.addEventListener("mouseover", movrEvent, false)
      document.addEventListener("mouseout", moutEvent, false)
    } else
    if (document.attachEvent) {
      document.attachEvent("onclick", clickEventIE)
      document.attachEvent("onmouseover", movrEventIE)
      document.attachEvent("onmouseout", moutEventIE)
    }
  }

  /* The only CSS style for words is a crosshair cursor.
     There are two ways of doing this, and both work in both
     IE and Moz, but the first seems more efficient in IE,
     the second in Moz. */
  function disableWordStyle() {
    if (document.styleSheets && wordStyleEnabled) {
      if (document.styleSheets[0].rules) {
        document.styleSheets[0].removeRule(wordRuleIndex)
      } else {
        document.styleSheets[0].cssRules[wordRuleIndex].style.cursor = "inherit"
      }
      wordStyleEnabled = false
    }
  }

  function enableWordStyle() {
    if (document.styleSheets && !wordStyleEnabled) {
      if (document.styleSheets[0].rules) {
        document.styleSheets[0].addRule("span.word", "cursor: crosshair", wordRuleIndex)
      } else {
        document.styleSheets[0].cssRules[wordRuleIndex].style.cursor = "crosshair"
      }
      wordStyleEnabled = true
    }
  }

  /* Same method for both browsers, since efficiency isn't an issue here. */
  function disableLinkStyle() {
    if (document.styleSheets && linkStyleEnabled) {
      if (document.styleSheets[0].rules) {
        document.styleSheets[0].rules[linkRuleIndex].style.cursor = "inherit"
      } else {
        document.styleSheets[0].cssRules[linkRuleIndex].style.cursor = "inherit"
      }
      linkStyleEnabled = false
    }
  }

  function enableLinkStyle() {
    if (document.styleSheets && !linkStyleEnabled) {
      if (document.styleSheets[0].rules) {
        document.styleSheets[0].rules[linkRuleIndex].style.cursor = "pointer"
      } else {
        document.styleSheets[0].cssRules[linkRuleIndex].style.cursor = "pointer"
      }
      linkStyleEnabled = true
    }
  }


  /* Look upwards in hierarchy for SPAN element of class tgtClass.
     Return element if found; otherwise null. */
  function findSpan(tgt) {
    var found = false;
    while (tgt.tagName != "HTML") {
      if (tgt.tagName == "SPAN" && tgt.className == tgtClass) {
        found = true;
        break;
      }
      tgt = tgt.parentNode
    }
    return found ? tgt : null
  }

  /* Search for a tag like <div class="text"> and return its ID;
     null if not found or has no ID (but this should not happen). */
  function findTextID(tgt) {
    var result = null
    while (tgt.tagName != "HTML") {
      if (tgt.tagName == "DIV" && tgt.className == "text") {
        result = tgt.id
        break
      }
      tgt = tgt.parentNode
    }
    return result
  }

  /* A selected word or phrase is one with a border around it.
     Clear away this border. */
  function clearSelection() {
    if (currentSel != null)
    {
      currentSel.style.borderStyle = "none"
      currentSel = null
    }
  }

  /* Action to take when certain kinds of <span> are clicked. Moz only */
  function clickEvent(evt) {
    clearSelection()
    var tgt = findSpan(evt.target)
    if (tgt != null && tgt.id) {
      currentSel = tgt
      currentSel.style.border = "salmon solid 1px"
      document.forms[0].action.value = tgtClass
      document.forms[0].textid.value = findTextID(tgt)
      document.forms[0].itemid.value = tgt.id
      document.forms[0].passage.value = getTextFrom(tgt)

      document.forms[0].submit()

      evt.stopPropagation()
    }
  }

  /* Mouseover and Mouseout events for certain kinds of <span>. Moz only.
     Think abt doing this via CSS later on. Too buggy in ver. 1.3. */

  function movrEvent(evt) {
    var tgt = findSpan(evt.target)
    if (tgt != null && tgt.id) {
      /* tgt.style.backgroundColor = "khaki" */
      tgt.style.backgroundImage = "url(parch.jpg)"
    }
  }

  function moutEvent(evt) {
    var tgt = findSpan(evt.target)
    if (tgt != null && tgt.id) {
      /* tgt.style.backgroundColor = "transparent" */
      tgt.style.backgroundImage = "none"
    }
  }

  /* Action to take when certain kinds of <span> are clicked. IE only */
  function clickEventIE() {
    clearSelection()
    var tgt = findSpan(event.srcElement)
    if (tgt != null && tgt.id) {
      currentSel = tgt
      currentSel.style.border = "salmon solid 1px"
      document.forms[0].action.value = tgtClass
      document.forms[0].textid.value = findTextID(tgt)
      document.forms[0].itemid.value = tgt.id
      document.forms[0].passage.value = getTextFrom(tgt)

      document.forms[0].submit()

      event.cancelBubble = true
    }
  }

  /* Mouseover events for certain kinds of <span>. IE only */
  function movrEventIE() {
    var tgt = findSpan(event.srcElement)
    if (tgt != null && tgt.id /* && tgt.className != "word" */) {
      /* tgt.style.backgroundColor = "khaki" */
      tgt.style.backgroundImage = "url(parch.jpg)"
    }
  }

  /* Mouseout events for certain kinds of <span>. IE only */
  function moutEventIE() {
    var tgt = findSpan(event.srcElement)
    if (tgt != null && tgt.id /* && tgt.className != "word" */) {
      /* tgt.style.backgroundColor = "transparent" */
      tgt.style.backgroundImage = "none"
    }
  }


  /* This emulates a standard hyperlink but actually talks to
     the servlet instead. */
  function oeaLink(el, linkTarget) {
    clearSelection()
    if (tgtClass == "link") {
      /* currentSel = el */
      /* currentSel.style.border = "salmon solid 1px" */
      var hashIndex = linkTarget.indexOf("#")
      if (hashIndex > 0) {
        var fname = linkTarget.substring(0, hashIndex)
	var aname = linkTarget.substring(hashIndex+1)
        document.forms[1].fname.value = fname
        document.forms[1].aname.value = aname

	document.forms[1].submit()

      }
    }
  }

  /* Recursive helper for getTextFrom */
  function concatFromNode(n, s) {
    var ss = s
    /* If this is a text node */
    if (n.nodeType == 3) {
      return ss + n.nodeValue
    }
    for (var m = n.firstChild; m != null; m = m.nextSibling) {
      if (m.nodeType == 1 && (m.className == "lnbox" || m.className == "hybox")) {
        continue
      }
      ss = concatFromNode(m, ss)
    }
    return ss
  }

  /* Returns the textual content of an element (and any nested elements). */
  function getTextFrom(n) {
    var s = ""
    return concatFromNode(n, s).trim()
  }
