// Player
// Player de audio

Player = function(config) {
	
	var _that,
		_elementString,
		_element,
		_player,
		_playItem,
		_playList,
		_isPlaying;
	
	// Constructor
	
	this.init = function(element, playList) {
		_that			= this;
		
		_elementString	= element;
		_element		= $(element);
		_playItem		= 0;
		_playList		= playList;
		
		this.modifyDOM();
		this.loadPlayList();
	};
	
	// Creamos los elementos del DOM
	
	this.modifyDOM = function() {
		
		var dom = '<div id="jquery_jplayer"></div><div class="jp-playlist-player"><div class="jp-interface"><div class="jp-songname"></div><ul class="jp-controls"><li><a href="#" id="jplayer_play" class="jp-play" tabindex="1">play</a></li><li><a href="#" id="jplayer_pause" class="jp-pause" tabindex="1">pause</a></li><li><a href="#" id="jplayer_stop" class="jp-stop" tabindex="1">stop</a></li><li><a href="#" id="jplayer_volume_min" class="jp-volume-min" tabindex="1">min volume</a></li><li><a href="#" id="jplayer_volume_max" class="jp-volume-max" tabindex="1">max volume</a></li><li><a href="#" id="jplayer_previous" class="jp-previous" tabindex="1">previous</a></li><li><a href="#" id="jplayer_next" class="jp-next" tabindex="1">next</a></li></ul><div class="jp-progress"><div id="jplayer_load_bar" class="jp-load-bar"><div id="jplayer_play_bar" class="jp-play-bar"></div></div></div><div id="jplayer_volume_bar" class="jp-volume-bar"><div id="jplayer_volume_bar_value" class="jp-volume-bar-value"></div></div><div id="jplayer_play_time" class="jp-play-time"></div><div id="jplayer_total_time" class="jp-total-time"></div></div><div id="jplayer_playlist" class="jp-playlist"><ul><li></li></ul></div></div>';
		_element.append(dom);
		_element.css({opacity:0});
		
		$("div.jp-play-time, div.jp-total-time").css({opacity:0});
		$("div.jp-progress").hide();
		$("#jplayer_stop").hide();
		//_element.animate({right:"-244px"}, "slow");
				
	};
	
	// Cargamos el playList

	this.loadPlayList = function(){
		$.getJSON(_playList, function(data) {
			_playList = data;
			_that.createPlayer();
			_that.addPlayerEvents();
		});
	};
	
	// Creamos el player >> jPlayer (jQuery plugin)
	
	this.createPlayer = function() {
		
		var jpPlayTime = $("#jplayer_play_time");
		var jpTotalTime = $("#jplayer_total_time");
		
		_player = $("#jquery_jplayer").jPlayer({
			swfPath: "js/lib/jquery",
			ready: function() {
				_that.displayPlayList();
				_that.playListInit(false);		// poner a true para que empieze reproduciéndose
			}
		})
		.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
			jpPlayTime.text($.jPlayer.convertTime(playedTime));
			jpTotalTime.text($.jPlayer.convertTime(totalTime));
		})
		.jPlayer("onSoundComplete", function() {
			_that.playListNext();
		});
		
		_element.animate({opacity:1}, "slow");
	};
	
	this.addPlayerEvents = function() {

		$("#jplayer_previous").click( function() {
			_that.playListPrev();
			$(this).blur();
			return false;
		});
	
		$("#jplayer_next").click( function() {
			_that.playListNext();
			$(this).blur();
			return false;
		});
		
		
		var playerTime = $("div.jp-play-time, div.jp-total-time");
		$(".jp-progress").mouseover( function() {
			playerTime.stop().animate({opacity: 1}, "normal");
			//return false;
		});
		
		$(".jp-progress").mouseout( function() {
			playerTime.stop().animate({opacity: 0}, "normal");
			//return false;
		});
		
		/*
		_element.mouseover( function() {
			_element.stop().animate({right: "0px"}, "slow");
		});
		
		_element.mouseout( function() {
			_element.stop().animate({right: "-244px"}, "slow");
		});
		*/
	};
	
	// Métodos de jPlayer > ejecución de canciones, etc
	
	this.startPlaying = function() {
		_that.playListInit(true);
	};
	
	this.pausePlaying = function() {
		$("#jquery_jplayer").jPlayer("pause");
	};
	
	this.displayPlayList = function() {
		
		$("#jplayer_playlist ul").empty();
		
		for (i=0; i < _playList.length; i++) {
			var listItem = (i == _playList.length-1) ? "<li class='jplayer_playlist_item_last'>" : "<li>";
				listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ _playList[i].name +"</a></li>";
				
			$("#jplayer_playlist ul").append(listItem);
			$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				
				if (_playItem != index) {
					_that.playListChange( index );
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
				
				$(this).blur();
				return false;
			});
		}
		
		
		$("#jplayer_playlist").hide();
		_element.animate({opacity: 1}, 2000);
		
	};

	this.playListInit = function(autoplay) {
		if (autoplay) {
			_that.playListChange( _playItem );
		} else {
			_that.playListConfig( _playItem );
		}
	};
	
	this.playListConfig = function( index ) {
		$("#jplayer_playlist_item_"+_playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
		$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current");
		_playItem = index;
		$("#jquery_jplayer").jPlayer("setFile", _playList[_playItem].mp3);
		
		$(".jp-songname").text(_playList[index].name);
	};

	this.playListChange = function( index ) {
		_that.playListConfig( index );
		$("#jquery_jplayer").jPlayer("play");
	};

	this.playListNext = function() {
		var index = (_playItem+1 < _playList.length) ? _playItem+1 : 0;
		_that.playListChange( index );
	};

	this.playListPrev = function() {
		var index = (_playItem-1 >= 0) ? _playItem-1 : _playList.length-1;
		_that.playListChange( index );
	};

	// Mover arriba / abajo el player
	this.moveUpPlayer = function(e) {
		_element.stop().animate({height:"50px"}, "slow");
	};
	
	this.moveDownPlayer = function(e) {
		_element.stop().animate({height:"37px"}, "slow");
	};
	
	// API desde otras secciones
	this.addSongAndPlay = function(name, mp3) {
		_isPlaying = !$("#jplayer_play").is(":visible");
		
		$("#jquery_jplayer").jPlayer("setFile", mp3).jPlayer("play");
		$(".jp-songname").text(name);
		_element.mouseover();
		
	};
	
	this.removeSong = function() {
		this.playListChange(_playItem);
		if (!_isPlaying) $("#jquery_jplayer").jPlayer("pause");
		
		_element.mouseout();
	};
	
	// ToString
	
	this.toString = function() {
		return 'Player ( element: ' + _elementString + ', playList: ' + _playList + ', currentSong: ' + _playItem + ' )';	
	};
	
	// llamada al constructor
	this.init(config.element, config.playList);
};

