$(document).ready(function(){
    $('#mascara').css('height', $(document).height()).hide(); 


    //  Função para listar diretórios passando um valor se houver subdominios   
    function base_path(num_diretorios){

        var documentBasePath = document.location.href;
        var paths = documentBasePath.split("/");
        var dir = "";
        if(num_diretorios >= 1){
            dir += "/"+paths[3];
        }
        if(num_diretorios >= 2){
            dir += "/"+paths[4];
        }
        var url_site = paths[0]+"//"+paths[2];
        return  url_site + dir;
    }
    
    $('#keyword').keyup(function(e) {
	//alert(e.keyCode);
	if(e.keyCode == 13) {
		$("#frmBuscaIndex").submit();
	}
    });

    // variaveis para as janelas
    var v_url;    // define base_path

    // desenvolvimento
    var url_padrao_site = base_path(0);
    
    // produção
    //var url_padrao_site = base_path();


    //  Apagar ou acender luzes
    function foco_na_mensagem(value){
        if(value == 'show'){
            $('#mascara').toggle();
        }
        if(value == 'hide'){
            $('#mascara').css('height', $(document).height()).hide();
        }
    }
    
    function show_message(msg,tempo,url){
        var mensagem = msg;
        
        $("#show_message_index").slideDown(100);
        $("#show_message_index").html(mensagem);
        
        setTimeout(function(){
            $("#show_message_index").hide();
            if(url != 0){
                window.location.href = url;
            }
        },tempo);

    }
    
    

    //  Monta a Janela de Alerta    

    function janela_open(titulo,msg,timeout,tempo,redirect,url){

        foco_na_mensagem('show');
        
        alert(msg);
        //        $("#dialog").attr("title","CWR - "+titulo);



        //        $("#dialog").html(msg);
        /*$('#dialog').dialog({
            autoOpen: false, 
            show: "blind", 
            hide: "explode", 
            width: 350, 
            height: 130, 
            close: function(){
                foco_na_mensagem('hide');
            }
        });*/

        //    $('#dialog').dialog('open');

        if( (timeout == 1) && (redirect == 1) ){
            setTimeout(function(){ 
                //            $('#dialog').dialog('close'); 
                foco_na_mensagem('hide');
                window.location.href = url; 
            }, tempo );
        }else{ 
            setTimeout(function(){ 
                //            #$('#dialog').dialog('close'); 
                foco_na_mensagem('hide');
            }, tempo );

        }
    }

    $("ul#notas_em_destaque").cycle({
        fx: 'fade'
    });


    $("#menu, #menu ul li, #menu div, #menu a").mouseover(function(){
        $("#painel").css("z-index","-1");
    });

    $("#menu, #menu ul li, #menu div, #menu a").mouseleave(function(){
        $("#painel").css("z-index","1");
    });


    $("#select_cid_apresenta").change(function () {
        window.location=url_padrao_site+"/CidadeApresenta/Listar/"+$(this).val();
    });


    $("#select_entrevista").change(function () {
        window.location=url_padrao_site+"/Entrevistas/Listar/"+$(this).val();
    });

    // Painel Administrativo

    //  Botão link para Cadastro
    $(".senha_cad_create").click(function(){
        window.location=url_padrao_site+"/Cadastro"; 
    });

    //  Botão de login do site    
    $(".senha_btn_signin").click(function(){



        var user = $("#email_form").val();
        var pass = $("#senha_form").val();



        var url_base_form = url_padrao_site+"/Autenticar/Usuario";
        var url_base_autenticado = url_padrao_site+"/Index/Restrito";

        $.post(url_base_form,{
            email_form : user, 
            senha_form : pass
        },function(r){

            if(r.resposta == "ok"){
                window.location.href = url_base_autenticado;
            }

            else if(r.resposta == "excesso_de_tentativas"){


                // config da janela          
                v_titulo = "Login";
                v_msg = "Você realizou 5 tentativas seguidas de acesso ao site, aguarde 10 minutos para poder tentar novamente!";
                v_tempo = 1;
                v_segundos = 5000;
                v_redirect = 0;
                v_url = 0;

                show_message(v_msg,v_segundos,v_url);

            }else{



                // config da janela    
                v_titulo = "Erro";
                v_msg = "Não foi possível efetuar login no site, tente novamente!";
                v_tempo = 1;
                v_segundos = 3000;
                v_redirect = 0;
                v_url = 0;

                show_message(v_msg,v_segundos,v_url);
            }  
        },"json");

    });

    //  Botão de Logout
    $(".senha_btn_signout").click(function(){

        var url_base_logout = url_padrao_site+"/Autenticar/Logout";
        var url_base_redirect = url_padrao_site;

        $.post(url_base_logout, function(r){

            if(r.resposta == "logout_ok"){                                                                 
                window.location.href = base_path(1);
            }else{
                alert(r.resposta);
            }

        },"json");



    });
    
    function get_marquee(){
        var at = Math.floor(Math.random()*10000000);
        var url = url_padrao_site+'/tools/marquee_cidade.html?'+at;
       
       $.post(url,function(r){
           var partes=r.split("|");
           $("#player_index .nome_player").html(partes[1]);
           $("#player_index .imagem").html(partes[2]);
       });
       
    }
    
    function janela(p_pag, p_width, p_heigth) 
{
    var desktop = window.open(p_pag,'pop','scrollbars=no,location=no,directories=no,status=no,resizable=no,copyhistory=yes,menubar=no,width='+p_width+',height='+p_heigth);
}
    
    $("#player_index .player").click(function(){
       $("#player_index .player").css("cursor","pointer");
            janela('http://www.cidadewebrock.ig.com.br/player.php','758','482');
      
    });
    
    
    setTimeout(function(){
        get_marquee();
    },1000);
    
    setInterval(function(){
        get_marquee();
    },300000)


});

