$(document).ready(function() {
						   
						   
		$("#container").css("visibility", "visible");
		
		
		/*
		--------------------------------------------------------------
		Setup the page
		--------------------------------------------------------------
		*/
		
		// Create variables
		var image_path = "";
		
		var curr_image = -1;
		var thumb_width = 60;
		var thumb_spacing = 3;
		var array_length = 0;
		var num_images = 0;
		var num_thumbs = 0;
		var gallery_nav_yOffest = 152;
		var mouseY = 0;
		
		if ($("#gallery_nav_slider").length){
			var gallery_nav_slider_pos = $("#gallery_nav_slider").position().top;
		}
		
		
		var image_array = [];
		var image_list = [];
		var thumb_list = [];
		
		var loop_toggle = true;
		
		$(".story").children(".FacebookLikeButton").hide();
		
		/*
		--------------------------------------------------------------
		Setup the page
		--------------------------------------------------------------
		*/
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Setup the process sub navs
		--------------------------------------------------------------
		*/
		
		
		$("#sub_nav").children("ul").children("li").children("a").hide();
		
		var sub_nav_list = $("#sub_nav").children("ul").children("li").children("a");
		var sub_nav_length = sub_nav_list.length;
		
		for (i = 0; i < sub_nav_length; i++) {
			
			var str = $(sub_nav_list[i]).attr("title");
			var sub_1 = str.substring(0,5);
			var sub_2 = str.substring(5,str.length);
			
			var myNav = $(sub_nav_list[i]);
			//var new_str = String(sub_1+"<span class=\"sub_name\">"+sub_2+"</span>");
			var new_str = String("<div class=\"bg_image\"></div>");
			myNav.html(new_str);
		}
		
		$("#sub_nav").children("ul").children("li").children("a").trigger('hover');
		
		//$("#sub_nav").children("ul").children("li").children("a").css("font", "News Gothic");
		
		$("#sub_nav").children("ul").children("li").children("a").show();
		
		


		/*
		--------------------------------------------------------------
		Setup the process sub navs
		--------------------------------------------------------------
		*/






		/*
		--------------------------------------------------------------
		Setup the image container
		--------------------------------------------------------------
		*/
		
		$("#image_container").children("p").children().hide();
					
		// Loop through the images
		$("#image_container").children("p").children().each(
    		
    		function(){
    		
    			if ($(this).is("a")) {
    				
    				// Find the source
    				image_path = $(this).children("img").attr("src");
    				
    				// Add to the array
    				image_array.push(image_path);

    			} else if ($(this).is("img")) {
    				
    				// Find the source
    				image_path = $(this).attr("src");
    				
    				// Add to the array
    				image_array.push(image_path);
    			}
   			}
		);
		
		// Save the array length
		array_length = image_array.length;
		
		// Create the images
		if ($("#image_container").length && array_length != 0) {  
			  
			var myList = $("#image_container");
			
			var myListItems = "";
			
			for (i = 0; i < array_length; i++) {
	
				myListItems += "<img src=\"" + image_array[i] + "\" />";
			}
			
			// Set the html
			myList.html(myListItems);
		} 
		
		// Save the home image list
		image_list = $("#image_container").children("img");
		num_images = image_list.length;

		
		// If the page does not have thumbs
		if ($('#thumb_slider').length == 0) {
		
			// Show the first project
			swapProjects(0);
		}
		
		// Once the thumb is loaded
		$("#image_container").children("img").load(function () {

			// Find the position of the nav
			var pos = Number($("#image_container").children("img").index($(this)));	
			
			if (pos == 0) {
				
				// Show the first project
				swapProjects(0);
			} else {
	
				$(this).fadeOut(0);
			
			}		   
		});
				
		/*
		--------------------------------------------------------------
		Setup the image container
		--------------------------------------------------------------
		*/

		


		
		
		/*
		--------------------------------------------------------------
		Setup the gallery thumbnails
		--------------------------------------------------------------
		*/
		
		// Create the thumbnails
		if ($('#thumb_slider').length) {  
			  
			var myList = $('#thumb_slider');
			
			var myListItems = "";
			
			for (i = 0; i < array_length; i++) {

				myListItems += "<div id=\"thumb\"><img src=\"" + image_array[i] + "\" /></div>";	
			}
			
			// Add the thumb slider
			myListItems += "<div id=\"thumb_select\"></div>";

			// Set the html
			myList.html(myListItems);
		} 
		
		// Make the thumbs invisible
		$("#thumb_slider").children("#thumb").children("img").hide();
		
		// Save the thumb_list
		thumb_list = $("#thumb_slider").children("#thumb");
		num_thumbs = thumb_list.length;
		
		// Once the thumb is loaded
		$("#thumb_slider").children("#thumb").children("img").load(function () {

			$(this).show();
			$(this).parent().fadeTo(0,.5);

			// Find the position of the nav
			var pos = Number($("#thumb_slider").children("#thumb").index($(this).parent()));	
			
			$(this).parent().css('left', pos*(thumb_width + thumb_spacing));
			
			if (pos == 0) {
				
				// Show the first project
				//swapProjects(0);
			}	
			
			// Show the first project
			swapProjects(0);
									   
		});

		/*
		--------------------------------------------------------------
		Setup the gallery thumbnails
		--------------------------------------------------------------
		*/
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Setup gallery nav
		--------------------------------------------------------------
		*/

		if ($("#gallery_nav_slider").children("ul:first-child").children("li").hasClass("current_page_parent")) {
			$("#gallery_nav_slider").css("left",-190);
			
			var p = $(".current_page_item").parent();
			p.css("visibility", "visible");
			
			$("#wrapper").addClass("brands");
		}
		
		$("#gallery_nav_slider").children("ul:first-child").children("li").children("a").click(function(e) {
		
			if ($(this).parent().children("ul").length) {
			
				e.preventDefault();
				
				$("#gallery_nav_slider").children("ul:first-child").children("li").children("ul").css("visibility", "hidden");
				
				var p = $(this).parent();
				
				p.children("ul").css("visibility", "visible");
			
				$("#gallery_nav_slider").animate({
					left:-190 }, 500, function() {});
				
			}

		});
		

		$(".back_to_clients").click(function(e) {
				
			$("#gallery_nav_slider").animate({
			left:0 }, 500, function() {});
		});

		
		/*
		--------------------------------------------------------------
		Setup gallery nav
		--------------------------------------------------------------
		*/

	
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Timers
		--------------------------------------------------------------
		*/
		
		function startGalleryLoop() {
			
			$(this).everyTime(5000, "gallery_loop", function() {
															 
				if (num_images > 1) {
					
					// Create a variable to save the new position
					var newPos = curr_image + 1;
					
					// Check the new number and set back to zero
					// if it's greater than the number of images
					if (newPos == num_images) newPos = 0;
		
					// Swap out the projects			
					swapProjects(newPos);
				}
			});
		}
		
		function stopGalleryLoop() {
			
			// Stop the timer
			$(this).stopTime("gallery_loop");
		}
		
		function startNavScroll() {
			
			$(this).everyTime(10, "nav_scroll", function() {
			
				// Create variables
				var container = $("#gallery_nav").height();
				var slider = $("#gallery_nav_slider").height();
				var min_y = container - slider;
				var y_pos = mouseY - gallery_nav_yOffest;
				
				if (y_pos < 100) {
					gallery_nav_slider_pos = gallery_nav_slider_pos + 5;
				}
				
				if (y_pos > container - 50) {
					gallery_nav_slider_pos = gallery_nav_slider_pos - 5;
				}	
	
				// Check the positions
				if (gallery_nav_slider_pos < min_y) {
					
					// Save the new position
					gallery_nav_slider_pos = min_y;
				}
				
				// Check the positions
				if (gallery_nav_slider_pos > 0) {
					
					// Save the new position
					gallery_nav_slider_pos = 0;
				}
				
				console.log(y_pos);		
				
				// Animate the slider
				$("#gallery_nav_slider").animate({
					top: gallery_nav_slider_pos }, 20, function() {});											 
				
			});
		}
		
		function endNavScroll() {
			
			// Stop the timer
			$(this).stopTime("gallery_loop");
		}
		
		/*
		--------------------------------------------------------------
		Timers
		--------------------------------------------------------------
		*/
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Functions
		--------------------------------------------------------------
		*/
		
		function swapProjects(pos) {
		
			if (curr_image != pos) {
		
				// Stop the loop
				stopGalleryLoop();
				
				if ($('#thumb_slider').length) {  
				
					// Animate the slider
					animateSlider(pos);
				}
	
				// Fade out the thumbs
				$("#thumb_slider").children("#thumb").fadeTo(200,.5);
				
				// Set the thumbs pointer
				$("#thumb_slider").children("#thumb").css("cursor","pointer");
				
				// Fade out the current image
				$(image_list[curr_image]).fadeOut("slow");
				
				// Fade in the current thumb
				$(thumb_list[pos]).fadeTo(0,1);
				
				// Set the currnt pointer
				$(thumb_list[pos]).css("cursor","default");
	
				// Fade in the new image
				//if ($(image_list[pos]).css("visibility") == "hidden") {
					//$(image_list[pos]).css("visibility", "visible");
					//$(image_list[pos]).fadeOut(0);
				//}
				
				$(image_list[pos]).css("visibility", "visible");
				$(image_list[pos]).fadeIn("slow");
				
				// Save the new position
				curr_image = pos;
				
				// If looping is on
				if (loop_toggle) {
					
					// Start the loop
					startGalleryLoop();
				}
			}
		}
		
		
		function animateSlider(pos) {
			
			// Create variables
			var container = $("#thumb_container").width();
			var w = (num_thumbs*(thumb_width + thumb_spacing)) + thumb_width;
			var min_x = container - w;
		
			// Find the positions
			var thumb_position = $(thumb_list[pos]).position().left;
			var slider_position = $("#thumb_slider").position().left;
			var curr_pos = thumb_position + slider_position;
			var new_pos = slider_position;
			var selected_pos = thumb_position + (thumb_width/2) - 7.5;
			
			// Find the scroll positions
			var num_visible_thumbs = container/(thumb_width + thumb_spacing);
			var scroll_left = Math.ceil(num_visible_thumbs/2)*(thumb_width + thumb_spacing);
			var scroll_right = Math.floor(num_visible_thumbs/2)*(thumb_width + thumb_spacing);
			
			// Check the positions
			if (curr_pos > scroll_left) {
				
				// Save the new position
				new_pos = scroll_left - thumb_position;
				if (new_pos < min_x) {
					new_pos = min_x;
				}
			}
			
			// Check the positions
			if (curr_pos < scroll_right) {
				
				// Save the new position
				new_pos = scroll_right - thumb_position;
				if (new_pos > 0) {
					new_pos = 0;
				}
			}
			
			// Slide the select position
			$("#thumb_select").animate({
				left: selected_pos }, 500, function() {});
			
			// Animate the slider
			//$("#thumb_slider").animate({
				//left: new_pos }, 500, function() {});
		}

		
		/*
		--------------------------------------------------------------
		Functions
		--------------------------------------------------------------
		*/
		
		
		
		
		
		/*
		--------------------------------------------------------------
		Mouse Event Functions
		--------------------------------------------------------------
		*/
		
		// Thumb hover event
		$("#thumb_slider").children("#thumb").hover(function () {
												
			// Find the position of the nav
			var pos = Number($("#thumb_slider").children("#thumb").index($(this)));
			
			// If this isn't the current image
			if (pos != curr_image) {
				
				// Fade the image
				$(this).fadeTo(100,1);
			}
			
		}, function () {
			
			// Find the position of the nav
			var pos = Number($("#thumb_slider").children("#thumb").index($(this)));
			
			// If this isn't the current image
			if (pos != curr_image) {
				
				// Fade the image
				$(this).fadeTo(200,.5);
			}
		});
		
		// Thumb click event
		$("#thumb_slider").children("#thumb").click(function() {
			
			// Find the position of the nav
			var pos = Number($("#thumb_slider").children("#thumb").index($(this)));	
			
			// If this isn't the current image
			if (pos != curr_image) {

				// Swap out the projects			
				swapProjects(pos);
			}			
		});
		
		
		// Gallery nav hover event
		$("#gallery_nav").hover(function () {
												
			//startNavScroll();
						
		}, function () {
			
			//endNavScroll();
			
		});
		
		$("#gallery_nav").mousemove(function(e) {
   			//mouseY = e.pageY;
 		});
		
		/*
		// Gallery nav hover event
		$("#gallery_nav").mousemove(function(e) {
			
			// Create variables
			var container = $("#gallery_nav").height();
			var slider = $("#gallery_nav_slider").height();
			var min_y = container - slider;
			var y_pos = e.pageY - gallery_nav_yOffest;
			
			if (y_pos < 100) {
				gallery_nav_slider_pos = gallery_nav_slider_pos + 5;
			}
			
			if (y_pos > container - 50) {
				gallery_nav_slider_pos = gallery_nav_slider_pos - 5;
			}	

			// Check the positions
			if (gallery_nav_slider_pos < min_y) {
				
				// Save the new position
				gallery_nav_slider_pos = min_y;
			}
			
			// Check the positions
			if (gallery_nav_slider_pos > 0) {
				
				// Save the new position
				gallery_nav_slider_pos = 0;
			}
			
			console.log(gallery_nav_slider_pos);		
			
			// Animate the slider
			$("#gallery_nav_slider").animate({
				top: gallery_nav_slider_pos }, 10, function() {});
		});
		*/
		
		/*
		--------------------------------------------------------------
		Mouse Event Functions
		--------------------------------------------------------------
		*/
		
});
