var total_items = featuredList.length;
var isPlaying = false;

function FeaturedBox(){
	
	this.currentId = Math.floor(Math.random()*total_items)-1;
	
	this.init = function(){
		$(".featuredbox #bt-preview").bind("click", function(){
			if($(this).hasClass("bt-play")){
				$(this).removeClass("bt-play");
				$(this).addClass("bt-stop");
				setSelectedSound($(this).attr("href"));
				clicky.log('home','Preview Track');
				isPlaying = true;
			}else{
				$(this).removeClass("bt-stop");
				$(this).addClass("bt-play");
				currentSound.stop();
				currentSound.destruct();
				isPlaying = false;
			}
			return false;
		})
	}
	
	 
	
	this.getItem = function(p_id){
		/*
		$(".featuredbox #mask #holder").animate({ 
			marginTop: y
		}, {duration: 'slow', easing: 'easeOutBack'});
		*/
		$(".preview-holder .track-title").hide().fadeIn(100);
		$(".preview-holder #bt-preview").attr("href",featuredList[p_id].mp3);
		$(".preview-holder .track-title").html(featuredList[p_id].title);
		$(".preview-holder .track-title").attr("href",featuredList[p_id].url);
		if(isPlaying){
			setSelectedSound($(".featuredbox #bt-preview").attr("href"));
		}
	}
	
	this.getNext = function(){
		featured.currentId++;
		if(featured.currentId>total_items - 1){
			featured.currentId=0;
		}
		featured.getItem(featured.currentId);
		return false;
	}
	
	this.getPrev = function(){
		featured.currentId--;
		if(featured.currentId < 0){
			featured.currentId = total_items-1;
		}
		featured.getItem(featured.currentId);
		
		return false;
	}
	
	
}

$(document).ready(function(){

	currentSound = {};
	
	/* SOUND MANAGER */
	soundManager.url = '/javascript/soundManager/';
	soundManager.debugMode = false;
	soundManager.flashVersion = 9;
	soundManager.useFlashBlock = false; // optionally, enable when you're ready to dive in
	
	// soundManager.useHTML5Audio = true;
	soundManager.onready(function() {
	  // Ready to use; soundManager.createSound() etc. can now be called.
	  
	});
	
	setSelectedSound = function(p_mp3){
		//selectedSound.closest('tr').addClass('playing')
		playSound(p_mp3);	
	}
	
	unsetSelectedSound = function(){
		
		if(currentSound){
			currentSound.stop();
			currentSound.destruct();
		}
		if(selectedSound){
			selectedSound.html('Play track');
			selectedSound.removeClass('playing');
			//selectedSound.closest('tr').removeClass('playing')
			selectedSound = null;
		}
	}
	
	playSound = function(p_path){
		//p_path = 'The+Hands+Of+Stone+-+Deeds.mp3'
		if(isPlaying){
			currentSound.stop();
			currentSound.destruct();
		}
		currentSound = soundManager.createSound({
			id:'snd',
			//serverURL: 'rtmp://s2cbc6ndie9kpq.cloudfront.net/cfx/st/',
			url:p_path,
			onfinish:function() {
				unsetSelectedSound();
			}
		}).play();
		return false;
	}
	
	
	featured = 	new FeaturedBox();
	featured.init();
	featured.getNext();
	
	$(".arrows-holder .right").bind("click",featured.getNext);
	$(".arrows-holder .left").bind("click",featured.getPrev);
	
})




