

var gallery = 
{
	images : new Array(),
	index : -1,
	image: false,
	timer : null,
	id: '#photo',
	
	calcHeight: function(new_width, image)
	{
		var height = parseInt( ( image.height * new_width ) / image.width );
		
		return height;
	},
	
	image_class: function( src )
	{
		var image = new Image();
		image.src = src;
		image.height = this.calcHeight( 700, image );
		image.width = 700;
		
		return image;
	},
	
	create: function (image)
	{
		this.images[ this.images.length ] = this.image_class( image );
	},
	
	next : function()
	{
		if( this.last() )
		{
			this.index = -1;
		}
		
		this.index++;
		
		this.image = this.images[ this.index ];
		this.load();
	},
	
	prev : function()
	{
		if( this.first() )
		{
			this.index = this.images.length;
		}
		
		this.index--;
		
		this.image = this.images[ this.index ];
		this.load();
	},
	
	last : function()
	{
		if( this.images.length - 1 == this.index )
		{
			return true;
		}else{
			return false;
		}
	},
	
	first : function()
	{
		if( this.index == 0 )
		{
			return true;
		}else{
			return false;
		}
	},
	
	init: function()
	{
		this.image = this.images[0];
		this.index = 0;
		this.load();
		
		var obj = this;
		
		$(".photos li").click( function(){ obj.select( $(this).index() ); } );
		$(".first").click( function(){ obj.select( 0 ); return false; } );
		$(".last").click( function(){ obj.select( obj.images.length - 1 ); return false; } );
		$(".prev").click( function(){ obj.prev(); return false } );
		$(".next").click( function(){ obj.next(); return false; } );
		$(".count").html( this.images.length );
		
		$(".control").click( function(){
			var mode = $(this).attr('href');
			
			if( mode == 'pause' )
			{
				obj.stop();
				$(this).attr('href', 'play' );
				$(this).html( 'Play &rsaquo;' );
			}else
			{
				obj.next();
				obj.start();
				$(this).attr('href', 'pause' );
				$(this).html( 'Pause' );
			}
			
			return false;
		} );
	},
	
	start : function()
	{
		this.timer = setTimeout( "gallery.next();", 8000 );
	},
	
	stop : function()
	{
		if( this.timer != null )
		{
			clearTimeout( this.timer );
		}
		
		this.timer = null;
		
		return true;
	},
	
	select : function(index)
	{
		this.index = index;
		this.image = this.images[ this.index ];
	
		this.load();
	},
	
	load: function()
	{
		this.stop();
		
		$( this.id ).attr('src', this.image.src );
		$( this.id ).attr('height', this.image.height );
		$( this.id ).attr('width', this.image.width );
		$(".download").attr('href', this.image.src );
		
		$(".photos li").fadeTo( 'fast', 0.4 );
		$(".photos li:eq(" + this.index + ")").fadeTo( 'fast', 1.0 );
		
		
		$(".index").html( this.index + 1 );
		
		
		this.start();
	}
	
}