var HorizontalScroll = Class.create();

HorizontalScroll.prototype = {
	
	initialize: function(containerName, controlBackName, controlForwardName, controlItemName, itemCount, displayItemCount, deltaScroll) {
		
		this.container = $(containerName);
		this.controlBack = $(controlBackName);
		this.controlForward = $(controlForwardName);		
		this.controlItemName = controlItemName;

		this.itemCount = itemCount;
		this.displayItemCount = displayItemCount;
		
		this.selectedItemIndex = 1;		
		this.deltaScroll = deltaScroll;
		this.minDeltaScroll = 5;
		
		this.scrollValue = 0;		
		this.scrollPosition = 0;

		this.scrollIntervalValue = 150;
		this.autoScrollIntervalValue = 5000;			
		
		
		this.scrollCoefficient = 0.6;		
		
		this.scrollInterval = null;		
		
		this.invalidateControl();
		
		this.controlBack.onmouseover = this.controlBackOver.bindAsEventListener(this);
		this.controlBack.onmouseout = this.controlBackOut.bindAsEventListener(this);
		this.controlForward.onmouseover = this.controlForwardOver.bindAsEventListener(this);
		this.controlForward.onmouseout = this.controlForwardOut.bindAsEventListener(this);		

		for (index = 1; index <= itemCount; index++) {			
			$(controlItemName + index).onmouseover = this.controlItemOver.bindAsEventListener(this);
			$(controlItemName + index).onmouseout = this.controlItemOut.bindAsEventListener(this);
		}
		
		for (index = 0; index < displayItemCount; index++) {
			var element = this.container.childNodes[index];			
			this.container.appendChild(element.cloneNode(element));
		}
	},
	
	startScroll: function() {
		t = this;
		this.scrollInterval = setInterval('t.moveScroll()', this.scrollIntervalValue);
	},
	
	stopScroll: function() {
		
		clearInterval(this.scrollInterval);
	},

	moveBack: function() {
			
		if (this.canMoveBack()) {		
	
			if (this.runAutoScroll)
				this.stopAutoScroll();			
			
			this.scrollValue = this.scrollValue + this.deltaScroll;
			this.selectedItemIndex = this.selectedItemIndex - 1;
			
		}
	},

	moveForward: function() {
			
		if (this.canMoveForward()) {
			
			if (this.runAutoScroll)
				this.stopAutoScroll();						
			
			this.scrollValue = this.scrollValue - this.deltaScroll;			
			this.selectedItemIndex = this.selectedItemIndex + 1;
		}
			
	},	
	
	moveTo: function(itemIndex) {

		if (this.canMoveTo(itemIndex)) {

			if (this.runAutoScroll)
				this.stopAutoScroll();		
			
			this.scrollValue = this.scrollValue + (this.selectedItemIndex - itemIndex) * this.deltaScroll;
			this.selectedItemIndex = itemIndex;
			
		}
	},	
	
	moveScroll: function() {

		if (this.scrollPosition != this.scrollValue) {			
			
			var currentDeltaScroll = (this.scrollValue - this.scrollPosition) * this.scrollCoefficient;
					
			this.scrollPosition = this.scrollPosition + currentDeltaScroll;		

			this.container.style.left = this.scrollPosition + 'px';
			
			this.invalidateControl();
			this.invalidateContainer();			
		}
		
	},	
	
	startAutoScroll: function() {
		this.runAutoScroll = true;
		t = this;
		this.autoScrollInterval = setInterval('t.autoScroll()', this.autoScrollIntervalValue);		
	},	
	
	stopAutoScroll: function() {

		this.runAutoScroll = false;
		clearInterval(this.autoScrollInterval);
		
		this.selectedItemIndex = this.getSelectedItemIndex();
		
		this.scrollValue = - (this.selectedItemIndex - 1) * this.deltaScroll;
		this.scrollPosition = - (this.selectedItemIndex - 1) * this.deltaScroll;		
		
		this.container.style.left = this.scrollPosition + 'px';		
	},
	
	autoScroll: function() {
		this.scrollValue = this.scrollValue - this.deltaScroll;
		this.selectedItemIndex = this.selectedItemIndex + 1;		
	},	
	
	invalidateControl: function() {
		
		switch (this.getSelectedItemIndex()) {
			case 1:
				Element.removeClassName(this.controlBack, 'normalBack');
				Element.removeClassName(this.controlBack, 'hoverBack');
				Element.addClassName(this.controlBack, 'disableBack');

				Element.removeClassName(this.controlForward, 'disableForward');				
				Element.addClassName(this.controlForward, 'normalForward');				
				
				break;
			case this.itemCount:
				
				Element.removeClassName(this.controlForward, 'normalForward');
				Element.removeClassName(this.controlForward, 'hoverForward');
				Element.addClassName(this.controlForward, 'disableForward');
				
				Element.removeClassName(this.controlBack, 'disableBack');
				Element.addClassName(this.controlBack, 'normalBack');			

				break;
			default:
			
				Element.removeClassName(this.controlBack, 'disableBack');
				Element.addClassName(this.controlBack, 'normalBack');
				
				Element.removeClassName(this.controlForward, 'disableForward');				
				Element.addClassName(this.controlForward, 'normalForward');
				
				break;
		}
		
		for (index = 1; index <= this.itemCount; index++) {
			if (index == this.getSelectedItemIndex()) {
				Element.removeClassName(this.controlItemName + index, 'normalItem');
				Element.addClassName(this.controlItemName + index, 'selectItem')
			} 
			else {
				Element.removeClassName(this.controlItemName + index, 'selectItem');
				Element.addClassName(this.controlItemName + index, 'normalItem')
			}
		}		
	},	
	
	invalidateContainer: function() {

		if (this.selectedItemIndex == this.itemCount + this.displayItemCount - 1) {
			this.selectedItemIndex = 1;
			this.scrollValue = 0;
			this.scrollPosition = 0;			
			this.container.style.left = 0 + 'px';
		}
	},	
	
	getSelectedItemIndex: function() {
		var returnIndex = this.selectedItemIndex;
		
		while (returnIndex > this.itemCount)
			returnIndex = returnIndex - this.itemCount;
		
		return returnIndex;		
	},	
	
	controlBackOver: function()	{
		
		if (this.selectedItemIndex != 1) {
			Element.addClassName(this.controlBack, 'hoverBack');
		}
	},	
	
	controlBackOut: function() {
		Element.removeClassName(this.controlBack, 'hoverBack');
	},	
	
	controlForwardOver: function() {
		if (this.selectedItemIndex != this.itemCount)
			Element.addClassName(this.controlForward, 'hoverForward');
	},	
	
	controlForwardOut: function() {
		
		Element.removeClassName(this.controlForward, 'hoverForward');
	},
	
	controlItemOver: function(event) {
		var scrollItem = Event.element(event);	
	
		if (scrollItem.className != 'selectItem')
			Element.addClassName(scrollItem, 'hoverItem');
	},

	controlItemOut: function(event) {
		var scrollItem = Event.element(event);	

		if (scrollItem.className != 'selectItem')
			Element.removeClassName(scrollItem, 'hoverItem');
	},	
	
	canMoveBack: function() {
		
		if (this.selectedItemIndex > 1)
			return true;		
		else
			return false;
	},	
	
	canMoveForward: function() {

		if (this.selectedItemIndex < this.itemCount)
			return true;		
		else
			return false;
	},	
	
	canMoveTo: function(itemIndex) {

		if (itemIndex != this.selectedItemIndex)
			return true;		
		else
			return false;
	}
};