/*------------------------------------------------------------------
Project:		SRP Music Group
Author:			Jay Contonio
Last change:	2009-03-29
-------------------------------------------------------------------*/



var captions = new Array();
var urls = new Array();

var currentIndex = 0;
var promotionHeight;
var numSlides;
var time = 8000;
var intervalID;

function initializePromotions()
{

	// Set promotion height
	promotionHeight = $('#mask').height();
	
	// Disable the up arrow in the promotion
	Splash.disableArrowUp();
	
	// Get the json data of all the latest promotions
	Splash.getPromotionData();
	
	// Add the click handlers to the arrows
	$('#info #up a').click(function() {
		Splash.moveUp();
		Splash.killTimer();
		return false;
	});
	$('#info #down a').click(function() {
		Splash.moveDown();
		Splash.killTimer();
		return false;
	});
	
	$('#mask .photo').fadeIn();
	
}

/* Splash object
--------------------------------------------- */
var Splash = {
	getPromotionData: function() {
		$.getJSON("/promotions.json", function(data) {
			$.each(data, function(i,item) {
				captions.push(item.promotion.caption);
				urls.push(item.promotion.url);
			});
			numSlides = captions.length - 1;
			Splash.setCaption(currentIndex);
			Splash.setupClickHandler();
			Splash.setTimer();
		});
	},
	disableArrowUp: function() {
		$("#up a").css({ cursor:'default',backgroundPosition:'bottom' });
	},
	disableArrowDown: function() {
		$("#down a").css({ cursor:'default',backgroundPosition:'bottom' });
	},
	enableArrowUp: function() {
		$('#up a').css({ cursor:'pointer',backgroundPosition:'top' });
	},
	enableArrowDown: function() {
		$('#down a').css({ cursor:'pointer',backgroundPosition:'top' });
	},
	setCaption: function(index) {
		$('#info .wrapper p').text(captions[index]);
	},
	setupClickHandler: function() {
		$('.photo').each(function(index,element) {
			$(element).click(function() {
				Splash.navigateTo(index);
			});
		});
	},
	setTimer: function() {
		intervalID = setInterval(Splash.moveDown, time);
	},
	killTimer: function() {
		clearTimeout(intervalID);
	},
	moveUp: function() {
		// Check if we're at the beginning
		if(currentIndex <= 0) { return; }
		
		currentIndex--;
		if(currentIndex == 0) { Splash.disableArrowUp(); }
		
		var currentPosition = $('#container').css('top');
		
		$('#container').animate({
			top: (parseFloat(currentPosition) + promotionHeight) + 'px'
		}, "medium");
		
		Splash.setCaption(currentIndex);
		Splash.enableArrowDown();
	},
	moveDown: function() {
		// Check if we're at the end
		if(currentIndex >= numSlides) {
			Splash.killTimer();
			return;
		}
		
		currentIndex++;
		if(currentIndex == numSlides) { Splash.disableArrowDown(); }
		
		var currentPosition = $('#container').css('top');
		
		$('#container').animate({
			top: parseFloat(currentPosition) - promotionHeight + 'px'
		}, "medium");
		
		Splash.setCaption(currentIndex);
		Splash.enableArrowUp();
	},
	navigateTo: function(index) {
		location.href = urls[index];
	}
}