(function($) {
	
	$.fn.scGallery = function( options ) {
		var settings, defaults;
		
		defaults = {
			loadingImage : '/sassart/images/loading.gif',
			overlayId    : 'overlay',
			modalId      : 'popup'
	    };
	    
	    settings = $.extend( {}, defaults, options );
	    
	    $.image = {
	    	list   : [],
	    	loaded : [],
	    	height : [],
	    	width  : [],
	    	same   : [],
	    	
			allLoaded : false,
	    	
			add  : function( id ) {
				$.image.list.push( id );
				$.image.list.push( 'full/' + id );						
			},
			
			preload : function() {
				var images = [];
				
				$.each( $.image.list, function( k, v ) {
					images.push( v );
				});
				
				$.preload( images, {
					base : settings.path,
					ext  : '.jpg',
					
					onComplete : function( data ) {
						$.image.loaded.push( data.original );
						
						if ( data.found == false ) {
							$.image.same.push( data.original.replace( "full/", "" ) );					
						}
								
						var img = new Image();
						img.src = data.image;
						$.image.width[data.original]  = img.width;
						$.image.height[data.original] = img.height;
					},
					
					onFinish : function( data ) {
						$.image.allLoaded = true;
					}
				}); 
			},
			
			check : function( image, callback ) {
				if ( $.image.allLoaded ) {
					callback( image );
				} else if ( $.inArray( image, $.image.loaded ) >= 0 ) {
					callback( image );
				} else {
					var waitForImage = function() {
						if( $.inArray( image, $.image.loaded ) === -1 ) {
							setTimeout( function () { waitForImage(); }, 50 );
						} else {
							callback( image );
						}
					};
					waitForImage();
				}			
			}	
		};
		
		$.inline = {
			created : false,
			image   : $( '#imgDiv img' ),
			link    : $( '#imgDiv a' ),
			
			create : function() {
				$.inline.link.click( function(e) { e.preventDefault(); $.modal.update( $.inline.link.attr( 'class' ) ); } );				
				$.inline.created = true;	
			},
			
			update : function( id, text ) {
				$.inline.link.attr( { 'class' : id } ).height($.inline.image.height()).width($.inline.image.width());
				$.inline.image.hide().attr( { 'alt' : text, 'title' : text } );
				$.image.check( id, $.inline.show );
			},
			
			show  : function( image ) {
				$.inline.image.attr( { 'src' : settings.path + image + '.jpg' } );

				$.inline.link.animate( { width : $.image.width[image] + 'px', height : $.image.height[image] + 'px' } );
				
				$.inline.link.queue( function () {
					//$.modal.loading.hide();
					$.inline.image.fadeIn();
					$( this ).dequeue();
				});		
			}								
		};

		$.modal = {
			created  : false,
			
			overlay  : $( '<div />' ),
			window   : $( '<div />' ),
			closeBtn : $( '<p class="center"><a href="#">Close</a><p>' ),
			loading  : $( '<img />' ),
			image    : $( '<img />' ),
			
			create : function() {
				$.modal.overlay.attr(  { 'id' : settings.overlayId } ).prependTo( 'body' ).click( function() { $.modal.close(); } );
				$.modal.window.attr(   { 'id' : settings.modalId } ).appendTo( $.modal.overlay ).draggable().click( function( e ) { e.stopPropagation(); e.preventDefault(); } );
				
				$.modal.image.attr(    { 'id' : settings.modalId + '-image', 'alt' : 'SassArt.com', 'title' : 'SassArt.com' } ).appendTo( $.modal.window );
				$.modal.image.hide();
				
				$.modal.closeBtn.appendTo( $.modal.window ).click( function( e ) { e.preventDefault(); $.modal.close(); } );
				
				$.modal.loading.attr(  { 'id' : settings.modalId + '-loading', 'src' : settings.loadingImage, 'alt' : 'Loading...' } ).appendTo( $.modal.window );
				
				$.modal.created = true;
			},
			
			update : function( id ) {
				if ( $.browser.msie ) {
					$.modal.overlay.show();				
				} else {
					$.modal.overlay.fadeIn();				
				}
				
				//$.modal.loading.show();
				
				if ( $.inArray( id, $.image.same ) >= 0 ) {
					$.image.check( id, $.modal.show );
				} else {
					$.image.check( 'full/' + id, $.modal.show );
				}
			},
			
			show  : function( image ) {
				$.modal.loading.hide();
				$.modal.image.attr( { 'src' : settings.path + image + '.jpg' } );

				if ( $.browser.msie ) {
					$.modal.window.height($.image.height[image]).width($.image.width[image]);				
				} else {
					$.modal.window.animate( { width : $.image.width[image] + 'px', height : $.image.height[image] + 'px' } );		
				}
								
				$.modal.window.queue( function () {
					if ( $.browser.msie ) {
						$.modal.image.show();				
					} else {
						$.modal.image.fadeIn();				
					}
					$( this ).dequeue();
				});		
			},
			
			close : function() {
				$.modal.overlay.fadeOut();
				
				$.modal.overlay.queue( function () {
					$.modal.image.hide();
					$.modal.image.attr( { 'src' : '' } );
					$( this ).dequeue();
				});
			}		
		};

	    return this.each( function( e ) {
	        $('#thumbnails img').preload();
			
			$( this ).find( 'a' ).each( function() {
				var id;
	        	id  = this.id;

				if ( id ) {
					$.image.add( id );
					$( this ).click( function( e ) {
						e.preventDefault();
						$.inline.update( id, $(this).find('img').attr('alt') );
	        		});
   				}
	        });
	        
	        if ( $.modal.created === false ) {
	        	$.modal.create();
	        }
	        
	        if ( $.inline.created === false ) {
	        	$.inline.create();
	        }
        
	        $.image.preload();
	    });
	};
})(jQuery);