// JavaScript Document
// Home slide slow

$(document).ready(function() {
		//Execute the slideShow
		setIndex();
		slideShow();
		timer = setTimeout('sliderContent()', 6000);
	});
	var zIndexNumber = 1000;
	function setIndex() {
		$('#sliderContent li').each(function() {
			$(this).css('zIndex', zIndexNumber);
			$(this).css({left: 375});
			zIndexNumber -= 10;
		});
		preparefirst();
	};
	function preparefirst() {
		var first = ($('#sliderContent li.show')?  $('#sliderContent li.show') : $('#sliderContent li:first'));
		first.css({left: 0});
	}
	function setOnclick() {
		for (var sld = 0; sld < slides.length; sld++) {
			var buttons = $(slides[sld]).find('p.slide-nav span');
			for (var i = 0; i < buttons.length; i++) {
				if (sld != i) {
					$(buttons[i]).hover( function() {
						$(this).css('cursor','pointer');
					});
					$(buttons[i]).click(
					function(index) {
						return function() {
							clearTimeout(timer);
							sliderContent(index);
						};
					}(i));
				}	
			}
		}
	};
	
	function slideShow() {
		slides = $('#sliderContent li');
		
		setOnclick();
		
		//Set the opacity of all images to 0
		$('#sliderContent li').css({opacity: 0.0});

		//Get the first image and display it (set it to full opacity)

		$('#sliderContent li:first').animate({opacity: 1.0});

	//Resize the width of the caption according to the image width
	$('#sliderContent div').css({left: -70});
}

function sliderContent(nextSlide) {
	if (!slideWorking) {
		slideWorking = true;

		//if no IMGs have the show class, grab the first image
		var currentspan = ($('#sliderContent li.show div')?  $('#sliderContent li.show div') : $('#sliderContent li:first div'));
		var currentp = ($('#sliderContent li.show div p')?  $('#sliderContent li.show div p') : $('#sliderContent li:first div p'));
		var currentimg = ($('#sliderContent li.show img')?  $('#sliderContent li.show img') : $('#sliderContent li:first img'));
		var current = ($('#sliderContent li.show')?  $('#sliderContent li.show') : $('#sliderContent li:first'));

			if (nextSlide == 0) {
				var next = $(slides[0]);
			} else if (nextSlide) {
				var next = $(slides[nextSlide]);
			} else {
			//Get next image, if it reached the end of the slideshow, rotate it back to the first image
			var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#sliderContent li:first') :current.next()) : $('#sliderContent li:first'));
		}

		// move span to right
		next.css({left: 0});
		next.css({opacity: 1});
		currentp.animate({opacity: 0.0}, 600);
		currentimg.animate({opacity: 0.0}, 600, function() {
			currentspan.animate({bottom: -850}, 600, function() {
				zIndexNumber -= 10;
				current.css('zIndex', zIndexNumber).removeClass('show').css({opacity: 0}).css('zIndex', zIndexNumber).css({left: 375});
				currentimg.css({opacity: 1});
				currentp.css({opacity: 1});
				currentspan.css({bottom: -315});
				next.addClass('show');

				slideWorking = false;
				
				clearTimeout(timer);
				timer = setTimeout('sliderContent()', 6000);
			});
		});
	}
}
