var DeepGallery = {
	bDebug: false,
	iNumImages: 0,
	aImages: new Array(),
	iShownImages: 3, // used to calculate the cycle pattern.
	iSpeed: 250, //in ms
	iDefaultVertMiddle: 0,
	iVertMiddle: 0, // the middle of the current respective to the default size.
	iVertBottom: 0, // The bottom of a default size sitting on the bottom but the middle of the current.
	bAnimInProgress: false,
	oSettings: {
		iNumShow: 5,
		iDefaultSize: {w:300,h:200},
		iCurrentSize: {w:400,h:300},
		sCurrentClass: 'galleryCurrent',
		sContainers: {
			main: 'idGalleryContainer',
			cache: 'idGalleryCache'
		},
		sButtons: {
			prev: 'idGalleryPrevious',
			next: 'idGalleryNext'
		},
		fCallbacks: {
			change: ''
		}
	},
	
	/**
	 * Initialise the gallery by setting everything up.
	 */
	init: function( aImages , oSettings ) {
		// Set the settings up and get the images in place.
		$.extend( this.oSettings , oSettings );
		this.aImages = aImages;

		// get the maximum number of records.
		this.iNumImages = (this.aImages.length-1); 
 
		// Sets the image container (the storeroom) to not display
		$("#"+this.oSettings.sContainers.images).css( 'display' , 'none' );
		// The middle of the image for the middle style.
		
		this.iVertMiddle = Math.floor((this.oSettings.iCurrentSize.h - this.oSettings.iDefaultSize.h)/2); 
		this.iVertBottom = Math.floor((this.oSettings.iCurrentSize.h - this.oSettings.iDefaultSize.h));
		this.iDefaultVertMiddle = Math.floor((this.oSettings.iDefaultSize.h/2));
		this.debug( this.iVertMiddle );
		
		this.set_links(0);
		this.set_cache();
		this.show(0 , "start");
	},
	show: function( iCurrent , sDirection ) {
		// Set the links up and assign prev next to this object
		var thisObj = DeepGallery;
		
		if ( !this.bAnimInProgress ) {
			this.bAnimInProgress = true;
			thisObj.set_links(iCurrent);
			$( "#"+thisObj.oSettings.sContainers.main +' img' ).each(function( i ){
				thisObj.debug( i );
				$(this).attr('id' , 'idImage_'+i );
			});
			if ( sDirection == "next" ) {

				$( "#"+this.oSettings.sContainers.main ).queue(function(){

					$( "#idImage_0" ).animate({
						width: 0, 
						height: 0,
						paddingTop: thisObj.iDefaultVertMiddle
					}, thisObj.iSpeed , false , 
					function(){
						$( this ).remove();
					});
										
					$( "#"+thisObj.oSettings.sContainers.main )
					.append( '<img id="idImage_3" src="'+thisObj.aImages[thisObj.iNext]+'"'+
							' style="float:left;padding-top:'+thisObj.iDefaultVertMiddle+'px;"'+
							' height="'+0+'"'+
							' width="'+0+'"'+
					' />' );
					
					$( "#idImage_1" ).animate(
							{
								width: thisObj.oSettings.iDefaultSize.w, 
								height: thisObj.oSettings.iDefaultSize.h,
								paddingTop: thisObj.iVertMiddle,
								opacity: 0.6
							}, 
							thisObj.iSpeed , 
							false , 
							function(){
								$(this).removeAttr('class');
							}
					);
					
					$( "#idImage_2" ).animate(
							{
								width: thisObj.oSettings.iCurrentSize.w, 
								height: thisObj.oSettings.iCurrentSize.h,
								paddingTop: 0,
								opacity: 1
							}, 
							(thisObj.iSpeed-50) , 
							false , 
							function() {
								thisObj.bAnimInProgress = false;
								$(this).attr('class',thisObj.oSettings.sCurrentClass);
								if ( thisObj.oSettings.fCallbacks.change ) {
									eval( thisObj.oSettings.fCallbacks.change+'('+ iCurrent +')' );
								}
							}
					);
					
					$( "#idImage_3" ).animate({
						width: thisObj.oSettings.iDefaultSize.w, 
						height: thisObj.oSettings.iDefaultSize.h,
						paddingTop: thisObj.iVertMiddle,
						opacity: 0.6
					}, 
					thisObj.iSpeed,
					false,
					function(){
						$(this).removeAttr('class');
					});
					
					$(this).dequeue();
				});
				this.debug( 'next' );
			}
			else if ( sDirection == "prev" ) {
				
				$( "#"+this.oSettings.sContainers.main ).queue(function(){
				
					$( "#"+thisObj.oSettings.sContainers.main )
					.prepend( '<img id="idImage_0" src="'+thisObj.aImages[thisObj.iPrev]+'"'+
							' style="float:left;padding-top:'+thisObj.iDefaultVertMiddle+'px;"'+
							' height="'+0+'"'+
							' width="'+0+'"'+
					' />' );
					
					$( "#"+thisObj.oSettings.sContainers.main +' img' ).each(function( i ){
						$(this).attr('id' , 'idImage_'+i );
					});
					
					$( "#idImage_3" ).animate({
						width: 0, 
						height: 0,
						paddingTop: thisObj.iDefaultVertMiddle,
						opacity: 0.6
					}, thisObj.iSpeed , false , 
					function(){
						$( this ).remove();
					});
					
					$( "#idImage_0" ).animate(
							{
								width: thisObj.oSettings.iDefaultSize.w, 
								height: thisObj.oSettings.iDefaultSize.h,
								paddingTop: thisObj.iVertMiddle,
								opacity: 0.6
							}, 
							thisObj.iSpeed , 
							false , 
							function(){
								thisObj.bAnimInProgress = false;
								$(this).removeAttr('class');
							}
					);
					
					$( "#idImage_1" ).animate(
							{
								width: thisObj.oSettings.iCurrentSize.w, 
								height: thisObj.oSettings.iCurrentSize.h,
								paddingTop: 0,
								opacity: 1
							}, 
							(thisObj.iSpeed-50) , 
							false , 
							function() {
								if ( thisObj.oSettings.fCallbacks.change ) {
									eval( thisObj.oSettings.fCallbacks.change+'('+ iCurrent +')' );
								}
								$(this).attr('class',thisObj.oSettings.sCurrentClass);
							}
					);
					$( "#idImage_2" ).animate({
						width: thisObj.oSettings.iDefaultSize.w, 
						height: thisObj.oSettings.iDefaultSize.h,
						paddingTop: thisObj.iVertMiddle,
						opacity: 0.6
					},
					thisObj.iSpeed,
					false,
					function(){
						$(this).removeAttr('class');
					});
					
					$(this).dequeue();
				});
				this.debug( 'prev' );
			}
			else if ( sDirection == "start" ) {

				$( "#"+this.oSettings.sContainers.main )
				.prepend( '<img id="idImage_2" src="'+this.aImages[this.iNext]+'"'+
						  ' style="float:left;padding-top:'+this.iVertMiddle+'px;"'+
						  ' height="'+this.oSettings.iDefaultSize.h+'"'+
						  ' width="'+this.oSettings.iDefaultSize.w+'"'+
						  ' />' );
				
				$( "#"+this.oSettings.sContainers.main )
				.prepend( '<img id="idImage_1" class="'+this.oSettings.sCurrentClass+'" src="'+this.aImages[iCurrent]+'"'+
						  ' style="float:left;"'+
						  ' height="'+this.oSettings.iCurrentSize.h+'"'+
						  ' width="'+this.oSettings.iCurrentSize.w+'"'+
						  ' />' );
				
				$( "#"+this.oSettings.sContainers.main )
				.prepend( '<img id="idImage_0" src="'+this.aImages[this.iPrev]+'"'+
						  ' style="float:left;padding-top:'+this.iVertMiddle+'px;"'+
						  ' height="'+this.oSettings.iDefaultSize.h+'"'+
						  ' width="'+this.oSettings.iDefaultSize.w+'"'+
						  ' />' );
				
				$('#idImage_0').animate({opacity:0.6},0);
				$('#idImage_2').animate({opacity:0.6},0);
				eval( thisObj.oSettings.fCallbacks.change+'('+ iCurrent +')' );
				this.bAnimInProgress = false;
			}
		}
	},


	/**
	 * Sets the links up for the next and previous buttons
	 */
	set_links: function( iCurrent ) {
		
		this.iPrev = (iCurrent-1);
		if ( this.iPrev < 0 ) {
			this.iPrev = this.iNumImages;
		}
		
		this.iNext = (iCurrent+1);
		if ( this.iNext > this.iNumImages ) {
			this.iNext = 0;
		}
		
		this.iPrevPrev = (iCurrent-2);
		if ( this.iPrevPrev < -1 ) {
			this.iPrevPrev = this.iNumImages-1;
		}
		else if ( this.iPrevPrev < 0 ) {
			this.iPrevPrev = this.iNumImages;
		}
		
		this.iNextNext = (iCurrent+2);
		if ( this.iNextNext > this.iNumImages+1 ) {
			this.iNextNext = 1;
		}
		else if ( this.iNextNext > this.iNumImages ) {
			this.iNextNext = 0;
		}
		
		this.debug(
		  'PrevPrev: '+this.iPrevPrev +
		  ' | Prev: '+this.iPrev +
		  ' | Current: '+iCurrent +
		  ' | Next: '+this.iNext +
		  ' | NextNext: '+this.iNextNext
		);
		
		$( "#"+this.oSettings.sButtons.prev ).attr( 'href' , 'javascript:DeepGallery.show('+ this.iPrev +',"prev");' );
		$( "#"+this.oSettings.sButtons.next ).attr( 'href' , 'javascript:DeepGallery.show('+ this.iNext +',"next");' );
	},
	
	
	set_cache: function() {
		var cachedImages = new Array();
		for ( var i = 0; i < this.aImages.length; i++ ) {
			cachedImages[i] = new Image(); 
			cachedImages[i].src = this.aImages[i];
		}
	},
	
	debug: function( sMsg ) {
		if( this.bDebug ) {
			if ( window.console ) { console.log( sMsg ); }
			else { alert( sMsg ) }
		}
	}
}