// JavaScript Document
$(document).ready(function() {
	
	jQuery.fn.exists = function(){return jQuery(this).length>0;} //tests for existence of object

	/* This section is for the interactive features on the picture gallery. The function for left_arrow handles functions
	for the left arrow. The function for right_arrow handles the function for right arrow. The function for image_button 		
	handles the functions of the button navigation underneath the pictures. move_counter is a variable that keeps count of 
	which image has been selected. */

	if ($(".pic_slider").exists()) //tests to see if a pic_slider is on page
	{
		
		var move_counter=1;
	
		// Inserts last div of pic_slider first and offsets box by 150px to prepare box for navigation
		$('.pic_slider img:last').insertBefore($('.pic_slider img:first'));
		$('.pic_slider').css({left: '-150px'});
	
		/* Right arrow animates pic_slider to the left +150px, puts the last div first and initializes x-coordinate of box
		to -150px */
		$(".left_arrow").click(function() {
			$(".pic_slider").animate({left: '+=150px'}, 300,function() {
				$(".pic_slider").css({left: '-150px'});
				$('.pic_slider img:last').insertBefore($('.pic_slider img:first'));
				
			});
			
			if (move_counter > 1)
				move_counter--;
			else
				move_counter = 5;
			
			/* Changes the color of selected button */	
			$('.image_button_over').addClass('image_button').removeClass('image_button_over');
			$("#image_button" + move_counter).removeClass('image_button').addClass('image_button_over');
		
		});
		
		/* Left arrow animates pic_slider to the left -150px, puts the first div last and initializes x-coordinate of box
		to -150px */
		$(".right_arrow").click(function() {
			
			$(".pic_slider").animate({left: '-=150px'}, 300,function() { 
				$('.pic_slider img:first').insertAfter($('.pic_slider img:last'));
				$(".pic_slider").css({left: '-150px'});
			});
			
			var numOfImages = $(".pic_slider img").length;
			
			if (move_counter < numOfImages)
				move_counter++;
			else
				move_counter = 1;
			
			/* Changes the color of selected button */
			$('.image_button_over').addClass('image_button').removeClass('image_button_over');
			$("#image_button" + move_counter).removeClass('image_button').addClass('image_button_over');
			
		});
		
		$(".image_button").click(function () {
			
			$('.image_button_over').addClass('image_button').removeClass('image_button_over');
			
			var clickedIndex = $(this).index(); //gets the index of clicked graphic
			var moveNum;
			
			$(this).removeClass('image_button').addClass('image_button_over');
			
			if (clickedIndex > move_counter) 
			{
				moveNum = clickedIndex - move_counter;
				
				/* moves order of boxes the same number of times as moveNum */
				for (i=0;i<moveNum;i++)
				{
						$('.pic_slider img:first').insertAfter($('.pic_slider img:last'));
				}
				
				move_counter = clickedIndex;
				
			}
			else if (clickedIndex < move_counter) 
			{
				moveNum = move_counter - clickedIndex;
				
				/* moves order of boxes the same number of times as moveNum */
				for (i=0;i<moveNum;i++)
				{
						$('.pic_slider img:last').insertBefore($('.pic_slider img:first'));
				}
				
				move_counter = clickedIndex;
			}
			
		});
		
		$(".image_button:first").removeClass('image_button').addClass('image_button_over');
		
	}
	
	/* This section handles the navigation features for the page. */
	
	$("#nav_box ul li ul").css("display","none");
	
	$("#nav_box ul li").click(function() {
		$(this).children("ul").slideToggle('slow');
		$(this).children("a").toggleClass("over");
	});
	
	$("#nav_box ul li ul.over").css("display","block");
	
	/* This section makes odd and even rows of table diffent colors for non-CSS3 browsers. */
	$("table tr:nth-child(odd)").css({
		background: "#72bcfa"
	});
	$("table tr.col").css({
		background: "none",
		padding: "2px"
	})
	$("table tr:nth-child(even)").css({
		background: "#92cafa"
	});
	
	
	$("table.jobs tr").hover(function() {
			$(this).children("td").css({
				background: "#003360",
				color: "#FFFFFF"
			}) 
		}, function() {
			
			$(this).children("tr:nth-child(odd) td").css({
				background: "#72bcfa",
				color: "#002342"
			})
			
			$(this).children("tr:nth-child(even) td").css({
				background: "#92cafa",
				color: "#002342"
			}) 
	});
	
});
