(function($) {
	
	$.fn.scGallery = function( options ) {
		var settings, defaults;
		
		defaults = {
			overlayId   : 'overlay',
			modalId     : 'popup'
	    };
	    
	    settings = $.extend( {}, defaults, options );
	    
	    $.image = {
	    	list : [],
	    	loaded : [],
	    	height : [],
	    	width  : [],
			allLoaded : false,
	    	
			add  : function( id ) {
				$.image.list.push( id );							
			},
			
			preload : function() {
				var images = [];
				
				$.each( $.image.list, function( k, v ) {
					images.push( v );
				});
				
				$.preload( images, {
					onComplete : function( data ) {
						$.image.loaded.push( data.original );
						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;
					}
				}); 
			}
		};
	    
	    $.modal = {
			created  : false,

			imageId  : 0,
			
			overlay  : $( '<div />' ),
			window   : $( '<div />' ),
			closeBtn : $( '<a class="close" href="#"><img src="/images/g_close.png" alt="Close" title="Close" /></a>' ),
			loading  : $( '<img />' ),
			image    : $( '<img />' ),
			controls : $( '<div />' ),
			navPrev  : $( '<a href="#"><img src="/images/g_prev.png" alt="Previous" title="Previous" /></a>' ),
			navNext  : $( '<a href="#"><img src="/images/g_next.png" alt="Next" title="Next" /></a>' ),
			
			create : function() {
				$.modal.overlay.attr(  { 'id' : settings.overlayId } ).prependTo( 'body' ).click( function() { $.modal.close(); } );
				$.modal.window.attr(   { 'id' : settings.modalId } ).draggable({ cursor: 'move' }).appendTo( $.modal.overlay ).click( function( e ) { e.stopPropagation(); } );
				
				$.modal.controls.attr( { 'id' : 'controls' } ).appendTo( $.modal.window );
				$.modal.navPrev.attr(  { 'href' : '#', 'title' : 'Previous' } ).appendTo( $.modal.controls ).click( function( e ) { e.preventDefault(); e.stopImmediatePropagation(); $.modal.prevNext( 'prev' ); } );
				$.modal.navNext.attr(  { 'href' : '#', 'title' : 'Next' } ).appendTo( $.modal.controls ).click( function( e ) { e.preventDefault(); e.stopImmediatePropagation(); $.modal.prevNext( 'next' ); } );
				
				$.modal.closeBtn.appendTo( $.modal.window ).click( function( e ) { e.preventDefault(); $.modal.close(); } );
								
				$.modal.image.attr(    { 'id' : settings.modalId + '-image', 'alt' : 'Shed Plant gallery', 'title' : 'Shed Plant gallery' } ).appendTo( $.modal.window );
				$.modal.image.hide();
				
				$.modal.loading.attr(  { 'id' : settings.modalId + '-loading', 'src' : settings.loadingImage, 'alt' : 'Loading...' } ).appendTo( $.modal.window );
				
				$.modal.created = true;
			},
			
			update : function( image, text ) {
				if ( $.browser.msie ) {
					$.modal.overlay.show();				
				} else {
					$.modal.overlay.fadeIn();				
				}
				$.modal.loading.show();

				$.each( $.image.list, function( k, v ) {
					if ( v == image ) {
						$.modal.imageId = k;				
					}
				});
				
				/* hide image outlines for opera */
				//$( 'img.pic' ).css( { outline : '0' } );
				
				$.modal.check( image );
			},
			
			show  : function( image ) {
				$.modal.loading.hide();
				
				$.modal.image.queue( function () {
					if ( $.browser.msie ) {
						$.modal.window.height($.image.height[image] + 35).width($.image.width[image]);				
					} else {
						$.modal.window.animate( { width : $.image.width[image] + 'px', height : ($.image.height[image] + 35) + 'px' } );		
					}
					
					$.modal.window.queue( function () {
						$.modal.image.attr( { 'src' : image } );
						
						if ( $.browser.msie ) {
							$.modal.image.show();				
						} else {
							$.modal.image.fadeIn();				
						}
						$( this ).dequeue();
					});
					$( this ).dequeue();
				});
			},
			
			prevNext : function( direction ) {
				$.modal.loading.show();
				
				if ( $.browser.msie ) {
					$.modal.image.hide();				
				} else {
					$.modal.image.fadeOut();				
				}
				
				if ( direction === 'prev' ) {
					if ( $.modal.imageId == 0 ) {
						$.modal.imageId = $.image.list.length - 1;				
					} else {
						$.modal.imageId =  $.modal.imageId - 1;			
					}				
				} else {
					if ( $.modal.imageId == $.image.list.length - 1 ) {
						$.modal.imageId = 0;				
					} else {
						$.modal.imageId = $.modal.imageId + 1;		
					}				
				}					
				
				$.modal.check( $.image.list[$.modal.imageId] );
			},
			
			close : function() {
				/* restore image outline */
				//$( 'img.pic' ).css( { outline : '1px solid #fdfdfc' } );
				
				$.modal.overlay.fadeOut();
				
				$.modal.overlay.queue( function () {
					$.modal.image.hide();
					$( this ).dequeue();
				});
			},
			
			check : function( image ) {
				if ( $.image.allLoaded ) {
					$.modal.show( image );
				} else if ( $.inArray( image, $.image.loaded ) >= 0 ) {
					$.modal.show( image );
				} else {
					var waitForImage = function() {
						if( $.inArray( image, $.image.loaded ) === -1 ) {
							setTimeout( function () { waitForImage(); }, 50 );
						} else {
							$.modal.show( image );
						}
					};
					waitForImage();
				}			
			}			
		};
	    
	    return this.each( function( e ) {
	        if ( $.modal.created === false ) {
	        	$.modal.create();
	        }			
	        
	        $( this ).find( 'a' ).each( function() {
				var id;
	        	id = $(this).attr('href');
				
				if ( id ) {
					$.image.add( id );
					$( this ).click( function( e ) {
	        			e.preventDefault();
						$.modal.update( id );
	        		});
   				}
	        }); 
        
	        $.image.preload();
	    });
	};
})(jQuery);

