
var Asset=new Hash({javascript:function(F,D){D=$extend({onload:$empty,document:document,check:$lambda(true)},D);var B=new Element("script",{src:F,type:"text/javascript"});
var E=D.onload.bind(B),A=D.check,G=D.document;delete D.onload;delete D.check;delete D.document;B.addEvents({load:E,readystatechange:function(){if(["loaded","complete"].contains(this.readyState)){E();
}}}).setProperties(D);if(Browser.Engine.webkit419){var C=(function(){if(!$try(A)){return ;}$clear(C);E();}).periodical(50);}return B.inject(G.head);},css:function(B,A){return new Element("link",$merge({rel:"stylesheet",media:"screen",type:"text/css",href:B},A)).inject(document.head);
},image:function(C,B){B=$merge({onload:$empty,onabort:$empty,onerror:$empty},B);var D=new Image();var A=$(D)||new Element("img");["load","abort","error"].each(function(E){var F="on"+E;
var G=B[F];delete B[F];D[F]=function(){if(!D){return ;}if(!A.parentNode){A.width=D.width;A.height=D.height;}D=D.onload=D.onabort=D.onerror=null;G.delay(1,A,A);
A.fireEvent(E,A,1);};});D.src=A.src=C;if(D&&D.complete){D.onload.delay(1);}return A.setProperties(B);},images:function(D,C){C=$merge({onComplete:$empty,onProgress:$empty},C);
if(!D.push){D=[D];}var A=[];var B=0;D.each(function(F){var E=new Asset.image(F,{onload:function(){C.onProgress.call(this,B,D.indexOf(F));B++;if(B==D.length){C.onComplete();
}}});A.push(E);});return new Elements(A);}});
 
 
 
 
 
 
 Fx.Scroll = new Class({

	Extends: Fx,

	options: {
		offset: {'x': 0, 'y': 0},
		wheelStops: true
	},

	initialize: function(element, options){
		this.element = this.subject = $(element);
		this.parent(options);
		var cancel = this.cancel.bind(this, false);

		if ($type(this.element) != 'element') this.element = $(this.element.getDocument().body);

		var stopper = this.element;

		if (this.options.wheelStops){
			this.addEvent('start', function(){
				stopper.addEvent('mousewheel', cancel);
			}, true);
			this.addEvent('complete', function(){
				stopper.removeEvent('mousewheel', cancel);
			}, true);
		}
	},

	set: function(){
		var now = Array.flatten(arguments);
		this.element.scrollTo(now[0], now[1]);
	},

	compute: function(from, to, delta){
		var now = [];
		var x = 2;
		x.times(function(i){
			now.push(Fx.compute(from[i], to[i], delta));
		});
		return now;
	},

	start: function(x, y){
		if (!this.check(arguments.callee, x, y)) return this;
		var offsetSize = this.element.getSize(), scrollSize = this.element.getScrollSize();
		var scroll = this.element.getScroll(), values = {x: x, y: y};
		for (var z in values){
			var max = scrollSize[z] - offsetSize[z];
			if ($chk(values[z])) values[z] = ($type(values[z]) == 'number') ? values[z].limit(0, max) : max;
			else values[z] = scroll[z];
			values[z] += this.options.offset[z];
		}
		return this.parent([scroll.x, scroll.y], [values.x, values.y]);
	},

	toTop: function(){
		return this.start(false, 0);
	},

	toLeft: function(){
		return this.start(0, false);
	},

	toRight: function(){
		return this.start('right', false);
	},

	toBottom: function(){
		return this.start(false, 'bottom');
	},

	toElement: function(el){
		var position = $(el).getPosition(this.element);
		return this.start(position.x, position.y);
	}

});

 
 
 
 Fx.Scroll2 = new Class({
	  
		 'Extends': Fx.Scroll,
	  
		 'styleString': Element.getComputedStyle,
		 'styleNumber': function(element, style) {
			 return this.styleString(element, style).toInt() || 0;
		 },
		 'borderBox': function(element) {
			 return this.styleString(element, '-moz-box-sizing') == 'border-box';
		 },
		 'topBorder': function(element) {
			 return this.styleNumber(element, 'border-top-width');
		 },
		'leftBorder': function(element) {
			 return this.styleNumber(element, 'border-left-width');
		 },
		 'isBody': function(element) {
			 return (/^(?:body|html)$/i).test(element.tagName);
		 }, 
		 'toElement': function(el) {
			 var offset   = {x: 0, y: 0};
			 var element  = $(el);
			
			 if (this.isBody(element)) {
				 return offset;
			 }
			 var scroll = element.getScrolls();
				   
			 while (element && !this.isBody(element)){
				 offset.x += element.offsetLeft;
				 offset.y += element.offsetTop;
				
				 if (Browser.Engine.gecko){
					 if (!this.borderBox(element)){
						 offset.x += this.leftBorder(element);
						 offset.y += this.topBorder(element);
					 }
					 var parent = element.parentNode;
					 if (parent && this.styleString(parent, 'overflow') != 'visible'){
						 offset.x += this.leftBorder(parent);
						 offset.y += this.topBorder(parent);
					 }
				 } else if (Browser.Engine.trident || Browser.Engine.webkit){
					 offset.x += this.leftBorder(element);
					 offset.y += this.topBorder(element);
				 }
	  
				 element = element.offsetParent;
				 if (Browser.Engine.trident) {
					 while (element && !element.currentStyle.hasLayout) {
						 element = element.offsetParent;
					 }
				 }
			 }
			 if (Browser.Engine.gecko && !this.borderBox(element)){
				 offset.x -= this.leftBorder(element);
				 offset.y -= this.topBorder(element);
			 }
			
			 var relative = this.element;
			 var relativePosition = (relative && (relative = $(relative))) ? relative.getPosition() : {x: 0, y: 0};
			 var position = {x: offset.x - scroll.x, y: offset.y - scroll.y};
			
			 return this.start(position.x - relativePosition.x, position.y - relativePosition.y);
		 }
	 });


