
////////////////////////////////////////////////////
//
// TOGGLE_SEARCH_OPTIONS FUNCTION
//
// Show 'Advanced Search' or 'ISBN' search options
//   * If div is already visible and user clicks on tab again, it disappears.
//   * Also changes link style of clicked tab.
//
////////////////////////////////////////////////////

function toggle_search_options(which, link, focus_on) {
    if (jQuery("#" + link).hasClass('selected')) {
        jQuery("#" + link).removeClass('selected');
	   jQuery("#" + which).addClass('invisible');
    } else {
	   jQuery(".search").addClass('invisible');
	   jQuery(".selected").removeClass('selected');
	   jQuery("#" + link).addClass('selected');
	   jQuery("#" + which).removeClass('invisible');
    }
    jQuery("#" + focus_on).focus();
}

// Toggle
function toggle(el, class_name) {
   jQuery('#' + el).toggleClass(class_name);
}

////////////////////////////////////////////////////
//
// TOGGLE TEXT
//
////////////////////////////////////////////////////

function toggle_text(el, def, alt) {
   if (jQuery('#' + el).html() == def) {
      jQuery('#' + el).empty().html(alt);
   } else {
      jQuery('#' + el).empty().html(def);
   }
}

////////////////////////////////////////////////////
//
// EMPTY_FORM FUNCTION
//
// Hit "Clear" button -> put all fields back to default state.
// NOTE: Not the state they were in when you arrived at the page - that would be "Reset," which is...
//   * a native HTML behavior
//   * what the button does if user doesn't have javaScript.
//
////////////////////////////////////////////////////

function empty_form(which_form, focus_on) {
   if (which_form == 'adv_search_form') {

      // Blank Out Text Fields
       var Blanks = ['kw_adv','title_adv','author_adv','publisher_adv','section_adv'] ;
      for( var i in Blanks ) {
	  jQuery('#' + Blanks[i]).val('');
      }

      // Reset Drop-Downs
      var Selects = ['class_adv','binding_adv','sort_by_adv','location_adv','days_old_adv'];
      for( var i in Selects ) {
	  jQuery('#' + Selects[i]).find('option:first').attr('selected', 'selected').parent('select');
      }

      // Uncheck Exact Title
      jQuery('#exact_title_adv').attr('checked', false);

      // Set Items Per Page To 100
      jQuery("#per_page_adv option[value='100']").attr('selected', 'selected');
   } else {
	  jQuery('#isbn').val('');
   }
   jQuery('.filled-in').removeClass('filled-in');
   jQuery('#kw_adv').focus();
}

////////////////////////////////////////////////////
//
// UNFLAG FUNCTION
// 
// PROBLEM: User mistakenly leaves a restricting condition (from their old search) on their new search.
//   * SERVER-SIDE: Add style 'filled-in' to label of a form element that has a value that is not the default value.
//   * CLIENT-SIDE: onChange, remove that style.
//
////////////////////////////////////////////////////

function unflag(which) {
    var id = jQuery(which).attr("id");
    if (jQuery("label[for='" + id + "']").hasClass('filled-in')) {
	jQuery("label[for='" + id + "']").removeClass('filled-in');
    }
}

////////////////////////////////////////////////////
//
// VIEW OTHERS FUNCTION
// 
// Displays "Also Available" table under search result...
//
////////////////////////////////////////////////////


