var CantonKitchens = {};
CantonKitchens.Home =
	function () {
		var gallery = $( '#gallery' );
		var imageIndex = 2;
		var imageList = gallery.find( 'LI' ).find( 'IMG' );
		var imageMain = gallery.find( 'IMG:first' );

		function rotateImages( imageIndex ) {
			var imageSource = 
				imageMain
					.attr( 'src' )
					.replace( /(room[0-9])/, 'room' + imageIndex );
			imageMain
				.delay( 5000 )
				.fadeOut( 
					'slow', 
					function() {
						imageList.each(
							function( index ) {
								$( this ).attr( 'src', $( this ).attr( 'src' ).replace( 'on', 'off' ) );
								if ( index == ( imageIndex - 1 ) ) {
									$( this ).attr( 'src', $( this ).attr( 'src' ).replace( 'off', 'on' ) );
								}
							}
						);
						$( this )
							.attr( 'src', imageSource )
							.delay( 300 )
							.fadeIn( 'slow' );
							imageIndex += 1;
							if ( imageIndex > imageList.length ) {
								imageIndex = 1;
							}
							rotateImages( imageIndex );
					}
				);
		}
		
		rotateImages( imageIndex );
	};
CantonKitchens.Gallery =
	function() {
		var galleryImage = $( '#main-content' ).find( 'IMG:first' );
		var galleryList = $( '#gallery-list' ).find( 'IMG' );

		galleryList.click(
			function() {
				var imageSource = 
					$( this )
						.attr( 'src' )
						.replace( '_small', '' );
				galleryImage.attr( 'src', imageSource );
			}
		);
	};
