// JavaScript Document
function addSelectionControls()
{
	jQuery.each($('#specialsGallery li.offer'), function()
									{
										$(this).css('opacity', (($(this).prevAll().length + 1) > 1 ? '0' : '1') );
										//appends a selection controls for the current promotion
										$('.controlList').append('<li class="item' + 
										 ($(this).hasClass('selected') ? ' selected' : '') +
										 '">' + ($(this).prevAll().length + 1).toString() + '</li>')
									});

	jQuery.each($('.controlList > .item'), function()
											{
												$(this).click(function(){
													var query = '.offer:eq(' 
															+ $(this).prevAll().length.toString() + ')';
													switchOffer(query);
													pausePromotionsSlide();
												});
											});
}

function switchOffer(target)
{
	$('.offer.selected').fadeTo('slow', 0).removeClass('selected');
	$(target).addClass('selected').fadeTo('slow', 1);
	$('.controlList > .item.selected').removeClass('selected');
	var query = '.controlList > .item:eq(' + $(target).prevAll().length.toString() + ')';
	$(query).addClass('selected');
}

function nextPromotion()
{
	var current = $('.offer.selected').prevAll().length;
	var next = (current < ($('#specialsGallery').children('.offer').length - 1) ? current + 1 : 0);
	var query = '.offer:eq(' + next.toString() + ')';
	switchOffer(query);
	promotionSlider = setTimeout(nextPromotion, 5000);
}

function startPromotionsSlide()
{
	addSelectionControls();
	promotionSlider = setTimeout(nextPromotion, 5000);
	//$('.controls .altText').text($('.specialOffer.selected img').attr('alt'));
}

function pausePromotionsSlide()
{
	clearTimeout(promotionSlider);
}

$(document).ready(startPromotionsSlide);
