YAHOO.namespace("cywalk.HTMLScroller");
/**
 * http://developer.yahoo.com/yui/articles/hosting/?animation&slider&yahoo-dom-event&MIN
 * 
 * <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js&2.7.0/build/animation/animation-min.js&2.7.0/build/dragdrop/dragdrop-min.js&2.7.0/build/slider/slider-min.js"></script>
 */
YAHOO.cywalk.HTMLScroller = function(scrollBox, scrollContent, width) {
	width = 940;
	this.box = YAHOO.util.Dom.get(scrollBox);
	this.sliderBox = YAHOO.util.Dom.get("sliderbg");
	this.upBox = YAHOO.util.Dom.get("uplink");
	this.downBox = YAHOO.util.Dom.get("downlink");
	this.minThumb = YAHOO.util.Dom.get("minthumb");
	
	// Scrollen mittels MouseWheel erm�glichen
	var scrollEvent = (YAHOO.env.ua.ie) ? "mousewheel" : "DOMMouseScroll";
	YAHOO.util.Event.addListener(this.box, scrollEvent, this.onMouseWheel, this, true);
	
	YAHOO.util.Dom.setStyle(this.box, "width", width + "px");
	YAHOO.util.Dom.setStyle(this.sliderBox, "width", width + "px");
	YAHOO.util.Dom.setStyle(this.downBox, "left", (width - 11) + "px");
	
	this.boxRegion = YAHOO.util.Dom.getRegion(this.box);
	this.boxHeight = this.boxRegion.bottom - this.boxRegion.top;
	
	this.content = YAHOO.util.Dom.get(scrollContent);
	this.contentRegion = YAHOO.util.Dom.getRegion(this.content);
	this.contentHeight = this.contentRegion.bottom - this.contentRegion.top;
	
	this.maxScrollPos = this.boxHeight - this.contentHeight;
	if (this.maxScrollPos > 0) {
		this.maxScrollPos = 0;
	}
	
	var thumbSize = width / (this.contentHeight / this.boxHeight);
	if (thumbSize >= width) {
		YAHOO.util.Dom.setStyle(this.minThumb, "display", "none");
	}
	else {
		YAHOO.util.Dom.setStyle(this.minThumb, "display", "block");
	}
	YAHOO.util.Dom.setStyle(this.minThumb, "width", thumbSize + "px");
	
	this.thumbRegion = YAHOO.util.Dom.getRegion(this.minThumb);
	this.thumbWidth = this.thumbRegion.right - this.thumbRegion.left;
	
	this.currentScrollPos = 0;
	this.scrollStep = Math.abs(Math.ceil(this.maxScrollPos/5));
	this.sliderMaxRange = width-thumbSize;
	
	// Slider-Komponenten	
    this.slider = YAHOO.widget.Slider.getHorizSlider(this.sliderBox, "minthumb", 0, this.sliderMaxRange);
    this.slider.animate = true;
	this.slider.subscribe("change", this.onSliderChange, this, true);
	
	this.scrollTo(0);
	
} // Ende des Konstruktors: HTMLScroller()

YAHOO.cywalk.HTMLScroller.prototype.onSliderChange = function(newValue) {
	
	var scrollPos = Math.ceil((newValue/this.sliderMaxRange)*this.maxScrollPos);
	this.scrollTo(scrollPos, true);
	
} // Ende der Methode: onSliderChange()

YAHOO.cywalk.HTMLScroller.prototype.onMouseWheel = function(e) {
	
	var e = e ? e : window.event;
  	var wheelData = e.detail ? e.detail *-40 : e.wheelDelta;
	
	if (wheelData > 0) {
		if (this.currentScrollPos < 0) {
			this.scrollTo(this.currentScrollPos + wheelData);
		}
		else if (this.currentScrollPos > 0) {
			this.scrollTo(0);
		}
	}
	else if (wheelData < 0) {
		if (this.currentScrollPos > this.maxScrollPos) {
			this.scrollTo(this.currentScrollPos + wheelData);
		}
		else if (this.currentScrollPos < this.maxScrollPos) {
			this.scrollTo(this.maxScrollPos);
		}
	}

	// Der Event soll nicht weiter gegeben werden.
	YAHOO.util.Event.stopEvent(e);

} // Ende der Methode: onMouseWheel()

YAHOO.cywalk.HTMLScroller.SCROLL_UP = -1;
YAHOO.cywalk.HTMLScroller.SCROLL_UP_MAX = -2;
YAHOO.cywalk.HTMLScroller.SCROLL_DOWN = 1;
YAHOO.cywalk.HTMLScroller.SCROLL_DOWN_MAX = 2;

YAHOO.cywalk.HTMLScroller.prototype.scroll = function(direction) {
	
	var scrollStart = this.currentScrollPos;
	var scrollEnd = scrollStart;
	
	switch(direction) {
		case YAHOO.cywalk.HTMLScroller.SCROLL_DOWN:
			if ((scrollStart - this.scrollStep) > this.maxScrollPos) {
				scrollEnd = scrollStart-=this.scrollStep;
			}
			else {
				scrollEnd = this.maxScrollPos;
			}
			break;
			
		case YAHOO.cywalk.HTMLScroller.SCROLL_DOWN_MAX:
			scrollEnd = this.maxScrollPos;
			break;
			
		case YAHOO.cywalk.HTMLScroller.SCROLL_UP:
			if ((scrollStart + this.scrollStep) < 0) {
				scrollEnd = scrollStart+=this.scrollStep;
			}
			else {
				scrollEnd = 0;
			}
			break;
			
		case YAHOO.cywalk.HTMLScroller.SCROLL_UP_MAX:
			scrollEnd = 0;
			break;
			
		default:
			return;
	}
	
	this.scrollTo(Math.ceil(scrollEnd));
	
} // Ende der Methode: scroll()

YAHOO.cywalk.HTMLScroller.prototype.scrollTo = function(position, silent) {
	
//	var positionDelta = (YAHOO.env.ua.ie) ? YAHOO.util.Dom.getY(this.box) : 0;
	var positionDelta = YAHOO.util.Dom.getY(this.box);
	
	YAHOO.util.Dom.setY(this.content, position + positionDelta);
	
	this.currentScrollPos = position;
	
	// Nur ausf�hren wenn das Silent-Flag nicht gesetzt wurde
	if (YAHOO.lang.isUndefined(silent) || (silent === false)) {
		this.slider.setValue((position/this.maxScrollPos)*this.sliderMaxRange, false, true, true);
	}
} // Ende der Methode: scrollTo