function view_others(isbns,isbn,condition,link_type) {
   if (condition == 'electronic') { th = "eBook Format" } else { th = "Condition" };
   other_copies_div = "other-copies-" + isbn;
   jQuery('#' + other_copies_div).html("<img src='/images/loading.gif' /> Loading...");
   other_copies_url = "/book?type=json_truncated&isbn=" + isbns.replace(/,/g,'&isbn=') + "&nofuzzy=1&order=price asc&jsoncallback=?";
   jQuery.ajax({
      url: other_copies_url,
      success: function(data) {
         r = eval("(" + data + ")");
         _other_copies = [];
         _other_copies_table = [];
         for(inkey in r) {
            if (r[inkey].price) {
               price = "$" + r[inkey].price;
            } else {
               price = "";
            }
            _other_copies_row = [];
            _other_copies_row.push("<tr>");
            _other_copies_row.push("<td style='width:200px; border:1px solid #EBE7DC'><!--" + r[inkey].price/1000 + " sort value-->" + r[inkey].class_external + " " + r[inkey].condition + "</td>");
            _other_copies_row.push("<td style='width:200px; border:1px solid #EBE7DC'>" + r[inkey].binding + "</td>");
            _other_copies_row.push("<td style='text-align:right; border:1px solid #EBE7DC; width:1px;'>" + price + "</td>");
            _other_copies_row.push("<td style='width:1px; border:1px solid #EBE7DC'><small><a href='/biblio/" + inkey + "' title='" + r[inkey].title + "'>more&nbsp;info</a></small></td>");
            _other_copies_row.push("<td style='width:1px; border:1px solid #EBE7DC'>" + add_to_cart_button(inkey, r[inkey].title) + "</td>");
            _other_copies_row.push("</tr>");
            _other_copies_table.push(_other_copies_row.join(''));
         }
         if (r) {
            top_row = "<tr style='background:#EBE7DC; border:1px solid ##B09B8A;'><th>" + th + "<!-- [<a title='See our Glossary for term descriptions.' href='javascript:open_window(\"/post?type=popup&name=glossary&#condition\");'>?</a>]--></th><th>Binding<!-- [<a title='See our Glossary for term descriptions.' href='javascript:open_window(\"/post?type=popup&name=glossary&#binding\");'>?</a>]--></th><th>Price</th><th>&nbsp;</th><th>&nbsp;</th></tr>";
            _other_copies_table = "<table cellpadding='2px' class='sortable'>" + top_row + _other_copies_table.sort().join(' ') + "</table>";
         }
         // closer_link = "<a href='javascript:close_this(\"" + other_copies_div + "\");'>X</a>";
         jQuery('#' + other_copies_div).html(_other_copies_table).css('display','none').fadeIn('slow');
      }
   });
   _v = link_type.split(':');
   TrackEvent("Search-Also-Available",_v[0],_v[1]);
}

function also_available(book_title, inkey, type, event_tracking_var, inkey_a, class_a, price_a, binding_a,status) {
   jQuery("#other-copies-" + inkey).html("<img src='/images/loading.gif' /> Loading...");
   var rows = [];
   for (i=0; i < inkey_a.split(':').length; i++) {
      rows.push("<tr>");
      rows.push("<td style='width:200px; border:1px solid #EBE7DC'>" + class_a.split(':')[i] + "</td>");
      rows.push("<td style='width:200px; border:1px solid #EBE7DC'>" + binding_a.split(':')[i] + "</td>");
      rows.push("<td style='width:200px; border:1px solid #EBE7DC;text-align:right;width:1px;'>$" + price_a.split(':')[i] + "</td>");
      rows.push("<td style='width:1px; border:1px solid #EBE7DC'><a href='/biblio/" + inkey_a.split(':')[i] + "'><small>more&nbsp;info</small></a></td>");
      rows.push("<td style='width:1px; border:1px solid #EBE7DC'>" + add_to_cart_button(inkey_a.split(':')[i], book_title) + "</td>");
      rows.push("</tr>");
   }
   var condition_format = (type == "Electronic") ? "eBook&nbsp;Format" : "Condition";
   var table_head = "<table><tr style='background:#EBE7DC; border:1px solid #B09B8A;'><th>" + condition_format + "</th><th>Binding</th><th>Price</th><th></th><th></th></tr>";
   var table_foot = "</table>";
   jQuery("#other-copies-" + inkey).fadeIn().html(table_head + rows.join('') + table_foot);
   _v = event_tracking_var.split(':');
   TrackEvent("Search-Also-Available",_v[0],_v[1]);
}


function add_to_cart_button(inkey, title) {
   return "<a href='/cgi-bin/Shopping_cart?add=" + inkey + "' title='Add &quot;" + title + "&quot; to your Shopping Cart'><img src='/images/buttons/addtocart82.gif'  style='border:0;' class='add-to-cart-button' /></a>";
}

