var clipLeft_ot = 0;
var clipHeight_ow = 295;
var clipRight_ob = 710;
var topper = 0;
var lyrWidth_olh = 0;
var time,theTime,theHeight,DHTML;
var amount = 5;
var xToMove;
var Moved = 0;
var MoveBack = 0;
var CurrentPosition = 0;
var TotalPositions = 0;
var intTotalToMove = 0;
var MoveByAmmounts =new Array()
var boolNoMove = 0 //ok to move.
var intMinimumMove = 50

var intDirection = 0
var intAmountMoved = 0
var intOriginalPixelsPerUnit = 1
var intPixelsPerUnit = intOriginalPixelsPerUnit
/*MoveByAmmounts[0] = 0
MoveByAmmounts[1] = 100
MoveByAmmounts[2] = 120
MoveByAmmounts[3] = 80
TotalPositions = 3*/


addLoadEvent(setScroller);
addLoadEvent(vis);
addLoadEvent(GetCoOrds);


function hideLoadImage_hs(){
	var e = document.getElementById('hs_loadingimage')
	e.style.display = 'none'
}
function showLoadImage_hs(){
	var e = document.getElementById('hs_loadingimage')
	e.style.display = 'block'
}

function GetCoOrds(){
    Moved = 0;
    var parent1 = $('getMeStupid') //the table
    var xParent = getXPos(parent1)
    var thisElement = getNextSibling(parent1)
    xNext = xParent
    
    iArrayLoop = 1
	MoveByAmmounts[0] = 0
	intTotalDistance = xParent+clipRight_ob
	
	// this loop goes through table cells to find the first one off the screen to the RHS
	while ( xNext < xParent+clipRight_ob+intMinimumMove && getNextSibling(thisElement) != null){
        //alert("!")
        nextElement = getNextSibling(thisElement);
	    MoveByAmmounts[1] = getXPos(nextElement)-xParent-clipRight_ob;
	    xNext = getXPos(nextElement);
	    //alert (xNext)
	    var thisElement = nextElement
	}
	
	// this loop continues through the table cells finding further scroll points until the end of the table.
    while (getNextSibling(nextElement) != null){
        iArrayLoop += 1;
        //alert("!")
        nextElement = getNextSibling(nextElement);
        xLast = xNext
        xNext = getXPos(nextElement)
        //alert(xNext)
        MoveByAmmounts[iArrayLoop] = xNext-xLast;
    }
    TotalPositions = iArrayLoop;
    
    var varFirst = MoveByAmmounts[1];
    var varSecond = MoveByAmmounts[2];
	intTotalToMove = (lyrWidth_olh-clipRight_ob);
    //alert('intTotalToMove = ' + intTotalToMove + ' | varFirst = ' + varFirst + ' | varSecond = ' +  varSecond);
    

	//xToMove = xNext - clipRight_ob - xParent; //amount of pixels to move
	
	//WriteNote = $('debugnote');
	//WriteNote.innerHTML = 'xParent = ' + xParent + ' | xNext = ' + xNext + ' | xToMove ' + xToMove + ' | Moved = ' + Moved;
}

// helper function to get only elements (fixes firefox bug!) http://www.agavegroup.com/?p=32
function getNextSibling(startBrother){
    if (startBrother.nextSibling != null){
        endBrother=startBrother.nextSibling;
        while(endBrother && endBrother.nodeType!=1){
                endBrother = endBrother.nextSibling;
                }
            return endBrother;
       }
}









function scrollayer(layername,direction)
{

	intDirection = direction
    if (!DHTML) return;
	thelayer = new getObj(layername);
	if (!thelayer) return;
	
	CurrentPosition += direction
	if (CurrentPosition > TotalPositions){
	    CurrentPosition = TotalPositions;
	    boolNoMove = 1
	}
	if (CurrentPosition < 0) {
	    CurrentPosition = 0;
	    boolNoMove = 1
	}
	
	//GetCoOrds();
	intThisMove = MoveByAmmounts[CurrentPosition]
	if (direction < 0){
	   intThisMove = MoveByAmmounts[CurrentPosition+1]
	}
	
	
	if ((boolNoMove != 1) && (intTotalToMove > 0)){
	    realscroll();
	}
	boolNoMove = 0
	
}

function stopScroll()
{
	if (time) clearTimeout(time);
	intPixelsPerUnit = intOriginalPixelsPerUnit;
	//intAmountMoved = 0;
	//alert(intPixelsPerUnit)
}




