deb=0
ant=0;
paused=0;



function Load_Player_MP3(){
	ant='';
	Counter=0;
	$(document).ready(function(){ 
		//alert("Executando 01")
		largura_progress = 173
		proxima=false;
		$("#jquery_jplayer").jPlayer({
			ready: function () {
				//$(this).setFile("../uploads/musicas/crop/black drawing chalks - life is a big holiday for us - my radio.mp3");
				//demoInstanceInfo($(this), $("#jplayer_info"));
			},
			cssPrefix: "different_prefix_example",
			volume: 50,
			oggSupport: false
		})

		ativaClickMusic();

		$(window).unload( function () {
			if($("#jquery_jplayer").jPlayer("getData", "diag.isPlaying")){
				if(ant!=''){
					set_mp3_statistic(null, ant.find('.bt_play').attr('id_music'), parseInt(LastPlayedTime/1000));
				}
			}
			if(Vant){
				set_video_statistic(id_video);
			}
		});



		$(".bt_share").each(function (i) {
			$(this).css('cursor', 'pointer');
			$(this).click(function(e){
				var relativeX = e.pageX - this.offsetLeft;    
				var relativeY = e.pageY - this.offsetTop;
				show_share(relativeX,relativeY-10, $(this).attr('tipo'), $(this).attr('alvo'));
			})
		});
	});

	$(".pop_rating_bg").each(function(e){
		$(this).css('width',$(this).attr('value')+"%");
	})
}


		function nextMusic(){

			//$('#div_debug').html("COMPLETOU");
			ant.find('.bt_play').attr('src','images/play_a.png');
			ant.find('.progresso').hide()
			//alert(LastPlayedTime);
			at = $(".div_player[posMUSIC='"+(parseInt(ant.attr('posMUSIC'))+1)+"']");								
			//if(at.html()==ant.html()) at=null;
			//alert("Conteudo do proximo index: "+at.html());
			//alert(ant.html());
			if(at.html()!=null){
					progresso = at.find('.progresso');										
					at.find('.bt_play').attr('src','images/play_c.png');
					progresso.show();
					set_mp3_statistic(at.find('.bt_play').attr('id_music'), ant.find('.bt_play').attr('id_music'), 30);
					$("#jquery_jplayer").setFile(at.find('.bt_play').attr('mp3')).play();
					$("#jquery_jplayer").onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {

						progresso.width((largura_progress/100)*playedPercentAbsolute);
						LastPlayedTime = playedTime;

						if (playedTime>=totalTime-100 && playedTime>0 ) {
							nextMusic()
						}; 
						if(function_exists('pp_player_info')){
							pp_player_info(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime)
						}

					})
					ant = at;
					if(function_exists('pp_set_music')){
						pp_set_music(ant.find('.bt_play'));
					}
			}else{
				set_mp3_statistic(null, ant.find('.bt_play').attr('id_music'), 30);
				ant=0;
			}


		}


	function ativaClickMusic(){		
		$(".bt_play").each(function (i) {
			//setanto posicao para cada objeto de player			
			$(this).parent().parent().attr('posMUSIC', i)
			$(this).css('cursor', 'pointer');
			$('.progresso').css('width',largura_progress);
			$(this).click(function(){
				$('.progresso').hide();
				progresso = $(this).parent().parent().find('.progresso');
				if(ant!=0 && ant.find('.bt_play').attr('mp3')==$(this).attr('mp3')){
					if(paused==0){
						$("#jquery_jplayer").pause();
						ant.find('.bt_play').attr('src','images/play_b.png');
						progresso.show();
						paused=1
					}else{
						$("#jquery_jplayer").play();
						ant.find('.bt_play').attr('src','images/play_c.png');
						progresso.show();
						paused=0
					}
					ant=0;
					ant=$(this).parent().parent();
				}else{
					if(ant!=0){	
						ant.find('.bt_play').attr('src','images/play_a.png');
						ant.find('.progresso').hide();
						set_mp3_statistic($(this).attr('id_music'), ant.find('.bt_play').attr('id_music'), parseInt(LastPlayedTime/1000));
					}else{
						set_mp3_statistic($(this).attr('id_music'));
					}
					if(Vant) set_video_statistic();//Verifica se tem video tocando simultaneamente e reseta ele
					$(this).attr('src','images/play_c.png');
					progresso.show();
					$("#jquery_jplayer").setFile($(this).attr('mp3')).play();
					/*
					$("#jquery_jplayer").onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
						progresso.width((largura_progress/100)*playedPercentAbsolute);
						LastPlayedTime = playedTime;
						Counter++;


						$('#div_debug').html(playedTime+"_"+Counter);

					})
					*/

					$("#jquery_jplayer").onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) { 

						progresso.width((largura_progress/100)*playedPercentAbsolute);
						LastPlayedTime = playedTime;

						if (playedTime>=totalTime-100 && playedTime>0 ) {
							//alert("TERMINOU");
							nextMusic();
						}; 
						if(function_exists('pp_player_info')){
							pp_player_info(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime)
						}	
					}) 

					$("#jquery_jplayer").onSoundComplete( function() {
					});

					ant = $(this).parent().parent();
				}	
				if(function_exists('pp_set_music')){
					pp_set_music($(this));
				}				
			})
			$(this).mouseover(function(){			
				if(ant!=0){
					if(ant.find('.bt_play').attr('mp3')!=$(this).attr('mp3')){
						$(this).attr('src','images/play_b.png');
					}
				}else{
					$(this).attr('src','images/play_b.png');
				}
			})
			$(this).mouseout(function(){
				if(ant!=0){
					if(ant.find('.bt_play').attr('mp3')!=$(this).attr('mp3')){
						$(this).attr('src','images/play_a.png');
					}
				}else{
					$(this).attr('src','images/play_a.png');
				}
			})
		});
	}



	function set_mp3_statistic(id_music, lastId,  lastTime){
		$.ajax({
			type: "POST",
			url: "ajax/statistic/statistic_mp3.php",
			data: "id_music="+id_music+"&lastId="+lastId+"&lastTime="+lastTime,
			async: false,
			success: function(msg){								
				//alert( "Data Saved: " + msg );
			}
		});
	}

	function set_video_time(time){
		Vtime = time;
	}
	Vant=0;
	function set_video_statistic(id_video){
		if(ant!=0){	//Verifica se tem MP3 tocando ao mesmo tempo
			ant.find('.bt_play').attr('src','images/play_a.png');
			$("#jquery_jplayer").stop();
			ant.find('.progresso').hide();
			set_mp3_statistic($(this).attr('id_music'), ant.find('.bt_play').attr('id_music'), parseInt(LastPlayedTime/1000));
		}
		if(Vant && Vant!=id_video){		
			//alert("Vídeo Anterior: "+Vant+" Tempo: "+Vtime);
			Vant=0;
		}
		if(id_video && id_video!=Vant){		
			//alert("Estatistica video: "+id_video);
			Vant = id_video;
		}
		stopVideoFlv();


		$.ajax({
			type: "POST",
			url: "ajax/statistic/statistic_video.php",
			data: "id_video="+id_video+"&lastId="+Vant+"&lastTime="+Vtime,
			async: true,
			success: function(msg){								
				//alert( "Data Saved: " + msg );
			}
		});

	}
	function stopVideoFlv(){
		$('.video_flv').each(function(i){
			getFlashMovieObject($(this).attr('id')).stopVideo();
		});
	}
	//alert("aqui");

