
var sscc = 0;
var idArtisteLecture = null;
var idArtisteAffichage = null;
var idAlbumPiste = null;
var idAlbum = null;
var idPiste = null;

$(document).ready(function() {
  
  $("#jquery_jplayer").jPlayer({
		swfPath: "/Jplayer.swf",
		solution: "html, flash",
		supplied: "mp3",
		ready: function() {
			//alert('OK');
			AfficheInfoJPlayer();
			
		},
		cssSelectorAncestor: "",
		cssSelector: {
			play: "#player_play",
			pause: "#player_pause",
			stop: "#player_stop",
			seekBar: "#player_progress_load_bar",
			playBar: "#player_progress_play_bar",
			currentTime: "#play_time",
			duration: "#total_time"
		},
		timeupdate: function(evt) {
			//console.log("event.progress");
			//console.log("event.progress " + evt.jPlayer.status.currentTime);
			// var pt = new Date(playedTime);
			// var ptm = (pt.getUTCMinutes() < 10) ? "0" + pt.getUTCMinutes() : pt.getUTCMinutes();
			// var pts = (pt.getUTCSeconds() < 10) ? "0" + pt.getUTCSeconds() : pt.getUTCSeconds();
			// $("#play_time").text(ptm+":"+pts);
			// var tt = new Date(totalTime);
			// var ttm = (tt.getUTCMinutes() < 10) ? "0" + tt.getUTCMinutes() : tt.getUTCMinutes();
			// var tts = (tt.getUTCSeconds() < 10) ? "0" + tt.getUTCSeconds() : tt.getUTCSeconds();
			// $("#total_time").text(ttm+":"+tts);
		},
		ended: function() {
			playListNext();
		}
	})
	
	
	$("#ctrl_prev").click( function() {
		playListPrev();
		return false;
	});
 
	$("#ctrl_next").click( function() {
		playListNext();
		return false;
	});
	
	$("#info_artiste").click( function() {
		//$("#artisteslettres").slideDown("fast", function() {
		//$("#albumsretour").slideUp("fast");
	    //$("#artistes").animate({scrollTop: sscc}, 800);
		//$("#artisteslettres").slideUp("slow");
		$("#albums").load(idArtisteLecture + ".html", function() {
			$("#albumsretour").show("fast", function() {
				$("#artisteslettres").slideUp("fast");									 
				idArtisteAffichage = idArtisteLecture;
				AffichageInfo();	
			});
		});
		var chem = "/mp3/"+artistes[idArtisteLecture]+"/folder.jpg";
	    chem = chem.replace("+","%20");
	    $("#marion").attr("src",chem);
    	//});
	});
 });

function AfficheInfoJPlayer() {
	var resultat = "";
	if ($("#jquery_jplayer").data("jPlayer").html.active) {
		resultat += "HTML ";
	} else {
		resultat += "NONE "
	}
	if ($("#jquery_jplayer").data("jPlayer").flash.active) {
		resultat += "FLASH";
	} else {
		resultat += "NONE  "
	} 
	$("#infoJPlayer").html(resultat);
}

function scrollto(i) {
	var le = String.fromCharCode(i+65);
	//alert(i);
	var sssccc = 0;
	var a = $(".majuscule").each(function (j) {
		if ($(this).text()==i)								   
	    	sssccc = this.offsetTop;   
      });
	$("#artistes").animate({scrollTop: sssccc}, 800);
}

function gotitre(id) {
	//console.log("gotitre : id=" + id);
	var decoup = id.split('_');

	idAlbumPiste = id;
	idAlbum = decoup[0];
	idPiste = decoup[1];
	//console.log("gotitre : idAlbum=" + idAlbum);
	//console.log("gotitre : idPiste=" + idPiste);
		
	//console.log("gotitre : avant affiche info ");
	AffichageInfo();
	//console.log("gotitre : apres affiche info ");
	play();
	//console.log("gotitre : apres play ");
		
	
	//console.log("gotitre end");
}

function play() {
	//console.log(">->->" + medias[idAlbum].titre);
	//console.log(">->->" + medias[idAlbum].pistes[idPiste]);
	var urlPiste = encodeURIComponent(medias[idAlbum].titre) + "/" + encodeURIComponent(medias[idAlbum].pistes[idPiste]);
	//console.log(">->->" + urlPiste);
	
	$("#jquery_jplayer").jPlayer( "setMedia", { mp3: "/mp3/" + urlPiste });
	
	$("#jquery_jplayer").jPlayer("play");

	AfficheInfoJPlayer();
}

function AffichageInfo() {
	//try {
		//console.log("AfficheInfo ");
		AfficheInfoJPlayer();
			$(".piste").removeClass("encours");
			var pospiste = $("#"+idAlbumPiste).offset().top;
			var scrollAlbums = $("#albums").scrollTop();
			var hauteurAlbums = $("#albums").height();
			if (pospiste<0) {
				var scrollDestination = pospiste - (hauteurAlbums/2);
				$("#albums").animate({scrollTop: scrollDestination}, 800);		
			}
			if (pospiste>hauteurAlbums) {
				var scrollDestination = scrollAlbums + (pospiste - (hauteurAlbums/2));
				$("#albums").animate({scrollTop: scrollDestination}, 800);			
			}
			//$("#info_artiste").html("<b>" + artistes[idArtiste] + "</b><br><b>" + albums[ida] + "</b>&nbsp;-&nbsp;(" + pistes[idAlbumPiste] + ")");
			$("#info_artiste").html("<b>" + medias[idAlbum].titre + "</b><br><b>" + medias[idAlbum].pistes[idPiste] + "</b>");
			$("#"+idAlbumPiste).addClass("encours");
		
		//console.log("AfficheInfo end");
	// }
	// catch (err) {
	// 	alert(err);
	// }
}

function playListNext() {
	idPiste++;
	if (idPiste>=medias[idAlbum].pistes.length) {
		idPiste = 0;
		idAlbum++;
		if (idAlbum>=medias.length) {
			idAlbum=0;
		}
	}
	idAlbumPiste = idAlbum + "_" + idPiste;
	AffichageInfo();
	play();
}
 
function playListPrev() {
	
	idPiste--;
	if (idPiste<0) {
		idAlbum--;
		if (idAlbum<0) {
			idAlbum=medias.length-1;
		}
		idPiste = medias[idAlbum].pistes.length-1;
	}
	idAlbumPiste = idAlbum + "_" + idPiste;
	AffichageInfo();
	play();
}




