var AgendaScroller = new Class({
	Implements: [Options, Events],
	
	options: {
		sButtonLeft: 'moveLeft',
		sButtonRight: 'moveRight',
		sLeftRightContainer: 'containerLR-body',
		sTimeContainer: 'containerLR-time',
		iLeftRightPosition: -840,
		iLeftRightMax: -2100,
		iLeftRightMin: 0,
		// 350ms voor 1 uur scrollen
		// 21 in agenda zichtbaar
		iLeftRightTime: 7350,
		iVisibleArea: 810,
		sButtonUp: 'moveUp',
		sButtonDown: 'moveDown',
		sUpDownContainer: 'containerUD',
		iUpDownPosition: 0,
		iUpDownMax: 0,
		iUpDownMin: 0,
		// 300ms voor per regel scrollen
		// 1 regel is 40px
		iUpDownTime: 300,
		
		sItemClass: 'div.timeUnit.event'
	},
	
	initialize: function(options){
		this.setOptions(options);
		
		$(this.options.sLeftRightContainer).setStyle('margin-left', this.options.iLeftRightPosition);
		$(this.options.sTimeContainer).setStyle('margin-left', this.options.iLeftRightPosition);
		
		this.lrAgendaScroll = new Fx.Morph(this.options.sLeftRightContainer, {
			transition: Fx.Transitions.linear,
			onComplete: this.deactivateButton
		});
		this.lrTimeScroll = new Fx.Morph(this.options.sTimeContainer, {
			transition: Fx.Transitions.linear
		});
		this.udScroll = new Fx.Morph(this.options.sUpDownContainer, {
			transition: Fx.Transitions.linear
		});
		
		this.checkFar;
		this.maskLeft = 0;
		this.maskRight = 0;
		
		this.itemCount = $$(this.options.sItemClass).length;
		this.dayCount = $$('div.day').length;

		this.options.iUpDownMax = (400 - (this.itemCount * 40) - (this.dayCount * 5)) > 0 
			? 0 : (400 - (this.itemCount * 40) - (this.dayCount * 5));
			
		// add scroll events to buttons
		this.addEvents();

		this.iLeftRightPosition = this.options.iLeftRightPosition;
		this.iUpDownPosition = this.options.iUpDownPosition;
	},
	
	scrollLeft: function(){
		$clear(this.checkFar);
		
		this.lrAgendaScroll.options.duration = (
			(this.iLeftRightPosition* -this.options.iLeftRightTime) / (this.options.iLeftRightMax * -1));
		this.lrAgendaScroll.start({
			'margin-left': [this.iLeftRightPosition,this.options.iLeftRightMin]
		});

		this.lrTimeScroll.options.duration = (
			(this.iLeftRightPosition*-this.options.iLeftRightTime) / (this.options.iLeftRightMax * -1));
		this.lrTimeScroll.start({
			'margin-left': [this.iLeftRightPosition,this.options.iLeftRightMin]
		});
		
		this.beginPeriodical(); 
	},
	
	deactivateButton: function(){
//		alert(this.name);
//		$(sButtonID).addClass('inactive');
	},
	
	beginPeriodical: function(){
		this.checkFar = this.checkPosition.periodical(50, this);
	},
	
	scrollRight: function(){
		$clear(this.checkFar);
		
		this.lrAgendaScroll.options.duration = (
			((this.options.iLeftRightMax - this.iLeftRightPosition) * -this.options.iLeftRightTime) / (this.options.iLeftRightMax * -1));
		this.lrAgendaScroll.start({
			'margin-left': [this.iLeftRightPosition,this.options.iLeftRightMax]
		});

		this.lrTimeScroll.options.duration = (
			((this.options.iLeftRightMax - this.iLeftRightPosition) * -this.options.iLeftRightTime) / (this.options.iLeftRightMax * -1));
		this.lrTimeScroll.start({
			'margin-left': [this.iLeftRightPosition,this.options.iLeftRightMax]
		});
		
		this.beginPeriodical();
	},
	
	checkPosition: function(){
		$$('div.timeUnit.event').each(function(oEl){
			sID = oEl.id.replace(/^event-/, '');
			
			if ((oEl.getStyle('margin-left').toInt()*-1) < (this.lrTimeScroll.element.getStyle('margin-left').toInt()-this.options.iVisibleArea)){
				$('farright-' + sID).removeClass('hidden');
			} else {
				$('farright-' + sID).addClass('hidden');
			}
			
			if (((oEl.getStyle('margin-left').toInt()+oEl.getStyle('width').toInt())*-1)
				< (this.lrTimeScroll.element.getStyle('margin-left').toInt() - 9))
			{
				$('farleft-' + sID).addClass('hidden');
			} else {
				$('farleft-' + sID).removeClass('hidden');
			}
		}.bind(this));
	},
	
	scrollUp: function(){
		this.udScroll.options.duration = (
			((this.iUpDownPosition/40) * -this.options.iUpDownTime));
		this.udScroll.start({
			'margin-top': [this.iUpDownPosition,this.options.iUpDownMin]
		});
	},

	scrollDown: function(){
		this.udScroll.options.duration = (
			((this.iUpDownPosition - this.options.iUpDownMax)/40) * this.options.iUpDownTime);
		this.udScroll.start({
			'margin-top': [this.iUpDownPosition,this.options.iUpDownMax]
		});
	},
	
	scrollStop: function(sScrollType){
		$clear(this.checkFar);
		
		if (sScrollType == 'LR'){
			this.lrAgendaScroll.cancel();
			this.lrTimeScroll.cancel();
			this.iLeftRightPosition = 
				this.lrAgendaScroll.element.getStyle('margin-left').replace(/([^\d-]*)/g, '');
		}
		
		if (sScrollType == 'UD'){
			this.udScroll.cancel();
			this.iUpDownPosition = 
				this.udScroll.element.getStyle('margin-top').replace(/([^\d-]*)/g, '');
		}
	},
	
	addEvents: function(){
		$(this.options.sButtonLeft).addEvent('mousedown', function(){
			$(this.options.sButtonLeft).addClass('down');
			this.scrollLeft();
		}.bind(this));
		
		$(this.options.sButtonRight).addEvent('mousedown', function(){
			$(this.options.sButtonRight).addClass('down');
			this.scrollRight();
		}.bind(this));
		
		$$($(this.options.sButtonLeft), $(this.options.sButtonRight)).addEvent('mouseup', function(oEvent){
			$(oEvent.target.id).removeClass('down');
			$clear(this.checkFar);
			this.scrollStop('LR');
		}.bind(this));
		
		$$($(this.options.sButtonLeft), $(this.options.sButtonRight)).addEvent('mouseout', function(oEvent){
			$(oEvent.target.id).removeClass('down');
			$clear(this.checkFar);
			this.scrollStop('LR');
		}.bind(this));
		
		$(this.options.sButtonUp).addEvent('mousedown', function(){
			$(this.options.sButtonUp).addClass('active');
			this.scrollUp();
		}.bind(this));
		
		$(this.options.sButtonDown).addEvent('mousedown', function(){
			$(this.options.sButtonDown).addClass('active');
			this.scrollDown();
		}.bind(this));
		
		$$($(this.options.sButtonUp), $(this.options.sButtonDown)).addEvent('mouseup', function(oEvent){
			$(oEvent.target.id).removeClass('active');
			this.scrollStop('UD');
		}.bind(this));
		
		$$($(this.options.sButtonUp), $(this.options.sButtonDown)).addEvent('mouseout', function(oEvent){
			$(oEvent.target.id).removeClass('active');
			this.scrollStop('UD');
		}.bind(this));
	},
	
	reInit: function(){
		$clear(this.checkFar);
		
		// remove events 
		this.removeEvents('mouseup');
		this.removeEvents('mouseout');
		this.removeEvents('mousedown');

		// reset timebar margin
		$('containerLR-time').setStyle('margin-left', 0);
		
		
		this.initialize(this.options);
	}
});