jQuery(document).ready(function() {

  // Check for Local Storage
  var localStorageExists = false;
  if (typeof(Storage) !== "undefined") {
    localStorageExists = true;
  }

  // Insert list type switcher on title index
  jQuery( 'div.t-alphaindex h1' ).append( '<div class="book-list-type text-xs-right">List Style &nbsp;<a href="#" id="listTypeSelectLarge" data-toggle="tooltip" title="Show Thumbnails"><span class="fa fa-2x fa-th-large" style="line-height:1; padding:0;"></span></a> &nbsp; &nbsp;<a href="#" id="listTypeSelectList" data-toggle="tooltip" title="Show List"><span class="fa fa-2x fa-th-list" style="line-height:1; padding:0;"></span></a></div>' );

  // if the switcher exists, set it based on localStorage value
  if (jQuery('div.book-list-type').length && localStorageExists) {
    var sortPref = localStorage.getItem("t-sortpref");
    switch(sortPref) {
      case 'list': {
        jQuery('#booklistThumb').hide();
        jQuery('#booklistTable').show();
        break;
      }
      default: {
        jQuery('#booklistTable').hide();
        jQuery('#booklistThumb').show();
        break;
      }
    }
  }

  // Make the login link work on the cart page.
  console.log(jQuery('#purchase-link a:contains("log in")'));
  jQuery('#purchase-link a:contains("log in")').removeAttr('onclick');
  jQuery('#purchase-link a:contains("log in")').attr('data-toggle', 'modal');
  jQuery('#purchase-link a:contains("log in")').attr('data-target', '#myModal');

  // Handle Book List Toggle
  jQuery('div.t-alphaindex h1').on('click', '#listTypeSelectLarge', function(e) {
    e.preventDefault();
    jQuery('.t-alphaindex.book-list-table').hide();
    jQuery('.t-alphaindex.book-list-thumb').show();
    if (localStorageExists) { localStorage.setItem('t-sortpref', 'thumb'); }
  });
  jQuery('div.t-alphaindex h1').on('click', '#listTypeSelectList', function(e) {
    e.preventDefault();
    jQuery('.t-alphaindex.book-list-thumb').hide();
    jQuery('.t-alphaindex.book-list-table').show();
    if (localStorageExists) { localStorage.setItem('t-sortpref', 'list'); }
  });
  jQuery('#quickSearchForm-1-searchText').attr('placeholder', 'search');


});