var HorizontalScroll = Class.create();

HorizontalScroll.prototype = {
	
	initialize: function(containerBlockName, controlsBlockName, itemsBlockName) {		
		
		var containerBlock = $(containerBlockName);
		var elementsBlock = containerBlock.childNodes[0];
		var controlsBlock = $(controlsBlockName);
		var itemsBlock = $(itemsBlockName);		
		
		this.containerBlock = containerBlock;
		this.elementsBlock = elementsBlock;
		this.itemsBlock = itemsBlock;
		
		this.deltaScroll = containerBlock.offsetWidth;
					
		this.selectedItemIndex = 1;		
		this.minDeltaScroll = 5;
		
		this.scrollValue = 0;		
		this.scrollPosition = 0;

		this.scrollIntervalValue = 150;
		this.autoScrollIntervalValue = 5000;		
		
		this.scrollCoefficient = 0.6;		
		
		this.scrollInterval = null;
		
		var elementsItemsWidth = 0;
	
		var elementsBlockChildNodes = elementsBlock.childNodes;		
		for (var elementsBlockChildIndex = 0; elementsBlockChildIndex < elementsBlockChildNodes.length; elementsBlockChildIndex++) {
		    var elementsBlockChildItem = elementsBlockChildNodes[elementsBlockChildIndex];
		    elementsItemsWidth += elementsBlockChildItem.offsetWidth;		    
		}
				
		this.itemCount = elementsItemsWidth / this.deltaScroll;
		
		if (this.itemCount != 0) {		    
	        var elementsItemWidth = elementsBlockChildNodes[0].offsetWidth;	        
	        this.displayItemCount = this.deltaScroll / elementsItemWidth;
	        
		    for (elementsBlockIndex = 0; elementsBlockIndex < this.displayItemCount; elementsBlockIndex++) {
			    var elementsBlockChildItem = elementsBlockChildNodes[elementsBlockIndex];			    
			    this.elementsBlock.appendChild(elementsBlockChildItem.cloneNode(elementsBlockChildItem));
		    }
		}
			
		var controlsBlockChildNodes = controlsBlock.childNodes;		
		for (var controlsBlockChildIndex = 0; controlsBlockChildIndex < controlsBlockChildNodes.length; controlsBlockChildIndex++) {
		    
		    var controlsBlockChildItem = controlsBlockChildNodes[controlsBlockChildIndex];
		    if (controlsBlockChildItem.className.indexOf('scrollButton') != -1) {
		        if (controlsBlockChildItem.className.toLowerCase().indexOf('back') != -1) {
		            this.scrollBack = controlsBlockChildItem;
		        }
		        if (controlsBlockChildItem.className.toLowerCase().indexOf('forward') != -1) {
		            this.scrollForward = controlsBlockChildItem;
		        }
		    }
		}		
		
		this.scrollBack.onclick = this.moveBack.bindAsEventListener(this);		
		this.scrollBack.onmouseover = this.scrollBackOver.bindAsEventListener(this);
		this.scrollBack.onmouseout = this.scrollBackOut.bindAsEventListener(this);		
		this.scrollForward.onclick = this.moveForward.bindAsEventListener(this);		
		this.scrollForward.onmouseover = this.scrollForwardOver.bindAsEventListener(this);		
		this.scrollForward.onmouseout = this.scrollForwardOut.bindAsEventListener(this);		

		for (scrollItemIndex = 1; scrollItemIndex <= this.itemCount; scrollItemIndex++) {
		    
		    var scrollItem = document.createElement('a');
		    
		    scrollItem.onclick = this.moveTo.bindAsEventListener(this);		    		
		    scrollItem.index = scrollItemIndex;
		    scrollItem.className = 'scrollItem normalItem';
			scrollItem.onmouseover = this.scrollItemOver.bindAsEventListener(this);
			scrollItem.onmouseout = this.scrollItemOut.bindAsEventListener(this);
			itemsBlock.appendChild(scrollItem);			
		}
		
		this.invalidateControl();		
	},
	
	startScroll: function() {
		this.scrollInterval = setInterval(this.moveScroll.bind(this), this.scrollIntervalValue);
	},
	
	stopScroll: function() {
		
		clearInterval(this.scrollInterval);
	},

	moveBack: function() {
			
		this.invalidateContainer();
		
		if (this.canMoveBack()) {		
	
			if (this.runAutoScroll)
				this.stopAutoScroll();			
			
			this.scrollValue = this.scrollValue + this.deltaScroll;
			this.selectedItemIndex = this.selectedItemIndex - 1;
			
		}
	},

	moveForward: function() {
			
		this.invalidateContainer();
		
		if (this.canMoveForward()) {			
			
			if (this.runAutoScroll)
				this.stopAutoScroll();						
			
			this.scrollValue = this.scrollValue - this.deltaScroll;			
			this.selectedItemIndex = this.selectedItemIndex + 1;
		}
			
	},	
	
	moveTo: function(evt) {

		this.invalidateContainer();
		var itemIndex = Event.element(evt).index;
		
		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.deltaScroll != this.containerBlock.offsetWidth) {
	        this.deltaScroll = this.containerBlock.offsetWidth;	        
	        
	        this.scrollValue = - (this.getSelectedItemIndex() - 1) * this.deltaScroll;
	        this.scrollPosition = this.scrollValue;
	        this.elementsBlock.style.left = this.scrollPosition + 'px';
	    }
				
		if (this.scrollPosition != this.scrollValue) {			
			
			var currentDeltaScroll = (this.scrollValue - this.scrollPosition) * this.scrollCoefficient;
					
			this.scrollPosition = this.scrollPosition + currentDeltaScroll;		

			this.elementsBlock.style.left = this.scrollPosition + 'px';
			
			this.invalidateControl();
		}		
		
	},	
	
	startAutoScroll: function() {
		this.runAutoScroll = true;
		this.autoScrollInterval = setInterval(this.autoScroll.bind(this), 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.elementsBlock.style.left = this.scrollPosition + 'px';		
	},
	
	autoScroll: function() {
		this.invalidateContainer();
		this.scrollValue = this.scrollValue - this.deltaScroll;		
		this.selectedItemIndex = this.selectedItemIndex + 1;			
	},	
	
	invalidateControl: function() {
		
		switch (this.getSelectedItemIndex()) {
			case 1:
				Element.removeClassName(this.scrollBack, 'normalBack');
				Element.removeClassName(this.scrollBack, 'hoverBack');
				Element.addClassName(this.scrollBack, 'disableBack');

				Element.removeClassName(this.scrollForward, 'disableForward');				
				Element.addClassName(this.scrollForward, 'normalForward');				
				
				break;
			case this.itemCount:
				
				Element.removeClassName(this.scrollForward, 'normalForward');
				Element.removeClassName(this.scrollForward, 'hoverForward');
				Element.addClassName(this.scrollForward, 'disableForward');
				
				Element.removeClassName(this.scrollBack, 'disableBack');
				Element.addClassName(this.scrollBack, 'normalBack');			

				break;
			default:
			
				Element.removeClassName(this.scrollBack, 'disableBack');
				Element.addClassName(this.scrollBack, 'normalBack');
				
				Element.removeClassName(this.scrollForward, 'disableForward');				
				Element.addClassName(this.scrollForward, 'normalForward');
				
				break;
		}
		
		for (index = 1; index <= this.itemCount; index++) {
			if (index == this.getSelectedItemIndex()) {
				Element.removeClassName(this.itemsBlock.childNodes[index - 1], 'normalItem');
				Element.addClassName(this.itemsBlock.childNodes[index - 1], 'selectItem')
			} 
			else {
				Element.removeClassName(this.itemsBlock.childNodes[index - 1], 'selectItem');
				Element.addClassName(this.itemsBlock.childNodes[index - 1], 'normalItem')
			}
		}		
	},	
	
	invalidateContainer: function() {

		if (this.selectedItemIndex == this.itemCount + this.displayItemCount - 1) {
			this.selectedItemIndex = 1;
			this.scrollValue = 0;
			this.scrollPosition = 0;			
			this.elementsBlock.style.left = 0 + 'px';
		}
	},	
	
	getSelectedItemIndex: function() {
		var returnIndex = this.selectedItemIndex;
		
		while (returnIndex > this.itemCount)
			returnIndex = returnIndex - this.itemCount;
		
		return returnIndex;		
	},	
	
	scrollBackOver: function()	{
		
		if (this.selectedItemIndex != 1) {
			Element.addClassName(this.scrollBack, 'hoverBack');
		}
	},	
	
	scrollBackOut: function() {
		Element.removeClassName(this.scrollBack, 'hoverBack');
	},	
	
	scrollForwardOver: function() {
		if (this.selectedItemIndex != this.itemCount)
			Element.addClassName(this.scrollForward, 'hoverForward');
	},	
	
	scrollForwardOut: function() {
		
		Element.removeClassName(this.scrollForward, 'hoverForward');
	},
	
	scrollItemOver: function(event) {	
		var scrollItem = Event.element(event);	
	
		if (scrollItem.className != 'selectItem')
			Element.addClassName(scrollItem, 'hoverItem');
	},

	scrollItemOut: 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;
	}
};