//================================================================
//		Java script functions and routines for HandMadeNZ
//		Author:		Stephen Lewis
//					www.Shed22.com
//
//		   Date		Changes
//		28 Oct 04	Original
//		22 Sep 08	table highlighting
//===============================================================

//-----------------------------------------------
//	Add Events
//	where:	elm		= Element
//			evType	= Event Type
//			fn		= Function name
//-----------------------------------------------
function addEvent(elm, evType, fn) 
{

	// cross-browser event handling for IE5+, NS6 and Mozilla 
	if (elm.addEventListener)
	{ 
		elm.addEventListener(evType, fn, false); 
		return true; 
	}
	else if (elm.attachEvent) 
	{
		var r = elm.attachEvent('on' + evType, fn); 
		return r; 
	}
	else
	{
		elm['on' + evType] = fn;
	}
}


//------------------------------------------------------
//	Set Table highlight
//------------------------------------------------------
function setHighlight(table)
{
  if (!table) return;

  var TDs = table.getElementsByTagName("td");
  for(var i = 0; i<TDs.length; i++) {
    TDs[i].onmouseover = crossHairOn;
    TDs[i].onmouseout  = crossHairOff;
  }
	
}

function crossHairOn()
{
  highlighter(this, '#ddd');
  this.style.backgroundColor = '#ccc';
}
function crossHairOff()
{
  highlighter(this, '');
  this.style.backgroundColor = '';
}

function highlighter(cell, color)
{
  //alert( 'cell type parent' + cell.parentNode.tagName);
  cell.parentNode.style.backgroundColor = color;
  var table = getTable(cell);
  var col = table.getElementsByTagName("col");
  col[cell.cellIndex].style.backgroundColor = color;
}

function getTable(obj)
{
  while (obj && obj.tagName.toLowerCase() != 'table')
  {
    obj = getTable(obj.parentNode);
  }
  return obj;
}

function getScrollPos()
{
	var pos = 0;
	
	if (typeof window.pageYOffset != 'undefined')
	{
		pos = window.pageYOffset;
	}
	else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0)
	{
		pos = document.documentElement.scrollTop;
	} 
	else if (typeof document.body.scrollTop != 'undefined')
	{
		pos = document.body.scrollTop
	} 

	return pos;

}

// scroll event
var	lastPos = 0;

window.setInterval( function()
{
	var scrollPos = getScrollPos();
	var newPos = scrollPos + ((scrollPos - lastPos) / 20) + 67;
	//document.title =  " Top =" + scrollPos + " Prev =" + lastPos + " New =" + newPos; 
	lastPos = scrollPos;
	var strPos = newPos.toString() + "px";
	var sideBar = document.getElementById("sidebar");
	
	//alert("Here");
	sideBar.style.top = strPos;
	
}, 500 );

//------------------------------------------------------
//	Add event listeners based on type of fields
//------------------------------------------------------
function addListeners()
{

	// check for function support by browser
	if (!document.getElementById)
   	return;

	// check for function support by browser
	if (!document.getElementsByTagName)
   	return;	
		
	// Table highlighting
	var	tbl = document.getElementById('grid');
	setHighlight(tbl);
	
}


// set the Window onLoad event ...
addEvent(window, 'load', addListeners);
