$(function() {
	if(document.getElementById('homeSlideshow')) {
		homeSlideshow();
	}
});


function homeSlideshow() {
	var $images = $('#homeSlideshow li'),
		$text = $('#homeSlideText li'),
		$bullets,
		bulletHtml,
		slideInterval = null,
		currentSlide = 0;
		
	// if slides are more than 1, continue
	if($images.length < 2) {
		return;
	}
	
	// build homeSlide Bullets
	bulletHtml = '<ul class="homeSlideBullets" id="homeSlideBullets"><li class="active"></li>';
	for(var x=0;x<$images.length-1;x++) {
		bulletHtml += "<li></li>";
	}
	bulletHtml += "</ul>";
	$('#homeSlideshow').after(bulletHtml);
	$bullets = $('#homeSlideBullets li');
	$bullets.click(function() {
		stopInterval();
		slideTo($.inArray(this,$bullets));
		startInterval();
	});
	
	$('#homeBody').hover(function() {
		stopInterval();
	},function() {
		startInterval();
	});
	
	// start slideshow
	startInterval();
	
	function nextInterval() {
		stopInterval();
		var num = currentSlide + 1;
		if(num >= $images.length) {
			num = 0;
		}
		slideTo(num);
		startInterval();
	}
	
	function slideTo(num) {
		$images.eq(currentSlide).hide();
		$text.eq(currentSlide).hide();
		$bullets.eq(currentSlide).removeClass('active');
		$images.eq(num).show();
		$text.eq(num).show();
		$bullets.eq(num).addClass('active');
		currentSlide = num;
	}
	
	function startInterval() {
		slideInterval = setTimeout(nextInterval, 6000);
	}
	function stopInterval() {
		clearTimeout(slideInterval);
		slideInterval = null;
	}
}
