
/*
 
*/ 

 var functions = {};
 var buttons = {};

 functions["contRight"] = new animSequence(0,0, 1,0, 'px','%', 10,50, 'continuous');
 functions["contDown"] = new animSequence(0,0, 0,1, 'px','%', 10,50, 'continuous');
 functions["contDownRight"] = new animSequence(0,0, 2,1, 'px','%', 10,50, 'continuous');

 functions["slideRight"] = new animSequence(0,0, 190,0, 'px','%', 10,50, 'single');
 functions["slideLeft"] = new animSequence(190,0, 0,0, 'px','%', 10,100, 'single');

 functions["bounceRight"] = new animSequence(0,0, 190,0, 'px','%', 10,50, 'bouncer',50);
 functions["bounceLeft"] = new animSequence(190,0, 0,0, 'px','%', 10,50, 'bouncer',50);	

 functions["repeatRight"] = new animSequence(0,0, 190,0, 'px','%', 10,50, 'repeater',1000);
 functions["repeateLeft"] = new animSequence(190,0, 0,0, 'px','%', 10,50, 'repeater',1000);	

 functions["stop"] = new animSequence(190,0, 0,0, 'px','%', 10,50, 'stop');


 function attachLinkEvents(selector,onAnim,offAnim,mode,idString){
	 if(idString==null || idString==''){
		 idString = selector;
		 idString = idString.replace(' ','');
		 idString = idString.replace('#','');
		 idString = idString.replace('.','');
	 }
	 elems = getElementsBySelector(selector);
	 if(elems.length<=0){
		 return;
	 }
	 for(var i=0;i<elems.length;i++){
		 var curElem = elems[i];

		 if(curElem.id==null  || curElem.id==''){
			 curElem.id = idString+i;
		 }
		 var linkId = curElem.id;
		 isRunning[linkId] = false;
		 isHovered[linkId] = false;
		 //targets[linkId] = linkId;
		 curElem.setAttribute("targetId",linkId);
		 buttons[linkId] = new animButton(linkId,onAnim,offAnim,mode);
		 addEvent(linkId,'mouseover',activateButton);
		 addEvent(linkId,'mouseout',deactivateButton);	

		 //load up the children next
		 attachChildren(curElem,linkId);
	 }
 }

 function attachChildren(parent,targetId){
            var child = parent.firstChild;
	    var i=0;
	    while ( child != parent.lastChild ){
		attachEventsToChild(child,targetId,i);	
		child = child.nextSibling;		
		i++;
	    }
	    //have to do the last one since it was
	    //missed in the loop
	    attachEventsToChild(child,targetId,i);
 }

 function attachEventsToChild(child,targetId,i){
	if ( child.nodeType == 1 ){
		addEvent(child,'mouseover',activateButton);
		addEvent(child,'mouseout',deactivateButton);
		child.setAttribute("targetId",targetId);
		if(child.id==null || child.id==''){
			child.id = parent + "_" + i;
		}
	}else{
		//alert(child.nodeType);
	}
 }

 /************************************
 The animSequence object
 startX = the X position for the start of the anim;
 startY = the Y posisiont for the start of the anim;
 endX = the X position for the end of the anim;
 endY = the Y posisiont for the end of the anim;
 unitX = the units for X ('px' or '%');
 unitY = the units for Y ('px' or '%');
 numSteps = the number of steps in the animation
 stepLength = the length of each anim 'frame'
 mode = the mode of anim 
	 'single' = the onAnim runs once and holds at the end position
	 'bouncer' = the onAnim runs, then then offAnim, repeat until 
			 mouse out then complete next offAnim and finish
	 'repeater' = run onAnim, then run onAnim again, etc....
	 'continuous' = run the onAnim and continue running until mouse out
 ************************************/
 function animSequence(startX,startY, endX,endY, unitX,unitY, numSteps,stepLength, mode,delay){
	 this.startX = startX;
	 this.startY = startY;
	 this.endX = endX;
	 this.endY = endY;
	 if(unitX==null || unitX==''){

		 this.unitX = 'px';
	 }else{
		 this.unitX = unitX;
	 }
	 if(unitY==null || unitY==''){
		 this.unitY = 'px';
	 }else{
		 this.unitY = unitY;
	 }
	/*
	 if(isOn==null || isOn==''){
		 this.isOn = false;
	}else{
		this.isOn = isOn;
	}
	*/
	if(mode==null || mode==''){
		this.mode = 'single';
	}else{
		this.mode = mode;
	}

	if(numSteps==null || numSteps==''){
		this.numSteps = 10;
	}else{
		this.numSteps = numSteps;
	}

	if(stepLength==null || stepLength==''){
		this.stepLength = 50;
	}else{
		this.stepLength = stepLength;
	}
	
	if(delay==null || delay==''){
		this.delay = this.stepLength;
	}else{
		this.delay = delay;
	}
}


