﻿var currentPOV = 0;
var povInterval;
$(function () {
	setPOVArtist(currentPOV);
	buildControls();
	setTimeout(startInterval, 4000);
	
	$("#controls .nav-controls a.prev").click(function () {
		clearInterval(povInterval);
		currentPOV = (currentPOV - 1 >= 0) ? currentPOV - 1 : featuredArtists.length - 1;
		setPOVArtist(currentPOV);
	});
	$("#controls .nav-controls a.next").click(function () {
		clearInterval(povInterval);
		currentPOV = (currentPOV + 1 < featuredArtists.length) ? currentPOV + 1 : 0;
		setPOVArtist(currentPOV);
	});

});
function startInterval() {
	povInterval = setInterval(function () {
		currentPOV = (currentPOV + 1 < featuredArtists.length) ? currentPOV + 1 : 0;
		setPOVArtist(currentPOV);
	}, 4000);
}


function setPOVArtist(index) {
	doPOVSlide(index);
	$("#caption .image-title").html(featuredArtists[index].title);
	var albumImage = $("<img />").attr("src", "/"+featuredArtists[index].albums[0].image).attr("alt", featuredArtists[index].albums[0].title);
	var link = $("<a />").attr("href", featuredArtists[index].albums[0].link).addClass("track-commerce-link").data("tracking", featuredArtists[index].albums[0].tracking);
	link.append(albumImage);
	$("#caption .image-album").empty().append(link);
	$("#caption .spotlight-album-title").empty().append($("<a />").attr("href", featuredArtists[index].albums[0].link).addClass("track-commerce-link").data("tracking", featuredArtists[index].albums[0].tracking).html(featuredArtists[index].albums[0].title));
	$("#caption .spotlight-album-price").empty().append("$" + featuredArtists[index].albums[0].price.split(".")[0] + "<span>." + featuredArtists[index].albums[0].price.split(".")[1] + "</span");
	$("#caption .download a").attr("href", featuredArtists[index].albums[0].link).data("tracking", featuredArtists[index].albums[0].tracking);
	$("#caption .spotlight-watch-now a").attr("href", "/" + featuredArtists[index].link);
	$("#thumbs li").removeClass("selected");
	$("#thumbs li:eq(" + currentPOV + ")").addClass("selected");
	// re-add the tracking
	
	$("#caption .track-commerce-link").click(function () {
		trackCommerce(extractAlbumCommerceData($(this).data("tracking")));
	});
}

function doPOVSlide(index) {
	var oldImage = $("#slideshow img");
	var newImage = $( "<img />" )
						.attr( "src", "/" + featuredArtists[index].image )
						
						.data( "index", index )
						.click( function () {
							location.href = "/" + featuredArtists[$( this ).data( "index" )].link;
						} );



	$( "#slideshow" )
		.append( newImage );

	if ( $.support.transition ) {

		oldImage
			.css( { x: 0 } )
			.transition( { x: -805 }, 750, function () {
				$( this ).remove();
			} );

		newImage
			.css( { x: "806px" } )
			.transition( { x: '0px' }, 750 );


	} else {
		oldImage
			.animate( { left: "-805px" }, 750, function () {
				$( this ).remove(); 
			} );
		newImage
			.css( { left: 806 } )	
			.animate( { left: 0 } );
	}


}

function buildControls() {
	$("#thumbs").empty();
	for (var i = 0; i < featuredArtists.length; i++) {
		var thumb = $("<li />").addClass("thumb").append($("<div class='thumb' />").attr("href", "#"+featuredArtists[i].link).attr("title", featuredArtists[i].title).append($("<img />").attr("src", "/"+featuredArtists[i].image))).data("index", i);
		thumb.click(function (event) {
			event.preventDefault();
			if ($(this).data("index") != currentPOV) {
				currentPOV = $(this).data("index");
				setPOVArtist(currentPOV);
				$("#thumbs li").removeClass("selected");
				$(this).addClass("selected");
			}
			clearInterval(povInterval);
		});
		$("#thumbs").append(thumb);
	}
	$(".thumb").tipTip({
		defaultPosition: "top",
		delay: 0
	});
}
