jQuery(document).ready(function() {

		
		var curr_image = 0;
		var loop_toggle = 1;
        
        // Separate the published and new work
        jQuery('*[title="published"]').parent().before('<li>++</li>');		
		
	
		/*
		--------------------------------------------------------------
		Setup the gallery images
		--------------------------------------------------------------
		*/
		var $main_nav = jQuery( '.main_nav' ).children( 'li' ).children( 'a' );
		
		var $gallery = jQuery( '#gallery_images' );
		var $gallery_nav_list = jQuery( '#gallery_controls' ).children( '.gallery-nav' );
		var num_images = $gallery_nav_list.length;
				
		var $prev = jQuery( '.prev-btn' );
		var $next = jQuery( '.next-btn' );
		var $pause = jQuery( '.pause-btn' );
		
		// Start the first project
		if ( num_images > 0 ) swapGalleryProjects();
		
		
		/*
		--------------------------------------------------------------
		Setup the gallery images
		--------------------------------------------------------------
		*/
	
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Timers
		--------------------------------------------------------------
		*/
		
		function startGalleryLoop() {
			
			jQuery( this ).everyTime( 5000, 'gallery_loop', function() {
															 
				if ( num_images > 1 ) {
				
					// Save the new pos
					curr_image++;
					
					// Check the new pos
					if (curr_image == num_images) curr_image = 0;
					
					// Swap the images
					swapGalleryProjects();
				}
			});
		}
		
		function stopGalleryLoop() {
			
			// Stop the timer
			jQuery( this ).stopTime( 'gallery_loop' );
		}
		
		/*
		--------------------------------------------------------------
		Timers
		--------------------------------------------------------------
		*/
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Functions
		--------------------------------------------------------------
		*/
		
		function swapGalleryProjects() {
		
			// Stop the timer
			stopGalleryLoop();

			// Remove any existing images
			$gallery.children( 'img' ).each( function( index ) {
				jQuery( this ).fadeOut( 'slow', function() {
					jQuery( this ).remove();
				} );
			});
			
			// Add a new image
			var url = jQuery( $gallery_nav_list[curr_image] ).attr( 'href' );
			var title = jQuery( $gallery_nav_list[curr_image] ).attr( 'rel' );
			var img = new Image();
			jQuery( img ).load( function () {
  
				jQuery( this ).hide();
				$gallery.append( this );
				jQuery( this ).fadeIn( 'slow' );
			} ).attr( 'src', url ).attr( 'title', title );
			
			// Set the link classes
			$gallery_nav_list.removeClass( 'selected' );
			jQuery( $gallery_nav_list[curr_image] ).addClass( 'selected' );
			
			// Restart the timer
			if (loop_toggle == 1) startGalleryLoop();
		}
		
		/*
		--------------------------------------------------------------
		Functions
		--------------------------------------------------------------
		*/
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Mouse Event Functions
		--------------------------------------------------------------
		*/

		// Main nav click event
		$main_nav.click( function( e ) {

			if ( jQuery( this ).next().is( 'ul' ) ) {
				
				// Prevent the default
				e.preventDefault();
				
				// Save a variabe
				var first_child = jQuery( this ).next().children( 'li:first' ).children( 'a' ).attr( 'href' );
				
				// Go to the first sub nav
				window.location = first_child;	
			}
		});

		// Gallery nav click event
		$gallery_nav_list.click( function( e ) {
			
			e.preventDefault();

			// Find the position of the button
			var pos = jQuery( this ).html();
			pos = pos.substr( 2 );
			pos = pos.substr( 0, ( pos.length-2 ) );
			curr_image = pos - 1;

			// Swap the gallery images
			swapGalleryProjects();
		});
		
		// Previous click
		$prev.click(function( e ) {
			e.preventDefault();
			// Set the curr image
			curr_image--;
			if ( curr_image < 0 ) curr_image = num_images-1;
			
			// Swap the gallery images
			swapGalleryProjects();
		});
		
		// Next click
		$next.click(function( e ) {
			e.preventDefault();
			// Set the curr image
			curr_image++;
			if ( curr_image == num_images ) curr_image = 0;
			
			// Swap the gallery images
			swapGalleryProjects();
		});
		
		// Pause click
		$pause.click(function( e ) {
			e.preventDefault();
			// Set the loop toggle
			if ( loop_toggle == true ) {
				loop_toggle = false;
				// Stop the timer
				stopGalleryLoop();
				// Set the html
				jQuery( this ).html( 'play' );
			} else {
				loop_toggle = true;
				// Stop the timer
				startGalleryLoop();
				// Set the html
				jQuery( this ).html( 'pause' );
			}
		});

		
		/*
		--------------------------------------------------------------
		Mouse Event Functions
		--------------------------------------------------------------
		*/
		
	});
