﻿var current = 0,
	prev = -1,
	total = 0,
	init = true;
	
$(document).ready(function() {	
	$('#backgrounds .bg').each(function() {
		$(this).hide();		
		$(this).removeClass("hideme");		
		total++;
	});	
});

$(window).load(function() {
	if (total > 0) {
		resizeBG();
		$(".image"+current).show();
		$(".big-claim"+current).fadeIn(1500);
		$(".small-claim"+current).fadeIn(1500);
		setTimeout(slideshow, 5000);
	}
	
	$(window).resize(function() {
		resizeBG();
	}).trigger("resize");
});
	
function resizeBG() {	
	var $bg = $(".image"+current),
		$stage = $(window);	
	var w = $bg.find("img").width(),
		h = $bg.find("img").height(),
		scale_w = $stage.width() / w,
		scale_h = $stage.height() / h,
		scale = Math.max(scale_w, scale_h);		
	$bg.find("img").css("width", Math.ceil(scale * w));
	$bg.find("img").css("height", Math.ceil(scale * h));
}

function slideshow() {
	var next,
		prev = current;
	if (current < (total - 1)) {
		next = current + 1;
	} else {
		next = 0;
	}
	current = next;
	var $nextBG = $(".image"+current),
		$prevBG = $(".image"+prev);
	$nextBG.show();
	resizeBG();
	$nextBG.hide();
	$(".big-claim" + prev).fadeOut(1000, function() {
		$(".big-claim" + current).fadeIn(1500);
	});
	$(".small-claim" + prev).fadeOut(1000, function() {
		$(".small-claim" + current).fadeIn(1500);
	});		
	$prevBG.fadeOut(1000, function() {
		$nextBG.css("display", "none");		
		$nextBG.fadeIn(1500, function() {
			setTimeout(slideshow, 10000);
		});
	});
}
