/*!
 *  bib2html utils
 *  Author: Andrew Watts
 *  Copyright 2008-2009 - University of Rochester : Brain and Cognitive Sciences
 *  Licensed under the LGPLv2.1: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 *  Assumes jquery.js is already loaded
*/

$(document).ready(
    function() {
        $('.keywords').hide();
        $('.bib').hide();
        $('.abstract').hide();
        resetsearch();
        $(':text[name="val"]').keypress(
            function(e){
                if (e.which == 13) { // enter key
                    $(':button[name="search"]').click();
                    e.preventDefault();
                }
            }
        );
    }
);

function toggleBib(bibname) {
     $('li#' + bibname + '> ul > li.bib').slideToggle('fast');
}

function toggleAbstract(abstname) {
    $('li#' + abstname + '> ul > li.abstract').slideToggle('fast');
}

function toggleKeywords(keyname) {
    $('li#' + keyname + '> ul > li.keywords').slideToggle('fast');
}

function wordify() {
    // remove a bunch of stuff to make it easy to copy the refs into word
    $('#content a').hide();
    $('.keywords').hide();
    $('.bib').hide();
    $('.abstract').hide();
    $('li > br').hide();
    $('.entry').css("border-bottom", "0px");
    $('#content > h2').hide();
}

function unwordify() {
    $('#content a').show();
    $('li > br').show();
    $('.entry').css("border-bottom", "1px dotted #3A508E");
    $('#content > h2').show();
}

// this creates a ":matches" selector by taking the ":contains" filter code from
// jQuery 1.2.6 (line 1403), but lowercasing the strings to do a case
// insensitive match
// TODO: figure out how to rewrite in the jquery 1.4.x manner
jQuery.extend(jQuery.expr[":"], {
    matches: function(a,i,m) {return (a.textContent||a.innerText||jQuery(a).text()||"").toLowerCase().indexOf(m[3].toLowerCase())>=0;} 
});

function dosearch() {
    var type, val, area, hl, hide, word, items;

    type = $(':input[name="type"]').val();
    val = $(':text[name="val"]').val();
    area = $(':input[name="area"]').val();
    pub = $(':input[name="pub"]').val();
    hl = $(':checkbox[value="highlight"]').attr('checked');
    hide = $(':checkbox[value="only show matches"]').attr('checked');
    word = $(':checkbox[value="format for word"]').attr('checked');

    clearsearch();
    
    items = $('li.entry');

    if(type && val) {
        items = $(items).filter(function() {return $(this).children('.' + type).is(':matches("' + val+ '")');});
    }

    if (area !='any') {
        items = $(items).filter(function() {return $(this).hasClass(area);});
    }

    if (pub !='any' && pub != 'anypaper' && pub != 'anyconf') {
        items = $(items).filter(function() {return $(this).hasClass(pub);});
    }

    if (pub === 'anypaper') {
        items = $(items).filter(function() {return $(this).hasClass('paper');});
    }

    if (pub === 'anyconf') {
        items = $(items).filter(function() {return $(this).hasClass('conf');});
    }

    items.addClass('selected');

    if (hl) {
        items.css('background-color', '#90EE90');
    }
    
    if (hide) {
        $('li.entry:not(.selected)').hide();
    }

    if (word) {
        wordify();
    }
}

function clearsearch() {
    $('.selected').css('background-color','').removeClass('selected');
    $('li.entry').show();
    unwordify();
}

function resetsearch() {
    clearsearch();
    $(':checkbox').attr('checked', false);
    $(':checkbox[value="only show matches"]').attr('checked', true);
    $(':text[name="val"]').val('');
    $(':input[name="type"]').val('year');
    $(':input[name="area"]').val('any');
    $(':input[name="pub"]').val('any');
}

