// JavaScript Document

var player = [];
var playerFormat = null;
var playerPath = "./audio/";

function Player(source) {
	this.source = source;
	this.audio = this.source.children("audio");
	
	this.index = 0;
	this.track = [];
	var data = this.source.children(".player-data");
	var list = this.source.children(".player-list");
	for(var i = 0; i < data.length; i++) {
		var song = data.eq(i);
		this.track[i] = {
			song : song.attr("data-song"),
			file : song.attr("data-file"),
			type : song.attr("data-type").split(" "),
			title : song.attr("data-title"),
			artist : song.attr("data-artist"),
			description : song.attr("data-description")};
		
		list.append($("<a />")
			.attr("href", "#")
			.attr("data-song", this.track[i].song)
			.html(String.fromCharCode(65 + i))
			.click(function(event) {
				event.preventDefault();
				
				var local = player[$(".player").index($(this).parents(".player"))];
				var index = local.source.find(".player-list a").index($(this));
				
				local.shuffle(index, true);
			}));
	}
	
	if(this.audio[0].canPlayType) {
		this.audio.bind("play", function(event) {
			var local = player[$(".player").index($(this).parents(".player"))];
			local.source.children(".player-toggle").addClass("player-active");
		});
		
		this.audio.bind("pause", function(event) {
			var local = player[$(".player").index($(this).parents(".player"))];
			local.source.children(".player-toggle").removeClass("player-active");
		});
		
		this.audio.bind("ended", function(event) {
			var local = player[$(".player").index($(this).parents(".player"))];
			local.source.children(".player-toggle").removeClass("player-active");
			
			local.shuffle((local.index + 1) % local.track.lenth);
		});
		
		this.audio.bind("progress", function(event) {
			var local = player[$(".player").index($(this).parents(".player"))];
			local.source.find(".player-progress .player-buffer").css({"width" : parseInt(((this.buffered.end(0) / this.duration) * 100)) + "%"});
		});
	
		this.audio.bind("timeupdate", function(event) {
			var local = player[$(".player").index($(this).parents(".player"))];
			local.source.find(".player-progress .player-position").css({"width" : Math.ceil(this.currentTime / this.duration * 100) + "%"});
		});
		
		this.source.children(".player-toggle").click(function(event) {
			event.preventDefault();
			
			var local = player[$(".player").index($(this).parents(".player"))];
			if(local.audio[0].paused) {
				local.play();
			} else {
				local.pause();
			}     
		});
	} else {
		this.source.addClass("disabled");
	}
}
	
		Player.initialize = function() {
			var test = $("<audio />");
			if(test[0].canPlayType("audio/ogg")) {
				playerFormat = {
					type : "ogg",
					mime : "audio/ogg"
				};
			} else if(test[0].canPlayType("audio/x-m4a")) {
				playerFormat = {
					type : "m4a",
					mime : "audio/x-m4a"
				};
			} else if(test[0].canPlayType("audio/mpeg")) {
				playerFormat = {
					type : "mp3",
					mime : "audio/mpeg"
				};
			}
			
			var search = $(".player");
			
			for(var i = 0; i < search.length; i++) {
				var element = search.eq(i);
				player[i] = new Player(search.eq(i));
				player[i].shuffle(0, false);
			}
			
			$(".player-button").eq(0).click(function(event) {
				event.preventDefault();
				
				var object = $(this).siblings(".player");
				if($(this).hasClass("active")) {
					$(this).removeClass("active");
					object.fadeOut(250);
				} else {
					$(this).addClass("active");
					object.fadeIn(250);
				}
			});
			
			$(".player-song").click(function(event) {
				event.preventDefault();
				
				$(".player-list a[data-song=\"" + $(this).attr("data-song") + "\"]").click();
			});
		}
	
	Player.prototype.play = function() {
		this.audio[0].play();
	};
	
	Player.prototype.pause = function() {
		this.audio[0].pause();
	};
	
	Player.prototype.shuffle = function(index, autoplay) {
		var a = this.source.find(".player-list a");
		a.eq(this.index).removeClass("player-selected");
		a.eq(index).addClass("player-selected");
		
		this.index = index;
		
		if(this.audio[0].playing) {
			this.audio[0].pause();
		}
		this.audio.attr("src", "");
		this.audio.html("");
		
		var song = this.track[this.index];
				
		var detail = this.source.children(".player-detail");
			detail.children(".player-title").html(song.title);
			detail.children(".player-artist").html(song.artist);
			detail.children(".player-description").html(song.description);
		
		var download = this.source.children(".player-download");
			download.html("");
		
		for(var i = 0; i < song.type.length; i++) {
			download.append($("<a />")
				.attr("href", playerPath + song.file + "." + song.type[i])
				.html(song.type[i]));
		}
		
		var progress = this.source.children(".player-progress");
			progress.children(".player-buffer").css("width", "0%");
		
		if(playerFormat) {
			this.audio.attr("src", playerPath + song.file + "." + playerFormat.type);
			//audio.load();
			
			if(this.audio[0].buffered != undefined && this.audio[0].buffered.length != 0) {
				this.source.find(".player-progress .player-buffer").css({"width" : parseInt(((this.audio[0].buffered.end(0) / this.audio[0].duration) * 100)) + "%"});
			}
		}
		
		this.audio.unbind("canplay");
		this.audio.removeAttr("autoplay");
		if(autoplay) {
			this.audio.bind("canplay", function(event) {
				this.play();
			});
			this.audio[0].play();
			this.audio.attr("autoplay", "");
		}
	};

$(document).ready(function() {
	Player.initialize();
});