var direction = null;

var Scroller = new Class({
	'Extends': Fx.Scroll2,
	Implements: [Events, Options],
	
	options: {
		area: 20,
		velocity: 1,
		onChange: function(x, y){
			var duration1 = Math.round(this.element.getScroll().x) * 2.5; 
			var duration2 = Math.round((this.element.getScrollSize().x - this.element.getSize().x-this.element.getScroll().x)) * 2.5; 
			if(duration1<750) {duration1 = 750}
			if(duration2<750) {duration2 = 750}
			
			if(x<0) {
				
				this.FxScroll.options.duration = duration1;
				
				this.FxScroll.toLeft();
			} else if(x>0) {
				this.FxScroll.options.duration = duration2;
					
				this.FxScroll.toRight();
			} else {
				this.FxScroll.pause();
			}
			
		}
	},

	initialize: function(element, options){
		this.setOptions(options);
		this.element = $(element);
		
		
		this.FxScroll = new  Fx.Scroll2(this.element,{wait: true  });
		this.listener = ($type(this.element) != 'element') ? $(this.element.getDocument().body) : this.element;
		this.timer = null;
		this.coord = this.getCoords.bind(this);
	},

	start: function(){
		this.listener.addEvent('mousemove', this.coord);
	},

	stop: function(){
		direction = null;
		this.FxScroll.pause();
		this.listener.removeEvent('mouseout', this.coord);
		this.timer = $clear(this.timer);
	},

	getCoords: function(event){
		
		this.page = (this.listener.get('tag') == 'body') ? event.client : event.page;
		if (!this.timer) this.timer = this.scroll.periodical(50, this);
		
	},

	scroll: function(){
    var size = this.element.getSize(), scroll = this.element.getScroll(), pos = this.element.getOffsets(), scrollSize = this.element.getScrollSize(), change = {'x': 0, 'y': 0};
    for (var z in this.page){
      if (this.page[z] < (this.options.area + pos[z]) && scroll[z] != 0)
        change[z] = (this.page[z] - this.options.area - pos[z]) * this.options.velocity;
      else if (this.page[z] + this.options.area > (size[z] + pos[z]) && scroll[z] + size[z] != scrollSize[z])
        change[z] = (this.page[z] - size[z] + this.options.area - pos[z]) * this.options.velocity;
    }
	  if(direction==null) {
      	this.fireEvent('change', [change.x,change.y]);
		if(change.x<0) {direction='left'}
		if(change.x>0) {direction='right'}
	  }
	  
	  if(change.x<0&&direction=='right') {
		  this.fireEvent('change', [change.x,change.y]);
		  direction = 'left';
	  }
	  
	  if(change.x>0&&direction=='left') {
		  this.fireEvent('change', [change.x,change.y]);
		  direction = 'right';
	  }
	  
	  if(change.x==0&&direction!=null) {
		  this.fireEvent('change', [change.x,change.y]);
		  direction = null;
	  }
	  
  }


});