/************************************
The button object
targetId = the id of the elem whos background is to move
onAnim = the name of the anim from the functions[] array to start when moused over
offAnim = the name of the anim fromt he functions[] array to start when moused out
mode = the mode of anim when the mouse is left on the link for a while
		single = the onAnim runs once and holds at the end position
		bouncer = the onAnim runs, then then offAnim, repeat until mouse out then complete next offAnim and finish
		repeater = run onAnim, then run onAnim again, etc....
		continuous = run the onAnim and continue running until mouse out
************************************/

function animButton(targetId,onAnim,offAnim,mode,curX,curY,unitX,unitY){
	this.targetId = targetId;
	this.onAnim = onAnim;
	this.offAnim = offAnim;
	if(mode==null || mode==''){ 
		this.mode = 'single';
	}else{
		this.mode = mode;
	}
	if(curX==null || curX==''){
		this.curX = 0;
	}else{
		this.curX = curX;
	}
	if(curY==null || curY==''){
		this.curY = 0;
	}else{
		this.curY = curY;
	}
	if(unitX==null || unitX==''){
		this.unitX = 'px';
	}else{
		this.unitX = unitX;
	}
	if(unitY==null || unitY==''){
		this.unitY = 'px';
	}else{
		this.unitY = unitY;
	}	
	this.isRunning = false;
	this.isHovered = false;
}






/************************************
Old Stuff
************************************/






var minTop = -20;
var maxTop = 20;

var minLeft = 190;
var maxLeft = 380;

var animStepLength = 60;
var numSteps = 40;
var linksStopped = {};
var linksRunning = {};




var isRunning = {};
var isHovered = {};
var targets = {};












function loadMover(){
	var links = getElementsByClass('testLink');
	for(var i=0; i<links.length; i++){
	    var linkId = links[i].id;
	    positions[linkId + "X"] = 0;
	    positions[linkId + "Y"] = 0;
	    addEvent(linkId,'mouseover',startBGRight);
	    addEvent(linkId,'mouseout',startBGUp);
	    linksStopped[linkId] = true;
	
	
	    var child = links[i].firstChild;
	    while ( child != links[i].lastChild ){
		var rightString = 'goRight('+linkId+');'
		if ( child.nodeType == 1 ){
			addEvent(child,'mouseover',startParentRight);
			addEvent(child,'mouseout',startParentUp);
		}
		child = child.nextSibling;		
	    }
	
	}
	
}

function startCont(e){
	targetId = getTargetElement(e).id;
	linksRunning[targetId] = true;
	if(linksStopped[targetId]){
		linksStopped[targetId] = false;
		moveCont(positions[targetId+"X"],positions[targetId+"Y"],2,1,targetId);
	}
}

function startBGDown(e){
	targetId = getTargetElement(e).id;
	linksRunning[targetId] = true;	
	if(linksStopped[targetId]){
		linksStopped[targetId] = false;
		moveBGDown(minTop,0,numSteps,targetId);
	}
}

function startBGUp(e){
	targetId = getTargetElement(e).id;	
	goUp(targetId);
	//linksRunning[targetId] = false;
	//moveBGUp(maxTop,0,numSteps,targetId);
}

function goUp(targetId){
	linksRunning[targetId] = false;
}


function startBGRight(e){
	targetId = getTargetElement(e).id;
	goRight(targetId);
}

function startParentRight(e){
	target = getTargetElement(e);
	parent = getParent(target);
	goRight(parent.id);
}

function startParentUp(e){
	target = getTargetElement(e);
	parent = getParent(target);
	goUp(parent.id);
}

function getParent(elem){
	var parent = elem.parentNode;
	if(isUndefined(parent)){
	    parent = elem.parentElement;
	}
	return parent;			
}


