//var filtro = new Object();
var filtro = 0;
var artistas = 0;
var generos = 0;
var orden = 0;
var inicio = 1;

$(document).ready(function() {

start('all');

}); /*FIN DE DOCUMENT.READY */


var start = function($ini) {

	//LIGHTBOX
	 
	$(".modalwindow").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade'
	});

	//HOVER DIV EFFECT
	
	$(function() {
	    // OPACITY OF BUTTON SET TO 50%
	    $(".noticia").css("opacity","0.5");
		    
	    // ON MOUSE OVER
	    $(".noticia").hover(function () {
						      
		// SET OPACITY TO 100%
		$(this).stop().animate({
		    opacity: 1.0
		}, "slow");
	    },
		    
	    // ON MOUSE OUT
	    function () {
		    
	    // SET OPACITY BACK TO 50%
	    $(this).stop().animate({
		    opacity: 0.5
		}, "slow");
	    });
	});
	
	//$('.customselect').sSelect();
	if ($ini == 'noticias' || $ini == 'all')
	{
	    $("[name='noticias_page']").click(function() {
		    $.post('ajax/', { func: "noticias", page: $(this).attr("href") } , function(data) {
			    $('#news').html(data);
			    start('noticias');
		    });
		    return false;
	    });
	}

	if ($ini == 'sesiones' || $ini == 'all')
	{
	    $("[id^='jplayer_play']").click(function(){
		    var nowPlay = this.id;
		    nowPlay = nowPlay.replace('jplayer_play','');
		    pauseAllPlayers(nowPlay);
	    });
	    $("[id^='jplayer_load_bar']").click(function(){
		    var nowPlay = this.id;
		    nowPlay = nowPlay.replace('jplayer_load_bar','');
		    pauseAllPlayers(nowPlay);
	    });
	    $("[name='sesiones_page']").click(function() {
		    $.post('ajax/', { func: "sesiones", page: $(this).attr("href") } , function(data) {
			    $('#moebiusfm').html(data);
			    inicio = 0;
			    start('sesiones');
		    });
		    return false;
	    });
	    _loadSesiones();
	}

	if ($ini == 'temas' || $ini == 'all')
	{
	    $('#loading_trk').hide();
	    $("[id^='jplayer_play']").click(function(){
		    var nowPlay = this.id;
		    nowPlay = nowPlay.replace('jplayer_play','');
		    pauseAllPlayers(nowPlay);
	    });
	    $("[id^='jplayer_load_bar']").click(function(){
		    var nowPlay = this.id;
		    nowPlay = nowPlay.replace('jplayer_load_bar','');
		    pauseAllPlayers(nowPlay);
	    });

	    $("[name='temas_page']").click(function() {
		    $('#loading_trk').show();
		    $.post('ajax/', { func: "temas", page: $(this).attr("href"), sello:filtro, artista:artistas, genero:generos, orden: orden } , function(data) {
			    $('#temas').html(data);
			    start('temas');
		    });
		    return false;
	    });
	    _loadTemas();
	}

	if ($ini == 'sellos' || $ini == 'all')
	{
	    $("#sellos [name^='sello_']").click(function() {
		$('#loading_trk').show();
		var gen = $(this).attr("name");
		gen = gen.substr(6);
		if (filtro == gen)
		{
		    $(this).children().attr('style','');
		    filtro = 0;
		}
		else
		{
		    $('#sellos [name="sello_'+filtro+'"]').children().attr('style','');
		    $(this).children().attr('style','border:4px solid #565a5e;');
		    filtro = (gen*1);
		}
		
/*		if (filtro[gen] == 1)
		{
		    $(this).children().attr('style','');
		    filtro[gen] = '0';
		}
		else
		{
		    $(this).children().attr('style','border:4px solid #565a5e;');
		    filtro[gen] = '1';
		}
*/
		$.post('ajax/', { func: "temas", sello: filtro, artista: artistas, genero: generos, orden: orden } , function(data) {
			$('#temas').html(data);
			start('temas');
		});
		return false;
	    });
	}
	
	if ($ini == 'artistas' || $ini == 'all')
	{
	    $("#artista").change(function() {
		$('#loading_trk').show();
		artistas = $("#artista").val();
		$.post('ajax/', { func: "temas", artista: artistas, genero: generos, sello: filtro, orden: orden } , function(data) {
			$('#temas').html(data);
			start('temas');
		});

	    });
	}

	if ($ini == 'generos' || $ini == 'all')
	{
	    $("#genero").change(function() {
		$('#loading_trk').show();
		generos = $("#genero").val();
		$.post('ajax/', { func: "temas", genero: generos, artista: artistas, sello: filtro, orden: orden } , function(data) {
			$('#temas').html(data);
			start('temas');
		});
	    });
	}
	
	if ($ini == 'ordenar' || $ini == 'all')
	{
	    $("[id^='orden_']").click(function(){
		$('#loading_trk').show();
		var ordena = this.id;
		ordena = ordena.replace('orden_','');
		if (ordena == 'up')
		{
			orden = 1;
			$("#orden_up").addClass('active');
			$("#orden_down").removeClass('active');
		}
		if (ordena == 'down')
		{
			orden=2;
			$("#orden_up").removeClass('active');
			$("#orden_down").addClass('active');
		}

/*

		if (ordena == 'up')
		{
		    if (orden == 1)
		    {
			orden = 0;
			$("#orden_up").removeClass('active');
			$("#orden_down").removeClass('active');
		    }
		    else
		    {
			orden = 1;
			$("#orden_up").addClass('active');
			$("#orden_down").removeClass('active');
		    }
		}
		if (ordena == 'down')
		{
		    if (orden == 2)
		    {
			orden = 0;
			$("#orden_up").removeClass('active');
			$("#orden_down").removeClass('active');			
		    }
		    else
		    {
			orden=2;
			$("#orden_up").removeClass('active');
			$("#orden_down").addClass('active');
		    }
		}
*/
		$.post('ajax/', { func: "temas", sello: filtro, artista: artistas, genero: generos, orden: orden } , function(data) {
			$('#temas').html(data);
			start('temas');
		});
	    });
	}
}

var pauseAllPlayers = function(id)
{
    for(x=1;x<=30;x++)
	if ($("#jquery_jplayer"+x).length == 1 && id != x)
	    if ($("#jquery_jplayer"+x).jPlayer("getData", "diag.isPlaying") == true)
		$("#jquery_jplayer"+x).jPlayer('pause');
}
