$(document).ready(function() {



		
		/*
		--------------------------------------------------------------
		Setup the home images
		--------------------------------------------------------------
		*/
		
		var num_home_images = $("#homepage_images").children("img").size();
		var home_image_list = $("#homepage_images").children("img");
		var curr_home_image = 0;
		//console.log(num_home_images);
		if ($('#homepage_images').length) {
			
			// Hide all of the homepage images
			$("#homepage_images").children("img").hide();
			
			// Show the first gallery image
			$(home_image_list[0]).show();
			
			// Start the loop
			startGalleryLoop();
		}
		
		/*
		--------------------------------------------------------------
		Setup the home images
		--------------------------------------------------------------
		*/
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Setup the gallery images
		--------------------------------------------------------------
		*/
		
		var num_gallery_images = $("#gallery_images").children("p").children("img").size();	
		var gallery_image_list = $("#gallery_images").children("p").children("img");
		var gallery_nav_list;
		
		if ($('#gallery_nav').length) {  
			  
			var myList = $('#gallery_nav');
			
			var myListItems = '<ul>';
			var newNum;
			
			for (i = 0; i < num_gallery_images; i++) {
				
				newNum = i+1;
				if (newNum < 10) newNum = Number("0" + (i+1));
				
				myListItems += '<li><a href="#">[ '+ newNum + ' ]</a></li>';
			}
			
			// Add to the string
			myListItems += '</ul>';
			
			// Set the html
			myList.html(myListItems);
			
			// Save the gallery nav list
			gallery_nav_list = $("#gallery_nav").children("ul").children("li");
			
			// Hide all of the gallery images
			$("#gallery_images").children("p").children("img").hide();
			
			// Show the first gallery image
			$(gallery_image_list[0]).show();
			
			// Highlight the first gallery nav
			$("#gallery_nav").children("ul").children("li:first").children("a").css("color","#6E6E6E");
			
			// Start the loop
			startGalleryLoop();
		} 
		
		/*
		--------------------------------------------------------------
		Setup the gallery images
		--------------------------------------------------------------
		*/
	
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Timers
		--------------------------------------------------------------
		*/
		
		function startGalleryLoop() {
			
			$(this).everyTime(5000, "gallery_loop", function() {
															 
				if (num_home_images > 1) {
					
					// Create a variable to save the new position
					var newPos = curr_home_image + 1;
					
					// Check the new number and set back to zero
					// if it's greater than the number of images
					if (newPos == num_home_images) newPos = 0;
		
					// Swap out the projects			
					swapProjects(newPos);
				}
											 
				
				if (num_gallery_images > 1) {
				
					// Save the new pos
					new_pos = curr_gallery_image + 1;
					
					// Check the new pos
					if (new_pos == num_gallery_images) new_pos = 0;
					
					// Swap the images
					swapGalleryProjects(new_pos);
				}
			});
		}
		
		function stopGalleryLoop() {
			
			// Stop the timer
			$(this).stopTime("gallery_loop");
		}
		
		/*
		--------------------------------------------------------------
		Timers
		--------------------------------------------------------------
		*/
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Functions
		--------------------------------------------------------------
		*/
		
		// Function used to swap projects
		function swapProjects(pos) {
			
			// Fade out the current image
			$(home_image_list[curr_home_image]).fadeOut("slow");
			
			// Fade in the new image
			$(home_image_list[pos]).fadeIn("slow");
			
			// Save the new position
			curr_home_image = pos;
		}
		
		function swapGalleryProjects(pos) {
			
			// Set color of the current number
			$(gallery_nav_list[curr_gallery_image]).children().css("color","#B5B5B5");
			
			// Set the color of the new link
			$(gallery_nav_list[pos]).children().css("color","#6E6E6E");
			
			// Fade out the current image
			$(gallery_image_list[curr_gallery_image]).fadeOut("slow");
			
			// Fade in the new image
			$(gallery_image_list[pos]).fadeIn("slow");
			
			// Save the new position
			curr_gallery_image = pos;
		}
		
		/*
		--------------------------------------------------------------
		Functions
		--------------------------------------------------------------
		*/
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Mouse Event Functions
		--------------------------------------------------------------
		*/
		



		// Main nav click event
		$("#logo").click(function(event) {
			
			// Prevent the defautl
			event.preventDefault();
			
			// Go to the url
	  		window.location = $(this).attr("url");
		});
		
		
		
			
		
		// Main nav click event
		$(".main_nav").children("li").children("a").click(function(event) {

			
			if ($(this).next().is("ul")) {
				
				// Prevent the default
				event.preventDefault();
				
				// Save a variabe
				var first_child = $(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").children("ul").children("li").click(function() {
			
			// Find the position of the nav
			var pos = $("#gallery_nav").children("ul").children("li").index($(this));
			
			// Stop the timer
			stopGalleryLoop();
			
			// Swap the gallery images
			swapGalleryProjects(pos);
			
			// Restart the timer
			if (loop_toggle == 1) startGalleryLoop();
		});
		
		
		
		
		
		// Gallery slideshow hover event
		$("#gallery_slideshow").children("ul").children("li").hover(function() {															
			
			// Set the text color
			$(this).children().css({'color': '#6E6E6E'});
		}, function() {
			
			// Set the text color
			$(this).children().css({'color': '#B5B5B5'});
		});
		
		
		
		
		// Gallery slideshow click event
		$("#gallery_slideshow").children("ul").children("li").click(function() {
			
			// Find the position of the button
			var pos = $("#gallery_slideshow").children("ul").children("li").index($(this));
			
			// Create varibles
			var new_pos;
			
			if (pos == 0) {
				
				// Save the new pos
				new_pos = curr_gallery_image - 1;
				
				// Check the new pos
				if (new_pos < 0) new_pos = num_gallery_images-1;
				
				// Stop the timer
				stopGalleryLoop();
				
				// Swap the gallery images
				swapGalleryProjects(new_pos);
				
				// Restart the timer
				if (loop_toggle == 1) startGalleryLoop();

			} else if (pos == 1) {
				
				// If the slideshow is playing...
				if ($(this).children().text() == "pause") {
					
					// Set the text
					$(this).children().text("play") ;
					
					// Stop the timer
					stopGalleryLoop();
					
					// Set the loop toggle
					loop_toggle = 0;
				
				// If hte slideshow is not player
				} else {
					
					// Set the text
					$(this).children().text("pause") ;
					
					// Start the timer
					startGalleryLoop();
					
					// Set the loop toggle
					loop_toggle = 1;
				}
				
			} else {
				
				// Save the new pos
				new_pos = curr_gallery_image + 1;
				
				// Check the new pos
				if (new_pos == num_gallery_images) new_pos = 0;
				
				// Stop the timer
				stopGalleryLoop();
				
				// Swap the gallery images
				swapGalleryProjects(new_pos);
				
				// Restart the timer
				if (loop_toggle == 1) startGalleryLoop();
			}
		});

		
		/*
		--------------------------------------------------------------
		Mouse Event Functions
		--------------------------------------------------------------
		*/
		
	});
