$(document).ready(function(){
  //Quick book advanced search
  if($('#quick_advanced_search'))
  {
    var biblio_search_widget = $('#biblio_search_widget');  
  
    if($('#advanced_search_close_link'))    
      $('#advanced_search_close_link').click(function() {
          biblio_search_widget.hide();
      })
      
    if($('#quick_advanced_search'))
      $('#quick_advanced_search').click(function() {            
          if(biblio_search_widget && biblio_search_widget.children('div'))   
            biblio_search_widget.toggle();
        });
  }
  
  if($("#show_advanced"))
  {
    $("#show_advanced").click(function() {
      $("#advanced_search").show();
      $("#show_advanced").hide();
      });
      
    $("#hide_advanced").click(function() {
      $("#advanced_search").hide();
      $("#show_advanced").show();
      });
    $("#id_lexile").change(function() {
        var lex = parseInt($(this).val());
        if (lex) {
          $("#id_lexile_min").val(lex-100);
          $("#id_lexile_max").val(lex+50);
        }
    });
  }

  //Like a book
  $(".like_book").click(function() {
    var nocache = Math.random();
    if ($(this).hasClass("add")) {
      $(this).html('<img src="/m/images/unlike_btn.png" alt="Unlike this book" />');
      $(this).toggleClass('add');
      $.getJSON("/book/like/"+this.id+"/?nocache="+nocache);
    } else {
      $(this).html('<img src="/m/images/bookInfoBtn04.jpg" width="82" height="33" alt="">');
      $(this).toggleClass('add');
      $.getJSON("/book/unlike/"+this.id+"/?nocache="+nocache);
    }
  });
  
//Like a book
  $(".anonymous_bb").click(function() {
    var nocache = Math.random();
    if ($(this).hasClass("add")) {
      $(this).html('<a href="#" class="anonymous_bb">Remove from My Reading List</a>');
      $(this).toggleClass('add');
      $.getJSON("/reading_list/add/"+this.id+"/?nocache="+nocache);
    } else {
      $(this).html('<a href="#" class="anonymous_bb">Add to Reading List</a>');
      $(this).toggleClass('add');
      $.getJSON("/reading_list/remove/"+this.id+"/?nocache="+nocache);
    }
    return false;
  });
  
  //Populate Lexile range
  $('#range_from_profile').click(function() {
	  $('#id_lexile_min').val($('#profile_lexile_min').text());
	  $('#id_lexile_max').val($('#profile_lexile_max').text());
	  return false;
  });
});

