<?xml version="1.0" encoding="ISO-8859-1"?>
<public:component xmlns:public="urn:HTMLComponent" id="HoverGrid" lightweight="true">
/* 
    Originally created by Luc Peeters for EDS
    
    Modified On 22 december 2005 By Luc Peeters
    - The select widget (INPUT or A) can be on the first or the last column now.
    - A new public property is added : 
        - SCROLLTO : this scrolls on document ready automatically to the highlited row (default is False).
        
    For full info on HTC : http://msdn.microsoft.com/workshop/components/htc/reference/htcref.asp
    
    
    2006-06-28: Ported to moz-behaviors emulator for FireFox by Fred Keultjes (EDS)
*/

<public:attach event="ondocumentready" handler="DoInit" />
<public:attach event="onkeydown" handler="DoKeyPress" />
<public:attach event="onmouseover" handler="mMouseOver" />
<public:attach event="onmouseout" handler="mMouseOut" />
<public:attach event="onclick" handler="DoClick" />

<public:property name="SCROLLTO" value="False" get="get_SCROLLTO" put="put_SCROLLTO"/>


<script type="text/javascript">
//<![CDATA[

// sorry, the behavior doensn't expose 'this' in handlers, so we have to store it
var Me = this;

var iFocusedRow = null;
var iFocusedRowIndex = 1;
var iFocusedSelectWidget = null;

var iScrollTo;

function get_SCROLLTO()
{
  return iScrollTo;
}
function put_SCROLLTO(v)
{
  iScrollTo = v;
}

function DoInit()
{
///*
//    if (iScrollTo == "False")
//      return; 
//    
//    /* dzyjq4: Note: scrollIntoView is Microsoft-specific */
//    
//    /* czt8rv : sometimes we get an error that Me.rows is null */
//    if(Me.rows == undefined) return;    
//    
//    /*
//    for ( i = 0; i<=Me.rows.length-1; i++)
//    {
//        if ( Me.rows.item(i).className.indexOf("gridSelItem")>=0 )
//        {
//        
//            Me.rows.item(i).scrollIntoView( false );
//            break;
//        }
//    }
//    */
//    
//    */
}

function mMouseOver()
{

    var obj = window.event.srcElement;
    if ( !obj )
	  return;
	
	var row = GetFiringTableRow();
	if ( row && row != iFocusedRow )
	{
	
	  var Widget = GetSelectWidget( row );
		if ( Widget )
		{
			fnHighLite ( row );
		}
	}
}
function mMouseOut()
{
	var obj = window.event.srcElement;
	if ( !obj )
	  return;
	
	if( iFocusedRow ) 
	{
		fnLowLite( iFocusedRow )
	}
}

function fnHighLite(aRow)
{
    aRow.style.cursor = "hand";
	
//  if( !aRow.bc )
//	{
//		aRow.bc = aRow.className;
//	}
//	 aRow.className = aRow.className.indexOf("gridSelItem")>=0 ? "gridSelHover" : "gridHover";

//	
	/*
	JB: This section automatically focusses the control you are hovering on => We dont want this...
    var widget = GetSelectWidget( aRow );
	if( widget )
	{
		try
		{
			if (widget != iFocusedSelectWidget)
			{
			  widget.focus();
			  iFocusedSelectWidget = widget;
			}
		}
		catch (ex)
		{
		}
	}
	*/

//	if ( iFocusedRow )
//	  fnLowLite ( iFocusedRow );		
//	  
	// set new status
	iFocusedRow = aRow;
	iFocusedRowIndex = aRow.rowIndex;
	
}

function fnLowLite(aRow) {

//	aRow.className = aRow.bc;
//	aRow.bc = null;
//	
	iFocusedRow = null;
}

function DoClick()
{    
//	var obj = window.event.srcElement;
//	if (!obj) return false;
//	
//	var row = GetFiringTableRow();
//    var SelectWidget = GetSelectWidget(row);
//    if (!SelectWidget) return false;
//	if (SelectWidget != obj && (obj.tagName == "INPUT" || obj.tagName == "A" || obj.tagName == "TEXTAREA" )) return false;
//					
//	SelectWidget.click();		
}

function DoKeyPress()
{  		
	switch ( window.event.keyCode )
	{
		case 13:
		    if(!Me.rows == undefined)
		    {
		        var widget = GetSelectWidget ( Me.rows.item(iFocusedRowIndex) );
			    if ( widget )
			    {													 
				    widget.click();
			    }
            }
			window.event.returnValue = false;
			break;
		case 38:
			PreviousRow();
			window.event.returnValue = false;
			break;
		case 40:
			NextRow();
			window.event.returnValue = false;
			break;	
	}
  
}
function PreviousRow()
{
    if(Me.rows == undefined) return;
	var newIndex = iFocusedRowIndex-1;
	if (newIndex == 0 )
	  newIndex = Me.rows.length - 1;
	  
	var newRow = Me.rows.item(newIndex);
	  
	var obj = GetSelectWidget( newRow );
	if( obj )
	{
		fnHighLite( newRow );
		newRow.scrollIntoView( false );
	}
}
function NextRow()
{
    if(Me.rows == undefined) return;
    var newIndex = iFocusedRowIndex+1;
    if( newIndex >= Me.rows.length )
        newIndex = 1;
    
    var newRow = Me.rows.item(newIndex);
	 
	var obj = GetSelectWidget( newRow ); 
	if( obj )
	{
		fnHighLite( newRow );
		newRow.scrollIntoView ( false );
	}
}

function GetSelectWidget( pRowToSearch )
{   
    if( !pRowToSearch )
      return null;
    
    // try first cell
    var Widget = SearchCellForWidget( 0, pRowToSearch );
    if ( ! Widget )
      // and try last cell
      Widget = SearchCellForWidget( pRowToSearch.cells.length - 1, pRowToSearch ); 
    
    return Widget;
}
function SearchCellForWidget( pColumn, pRowToSearch )
{
    var childNodesOfCell = pRowToSearch.cells.item(pColumn).childNodes;
    
    var index = 0;
    while( index<childNodesOfCell.length )
    {
      var node = childNodesOfCell.item(index);
      if(  node.tagName == null )
      {
        // assume it is a Text object of Mozilla. Skip it;
      }
      else if( node.tagName == "INPUT" || node.tagName == "A" || node.tagName == "SELECT")
      {
          return node;
      }
      else
      {
        return null;
      }
        
      index++;
    }
    
    return null;
}

function GetFiringTableRow()
{
	var obj = window.event.srcElement;
	if ( !obj ) return null;
	
	// if Ajax control, return null
	if(obj.className.substring(0,4) == "ajax") return null;
	
	// JB: parent up till we find a Table with the className set to "HoverGrid"
	// This is to avoid tables from usercontrols	
	var lLastRow = null;
	while(true)
	{
	    if(obj.tagName == "TR") 
	    {
	        // Remember last row
	        lLastRow = obj;
	    }
        else if(obj.tagName == "TABLE")
        {
            if(obj.className == "HoverGrid")
            {
                // We are at the GridTable, return the last row we passed
                return lLastRow;        
            }
            else
            {
                //return null;   
            }
        }
	    
	    // If we arrive at the top, return null
	    if(obj.parentNode == null) return null;
	    
	    // Parent up
	    obj = obj.parentNode;
	}	
}
//]]>
</script>
</public:component>