var JSONROOT='/_portaljs/'
function fetchIntoMain(dataURL,rowFormatter,queryPars){destEl=$("#mainbox");destEl.empty();ul=$('
');destEl.append(ul);$.getJSON(dataURL,queryPars,function(data,textStatus,xhr){data.map(function(row){ul.append(rowFormatter(row));})})}
function updateForInput(){words=$("#textsearch").val();if(!words){$("#mainbox").empty();$("#mainbox").append($(""));}else{fetchIntoMain(JSONROOT+'byFulltext',formatResourceHeader,{'q':words});}}
function textsearchKey(ev){if(ev.which==13){updateForInput($('#mainbox'));}}
function makeMainFetcher(dataURL,recordFormatter){return function(){fetchIntoMain(dataURL,recordFormatter);}}
function makeFormatter(templateName){return function(data){return renderTemplate(templateName,data);}}
fetchTitles=makeMainFetcher(JSONROOT+'titles',formatResourceHeader);fetchSubjects=makeMainFetcher(JSONROOT+'subjects',makeFormatter("tmpl_subjectHeader"));fetchAuthors=makeMainFetcher(JSONROOT+'authors',makeFormatter("tmpl_authorHeader"));fetchFulltext=updateForInput;function formatResourceHeader(resMeta){return $(renderTemplate(resMeta["browseable"]?"tmpl_resHead":"tmpl_resHeadNobrowse",resMeta));}
function formatResourceRecord(destObject,resDetails){return renderTemplate("tmpl_resDetails",resDetails);}
function makeMatchAdder(queryURL,keyName){return function(handle){destElement=$("");handle.parent().append(destElement);pars={};pars[keyName]=handle.attr("value");$.getJSON(queryURL,pars,function(data,textStatus,xhr){data.map(function(row){destElement.append(formatResourceHeader(row))});});}}
function addServiceInfo(handle){var parts=handle.attr("value").split(',');var destElement=handle.parent();$.getJSON(JSONROOT+'serviceInfo',{'resId':parts[1],'sourceRD':parts[0]},function(data,textStatus,xhr){destElement.append(formatResourceRecord(destElement,data[0]));});}
function _makeToggler(dataAdder){return function(handle){var ob=$(handle);var childMeta=ob.parent().find(".fold");if(childMeta.length==0){dataAdder(ob);var isOpen=false;}else{var isOpen=childMeta.css("display")!='none';}
if(isOpen){ob.find(".handlearrow").html("▶");childMeta.css("display","none");}else{ob.find(".handlearrow").html("▼");childMeta.css("display","block");}}}
toggleDetails=_makeToggler(addServiceInfo);toggleSubjectResources=_makeToggler(makeMatchAdder(JSONROOT+'bySubject','subject'));toggleAuthorResources=_makeToggler(makeMatchAdder(JSONROOT+'byAuthor','author'));$(document).ready(function(){$("#tab_placeholder").replaceWith($($.trim(document.getElementById("tabbar_store").innerHTML)));$("#tabset_tabs li").bind("click",makeTabCallback({'by-subject':fetchSubjects,'by-author':fetchAuthors,'by-title':fetchTitles,'by-text':fetchFulltext}));$("#textsearch").bind("keypress",textsearchKey);});