try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}
function carrega(n){

    //Exibe o texto carregando na div conteúdo
    var conteudo=document.getElementById("agenda");
    conteudo.innerHTML='<div class="carregando">Carregando, por favor aguarde.</div>';

    //Abre a url
    xmlhttp.open("GET", base_url+"agenda/ajax/"+n,true);

    //Executada quando o navegador obtiver o código
    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4){

            //Lê o texto
            var texto=xmlhttp.responseText;

            //Desfaz o urlencode
            texto=texto.replace(/\+/g," ");
            texto=unescape(texto);

            //Exibe o texto no div conteúdo
            conteudo.innerHTML=texto;
            $('a[rel=gallery]').lightBox();
        }
    }
    xmlhttp.send(null);
}

function carregaConsole(n) {
    //Exibe o texto carregando na div conteúdo
    var conteudo=document.getElementById("agenda_console");
    //conteudo.innerHTML='<div class="carregando">Carregando, por favor aguarde.</div>';

    //Abre a url
    xmlhttp.open("GET", base_url+"agenda/ajaxConsole/"+n,true);

    //Executada quando o navegador obtiver o código
    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4){

            //Lê o texto
            var texto=xmlhttp.responseText;

            //Desfaz o urlencode
            texto=texto.replace(/\+/g," ");
            texto=unescape(texto);

            //Exibe o texto no div conteúdo
            conteudo.innerHTML=texto;
            carrega(n);
        }
    }
    xmlhttp.send(null);
}

function votar(album, foto){

    //Exibe o texto carregando na div conteúdo
    var conteudo=document.getElementById("voto");
    conteudo.innerHTML='Aguarde...';

    //Abre a url
    xmlhttp.open("GET", base_url+"coberturas/votar/"+album+"/"+foto,true);

    //Executada quando o navegador obtiver o código
    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4){

            //Lê o texto
            var texto=xmlhttp.responseText;

            //Desfaz o urlencode
            texto=texto.replace(/\+/g," ");
            texto=unescape(texto);

            //Exibe o texto no div conteúdo
            conteudo.innerHTML=texto;
        }
    }
    xmlhttp.send(null);
}

function votarPhoto(enquete, foto){

    //Exibe o texto carregando na div conteúdo
    var conteudo=document.getElementById("voto");
    conteudo.innerHTML='Aguarde...';

    //Abre a url
    xmlhttp.open("GET", base_url+"enquetes/votar/"+enquete+"/"+foto,true);

    //Executada quando o navegador obtiver o código
    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4){

            //Lê o texto
            var texto=xmlhttp.responseText;

            //Desfaz o urlencode
            texto=texto.replace(/\+/g," ");
            texto=unescape(texto);

            //Exibe o texto no div conteúdo
            conteudo.innerHTML=texto;
        }
    }
    xmlhttp.send(null);
}


function solicitar(album, photo)
{
    $('#form').show();
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo('slow',0.6);
    
    $('#id_album').attr('value', album);
    $('#id_foto').attr('value', photo);
    $('#response').html('');
    $('.form_window').show();
}

$(function() {
    $('#mask, .close').click(function() {
        $('#mask, .form_window').hide();
    });
    
    $("#submit").click(function(){
        var nome = $('#nome').val();
        var email = $('#email').val();
        var er = new RegExp(/^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z0-9]{2,}(\.[a-z0-9]{2,})?$/);
        var error = '';
        
        if (nome == '') {
            error += 'O campo Nome é obrigatório.<br />';
        }
        
        if (email == '') {
            error += 'O campo E-mail é obrigatório.<br />';
        }
        
        if (email) {
            if ( ! er.test(email)) {
                error += 'O campo E-mail deve conter um endereço de e-mail válido.<br />';
            }
        }
        
        
        if (! error) {
            $('#error').html('');
            // Mostra tela Loding
            $('#response').ajaxStart(function(){
                $(this).html('Aguarde...');
                $('#form').hide();
            });

            $.post(base_url+'coberturas/solicitar', {
                nome: $('#nome').val(),
                email: $('#email').val(),
                id_album: $('#id_album').val(),
                id_foto: $('#id_foto').val()
            }, function(response) {
                $('#response').html(unescape(response));
                $('#response').fadeIn();
                
                $('#nome, #email').attr('value', '');
            });

            // Esconde Loading
            $("#response").ajaxSuccess(function(){
                //$(this).hide();
                //$('#form').show();
            });
        }
        else
        {
            $('#error').html(error);
        }
            
        return false;
    });
});

