var Promo = {

	active: 0,
	timeout: false,

	initialize: function() {
		$('#promo-gallery-controls a').each(function(i) {
			$(this).bind('mouseover', function(e) {
				Promo.show(i);
			})
		})

		$('#promo').bind('mouseenter', function() {
			Promo.clearInterval();
		}).bind('mouseleave', function() {
			Promo.startInterval();
		})

		Promo.show(0);
		Promo.startInterval();
	},

	next: function() {
		Promo.show((Promo.active < ($('#promo-data li').length - 1)) ? Promo.active + 1 : 0);
	},

	show: function(i) {
		Promo.active = i;

		var links = $('#promo-gallery-controls a').removeClass('active');;
		$(links[i]).addClass('active');

		var content = $('#promo-data li:nth-child(' + (i + 1) + ')'),
			oldImg = $('#promo-content img').removeClass('new'),
			newImg = $('img', content).clone().addClass('new').appendTo($('#promo-gallery-content'));

		newImg.css({
			opacity: 0
		}).animate({
			opacity: 1
		}, {
			duration: 300,
			complete: function() {
				oldImg.remove();
				$(this).removeClass('new');
			}
		})

		$('#promo-gallery-overlay').empty().append($('a', content).clone().empty().append('<img src="/NastyPixel.gif" />'));
	},

	startInterval: function() {
		Promo.clearInterval();
		Promo.timeout = setTimeout(function() {
			Promo.next();
			Promo.startInterval();
		}, 3500);
	},

	clearInterval: function() {
		if (Promo.timeout) {
			clearTimeout(Promo.timeout);
		}
	}

}

$(function() {
	Promo.initialize();
})
