	Element.addMethods({
	  scrollTo: function(element, left, top){
	    var element = $(element);
	    if (!element) return;
	    if (arguments.length == 1){
	      var pos = element.cumulativeOffset();
	      window.scrollTo(pos[0], pos[1]);
	    } else {
	    	if (!element) return;
		    element.scrollLeft = left;
		    element.scrollTop  = top;
	    }
	    return element;
	  }
	});


    Object.extend(Event, {
		wheel:function (event) {
			var delta = 0;
			if (!event) event = window.event;
			if (event.wheelDelta) {
			    delta = event.wheelDelta/120; 
				if (window.opera) delta = -delta;
			} else if (event.detail) { delta = -event.detail/3;}
			return Math.round(delta);
		}
	});
    
    function disableSelection(target){
		if (typeof target.onselectstart!="undefined") target.onselectstart=function(){return false;}
		else if (typeof target.style.MozUserSelect!="undefined") target.style.MozUserSelect="none"; else target.onmousedown=function(){return false};
	}
    
    var CarusselClass = Class.create();
	
	CarusselClass.prototype = {
		initialize: function (params) {
           // console.log("init");
			this.posAt = 0;
			this.params			= Object.extend({
				obj: false,
				ll: false,
				rl: false,
				dir: 'horizontal',
				step: 1,
				additionalFunction: null,
				itemSize: 'auto',
				onRoll: null,
				scrollBar: null,
				observeKey: true
			}, params || {});
			if (this.params.obj) {
				this.obj		= this.params.obj;
				this.dir		= (this.params.dir == 'horizontal');
				if (this.obj.select('.objholder').length) {
					this.objHolder	= this.obj.select('.objholder').first();
				} else {
					this.objHolder	= this.obj;
				}
				this.objs		= [];
				this.obj.select('.obj').each(function(o, i) {
					o.xSize = (this.params.itemSize == 'auto' ?(this.dir?o.getWidth():o.getHeight()) : this.params.itemSize);
					this.objs.push(o);
				}.bind(this));
				disableSelection(this.params.ll);
				disableSelection(this.params.rl);
				this.params.ll.observe('mousedown', this.roll.bind(this, -1, true, 0, false, true));
				this.params.ll.observe('mouseup', this.stopAutomaticRoll.bind(this));
				this.params.ll.observe('mouseout', this.stopAutomaticRoll.bind(this));				
				this.params.rl.observe('mousedown', this.roll.bind(this, 1, true, 0, false, true));
				this.params.rl.observe('mouseup', this.stopAutomaticRoll.bind(this));
				this.params.rl.observe('mouseout', this.stopAutomaticRoll.bind(this));						
				if (this.params.observeKey == true) {
					Event.observe(document, 'keydown', this.onKey.bindAsEventListener(this));
				}
				this.params.ll.observe('dblclick', this.roll.bind(this, -1, true));
				this.params.rl.observe('dblclick', this.roll.bind(this, 1, true));
				this.objScroller	= this.objHolder.select('.in').first();

				if (!this.dir) {
					Event.observe(this.obj, 'mousewheel', this.onWheel.bindAsEventListener(this));
					Event.observe(this.obj, 'DOMMouseScroll', this.onWheel.bindAsEventListener(this));	
				}
				this.maxScroll		= Math.max(0, (this.dir?this.objScroller.getWidth():this.objScroller.getHeight()) - (this.dir?this.objHolder.getWidth():this.objHolder.getHeight()));
				if (this.params.scrollBar) {
					if (!this.dir) {
						Event.observe(this.params.scrollBar, 'mousewheel', this.onWheel.bindAsEventListener(this));
						Event.observe(this.params.scrollBar, 'DOMMouseScroll', this.onWheel.bindAsEventListener(this));		
					}
					this.scrollBar	= this.params.scrollBar;
					this.scroller	= this.scrollBar.select('.sci').first();
					if (this.scroller.hasClassName('xxx') && isIE7) {
						this.scroller.style.position = 'static';
					}					
					if (!this.dir) {
						var dim = this.objHolder.getHeight() / this.objScroller.getHeight();
					} else {
						var dim = this.objHolder.getWidth() / this.objScroller.getWidth();
					}
					if (dim < 1) {
						this.params.ll.addClassName('unon');	
						this.params.ll.style.visibility = 'visible';
						this.params.rl.style.visibility = 'visible';
						this.scroller.removeClassName('passive');	
						if (!this.dir) {
							var w		= this.scrollBar.getHeight();
							var wSmall	=  Math.max(20, Math.ceil(w*dim));
							this.scroller.style.height	= wSmall+'px';
							if (this.scroller.select('.helper').length) this.scroller.select('.helper').first().style.height = wSmall+'px';
						} else {
							var w = this.scrollBar.getWidth();
							var wSmall	=  Math.max(20, Math.ceil((w*dim)));
							this.scroller.style.width	= wSmall+'px';
						}
						this.wSmall			= wSmall;
						
						disableSelection(this.scrollBar);
						disableSelection(this.scroller);

						this.scrollBarMax	= w - wSmall;
						this.scrollBarRatio	= dim;

						this.scrollBar.observe('mousedown', function(e, wSmall) {
							if (Event.element(e) == this.scroller || Event.element(e).descendantOf(this.scroller)) return;
							var scrollTo	= (Event.pointerY(e) - Element.cumulativeOffset(this.scrollBar).top) / this.scrollBar.getHeight();
							this.roll((this.objScroller.getHeight()-this.objHolder.getHeight()) * scrollTo );
						}.bindAsEventListener(this, wSmall));
						
						this.scroller.observe('mousedown', function(e, wSmall) {
							this.scroller.addClassName('on');
							this.MOffset = Event.pointerY(e) - Element.cumulativeOffset(this.scroller).top;
							Event.observe(document, 'mousemove', function(e){
								var offset = 0;
								var scrollTo	= (Event.pointerY(e) - Element.cumulativeOffset(this.scrollBar).top - this.MOffset) / this.scrollBarMax;
								this.roll((this.objScroller.getHeight()-this.objHolder.getHeight()) * scrollTo, false, true, true);
							}.bind(this));
						}.bindAsEventListener(this));	
											
						document.observe('mouseup', function() {
							Event.stopObserving(document, 'mousemove'); 
							this.scroller.removeClassName('on');
						}.bind(this));		
						if (document.location.hash != '#' && document.location.hash != '') {
							window.setTimeout(function() {this.inRoll(this.objHolder.scrollTop)}.bind(this), 500);	
						}						
					} else {
						
					}
				}
			}
		},
		
/*		reInitializeHeight: function() {
			this.maxScroll		= Math.max(0, (this.dir?this.objScroller.getWidth():this.objScroller.getHeight()) - (this.dir?this.objHolder.getWidth():this.objHolder.getHeight()));			
			this.objs.each(function(o, i) {
				o.xSize = (this.params.itemSize == 'auto' ?(this.dir?o.getWidth():o.getHeight()) : this.params.itemSize);
			}.bind(this));	
			
			if (!this.dir) {
				var dim = this.objHolder.getHeight() / this.objScroller.getHeight();
			} else {
				var dim = this.objHolder.getWidth() / this.objScroller.getWidth();
			}					
			
			if (!this.dir) {
				var w		= this.scrollBar.getHeight();
				var wSmall	=  Math.max(40, Math.ceil(w*dim));
				this.scroller.style.height	= wSmall+'px';
				if (this.scroller.select('.helper').length) this.scroller.select('.helper').first().style.height = wSmall+'px';
			} else {
				var w = this.scrollBar.getWidth();
				var wSmall	=  Math.max(40, Math.ceil((w*dim)));
				this.scroller.style.width	= wSmall+'px';
			}
			this.wSmall			= wSmall;
			this.scrollBarMax	= w - wSmall;
			this.scrollBarRatio	= dim;
			this.inRoll(this.lastinRoll);
			return true;
		},*/
				
		roll: function(pos, wheel, nopos, noDuration, continuous) {	
			var okPos = pos * 1;
			var onLimit = false;
			var prePos = this.posAt * 1;
			if (!wheel) {
				this.posAt = pos;
			} else {
				var pos = pos * 60;
				this.posAt += pos;
			}
			if (this.posAt <= 0) {
				this.posAt = 0;
				onLimit = (prePos == 0);
			}
			var scrollTo = this.posAt;
			if (scrollTo >= this.maxScroll) {
				scrollTo	= this.maxScroll;
				this.posAt	= this.maxScroll;
				onLimit		= (prePos == this.maxScroll);
			}
			if (this.tween) {
				this.tween.cancel();
			}
			this.tweenStart		= (this.dir?this.objHolder.scrollLeft:this.objHolder.scrollTop);
			this.tweenSize		= scrollTo - this.tweenStart;
			this.tween			=  new Effect.Tween(null,
				0,
				1,
				{	
					duration: (noDuration ? .05 : (!wheel?.05:.05)),
					afterUpdate: (this.params.onRoll ?this.params.onRoll:null),
					afterFinish : (this.params.onRoll ?this.params.onRoll:null)
				},
				function(p) {
					var add = -Math.pow(p-1, 4)+1;
					this.inRoll(this.tweenStart + this.tweenSize * add);		
				}.bind(this)
			);
			if (continuous) {
				if (!onLimit) {
					if (this.automaticroll)	window.clearTimeout(this.automaticroll);
					this.automaticroll = window.setTimeout(this.roll.bind(this, okPos, wheel, nopos, false, true), 200);					
				}
			}
			return onLimit;
		},
		stopAutomaticRoll: function() {
			if (this.automaticroll) window.clearTimeout(this.automaticroll);
		},
		inRoll: function(value) {
			this.lastinRoll = value;
			if (this.dir) {
				this.objHolder.scrollTo(value, 0);
			} else {
				this.objHolder.scrollTo(0, value);
			}
			if (Math.round(value) <= 0) {
				this.params.ll.addClassName('unon');	
			} else {
				this.params.ll.removeClassName('unon');	
			}
			if (value >= this.maxScroll) {
				this.params.rl.addClassName('dnon');	
			} else {
				this.params.rl.removeClassName('dnon');	
			}				
			if (this.params.additionalFunction) {
				this.params.additionalFunction(value);	
			}
			if (this.scrollBarRatio) {
				if (this.scroller.hasClassName('xxx') && !isIE7) {
					this.scroller.style.top = (value / (this.objScroller.getHeight()-this.objHolder.getHeight()))*this.scrollBarMax + 'px';
				} else {
					this.scroller.style.marginTop = (value / (this.objScroller.getHeight()-this.objHolder.getHeight()))*this.scrollBarMax + 'px';
				}
			}
		},
		onWheel: function(e) {
			var ew = Event.wheel(e);
			if (!this.roll(ew*-1, true)) Event.stop(e);
		},
		onKey: function(event) {
			var key_code = event.charCode ? event.charCode : event.keyCode;
			if (Event.KEY_DOWN == key_code || Event.KEY_PAGEDOWN == key_code) {
				if (!this.roll(1, true)) Event.stop(event);								
			} else if (Event.KEY_UP == key_code || Event.KEY_PAGEUP == key_code) {
				if (!this.roll(-1, true)) Event.stop(event);				
			}		
		},
		scrollToObject: function(o) {
			if (o && o.tagName == 'A' && o.name) {
				document.location.hash = '#'+o.name;
				this.inRoll(this.objHolder.scrollTop);
				this.posAt = this.objHolder.scrollTop;
			}
		}
	}
    var f = function() {
        
        termekCarussel = new CarusselClass({
            obj: $('termekHolder'),
            ll: $('contentScroller').select('.u').first(),
            rl: $('contentScroller').select('.d').first(),
            scrollBar: $('contentScroller').select('.sc').first(),
            dir: 'vertical',
            step: 1,
            itemSize: 100,
            onRoll: null
        });
        console.log($('termekHolder'));
    }
