var yd = YAHOO.util.Dom;
var ye = YAHOO.util.Event;


(function() {
    
    function scroll(step) {
        var y = parseInt(yd.getStyle(this.wrapElement, "top").replace(/[^0-9]/g, ""), 10);
		this.scrollTo(y + step);
    }
    
	Scroller = function(config) {
		this.scrollbar = yd.get(config.scrollbar);
		this.scroller = yd.get(config.scroller);
		this.up = yd.get(config.up);
		this.down = yd.get(config.down);
		this.scrollStep = config.scrollStep || 10;
		this.element = yd.get(config.element);
		this.wrapElement = yd.getFirstChild(this.element);
		this.anim = null;
		ye.addListener(window, "load", function() {
			var scrollerOffsetTop = (typeof config.offsetTop == "number") ? config.offsetTop : 0;
			var scrollerOffsetBottom = (typeof config.offsetBottom == "number") ? config.offsetBottom : 0;
			
			// Getting min and max y
			var scrollBarHeight = this.scrollbar.offsetHeight;
			var scrollerHeight = this.scroller.offsetHeight;
			
			this.dd = new YAHOO.util.DD(this.scroller);
			this.dd.setXConstraint(0, 0);
			this.dd.setYConstraint(scrollerOffsetTop, scrollBarHeight - scrollerHeight - scrollerOffsetBottom);
			
			
			this.dd.on("dragEvent", function(e) {
				var p = this.getScrollPercent();
				this.setScrollPercent(p);
			}, this, true);
			
			if(this.up && this.down)
			{
			    ye.addListener(this.up, "click", this.onScrollUp, this, true);
			    ye.addListener(this.down, "click", this.onScrollDown, this, true);
			    
			}
			
			this.refresh();
		}, this, true);

		

	};

	Scroller.prototype = {
		getScrollPercent : function() {
	
			var y = yd.getY(this.scroller);
			return (y - this.dd.minY) / (this.dd.maxY - this.dd.minY);
		},
		onScrollDown : function(e) {
		    ye.stopEvent(ye.getEvent(e));
		    scroll.call(this, this.scrollStep);		    
		},
		onScrollUp : function(e) {
		    ye.stopEvent(ye.getEvent(e));
		    scroll.call(this, -this.scrollStep);		    		    
		},
		refresh : function() {
			var totalHeight = this.wrapElement.offsetHeight;
			var visibleHeight = this.element.offsetHeight + 10;
			
			
			this.scrollbar.style.visibility = (totalHeight <= visibleHeight) ? 'hidden' : 'visible';
		},
		scrollTo : function(y) {
		    
		    var totalHeight = this.wrapElement.offsetHeight;
		    var visibleHeight = this.element.offsetHeight;
    		
		    y = Math.min(totalHeight - visibleHeight, y);
		    y = Math.max(0, y);
    			
		    this.wrapElement.style.top = "-" + y + "px";
    		
            if (document.selection)
            {
                document.selection.empty();
            }
            else if (window.getSelection)
            {
                window.getSelection().removeAllRanges();
            }
            
            var percent = y / (totalHeight - visibleHeight);
            
            var scrollerY = percent * (this.dd.maxY - this.dd.minY) +  this.dd.minY;
            yd.setY(this.scroller, scrollerY);
            
            
		
		},
		setScrollPercent : function(percent) {
			var totalHeight = this.wrapElement.offsetHeight;
			var visibleHeight = this.element.offsetHeight;
			var y = (totalHeight - visibleHeight) * percent;
			
			
			this.wrapElement.style.top = "-" + y + "px";
		}
	}


})();

