/*
	This looks at each of the items in the scroll sets array and sets them to scroll if the number 
	contained excedes the maxNumberofItems property
*/
var ScrollFrameDelegate = {
	scrollSets : [
/*	SAMPLE OBJECT		
	{
		'scrollContainer' : [string - css selector of the container element ], 
		'scrollItemSelector' : [string - css selector of the item element], 
		'maxNumberofItems' : [int - number that if exceded sets the scroll] 
	}
*/
		{'scrollContainer' : '.relatedPersonList', 'scrollItemSelector' : 'li', 'maxNumberofItems' : 10 },
		{'scrollContainer' : '.relatedList', 'scrollItemSelector' : 'li', 'maxNumberofItems' : 10 },
		{'scrollContainer' : '.relatedImageList', 'scrollItemSelector' : '.relatedTeaser', 'maxNumberofItems' : 5 }
	],
	initialize : function () {
		for(var i = 0, l = this.scrollSets.length; i<l ; i++ ) {
			var ss = this.scrollSets[i];
			var scrollContainers = $$(ss.scrollContainer);
			for(var j = 0, l2 = scrollContainers.length; j<l2; j++ ) {
				var sc = scrollContainers[j];
				var sis = sc.getElements(ss.scrollItemSelector);
				if (sis.length>ss.maxNumberofItems) {
					var height = 0;
					for(var k = 0, l3 = (ss.maxNumberofItems - 1); k<l3; k++ ) {
						height = height + sis[k].getCoordinates().height;
					}
					new ScrollFrame(sc, height - 1);
				}
			}
		}
	}
};
var ScrollFrame = new Class({
	initialize : function (container, height) {
		container.addClass('scrollFrame')
		this.content = new Element('div', {'class': 'scrollFrame_textContent'});
		this.content.adopt(container.getChildren());
		container.adopt(this.content);
		var scrollBar = new Element('div', {'class': 'scrollFrame_scrollBar'});
		container.adopt(scrollBar);
		var scrollUpBtn = new Element('div', {'class': 'scrollFrame_scrollupBtn'});
		scrollBar.adopt(scrollUpBtn);
		var track = new Element('div', {'class': 'scrollFrame_track'});
		scrollBar.adopt(track);
		var handle = new Element('div', {'class': 'scrollFrame_handle'});
		track.adopt(handle);
		var scrollDownBtn = new Element('div', {'class': 'scrollFrame_scrollDownBtn'});
		scrollBar.adopt(scrollDownBtn);
		
		container.setStyle('height', height+'px');
		track.setStyle('height', (height - scrollUpBtn.getCoordinates().height - scrollDownBtn.getCoordinates().height));
		
		var gs = container.getSize();
		var steps = (gs.scrollSize.y - gs.size.y);
		this.slider = new Slider(track, handle, {	
			steps: steps,
			mode: 'vertical',
			onChange: this.doScrollContent.bind(this)
		});
		this.slider.set(0);
		
		this.content.addEvent('mousewheel', this.doWheelStep.bindAsEventListener(this));
		track.addEvent('mousewheel', this.doWheelStep.bindAsEventListener(this));
		
		scrollUpBtn.addEvent('mousedown', this.doInitButtonButton.pass(['up'], this));
		scrollUpBtn.addEvent('mouseup', this.doCancelButton.bind(this));
		scrollUpBtn.addEvent('mouseleave', this.doCancelButton.bind(this));
		
		scrollDownBtn.addEvent('mousedown', this.doInitButtonButton.pass(['down'], this));
		scrollDownBtn.addEvent('mouseup', this.doCancelButton.bind(this));
		scrollDownBtn.addEvent('mouseleave', this.doCancelButton.bind(this));
		
	},
	doCancelButton: function () {
		this.buttonIsDepressed = false;
	},
	doInitButtonButton: function (dir) {
		this.buttonIsDepressed = true;
		this.doButtonStep(dir);
	},
	doButtonStep: function(dir) {
		if (this.buttonIsDepressed == true) {
			if (dir == 'up') {
				var newStep = this.slider.step - 30;
			} else {
				var newStep = this.slider.step + 30;
			}
			this.slider.set(newStep);
			this.doButtonStep.delay(150, this, [dir]);
		}
	},
	doScrollContent: function() {
		this.content.setStyle('top', '-' + this.slider.step + 'px');
	},
	doWheelStep: function(e) {
		e = new Event(e).stop();
		var newStep = this.slider.step - e.wheel * 30;
		this.slider.set(newStep);
	}
});