var tweenDuration = 2000;
var imageCount;
var selectedImage = 0;
var interval;

jQuery(document).ready(function(){
	//set the image count
	imageCount = jQuery(".header_image").length;
	
	//get the first speed and fade
	var speed = getSpeed(jQuery('.header_image').eq(selectedImage).attr('rel'));
	var fade = getFade(jQuery('.header_image').eq(selectedImage).attr('rel'));
	
	//tween the images
	interval = setTimeout(function(){ tweenImages(fade); },speed);
});

function tweenImages(animate){
	//set the animation value
	var anim = (animate==false) ? false : true;
	
	if(anim){
		//animate the current image out
		jQuery(".header_image").eq(selectedImage).animate({'opacity':0},tweenDuration);
	} else {
		//just hide it
		jQuery(".header_image").eq(selectedImage).hide();
	}
	
	//increment selected image
	selectedImage++;
	
	//reset the selected image
	if(selectedImage >= imageCount){
		selectedImage = 0;
	}
	
	//get options
	var speed = getSpeed(jQuery(".header_image").eq(selectedImage).attr('rel'));
	var fade = getFade(jQuery(".header_image").eq(selectedImage).attr('rel'));
	
	if(anim){
		//animate the next image in
		jQuery(".header_image").eq(selectedImage).css({'opacity':0}).show().animate({'opacity':1},tweenDuration,function(){
			setTimeout(function(){ tweenImages(fade); },speed);
		});
	} else {
		jQuery(".header_image").eq(selectedImage).show();
		setTimeout(function(){ tweenImages(fade); },speed);
	}
}

function getSpeed(rel){
	return parseInt(rel.split('|')[0]);
}

function getFade(rel){
	return (rel.split('|')[1]==1) ? true : false;
}
