// JavaScript Document

$(document).ready(function(){
			
		var intervalID;
		var curImage;
		var index = 0;
		var next;
		var count = $('.header').children().size();
		
		$('.main_image').hover(
		   function(){$('.controls').show();},
		   function(){$('.controls').hide();}
		 );
		
		$(".main_image .desc").animate({ opacity: 0.50 }, 1 ); //Set Opacity

		$('.left_arrow').click(function(){
			next = index - 1;
			if (next < 0) {
				next = count-1;
			}
			
			rotate();
			index = next;
		});
		
		$('.right_arrow').click(function(){
			next = index + 1;
			if (next > count - 1) {
				next = 0;
			}
			rotate();
			index = next;
		});
		
		$('.header').children().each(function() {
			var obj = $(this).children()[index];

			$(this).mouseover(function() {
				window.clearInterval(intervalID);
			});
			$(this).mouseout(function() {
				intervalID = window.setInterval(autoRotate, 10000);
			});

		});
		
		function rotate(){
			$('.header').children().eq(index).fadeOut('slow', function() {
				$('.header').children().eq(next).fadeIn('slow');
			});
		}
		
				
		function autoRotate() {
			next = index + 1;
			if (next > count - 1) {
				next = 0;
			}
	        rotate();
			index = next;	 
		}
	
	var intervalID = window.setInterval(autoRotate, 10000);
	
});


