$(document).ready(function() {

	var curImage = 0;
	var playing = true;
	var playTimeout = false;
	
	$('#carousel img:gt(0)').hide();
	
	switchImage = function(x) {
		$('#carousel-paging a:eq(' + curImage + ')').removeClass("carousel-selected");
		$('#carousel-paging a:eq(' + x + ')').addClass("carousel-selected");
		$('#carousel-text h1').html(carouselTitles[x]);
		$('#carousel-text span').html(carouselTexts[x]);
		$('#carousel img:eq(' + curImage + ')').fadeOut(400);
		$('#carousel img:eq(' + x + ')').fadeIn(1000, function() {
			startPlay();
		});
		curImage = x;
	};
	
	stopPlay = function () {
		clearTimeout(play);
		playTimeout = false;
	}
	
	startPlay = function() {
		if (playing) {
			if (!playTimeout) {
				play = setTimeout(function() {
					playTimeout = false;
					switchImage((curImage + 1) % $('#carousel img').length);
				}, 6000);
				playTimeout = true;
			}
		}
	}
	
	$("#bannerContainer").hover(function() {
		playing = false;
		stopPlay();
	}, function() {
		playing = true;
		startPlay();
	});
	
	$('#carousel-paging a').click(function() {
		stopPlay();
		switchImage($(this).attr("rel") - 1);
		startPlay();
	});
	
	playing = true;
	switchImage(0);
	
});
