function GRVScroller(scroller){
	this.interval = null;
	this.t = 0;
	this.scroller = scroller;
	this.currentNode = null;
	this.period = 30;
	this.animationInterval = 15;
	this.itemTag = "IMG";
}

GRVScroller.prototype.scrollToNode = function( endNode ){
		
	if (null == this.currentNode){
		this.x1=0;
	} else {
		this.x1 = this.currentNode.offsetLeft - this.scroller.offsetLeft;
	}
	this.x2 = endNode.offsetLeft  - this.scroller.offsetLeft;
	this.dx = this.x2 - this.x1;

	this.currentNode = endNode;	
	if (null != this.interval){
		clearInterval(this.interval);
		this.interval = null;		
	}
	var o = this;
	this.t = 0;
	this.interval = setInterval(function() {o.animate()}, this.animationInterval);
	
} 

GRVScroller.prototype.setFirstNodeAsCurrent = function() {
	this.currentNode = this.getItemsInScrollArea()[0];
}

GRVScroller.prototype.animate = function() {
	if (this.t > this.period){
		clearInterval(this.interval);
		this.interval = null;
		this.t = 0;
	} else {
		var m = sineInOut(this.t, this.x1, this.dx, this.period);
		this.scroller.scrollLeft = m;
		this.t += 1;
	}
	
}


GRVScroller.prototype.getItemsInScrollArea = function() {

	var scrollerContainer = document.getElementById( this.scroller.id + "Scroller" );
	
	var items = [];
	var itemCount =0;
	for (var i=0,c = scrollerContainer.childNodes.length; i < c; i++) {
		var n = scrollerContainer.childNodes[ i ];
		if (null != n.tagName && this.itemTag == n.tagName.toLowerCase() ){
			items[itemCount] = n;
			itemCount++;
		}
	}
	return items;
}

GRVScroller.prototype.scrollToNext = function(){

	var itemList = this.getItemsInScrollArea();
	
	if (itemList.length <= 1)
		return ;
		
	var nextNode = null;
	
	if (null == this.currentNode)
		nextNode = itemList[ 1 ];
	
	if (null == nextNode) {
		var currentNodeIndex = -1;
		for(var i=0,c=itemList.length; i < c; i++){
			var n = itemList[i];
			if (n.id == this.currentNode.id){
				currentNodeIndex = i;
				break;
			}
		}
		
		if (itemList.length == currentNodeIndex +1) {
			nextNode = itemList[ 0 ];
		} else {
			nextNode = itemList[ currentNodeIndex + 1];
		}
		
	}
	
	if (null != nextNode)
		this.scrollToNode( nextNode );
		
}

GRVScroller.prototype.scrollToPrevious = function(){
	
	var itemList = this.getItemsInScrollArea();
	
	if (itemList.length <= 1)
		return ;

	
	var previousNode = null;
	
	if (null == this.currentNode)
		previousNode = itemList[ itemList.length -1 ];
	
	if (null == previousNode) {
		var currentNodeIndex = -1;
		for(var i=0,c=itemList.length; i < c; i++){
			var n = itemList[i];
			if (n.id == this.currentNode.id){
				currentNodeIndex = i;
				break;
			}
		}
		
		if (0 == currentNodeIndex) {
			previousNode = itemList[ itemList.length -1 ];
		} else {
			previousNode = itemList[ currentNodeIndex - 1];
		}
		
	}
	
	if (null != previousNode)
		this.scrollToNode( previousNode );
}