function realscroll(){
	
	if (!DHTML) return;
	
	intScrollMax = 20;
	
	if (intDirection < 0){
		if (intAmountMoved==0){
			intPixelsPerUnit = intPixelsPerUnit*(-1);
		}
		//alert(intPixelsPerUnit)
		intPixelsPerUnit = (intPixelsPerUnit - 1)
		//alert(intPixelsPerUnit)
	}
	else{
		intPixelsPerUnit = (intPixelsPerUnit + 1);
	}
	
	if (intPixelsPerUnit > intScrollMax){
		intPixelsPerUnit = intScrollMax;
	}
	if (intPixelsPerUnit < intScrollMax*(-1)){
		intPixelsPerUnit = intScrollMax*(-1);
	}
	
	var intUnitOfTime = 10;
	
	
	
	
	//WriteNote = $('WriteNote');
	//WriteNote.innerHTML = 'intAmountMoved = ' + intAmountMoved + ' | intTotalToMove = ' + intTotalToMove + ' | topper = ' + topper + ' | clipLeft_ot = ' + clipLeft_ot + ' | clipRight_ob = ' + clipRight_ob + ' | lyrWidth_olh = ' + lyrWidth_olh;
	
	if (clipLeft_ot < 0 || ((intAmountMoved+intPixelsPerUnit >= intTotalToMove) && intDirection > 0))
	{
		if (clipLeft_ot < 0){
			//alert("L")
			topper = 0;
			intAmountMoved = 0;
			clipLeft_ot = 0;
		}
		else{
			//alert("R")
			//alert(intAmountMoved + ' ' + intTotalToMove )
			//clipLeft_ot = lyrWidth_olh;
			//clipRight_ob = 0;
			
			topper = (intTotalToMove*-1);
			
			//topper += intPixelsPerUnit;
			//clipRight_ob = intAmountMoved;
			clipRight_ob = lyrWidth_olh;
			
			
		}
		
		//clipLeft_ot -= intPixelsPerUnit;
		//clipRight_ob -= intPixelsPerUnit;
		//topper += intPixelsPerUnit;
		if (document.getElementById || document.all)
		{
			thelayer.style.left = topper + 'px';
		}
		else if (document.layers)
		{
			thelayer.style.left = topper;
		}
		
		//WriteNote.innerHTML = 'intAmountMoved = ' + intAmountMoved + ' | intTotalToMove = ' + intTotalToMove + ' | topper = ' + topper + ' | clipLeft_ot = ' + clipLeft_ot + ' | clipRight_ob = ' + clipRight_ob + ' | lyrWidth_olh = ' + lyrWidth_olh;
		return;
	}
	
	intAmountMoved += intPixelsPerUnit
	clipLeft_ot += intPixelsPerUnit;
	clipRight_ob += intPixelsPerUnit;
	topper -= intPixelsPerUnit;
	
	if (document.getElementById || document.all)
	{
		thelayer.style.left = topper + 'px';
	}
	else if (document.layers)
	{
		thelayer.style.left = topper;
	}
	
	time = setTimeout('realscroll()',intUnitOfTime);
	//Moved += amount
        
        //GetCoOrds();
   
    
}


function setScroller(){
	DHTML = (document.getElementById || document.all || document.layers)
	if (!DHTML) return;
	var x = new getObj('exampleScroller');
	if (document.layers)
	{
		lyrWidth_olh = x.style.clip.right;
		lyrWidth_olh += 20;
		//x.style.clip.top = 0;
		//x.style.clip.left = clipLeft_ot;
		//x.style.clip.right = clipRight_ob;
		//x.style.clip.bottom = clipHeight_ow;
	}
	else if (document.getElementById || document.all)
	{
		lyrWidth_olh = x.obj.offsetWidth;
		var clipstring = 'rect('+0+'px,'+clipRight_ob+'px,'+clipHeight_ow+'px,'+clipLeft_ot+'px)';
		//x.style.clip = clipstring;
	}
	
}

function vis(val)
{
	if (!DHTML) return;
	var f = new getObj('exampleScroller');
	f.style.visibility = 'visible';
}

function getObj(name)
{
   // alert(name)
  if (document.getElementById)
  {
    this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
    this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

function getXPos(el) 
{
	if (document.getBoxObjectFor) 
	{ 
		var bo = document.getBoxObjectFor(el); 
		return bo.x; 
	} 
	else if (el.getBoundingClientRect) 
	{ 
		var rect = el.getBoundingClientRect(); 
		return rect.left; 
	} 
	else
	{
		var x;
		x = 0;
		
		while(el != null) 
		{
			if(el.offsetLeft != null && typeof(el.offsetLeft) == "number")
				x += el.offsetLeft;	

			el = el.offsetParent;
		}
			
		return x;
	}
}