function goRight(targetId){
	linksRunning[targetId] = true;	
	if(linksStopped[targetId]){
		linksStopped[targetId] = false;
		moveBGRight(minLeft,0,numSteps,targetId);
	}
}




function startBGLeft(e){
	targetId = getTargetElement(e).id;	
	linksRunning[targetId] = false;
	//moveBGLeft(maxLeft,0,numSteps,targetId);
}


function moveCont(curXPos,curYPos,offsetX,offsetY,elemId){
	curXPos += offsetX;
	curYPos += offsetY;
	positions[elemId+"X"] = curXPos;
	positions[elemId+"Y"] = curYPos;
	document.getElementById(elemId).style.backgroundPosition= curXPos + 'px ' + curYPos + 'px';
	if(linksRunning[elemId]==true){
		setTimeout("moveCont("+curXPos+","+curYPos+","+offsetX+","+offsetY+",'"+elemId+"')",animStepLength);
	}
}
	
function moveBGDown(curPos,curStep,numSteps,elemId){
	if(curPos >= maxTop){
		moveBGUp(curPos,0,numSteps,elemId);
	}else{
		stepSize = (maxTop - minTop) / numSteps;
		curPos += stepSize;
		document.getElementById(elemId).style.backgroundPosition='0px ' + curPos + 'px';
		curStep = curStep + 1;
		setTimeout("moveBGDown("+curPos+","+curStep+","+numSteps+",'"+elemId+"')",animStepLength);
	}
}



function moveBGUp(curPos,curStep,numSteps,elemId){
	if(curPos <= minTop){
		if(linksRunning[elemId] == true){
			moveBGDown(curPos,0,numSteps,elemId);
		}else{
			linksStopped[elemId] = true;
		}
	}else{
		stepSize = (maxTop - minTop) / numSteps;
		curPos -= stepSize;
		document.getElementById(elemId).style.backgroundPosition='0px ' + curPos + 'px';
		curStep = curStep + 1;
		setTimeout("moveBGUp("+curPos+","+curStep+","+numSteps+",'"+elemId+"')",animStepLength);
	}
}



function moveBGRight(curPos,curStep,numSteps,elemId){
	if(curPos >= maxLeft){
		moveBGLeft(curPos,0,numSteps,elemId);
	}else{
		stepSize = (maxLeft - minLeft) / numSteps;
		curPos += stepSize;
		document.getElementById(elemId).style.backgroundPosition=curPos + 'px 50%';
		curStep = curStep + 1;
		setTimeout("moveBGRight("+curPos+","+curStep+","+numSteps+",'"+elemId+"')",animStepLength);
	}
}



function moveBGLeft(curPos,curStep,numSteps,elemId){
	if(curPos <= minLeft){
		if(linksRunning[elemId] == true){
			moveBGRight(curPos,0,numSteps,elemId);
		}else{
			linksStopped[elemId] = true;
		}		
	}else{
		stepSize = (maxLeft - minLeft) / numSteps;
		curPos -= stepSize;
		document.getElementById(elemId).style.backgroundPosition = curPos + 'px 50%';
		curStep = curStep + 1;
		setTimeout("moveBGLeft("+curPos+","+curStep+","+numSteps+",'"+elemId+"')",animStepLength);
	}
}

















/**********************************************
	The real meat
**********************************************/

function activateButton(e){
	targetButton = getTargetElement(e);
	targetId = targetButton.getAttribute("targetId");
	//alert(targetId);
	activateAnim(targetId);
}

function deactivateButton(e){
	targetButton = getTargetElement(e);
	targetId = targetButton.getAttribute("targetId");
	deactivateAnim(targetId);
}



function activateAnim(elemId){
	button = buttons[elemId];
	//alert(elemId);
	button.isHovered = true;
	if(button.isRunning){
		;//do nothing we're already moving
	}else{
		button.isRunning = true;
		fireOnAnim(elemId);
	}
}

function deactivateAnim(elemId){
	//alert(elemId);
	button = buttons[elemId];
	button.isHovered = false;
	if(button.isRunning){
		;//do noting we're already moving
	}else{
		button.isRunning = true;
		fireOffAnim(elemId);
	}
}



function stopBG(elemId){
	linksStopped[elemId] = true;
}


