/*
 * Korrekturen zum Verhalten des Sliders
 */

Slider.implement({
	clickedElement: function(event){
		/*
		 * Wenn man auf den Klotz klickt, sollte dieser nicht mit der Mitte unter
		 * die Maus springen.
		 */
		var knPos = this.knob.getPosition()[this.axis];
		var knSize = this.knob.getSize()[this.axis];
		if(
			event.page[this.axis] < knPos ||
			event.page[this.axis] > (knPos+knSize)
		) {
			var dir = this.range < 0 ? -1 : 1;
			position = event.page[this.axis] - this.element.getPosition()[this.axis] - this.half;
			position = position.limit(-this.options.offset, this.full -this.options.offset);

			this.step = Math.round(this.min + dir * this.toStep(position));
			this.checkStep();
			this.end();
			this.fireEvent('tick', position);
			/*
			 * Hier muss das Dragging explizit gestartet werden!
			 * @url http://n2.nabble.com/Bug-in-Slider-%28jumping-knob%29-td1340440.html
			 */
			this.drag.start(event);
		};
	}
});

/*
 * @link http://solutoire.com/2008/03/10/mootools-css-styled-scrollbar/
 * Sehr stark gepatchte Version des obigen Skripts.
 */
var Scrollbar = new Class({
	slider: false,
	initialize: function(content,scrollbar,handle,horizontal,ignoreMouse,handleStart,mousewheelCallback) {
		var _this = this;
		var size = content.getSize();
		var scroll = content.getScrollSize();
		var steps = (horizontal?(scroll.x-size.x):(scroll.y-size.y));
		/*
		 * Fix für aktuelle Opera-Browser. In die Berechnung des scrollbaren Bereichs
		 * fließt scheinbar das padding oben und unten nicht mit ein.
		 */
		if(Browser.Engine.presto && !horizontal) {
			steps += content.getStyle('padding-top').toInt()+content.getStyle('padding-bottom').toInt();
		};
		this.slider = new Slider(scrollbar, handle, {
			steps: steps,
			mode: (horizontal?'horizontal':'vertical'),
			onChange: function(step){
				//Scrolls the content element in x or y direction.
				var x = (horizontal?step:0);
				var y = (horizontal?0:step);
				content.scrollTo(x,y);
			}
		}).set(handleStart);
		if(!(ignoreMouse)){
			//Scroll the content element when the mousewheel is used within the
			//content or the scrollbar element.
			$$(content,scrollbar).addEvent('mousewheel', function(e){
				e = new Event(e).stop();
				var step = _this.slider.step - e.wheel * 30;
				_this.slider.set(step);
				if($chk(mousewheelCallback)) mousewheelCallback.run(e);
			});
		};
	}
});

window.addEvent('domready',function() {
	['content'].each(function(id) {
		var el = $(id);
		if($chk(el)) {
			//Allgemein Scrollbalken auf Element deaktivieren
			el.setStyle('overflow','hidden');
			//Ggf. Scrollbalken einblenden
			var height = el.getSize().y;
			var sHeight = el.getScrollSize().y;
			var hHeight = (height*(height/sHeight));
			var sPos = 0;
			if(location.href.match(/#c([0-9]+)/)) {
				var anchor = el.getElement('#c'+RegExp.$1);
				if($chk(anchor)) sPos = anchor.getPosition(el).y;
			};
			if(sHeight > height) {
				var bar = new Element('div',{'id': id+'_scrollbalken'});
				var handle = new Element('div',{'id': id+'_scrollklotz', 'styles': {'height': hHeight}});
				el.getParent().adopt(bar.adopt(handle));
				new Scrollbar(el,bar,handle,false,false,sPos,false);
			};
		};
	});
});
