;(function($) {

	$.cpGallery = function( scope, options )
	{
		var _this = this;
		this.options = options;
		this.dom = $( scope );
		this.thumbs = this.dom.find( options.thumbs );
		
		this.currentIndex = 0;
		this.mediumImage = this.dom.find( options.medium )
		
		// this.log( this );
		
		this.initThumbs();
		this.initNav();
		
		if( $.isFunction( this.options.beforeInit ) )
			this.options.beforeInit.call( this );

		this.dom.bind( 'goTo', function( event, index ){
			_this.goTo( index );
		});
	};

	$.extend( $.cpGallery.prototype, {
		goTo: function( index )
		{
			var _this = this;
			// this.log( 'goTo: ' + index );
			
			if( index > this.options.imageUrls.length - 1 )
				index = 0;
				
			if( index < 0 )
				index = this.options.imageUrls.length - 1;
			
			if( this.options.imageUrls[index] && this.currentIndex != index )
			{
				var url = this.options.imageUrls[index],
					_img = new Image();

				_img.src = url;
				_img.alt = $( this.thumbs[index] ).find( 'img' ).attr( 'alt' );

				var img = $( _img ).hide();
				
				if( this.isImageLoaded( _img ) )
				{
					this._swapImage( index, img );					
				}
				else
				{
					this.mediumImage.html( '<div class="loader">' + this.options.loadingText + '</div>' );
					this.dom.find( this.options.captionClass ).html( '&nbsp;' );
					img.load( function(){
						_this._swapImage( index, img );
					});					
				}

			}
			this.currentIndex = index;
		}
		,_swapImage: function( index, img )
		{
			var _this = this,
				dim = {
					width: img.get(0).width,
					height: img.get(0).height
				},
				thumb = $( this.thumbs[index] ) || null;
				
			if( $.isFunction( this.options.beforeDisplay ) )
				this.options.beforeDisplay.call( this, index, img, thumb );
			
			this.mediumImage.empty().stop().animate( dim, 'fast', function(){
				_this.mediumImage.append( img );
				img.stop().fadeIn( 'fast', function(){
					if( $.isFunction( _this.options.afterDisplay ) )
						_this.options.afterDisplay.call( _this, index, img, thumb );					
				});			
			});
		}
		,initThumbs: function()
		{
			var _this = this;
			this.thumbs.each(function( id ){
				var $this = $(this),
					_thumb = ( $this.find( 'a' ).size() ) ? $this.find( 'a' ) : $this;
					
				_thumb.click( function( e ){
					_this.goTo( id );
					e.preventDefault();
				});
			});
		}
		,initNav: function()
		{
			var _this = this;
			this.dom.find( this.options.prevBtnClass ).filter( ':first' ).click(function( e ){
				_this.goTo( _this.currentIndex - 1 );
				e.preventDefault();
			});
			this.dom.find( this.options.nextBtnClass ).filter( ':first' ).click(function( e ){
				_this.goTo( _this.currentIndex + 1 );
				e.preventDefault();
			});			
		}
		,isImageLoaded: function( img )
		{
			if( !img.complete )
				return false;

			if( typeof img.naturalWidth != undefined && img.naturalWidth == 0 )
				return false;

			return true;
		}
		,log: function( a ){
			_log( a );
		}
					
	});	
	
	$.fn.cpGallery = function( options )
	{	
		options = $.extend( {}, $.fn.cpGallery.defaults, options );
				
        return this.each( function(){
			new $.cpGallery( this, options );
        });
    };	

	$.fn.cpGallery.defaults = {
		baseHref: '/aa',
		imageUrls: [],
		thumbs: 'ul li',
		medium: '.medium-image',
		prevBtnClass: '.prev',
		nextBtnClass: '.next',
		loadingText: 'Loading...',
		captionClass: '.caption',
		beforeInit: false,
		beforeDisplay: false,
		afterDisplay: false
	};

})(jQuery);