function fireOnAnim(elemId){
	button = buttons[elemId];	
	var seqName = button.onAnim;
	seq = functions[seqName];
	
	
	moveBG(elemId,button,seq,0,'onAnim');
	
}

function fireOffAnim(elemId){
	button = buttons[elemId];	
	var seqName = button.offAnim;
	seq = functions[seqName];
	if(seq.mode=='stop'){
		button.isRunning=false;
	}else{		
		moveBG(elemId,button,seq,0,'offAnim');
	}
}


function findNextMove(elemId){

}


function checkNextMove(elemId,curStep,whichAnim){
	button = buttons[elemId];
	var seqName = eval('button.'+whichAnim);
	seq = functions[seqName];
	if( seq.mode=='continuous' && !button.isHovered){
		doMoveString = "fireOffAnim('"+elemId+"')";
		setTimeout(doMoveString,seq.stepLength);
	}else if( seq.mode=='bouncer' && curStep==seq.numSteps && whichAnim=='onAnim'){ //always start the offAnim for a bouncer
		doMoveString = "fireOffAnim('"+elemId+"')";
		setTimeout(doMoveString,seq.delay);
	}else if( seq.mode=='bouncer' && curStep==seq.numSteps && whichAnim=='offAnim' && button.isHovered){ 
		doMoveString = "fireOnAnim('"+elemId+"')";
		setTimeout(doMoveString,seq.delay);
	}else if( seq.mode=='repeater' && curStep==seq.numSteps && whichAnim=='onAnim' && button.isHovered){ 
		button.curX = seq.startX;
		button.curY = seq.startY;
		doMoveString = "fireOnAnim('"+elemId+"')";
		setTimeout(doMoveString,seq.delay);
	}else if( seq.mode=='repeater' && curStep==seq.numSteps && whichAnim=='onAnim' && !button.isHovered){ 
		doMoveString = "fireOffAnim('"+elemId+"')";
		setTimeout(doMoveString,seq.stepLength);
	}else if( seq.mode=='single' && curStep==seq.numSteps && !button.isHovered && whichAnim=='onAnim'){
		doMoveString = "fireOffAnim('"+elemId+"')";
		setTimeout(doMoveString,seq.stepLength);
	}else if( seq.mode=='single' && curStep==seq.numSteps && button.isHovered && whichAnim=='offAnim'){
		doMoveString = "fireOnAnim('"+elemId+"')";
		setTimeout(doMoveString,seq.stepLength);
	}else if( curStep!=seq.numSteps ){
		doMoveString = "doMove('"+elemId+"',"+curStep+",'"+whichAnim+"')";
		setTimeout(doMoveString,seq.stepLength);
	}else if( curStep==seq.numSteps){
		button.isRunning=false;
	}else{
		//alert("don't know how to handle next move " + seqName + " " + button.isHovered + " " + seq.mode  + " " + button.onAnim + " " + button.offAnim);
		
	}			
	
}

function doMove(elemId,curStep,whichAnim){

	button = buttons[elemId];
	var seqName = eval('button.'+whichAnim);
	seq = functions[seqName];
	moveBG(elemId,button,seq,curStep,whichAnim);
}

function moveBG(elemId,button,seq,curStep,whichAnim){
	if(curStep==seq.numSteps){
		findNextMove(elemId,whichAnim);
	}else{
		if(curStep==0 && seq.mode!='continuous'){
			button.curX = seq.startX;
			button.curY = seq.startY;
			button.unitX = seq.unitX;
			button.unitY = seq.unitY;
		}
		var stepSizeX = 0;
		var stepSizeY = 0;
		if(seq.mode=='continuous'){
			stepSizeX = seq.endX;
			stepSizeY = seq.endY;
			curStep = 0;
		}else{
			if(seq.startX == seq.endX){
				stepSizeX=0;
			}else{
				stepSizeX = (seq.endX - seq.startX) / seq.numSteps;
			}
			if(seq.startY == seq.endY){
				stepSizeY=0;
			}else{
				stepSizeY = (seq.endY - seq.startY) / seq.numSteps;
			}
			curStep++;
		}

		

		button.curX += stepSizeX;
		button.curY += stepSizeY;
		posString = button.curX.toString() + button.unitX + ' ' + button.curY.toString() + button.unitY;

	
		document.getElementById(elemId).style.backgroundPosition = posString;

		checkNextMove(elemId,curStep,whichAnim);
	}
}

