var current_header = 0;
var animation_time = 750;
var header_timeout = 5000;
var header_interval;
var img_len;

// adds a listener to the current browsers "on load" event to create/setup the slideshows
if (window.attachEvent) {
	window.attachEvent('onload', show_headers);
} else if (window.addEventListener) {
	window.addEventListener('load', show_headers, false);
} else {
	document.addEventListener('load', show_headers, false);
}

function show_headers(){
	//get the header image length
	img_len = jQuery(".header_image").length;
	
	if(img_len > 1){
		header_interval = setInterval('changeImage(1)',header_timeout);
	}
}

function changeImage(dir){
	//return false if animated
	if(jQuery(".header_image").is(":animated")) return false;
	
	//clear the interval and reset it
	clearInterval(header_interval);
	header_interval = setInterval('changeImage(1)',header_timeout);
	
	//get the data
	var cur_img = current_header;
	
	//get the next image
	if(current_header + dir >= img_len){
		current_header = 0;
	} else if(current_header + dir < 0){
		current_header = img_len - 1;
	} else {
		current_header += dir;
	}
	
	//set the text for the current image
	jQuery("#current_image").html(current_header+1);
	
	//hide the previous link and show the current one
	jQuery(".header_link").eq(cur_img).hide();
	jQuery(".header_link").eq(current_header).show();
	
	//fade the images in and out
	jQuery(".header_image").eq(cur_img).fadeOut(animation_time);
	jQuery(".header_image").eq(current_header).fadeIn(animation_time);
}
