/*
 * ============================================================================
 *                   The ProQuality Inc.
 *         Rapid Efficent Low cost And Xtreme (RELAX)
 *	           Advanced Solutions Framework (ASF)
 *                  Cristian Teodorescu
 *                      Version 6.0
 * ============================================================================
 * 
 *    Copyright (C) 1999 The ProQuality Inc. Foundation. All rights reserved.
 * 
 * The use of this software in any form is strictly prohibited unless you have 
 * purchased a licence from ProQuality Inc. or you have a software agreement with 
 * ProQuality Inc.
 * 
 * Redistributions of source code must retain copyright statements
 * and notices. Redistributions must also contain a copy of this
 * document.
 *
  THIS SOFTWARE IS PROVIDED BY PROQUALITY AND CONTRIBUTORS ``AS IS'' AND
  ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PROQUALITY OR ITS
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  DAMAGE.
    
 *
 */
function Hover( inputNode ) {
  inputNode.hover = this;
  this.inputNode = inputNode;

  var nodeWrapper = new NodeWrapper( inputNode );
  
  // var value = inputNode.value;

  this.inputNode.style.width =  nodeWrapper.width();  
  var value = inputNode.ownerDocument.createTextNode( inputNode.value );
  this.nodeValue = value.nodeValue;  
  var paragraphNode = inputNode.ownerDocument.createElement( "SPAN" );
  paragraphNode.appendChild( value );
  this.textNode = value;
  paragraphNode.style.position = "absolute";
  paragraphNode.style.top = nodeWrapper.offsetTop() + 15;
  paragraphNode.style.left = nodeWrapper.offsetLeft() + 1;
  paragraphNode.style.wordWrap = "break-word"; // This works in IE but not Mozilla
  inputNode.onmouseover = handleHoverOnMouseOver; //display
  inputNode.onmouseout = handleHoverOnMouseOut; //hide
  inputNode.onkeyup = handleHoverOnKeyPress; //display
  inputNode.onfocus = handleHoverOnMouseOver;  //display
  inputNode.onblur = handleHoverOnMouseOut;  //hide
  decorate( paragraphNode );

  paragraphNode.style.display = "none";
  this.paragraphNode = paragraphNode;
  inputNode.parentNode.insertBefore( paragraphNode, inputNode );
}
Hover.prototype.display = hoverDisplay;
Hover.prototype.hide = hoverHide;
Hover.prototype.isTextTooWide = hoverIsTextTooWide;
Hover.prototype.updateText = hoverUpdateText;

function hoverDisplay() {
  if ( this.isTextTooWide() ) {
    this.paragraphNode.style.zIndex = 1;
  }
}

function hoverHide() {
  this.paragraphNode.style.display = "none";
}

function hoverIsTextTooWide() {
  var wrappedInputNode = new NodeWrapper( this.inputNode );
  var wrappedParagraphNode = new NodeWrapper( this.paragraphNode );
  this.paragraphNode.style.zIndex = -1 // TODO: need to calculate this relative to the main doc.
  this.paragraphNode.style.display = "block";
  this.paragraphNode.style.width = wrappedParagraphNode.width();
  if ( wrappedInputNode.width() < wrappedParagraphNode.width() ) {
    return true;
  } else {
    return false;
  }
}

function hoverUpdateText( textNode ) {
  this.paragraphNode.replaceChild( textNode, this.textNode );
  this.textNode = textNode;
}

function attachHoverListeners( document ) {
  var inputNodes = document.getElementsByTagName( "INPUT" );
  var inputNode;
  for ( var i = 0; i < inputNodes.length; ++i ) {
    inputNode = inputNodes[ i ];
    inputNode.onfocus = initializeHover;
    inputNode.onmouseover = initializeHover;
  }
}

function initializeHover( event ) {
  if ( !document.NODE_WRAPPER_LOADED ) {
    return;
  }
  var inputNode = getSourceNode( event );
  if ( !inputNode.hover  ) {
    inputNode.onfocus = null;
    inputNode.onhover = null;
    new Hover( inputNode );
    inputNode.onmouseover( event );
  }
}

function handleHoverOnMouseOut( event ) {
  var inputNode = getSourceNode( event );
  var hover = inputNode.hover;
  hover.paragraphNode.style.width = "auto";
  hover.hide();
}

function handleHoverOnMouseOver( event ) {
  var inputNode = getSourceNode( event );
  var hover = inputNode.hover;
  hover.display();
  // The following measures the width of the text versus the width of the input field. I don't know
  // how to duplicate this in Mozilla.
  // alert( wrappedInputNode.width() + " " + hover.inputNode.createTextRange().boundingWidth );
}
/*
To test the following, I need a mock event. The event needs a source node. The source node needs a hover, value and ownerDocument attribute.
*/
function handleHoverOnKeyPress( event ) {
  var inputNode = getSourceNode( event );
  var hover = inputNode.hover;
  var value = inputNode.value;
  textNode = inputNode.ownerDocument.createTextNode( value );
  hover.updateText( textNode );
  hover.display();
}

function decorate( node ) {
  node.style.borderWidth = "1px";
  node.style.borderRightColor = "#cccccc";
  node.style.borderBottomColor = "#cccccc";
  node.style.borderLeftColor = "#333333";
  node.style.borderTopColor = "#333333";
  node.style.borderStyle = "solid";
  node.style.padding = "2px";
  node.style.backgroundColor = "white";
  node.style.font= ".9em \"Lucida Grande\", Verdana, Geneva, Arial, sans-serif";
}
