$(document).ready(function() {
	 
       //box de mensagens
       $('.closeable').live("click",function(){
			$(this).parent().hide("slow",function(){
				$('.closeable').parent().remove();
			});
	   });
       
       $('.message').append('<em class="closeable" title="clique para fechar"></em>');
       
   // Add Scroller Object
    $jScroller.add("#scrollerContent","#scroller","left",10);
    // Start Autoscroller
    $jScroller.start();
    
    $('#bt-ampliar').click(function() {
        var id = $('#idEnquete_Pergunta').val();//405 450
        MM_openBrWindow('enquete.php?id=' + id,'Enquete','scrollbars=yes,resizable=1');
		return false;       
    });
    
    $('#bt-result').click(function() {
        var id = $('#idEnquete_Pergunta').val();
        MM_openBrWindow('enquete_resultado.php?id=' + id,'','scrollbars=yes,resizable=1');
		return false;    
    });
    
    var pag = 0;
    var totalPag = 0;
    $.ajax({
       type: "POST",
       url: "carregarRanking.php",
       data: "totalPag=true",
       success: function(msg){
         totalPag = msg;
       }
     });
    $('a.rddPrimeiraPag').click(function(){
        pag = 0;
        carregarRanking(pag);
    });
    
    $('a.rddUltimaPag').click(function(){
        pag = totalPag;
        carregarRanking(pag); 
    });
    
    $('a.rddAnteriorPag').click(function(){
        if(pag != 0)
        {
            pag = pag - 1;
            carregarRanking(pag);
        }
    });
    
    $('a.rddProximaPag').click(function(){
        if(pag != totalPag)
        {
            pag = pag + 1;
            carregarRanking(pag);
        }
 
    });
    
    function carregarRanking(pag)
    {
        $.ajax({
           type: "POST",
           url: "carregarRanking.php",
           data: "numPag=" + pag,
           success: function(msg){
             $('ul.ranking').html(msg);
           }
         });
    }
    
    $('#bt-resultPopup').click(function() {
        var id = $('#idEnquete_PerguntaPopup').val(); 
        location.href='enquete_resultado.php?id=' + id;    
    });
    
    $('.back').click(function(){
       history.back(); 
    });    
    
    $('#galeria a').lightBox();
    
    $('#album a').lightBox({txtImage: 'Imagem',
	txtOf: 'de'});
    
    $('.inativo').attr('href','javascript:void(0)');
    
    $('#slidepainel li span').hide();
    $('#slidepainel li:first span').addClass('imgAtual').show();   
    
    $('#slidepainel li:first a').addClass('itens-painelHover');    

    var rotateImgPainel = 1;
   
    var rotateImg = setInterval(function(){
        rotateImagemPainel();
    }, 5000);
   
    $('.itens-painel').hover(function() {
        $('.itens-painel').each(function(){
            $(this).removeClass('itens-painelHover');
        });
        $('.imgAtual').each(function(){
            $(this).fadeOut("slow",function(){
                $(this).removeClass('imgAtual');
            });
        });
        
        $(this).addClass('itens-painelHover');
        //$(this).parent().children("span").addClass('imgAtual');
        $(this).next().fadeIn("slow",function(){
           $(this).addClass('imgAtual'); 
        });
        
        rotateImgPainel = $('.itens-painel').index(this) + 1;
        clearInterval(rotateImg);
    },function(){
        rotateImg = setInterval(function()
        {
            rotateImagemPainel();
        }, 5000);
    });
    
    function rotateImagemPainel()
    {
        
        var listaImagens = $('#slidepainel li');
        var linkAtual = $('#slidepainel li:nth-child('+ rotateImgPainel +') a');
        linkAtual.removeClass('itens-painelHover');
        $('#slidepainel li:nth-child('+ rotateImgPainel +') span').fadeOut(1000,function(){
            $(this).removeClass('imgAtual');
        });
        
        if(rotateImgPainel < listaImagens.length)
        {
            rotateImgPainel++;
            var itemAtual = $('#slidepainel li:nth-child('+ rotateImgPainel +') a');
            itemAtual.addClass('itens-painelHover');
            itemAtual.next().addClass('imgAtual').fadeIn(1000);
        }
        else
        {
            itemAtual = $('#slidepainel li:first a'); 
            itemAtual.addClass('itens-painelHover');
            itemAtual.next().addClass('imgAtual').fadeIn(1000);
            rotateImgPainel = 1;
        }
    }

	var rotateLstProgramas = 2;
	$('ul.listaProgramas:not(ul.listaProgramas:first)').hide();
   
   function rotateListaProgramas()
   {
		var listaProgramas = $('ul.listaProgramas');
		$('ul.listaProgramas:nth-child('+ rotateLstProgramas +')').fadeOut(500, function(){
				if(rotateLstProgramas <= listaProgramas.length)
				{
					rotateLstProgramas++;
					var itemAtual = $('ul.listaProgramas:nth-child('+ rotateLstProgramas +')')
					itemAtual.fadeIn(500);                        
				}
				else
				{
					itemAtual = $('ul.listaProgramas:first'); 
					itemAtual.fadeIn(500)
					rotateLstProgramas = 2;
				}
			}
		);
	}
	
	setInterval(function()
	{
		rotateListaProgramas();
	}, 5000);
    
    function contagemRegressiva()
    {
        ano = 2010;
        mes = 6;
        dia = 11;
        hora = 11;
        minuto = 00;
        segundo = 00;
        dataFinal = new Date(ano, mes-1, dia, hora, minuto, segundo);
    
        dataInicial = new Date();
        goSeg = Math.floor((dataFinal-dataInicial)/1000);
        seg = goSeg%60;
        goMin = Math.floor(goSeg/60);
        min = goMin%60;
        goHoras = Math.floor(goMin/60);
        horas = goHoras%24;
        dias = Math.floor(goHoras/24);
        if (seg<10) {
        seg = "0"+seg;
        }
        if (min<10) {
        min = "0"+min;
        }
        if (horas<10) {
        horas = "0"+horas;
        }
        if(dias < 2) {
            $('li.texto').text('dia');
        }
        if(dias > 0)
        	$('li.dia').text(dias);
        else
        	$('li.dia').text("0");
        if(horas > 0)
        	$('li.hora').text(horas);
        else
        	$('li.hora').text("0");
        if(min > 0)
        	$('li.min').text(min);
        else
        	$('li.min').text("0");
        if(seg > 0)
        	$('li.segundos').text(seg);
        else
        	$('li.segundos').text("0");
    }
    
    setInterval(function()
	{
		contagemRegressiva();
	}, 1000);
    
});
