$(document).ready(readyHome);

var current_photo = 0;
var photo_total;
var timer;

function readyHome() {	
	$(".header_slideshow_btn").click(setPhotoTo);
	$("#header_slideshow_prev").click(setPhotoPrev);
	$("#header_slideshow_next").click(setPhotoNext);
	photo_total = $(".header_slideshow_btn").length;
	setTextOverlay();
	startTimer();
}
function startTimer() {
	clearTimeout(timer);
	timer = setTimeout(ticker, 6000);
}
function stopTimer() {
	clearTimeout(timer);
}
function ticker() {
	startTimer();
	current_photo++;
	if(current_photo > (photo_total-1)) current_photo = 0;
	transitionThePhoto();
}
function setPhotoTo() {
	var num = $(this).attr("id");
	num = num.replace("header_slideshow_btn_","");
	num = num-1;
	clearTimeout(timer);
	timer = setTimeout(ticker, 6000);
	current_photo = num;
	transitionThePhoto();
}
function setPhotoPrev() {
	clearTimeout(timer);
	timer = setTimeout(ticker, 6000);
	current_photo--;
	if(current_photo < 0) current_photo = (photo_total-1);
	current_photo = current_photo;
	transitionThePhoto();
}
function setPhotoNext() {
	clearTimeout(timer);
	timer = setTimeout(ticker, 6000);
	current_photo++;
	if(current_photo > (photo_total-1)) current_photo = 0;
	current_photo = current_photo;
	transitionThePhoto();
}
function transitionThePhoto() {
	$("#header_slideshow_photos").animate({ top:0-(current_photo*296)+"px" });
	for(i = 0; i < photo_total; i++) {
		if(i == current_photo) {
			$("#header_slideshow_btn_"+(i+1)).addClass("header_slideshow_current");
		} else {
			$("#header_slideshow_btn_"+(i+1)).removeClass("header_slideshow_current");
		}
	}
}
function setTextOverlay() {
	for(i = 0; i < photo_total; i++) {
		_current_photo = "#header_slideshow_photo"+(i+1);
		_large_text = $(_current_photo + " .large").height();
		_small_text = $(_current_photo + " .small").height();
		_target_height = _large_text + _small_text + 43;
		$(_current_photo + " .text_overlay").height(_target_height);
	}
}
