var VerticalScroll = Class.create();

VerticalScroll.prototype = {
	
	initialize: function(itemContainer, itemName, controlName, altName) {		
		
		this.error = false;
		this.itemContainer = $(itemContainer);		
		
		if (this.itemContainer == null) {
		    this.error = true;
		    return;
		}
		
		this.itemName = itemName;
		this.controlName = controlName;		
		this.altName = altName;
		
		this.selectedItemIndex = 1;
		this.scrollPosition = 0;
		this.scrollValue = 0;
		
		this.scrollCoefficient = 0.2;
		this.minScrollValue = 5;
		this.scrollIntervalValue = 10;
		
		this.autoScrollIntervalValue = 5000;
		
		this.runAutoScroll = false;
		this.canScroll = true;
				
		var index = 1;
				
		while ($(this.itemName + index) != null) {		    		    		    
		    
		    var control = $(this.controlName + index);		    		    
		    var alt = $(this.altName + index);
		    
		    control.onclick = this.moveTo.bindAsEventListener(this);
		    
		    if (alt != null) {
		        control.onmouseover = this.mouseOver.bindAsEventListener(this);
		        control.onmouseout = this.mouseOut.bindAsEventListener(this);
		        control.onmousemove = this.mouseMove.bindAsEventListener(this);
		    }
		    		    		    
		    index++;		    
		}		
		
		this.itemCount = index - 1;					
		
		this.deltaScroll = this.itemContainer.offsetHeight / this.itemCount;											
				
		var element = $(this.itemName + '1')
		this.itemContainer.appendChild(element.cloneNode(element));	
		
		this.itemContainerWidth = this.itemContainer.offsetWidth;				
		
		this.start();
	},	
	
	start: function() {
	    if (this.error == false)
	        this.interval = setInterval(this.moveScroll.bind(this), this.scrollIntervalValue);
	},
	
	stop: function() {
	    
	    if (this.error == false) {
	        clearInterval(this.interval);
	        this.stopAutoScroll();
	    }
	},
		
	moveTo: function(event) {	
	
	    this.canScroll = false;
	    
	    var itemId = Event.element(event).id;
	    var itemIndex = itemId.substr(this.controlName.length);	    
	    
		this.invalidateContainer();		

		if (this.canMoveTo(itemIndex)) {					
			
			if (this.runAutoScroll) {
				this.stopAutoScroll();				
						
				setTimeout(this.startAutoScroll.bind(this), 5000);
			}		    
			
			this.scrollValue = this.scrollValue + (this.selectedItemIndex - itemIndex) * this.deltaScroll;		
			
			$(this.controlName + this.selectedItemIndex).className = 'normalcontrol';			
			this.selectedItemIndex = itemIndex;			
			$(this.controlName + itemIndex).className = 'selectcontrol';			
		}
		
		this.canScroll = true;
	},	
	
	moveScroll: function() {				
	
	    if (this.canScroll == true) {
	    
	        this.invalidateScroll();		    
		
		    if (this.scrollPosition != this.scrollValue) {		       
			
			    var currentDeltaScroll = (this.scrollValue - this.scrollPosition) * this.scrollCoefficient;					
			    this.scrollPosition = this.scrollPosition + currentDeltaScroll;		

                if (Math.abs(this.scrollPosition - this.scrollValue) < this.minScrollValue)
	    	        this.scrollPosition = this.scrollValue;
		    
    			this.itemContainer.style.top = this.scrollPosition + 'px';
			
			    this.invalidateControl();
		    }
		}
		
	},	
	
	invalidateScroll: function() {	
	
	    if (this.itemContainerWidth != this.itemContainer.offsetWidth) {
	    
	        this.itemContainerWidth = this.itemContainer.offsetWidth;
	    
	        this.itemContainerHeight = this.itemContainer.offsetHeight;
	        this.itemContainerHeight -= this.itemContainerHeight / (this.itemCount + 1);
	        
		    this.deltaScroll = this.itemContainerHeight / this.itemCount;		    		    
		    
		    this.scrollValue = -(this.getSelectedItemIndex() - 1) * this.deltaScroll;		
		    this.scrollPosition = this.scrollValue;
		    this.itemContainer.style.top = this.scrollValue + 'px';		    
		}
	
	},
	
	startAutoScroll: function() {
		
		if (this.error == false) {
		    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.itemContainer.style.top = this.scrollPosition + 'px';		
	},
	
	autoScroll: function() {
		
		if (this.scrollPosition == this.scrollValue) {
		    
		    this.canScroll = false;		
		
	    	this.invalidateContainer();
    		this.scrollValue = this.scrollValue - this.deltaScroll;		
		
		    $(this.controlName + this.getSelectedItemIndex()).className = 'normalcontrol';		
	    	this.selectedItemIndex = parseInt(this.selectedItemIndex) + 1;		
    		$(this.controlName + this.getSelectedItemIndex()).className = 'selectcontrol';				
		
		    this.canScroll = true;		
		}
		
	},	
	
	invalidateControl: function() {		
		
		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 + 1) {
			
			this.selectedItemIndex = 1;
			this.scrollValue = 0;
			this.scrollPosition = 0;			
			this.itemContainer.style.top = 0 + 'px';
		}
	},	
	
	getSelectedItemIndex: function() {
		
		var returnIndex = this.selectedItemIndex;
		
		while (returnIndex > this.itemCount)
			returnIndex = returnIndex - this.itemCount;
		
		return parseInt(returnIndex);		
	},			
		
	canMoveTo: function(itemIndex) {

		if (itemIndex != this.selectedItemIndex)
			return true;		
		else
			return false;
	},
	
	mouseOver: function(event) {	    	  
	  
	    var itemId = Event.element(event).id;
	    var itemIndex = itemId.substr(this.controlName.length);	    
	    
	    var altItem = $(this.altName + itemIndex);
	    var parentItem = altItem.parentNode;
	    
	    var realOffset = Position.cumulativeOffset(parentItem);    	        
	    	        
	    altItem.style.top = (Event.pointerY(event) - realOffset[1] - 45) + 'px';
	    altItem.style.left = (Event.pointerX(event) - realOffset[0] - altItem.offsetWidth / 2) + 'px';
	    
	    altItem.style.display = 'block';
	    
	    altItem.style.top = (Event.pointerY(event) - realOffset[1] - altItem.offsetHeight - 10) + 'px';	    
	},
	
	mouseOut: function(event) {	
	
	    var itemId = Event.element(event).id;
	    var itemIndex = itemId.substr(this.controlName.length);		    
	
	    $(this.altName + itemIndex).style.display = 'none';
	},
	
	mouseMove: function(event) {	    
	    
        var itemId = Event.element(event).id;
	    var itemIndex = itemId.substr(this.controlName.length);		    
	    
	    var altItem = $(this.altName + itemIndex);    
	    var parentItem = altItem.parentNode;
	    
	    var realOffset = Position.cumulativeOffset(parentItem);
	    	        
	    altItem.style.top = (Event.pointerY(event) - realOffset[1] - altItem.offsetHeight - 10) + 'px';	    
	    altItem.style.left = (Event.pointerX(event) - realOffset[0] - altItem.offsetWidth / 2) + 'px';
	}
};