var View = {
	
	//----------------------------------------------------------
	// Initialize
	//
	
	Initialize: function( Webroot, EditURL, DeleteURL, Data )
	{
		
		if( window.location.hash )
			var ID = this.GetHashID( );
		
		this.EditURL = EditURL;
		this.DeleteURL = DeleteURL;
		this.Webroot = Webroot;
		this.Data = Data;
		this.Current = 0;
		//this.Pagination( );
		
		this.SelectPage( ID >= 1 ? ID - 1 : 0 );
		$( document ).keyup( this.DetermineKey );
		
		/*if( ID >= 1 )
			$( '#pages a' ).eq( ID - 1 ).click( );
		else
			$( '#pages a' ).eq( 0 ).click( );*/
		
	},
	
	//----------------------------------------------------------
	// Get the initial page
	//
	
	GetHashID: function( )
	{
		
		return Number( window.location.hash.substr( 1 ) );
		
	},
	
	//----------------------------------------------------------
	// View a picture
	//
	
	View: function( Data )
	{
		
		this.ImageID = Data[0];
		$( '#image' ).css( 'background-image', 'url(' + this.Webroot + 'img/uploads/' + Data[1] + ')' );			
		var attr = $( '#attributes' ).html( Data[2] );
		if( Data[3] )
			attr.append( '<br /><br /><a href="' + Data[3] + '">movie</a>' );
		
		// Fill in links
		
		$( '#picture-edit' ).attr( 'href', this.EditURL + this.ImageID );
		$( '#picture-delete' ).attr( 'href', this.DeleteURL + this.ImageID );
		
		// Replace fonts
		
		Cufon.replace( '#attributes' );
		
	},
	
	//----------------------------------------------------------
	// Create pagination
	//
	
	Pagination: function( )
	{
	
		var Amount = this.Data.length;
		var Container = $( '#pages' );
		for( var i = 0; i < Amount; i ++ )
		{
		
			$( '<span></span>' ).appendTo( Container )
								.html( i + 1 ).attr( 'page', i )
			$( '<a href="#"></a>' ).appendTo( Container )
								   .html( i + 1 ).attr( 'page', i )
								   .click( this.SelectPage );
			
		}
		$( 'span', Container ).hide( );
		
	},
	
	//----------------------------------------------------------
	// Determine what key was pressed
	//
	
	DetermineKey: function( e )
	{
		if( e.keyCode == 39 )
			View.NextPage( );
		else if( e.keyCode == 37 )
			View.PreviousPage( );
	},

	//----------------------------------------------------------
	// Next page
	//
	
	NextPage: function( )
	{
		this.SelectPage( this.Current < this.Data.length - 1 ? this.Current + 1 : 0 );
	},
	
	//----------------------------------------------------------
	// Previous page
	//
	
	PreviousPage: function( )
	{
		this.SelectPage( this.Current == 0 ? this.Data.length - 1 : this.Current - 1 );
	},
	
	//----------------------------------------------------------
	// Select a page
	//
	
	SelectPage: function( ID )
	{
		
		this.Current = Number( ID );
	
		//var I = $( this );
		//var ID = Number( I.attr( 'page' ) );
		var Data = View.Data[ this.Current ];
		
		// Change the color of the number
		
		/*if( View.SelectedPage )
			View.SelectedPage.show( ).prev( ).hide( );
		( View.SelectedPage = I.hide( ) ).prev( ).show( );*/
		
		// View the picture
		
		View.View( Data );
		
	}
	
};
