$(document).ready(function () {
//Title and Password
    $('#chkBusca, #chkAbstract').click(function() {
        if($('#chkBusca').attr('checked') || $('#chkAbstract').attr('checked')){
            $('#valorBusca').focus();
        }
    });
    $("#valorBusca").blur(function () {
         if($(this).val() == ''){
             $('#chkBusca').attr('checked', false);
             $('#chkAbstract').attr('checked', false);
         }
    });
    $("#valorBusca").click(function () {
         $('#chkBusca').attr('checked', true);
    });
    $("#valorBusca").keydown(function (event) {
        if(event.keyCode == 13){
            $("#form_busca").submit();
        }
    });

//Author
    $('#chkAuthors').click( function() {
        if($(this).is(':checked')){        
            $("#valorBuscaAuthors").focus();
        }
    });
    $("#valorBuscaAuthors").click(function () {
        $('#chkAuthors').attr('checked', true);
    });
    $("#valorBuscaAuthors").blur(function () {
        if($(this).val() == ''){
            $('#chkAuthors').attr('checked', false);
        }
    });
    $("#valorBuscaAuthors").keydown(function (event) {
        if(event.keyCode == 13){
            $("#form_busca").submit();
        }
    });
    

//Volume    
    $('#paperArea').change(function() {
        $('#chkType').attr('checked', true);
    });

//Submit

    $('#submit_button').click(function(){
        $('#form_busca').submit();
    });
    
//click on authors
    $('.author_name').click(function(){
            $('#chkBusca').attr('checked', false);
            $('#chkAbstract').attr('checked', false);
            $('#valorBusca').val('');

            $('#chkType').attr('checked', false);
            
            $('#chkAuthors').attr('checked',true);
            $('#valorBuscaAuthors').val($(this).attr('title'));
            $('#form_busca').submit();
    });    
    

});


function highlight(found, foundAuthor)
{
    var regex,regexAuthor;

    //acentuacao
    found = found.replace(/(\.|\,)/gi,"\\$1");
    found = found.replace(/a/gi,"[a,á,à,ã,â,ä]");
    found = found.replace(/e/gi,"[e,é,è,ê,ë]");
    found = found.replace(/i/gi,"[i,í,ì,î,ï]");
    found = found.replace(/o/gi,"[o,ó,ò,õ,ô,ö]");
    found = found.replace(/u/gi,"[u,ú,ù,û,ü]");
    found = found.replace(/c/gi,"[c,ç]");
    foundAuthor = foundAuthor.replace(/(\.|\,)/gi,"\\$1");
    foundAuthor = foundAuthor.replace(/a/gi,"[a,á,à,ã,â,ä]");
    foundAuthor = foundAuthor.replace(/e/gi,"[e,é,è,ê,ë]");
    foundAuthor = foundAuthor.replace(/i/gi,"[i,í,ì,î,ï]");
    foundAuthor = foundAuthor.replace(/o/gi,"[o,ó,ò,õ,ô,ö]");
    foundAuthor = foundAuthor.replace(/u/gi,"[u,ú,ù,û,ü]");
    foundAuthor = foundAuthor.replace(/c/gi,"[c,ç]");

    //varias palavras
    found = found.split(' ');
    found = found.join('|');
    foundAuthor = foundAuthor.split(' ');
    foundAuthor = foundAuthor.join('|');

    //cria regex
    regex = new RegExp('('+found+')', 'gi');
    regexAuthor = new RegExp('('+foundAuthor+')', 'gi');

    if(found){
        $('.title').each(function()
                         {
                             $(this).replaceWith('<div class="title">' +
                                 $(this).text().replace(regex, "<span class='highlight'>$1</span>")+'</div>');
                         });
    }
    if(foundAuthor){
        $('.author a').each(function()
                            {
                                if($(this).text().replace(regexAuthor) != -1){
                                    $(this).replaceWith( '<a href="' + $(this).attr('href')
                                                       + '" class="author" title="'+ $(this).attr('title') +'">'
                                                       + $(this).text().replace(regexAuthor, "<span class='highlight'>$1</span>")+'</a>');
                                }
                            });
    }
}
