


var MediaSlider = Class.create({
	slider: null,
	options: null,
	panelDimentions: null,
	nbPanels: 0,
	nbElementsInPanel: 0,
	currentPanelIndex: 0,
	
	initialize: function( slider, options )
	{
		this.slider = $(slider);
		if ( !this.slider ) throw ("The slider object doesn't exists.");
		
		this.options = Object.extend({
			direction: 'horizontal',
			duration: 1,
			onNextLimit: function() {},
			onPreviousLimit: function() {},
			onNoLimit: function() {}
		}, options || {});
		
		this.panelDimentions = this.slider.getDimensions();
		if ( this.options.direction == 'horizontal' )
		{
			this.nbPanels = Math.round( (this.slider.down().getWidth())/this.panelDimentions.width );
			try {
				this.nbElementsInPanel = Math.floor( this.panelDimentions.width/this.slider.down().down().getWidth() );
			} catch (e) {this.nbElementsInPanel = 0;}
		}
		else
		{
			this.nbPanels = Math.round( (this.slider.down().getHeight())/this.panelDimentions.height );
			try {
				this.nbElementsInPanel = Math.floor( this.panelDimentions.height/this.slider.down().down().getHeight() );
			} catch (e) {this.nbElementsInPanel = 0;}
		}
		
		this.currentPanelIndex = 0;
		
		this.options.onPreviousLimit();
		if ( this.nbPanels < 2 )
			this.options.onNextLimit();
		
		var selected_thumbs;
		// URL Anchors
		if ( window.location.hash.match(/^#m_/) )
			this.slideToElement( window.location.hash.substring(1) );
		// Selected thumb
		else if ( ( selected_thumbs = $$('a.thumb.selected') ).length === 1 )
			this.slideToElement( selected_thumbs[0], false );
	},
	
	getCleanIndex: function( index )
	{
		if ( index < 0 ) return 0;
		else if ( index >= this.nbPanels ) return this.nbPanels-1;
		else return index;
	},
	
	slideToElement: function( element, animate )
	{
		element = $(element);
		if ( element )
		{
			var panelIndex = Math.floor( (this.slider.down().childElements().indexOf(element))/this.nbElementsInPanel );
			this.slideTo(panelIndex, animate);
		}
	},
	
	slideTo: function( index, animate )
	{
		index = this.getCleanIndex(index);
		if ( index == this.currentPanelIndex )
			return false;
			
		if ( index == 0 )
		{
			this.options.onPreviousLimit();
			if ( this.currentPanelIndex == this.nbPanels-1 )
				this.options.onNoLimit('fromNext');
		}
		else if ( index == this.nbPanels-1 )
		{
			this.options.onNextLimit();
			if (this.currentPanelIndex == 0 )
				this.options.onNoLimit('fromPrevious');
		}
		else
		{
			if (this.currentPanelIndex == 0 )
				this.options.onNoLimit('fromPrevious');
			else if ( this.currentPanelIndex == this.nbPanels-1 )
				this.options.onNoLimit('fromNext');
		}

		if ( this.options.direction == 'horizontal' )
		{
			if ( animate === false ) {
				this.slider.scrollLeft = ( index * this.panelDimentions.width );
			} else {
				new Effect.ScrollHorizontal( this.slider, {
					to: ( index * this.panelDimentions.width ),
					duration: this.options.duration
				});
			}
		}
		else
		{
			if ( animate === false ) {
				this.slider.scrollTop = ( index * this.panelDimentions.height );
			} else {
				new Effect.ScrollVertical( this.slider, {
					to: ( index * this.panelDimentions.height ),
					duration: this.options.duration
				});
			}
		}
		this.currentPanelIndex = index;
	},
	
	next: function()
	{
		this.slideTo( this.currentPanelIndex+1 );
	},
	
	previous: function()
	{
		this.slideTo( this.currentPanelIndex-1 );
	}
});

var Slider;
Event.observe( window, 'load', function() {
	Slider = new MediaSlider('scroller', {
		onNextLimit: function() {
			$$('a.next').invoke('setStyle',{'visibility':'hidden'});
		},
		onPreviousLimit: function() {
			$$('a.previous').invoke('setStyle',{'visibility':'hidden'});
		},
		onNoLimit: function(from) {
			if ( from == 'fromPrevious' )
				$$('a.previous').invoke('setStyle',{'visibility':'visible'});
			else
				$$('a.next').invoke('setStyle',{'visibility':'visible'});
		}
	});
});