/**
 * @author Owner
 */

$(function() {
	// Declare global variables
	var totalItems = $('#feature .content .item').length;
	var currentItem = null;
	var inTransition = false;
	var captionAnimationSpeed = 500;
	var imageAnimationSpeed = 500;
	var interval;
	console.log('total number of items ' + totalItems);
	$('#feature .nav .item').bind('click', function(/* Event */ e) {
		clearInterval(interval);
		var elm = $(e.target);
		if (elm.is('h1') || elm.is('p')) {
			elm = elm.parent();
		}
		showItem(elm.attr('id').replace(/item-/i, ''));
	});
	function showItem(id) {
		if (!inTransition) {
			inTransition = true;
			if (currentItem) {
				$('#item-' + currentItem).removeClass('current');
				$('#item-' + id).addClass('current');
				$('#caption-' + currentItem).animate({opacity: 0.0, height: '0'}, captionAnimationSpeed, function() {
					$('#caption-' + id).animate({opacity: 0.7, height: '64px'}, captionAnimationSpeed);
					$('#content-' + currentItem).animate({opacity: 0.0}, imageAnimationSpeed, function() {
						$(this).css('display', 'none');
					});
					$('#content-' + id).css('opacity', 0.0).css('display', 'block').animate({opacity: 1.0}, imageAnimationSpeed, function() {
						currentItem = id;
						inTransition = false;
					});
				});
			} else {
				$('#item-' + id).addClass('current');
				$('#caption-' + id).animate({opacity: 0.7, height: '64px'}, 250, function() {
					currentItem = id;
					inTransition = false;
				});
			}
		}
	}
	showItem(1);
	interval = setInterval(function() {
		id = (currentItem + 1 <= totalItems) ? currentItem + 1 : 1;
		showItem(id);
	}, 5000)
});