$(document).ready(function(){
	$('.playPause').click(function(){
		
		$(this).siblings('p').removeClass('blur');
		$(this).parent('li').siblings('li').children('p').addClass('blur');
		
	});
	
	AudioPlayer();
	
});



function AudioPlayer(){
	
	var audio = document.createElement('audio');
	if(!!audio.canPlayType){
		Native = new NativeAudio();
	} else {
		Flash = new FlashAudio();
	}

}


// NativeAudio object

function NativeAudio(){
	this.fileExtension = this.checkAudioType();
	this.appendAudioType();
	
	this.newSong = true;
	this.audioIsPlaying = false;
	this.songEnded = false;
	this.currentAudio;
	
	$('.playPause').click(function(){
		var self = $(this);
		Native.playAudio(self);
	});
}

NativeAudio.prototype = {
	
	playAudio: function(self){
		
		this.currentIndex = $(self).parent('li').index();
		
		if($(self).hasClass('playing')){
			this.currentAudio.pause();
			$(self).removeClass('playing').addClass('paused');
			this.audioIsPlaying = false;
		} else if($(self).hasClass('paused')){
			this.currentAudio.play();
			$(self).removeClass('paused').addClass('playing');
			this.audioIsPlaying = true;
		} else {
			this.newSong = true;
		}
		
		if(this.newSong){
			
			var allPlayButtons = $('.playPause');
			for(var i = 0; i < allPlayButtons.length; i++){
				$(allPlayButtons[i]).removeClass('playing').removeClass('paused');
			}
			
			if(this.currentAudio){
				this.currentAudio.pause();
				this.currentAudio.currentTime = 0;
			}
			
			// set current audio to track selected
			this.currentAudio = $('audio').get(this.currentIndex);
			$(self).addClass('playing');
			this.currentAudio.play();
			
			// audio is now playing
			this.audioIsPlaying = true;
			this.newSong = false;
			
		}	
		
	},
	
	checkAudioType: function(){
		var audio = document.createElement('audio');
		
		if(!!audio.canPlayType('audio/mpeg')){
			return '.mp3';
		} else if(!!audio.canPlayType('audio/ogg')){
			return '.ogg';
		} else {
			return '.wav';
		}
	},
	
	appendAudioType: function(){
		var allAudio = document.getElementsByTagName('audio');
		
		for(var i = 0; i < allAudio.length; i++){
			var thisSource = $(allAudio[i]).attr('src');
			$(allAudio[i]).attr('src', thisSource + this.fileExtension);
		}
	}
	
}

// FlashAudio object

function FlashAudio(){
	
	var self = this;
	
	soundManager.onready(function(){
		self.setupAudio();
	});
	
	this.newSong = true;
	this.audioIsPlaying = false;
	this.songEnded = false;
	this.currentAudio = "";
	
	$('.playPause').click(function(){
		var self = $(this);
		Flash.playAudio(self);
	});
}

FlashAudio.prototype = {
	
	playAudio: function(self){
		
		this.currentIndex = $(self).attr('id');
		
		if($(self).hasClass('playing')){
			this.currentAudio.pause();
			$(self).removeClass('playing').addClass('paused');
			this.audioIsPlaying = false;
		} else if($(self).hasClass('paused')){
			this.currentAudio.play();
			$(self).removeClass('paused').addClass('playing');
			this.audioIsPlaying = true;
		} else {
			this.newSong = true;
		}
		
		if(this.newSong){
			
			var allPlayButtons = $('.playPause');
			for(var i = 0; i < allPlayButtons.length; i++){
				$(allPlayButtons[i]).removeClass('playing').removeClass('paused');
			}
			
			if(this.currentAudio){
				this.currentAudio.pause();
				this.currentAudio.setPosition(0);
			}		
			
			// set current audio to track selected
			this.currentAudio = soundManager.getSoundById(this.currentIndex);
			$(self).addClass('playing');
			this.currentAudio.play();
			
			// audio is now playing
			this.audioIsPlaying = true;
			this.newSong = false;
			
		}	
	
	},
	
	setupAudio: function(){
		
		var allAudio = document.getElementsByTagName('audio');
		var songArray = ['one', 'two', 'three'];
		
		for(var i = 0; i < allAudio.length; i++){
			soundManager.createSound({
				id: songArray[i],
				url: $(allAudio[i]).attr('src') + ".mp3"
			});
		}
		
	},
	
}

// Ajax for TopSpin download

function getDownload(){
	var email_address = $('#email').val();
	$('#submit').css({ 'color' : '#323232' });
	$('.loading').fadeIn('fast');
	$.ajax({
		url: "http://app.topspin.net/api/v1/fan/create_fan",
		type: "GET",
		dataType: 'jsonp',
		data:  ({ 'fan' : {
						   email: '' + email_address + '',
						   source_campaign: 'https://app.topspin.net/api/v1/artist/17358/campaign/10190003', 
						   referring_url: 'http://greenriverordinance.com',
						   confirmation_target: 'http://www.topspindownloads.com/confirm/',
						   artist_id:  '17358'
						   }
				}),
		success: function(resp) {
			$('#emailContainer').fadeOut('fast', function(){
				$(this).remove();
				$('#emailBox').append('<p>Check your inbox for download!  </p>');
			});
		}, 
		error: function(){
			$('.loading').fadeOut('fast', function(){
				$('#submit').css({ 'color' : '#e9e9e9' });
			});
			$('#email').css({ 'border' : '1px solid red' });
			
		}

	});	
}


