/*
 * Animation functions for the Red boxes at the bottom.
 * 
 */
//ie: RedboxScroller('#tray', 'sc_menu', '.redbox', 20);
function RedboxScroller(strContainerName, strDivName, strBoxClass, intAnimDelay) {
	//pointer to self
	//var objectPointer = this;
	
	this.strContainerName 	= strContainerName;
	this.strDivName			= strDivName;
	this.strBoxClass		= strBoxClass;
	this.intAnimDelay		= intAnimDelay;

	//state properties
	this.currentDirection 	= "left";
	this.currentMoveLeft	= 0;
	this.lastMouseX 		= 0;
	this.pausedAnimation 	= false;
	
	//function declarations;
//	this.getDivObjectWithName 	= getDivObjectWithNameFunction;
//	this.calculateValidMove 	= calculateValidMoveFunction;
//	this.animateContents 		= animateContentsFunction;
	
	//Get our elements for faster access and set overlay width
	this.divObj 			= $("#"+strDivName),
								ul = $("ul."+strDivName),
								// unordered list's left margin
								ulPadding = 15;
	//Get div layer's width
	this.divWidth			= this.divObj.width;
	this.containerWidth 	= $(this.strContainerName).width();
	
	this.boxesCount			= $(this.strBoxClass).length;
	this.singleBoxWidth 	= $("div " + this.strBoxClass + ":first").width();
	this.allBoxesWidth		= (this.boxesCount * this.singleBoxWidth);
	this.spacesWidth		= (this.boxesCount+1) *32;
	this.movingContentWidth = this.allBoxesWidth + this.spacesWidth;
	
	this.movingRange		= 0;
	
	if (this.containerWidth >= this.movingContentWidth) {
		//contents fits in container area so no moving necessary
		this.movingRange = 0;
	} else {
		this.movingRange = (this.movingContentWidth) - this.containerWidth; 
	}
	
	/*  alert("values are [reboxescount]:" + redboxesCount 
	+ "\n[containerWidth]:" + containerWidth
		+ "\n[redboxWidth]:" + redboxWidth 
		+ "\n[contentsWidth]:" + contentsWidth 
		+ "\n[maxMoveDistance]:" + maxMoveDistance
		+ "\n[spacesWidth]:" + spacesWidth );
	*/
	
	//Remove scrollbars
    //this.div.css({overflow: 'hidden'});
	
	this.animateContents(this);
}


function getDivObjectWithNameFunction(strDivName) {
	return $("." + strDivName), 
			ul = $("ul." + strDivName),
			// unordered list's left margin
			ulPadding = 15;
}

function animateContentsFunction(thisObj) {

	// unordered list's left margin
	ulPadding = 15; 
	
	 $(document).everyTime(30, function (){
		if (!thisObj.pausedAnimation){
			var newMove = thisObj.calculateValidMove(thisObj, thisObj.currentMoveLeft, thisObj.movingRange);
			//this.div = this.getDivObjectWithName(this.strDivName);
			thisObj.divObj.scrollLeft(newMove);
			//save new value to current move value
			thisObj.currentMoveLeft = newMove;
		}
		 		
	  });
/*	 
	 //When user move mouse over the redboxes
	 thisObj.divObj.mousemove(function(e){
	 //var left = (e.pageX - div.offset().left) * (containerWidth-divWidth) / divWidth;
	      
	 //mouse coordinate in the container div
	 var curMouseX = (e.pageX -  $("."+ thisObj.strDivName).offset().left);
	 //default to current positioning
	 var mouseMoveLeft = thisObj.currentMoveLeft;
	 var debug_str = "values are [e.pageX]:" + e.pageX 
						+ "\n[left]:" + left
						+ "\n[containerX]:" + containerX;					
	 //$("#debug").html(debug_str);

	  //TODO: build in marge for steady position (x radius around entry point that stops motion)
	  if (curMouseX <= thisObj.lastMouseX) {
		  //calculate value when moving left with mouse
		  mouseMoveLeft = thisObj.currentMoveLeft + (thisObj.lastMouseX-curMouseX);
	  } else {
	      //calculate value when moving right with mouse
		  mouseMoveLeft = thisObj.currentMoveLeft - (curMouseX-thisObj.lastMouseX);
	  }
	  
	  //save position to last position	
	  thisObj.lastMouseX = curMouseX;

	      //perform the actual scroll
	      div.scrollLeft(mouseMoveLeft);
	    });
*/
	    //mouse enters redbox div; set pause flag to true
	 thisObj.divObj.mouseover(function(e){
		 thisObj.pausedAnimation = true;
  		//lastMouseX = (e.pageX -  $(".sc_menu").offset().left);	      
  	});

		//mouse leaves redbox div; set pause flag to false
	 	thisObj.divObj.mouseout(function(e){
	 	thisObj.pausedAnimation = false;	      
  	});
}

function calculateValidMoveFunction(thisObj, currentMoveLeft, maxMoveDistance) {
	var newMoveLeft = currentMoveLeft;

		if (thisObj.currentDirection == "left") {
			if (currentMoveLeft < maxMoveDistance){
				newMoveLeft = currentMoveLeft+1;
			}else {
				thisObj.currentDirection = "right";
				newMoveLeft = currentMoveLeft-1;
			}
		} else {
			if (currentMoveLeft > 0){
				newMoveLeft = currentMoveLeft-1;
			} else {
				thisObj.currentDirection = "left";
				newMoveLeft = currentMoveLeft+1;
			}
		}
	//return new Left moving value
	return newMoveLeft;
}

RedboxScroller.prototype.getDivObjectWithName = getDivObjectWithNameFunction;
RedboxScroller.prototype.animateContents = animateContentsFunction;
RedboxScroller.prototype.calculateValidMove = calculateValidMoveFunction;
