var Presentation = Class.create();
Presentation.prototype =
{
	initialize: function( element )
	{
		var slide = element.select( '.slide' )[ 0 ];
		var slidesNavElement = element.select( '.slidesNavigator' )[ 0 ];
		var bookmarksNavElement = element.select( '.bookmarksNavigator' )[ 0 ];
		
		this.slideImage = slide.select( 'img' )[ 0 ];
		this.bookmarkLabel = element.select( '.bookmark' )[ 0 ];
		this.selector = this.createSelector( slidesNavElement, bookmarksNavElement );
		this.handleEvents( slide, slidesNavElement, bookmarksNavElement );
		this.nextSlide();
	},
	
	loadSlide: function( url )
	{
		this.slideImage.writeAttribute( 'src', url );
		this.selector.select( url );
	},
	
	nextSlide: function()
	{
		this.loadSlide( this.selector.getNextUrl() );
	},
	
	createSelector: function( slidesNavElement, bookmarksNavElement )
	{
		var selectionMap = $H();
		var pager = new Pager( slidesNavElement, 10 );
		var slidesNavigators = slidesNavElement.select( 'li' );
		var bookmarksNavigators = bookmarksNavElement.select( 'li' );
		
		var bookmarksNavigator = undefined;
		for( var i = 0; i < slidesNavigators.length; i++ ) {
			var url = slidesNavigators[ i ].select( 'a' )[ 0 ].readAttribute( 'href' );
			for( var k = 0; k < bookmarksNavigators.length; k++ )
				if( url == bookmarksNavigators[ k ].select( 'a' )[ 0 ].readAttribute( 'href' ) )
					bookmarksNavigator = bookmarksNavigators[ k ];
			var navigators = $A( [ slidesNavigators[ i ], bookmarksNavigator ] ).compact();
			selectionMap.set( url, { index: i, navigators: navigators, bookmark: bookmarksNavigator.firstChild.firstChild.nodeValue } );
		}
		
		return {
			pager: pager,
			bookmarkLabel: this.bookmarkLabel,
			selectionMap: selectionMap,
			index: undefined,
			select: function( url ) {
				slidesNavigators.concat( bookmarksNavigators ).each( function( item ) { item.removeClassName( 'selected' ) } );
				var selection = this.selectionMap.get( url );
				selection.navigators.each( function( item ) { item.addClassName( 'selected' ) } );
				this.pager.select( selection.index );
				this.bookmarkLabel.innerHTML = selection.bookmark;
				this.index = selection.index;
			},
			getNextUrl: function() {
				var nextIndex = ( this.index == undefined || this.index == slidesNavigators.length - 1 ) ? 0 : this.index + 1;
				var nextUrl = undefined;
				this.selectionMap.each(
					function( item ) {
						if( item.value.index == nextIndex )
							nextUrl = item.key;
					}
				);
				return nextUrl;
			}
		};
	},
	
	handleEvents: function( slide, slidesNavElement, bookmarksNavElement )
	{
		var anchors = slidesNavElement.select( 'a' ).concat( bookmarksNavElement.select( 'a' ) );
		anchors.each(
			function( item ) {
				var url = item.readAttribute( 'href' );
				item.observe( 'click', 	this.loadSlide.bind( this, url ) );
				item.writeAttribute( 'href', '#' );
				item.onclick = function() { return false };
			}.bind( this )
		);
		slide.observe( 'click', this.nextSlide.bind( this ) );
	}
}
function initPresentation() {
	$$('.presentation').each( function(item) { new Presentation(item) } );
}
document.observe( 'dom:loaded', initPresentation );		
