// background rotation vars
var pt_delay		= 8000;
var pt_fade			= 2000;
var pt_current		= 0;
var pt_slides		= '';

// run when dom is available
$(document).ready(function(){
	pt_get_slides();
});

// get slide data as json
function pt_get_slides(){
	$.getJSON("/home/slides", function(json){
		// set current slide
		pt_current = $('#feature h2').get(0).id.match(/\d/) - 1;
		// set json variable
		pt_slides = json;
		// start slide timer
		setTimeout(pt_photo_rotate, pt_delay);
	});
}

// change background images and taglines
function pt_photo_rotate(){
	// increment through slides
	pt_current++;
	if(pt_current>=pt_slides.length) pt_current = 0;
	// tag current photo for removal
	$('#feature .photo').addClass('oldbg');
	// setup new slide image
	img = new Image();
	$(img).css('display','none')
		// set new image attributes
		.attr({src: pt_slides[pt_current].slide.image, alt: pt_slides[pt_current].slide.tagline1+' '+pt_slides[pt_current].slide.tagline2})
		.addClass('photo')
		// when new image has loaded...
		.load(function(){
			// fade out tagline
			$('#feature h2').fadeOut('', function(){
				// alter, fade in tagline
				$(this).html(pt_format_tagline(pt_current)).fadeIn()
			});
			// add new image
			$('#feature').append(this);
			// fade, remove old image
			$(this).fadeIn(pt_fade, function(){
				$('.oldbg').remove();
				// restart slide timer
				setTimeout(pt_photo_rotate, pt_delay);
			});
	});
}

// format the tagline html (optional url)
function pt_format_tagline(pt_current){
	tagline = pt_slides[pt_current].slide.tagline1+'<br/>'+pt_slides[pt_current].slide.tagline2;
	if(pt_slides[pt_current].slide.url){
		tagline = '<a href="' + pt_slides[pt_current].slide.url + '">' + tagline + ' <span></span></a>';
	}
	if(pt_slides[pt_current].slide.caveat){
		tagline = tagline + ' <em>' + pt_slides[pt_current].slide.caveat + '</em>';
	}
	return tagline;
}