$(function() { var templates = { indexheader : $(".index-header")[0], entry : $(".entry")[0] }; var indexHTML = '
'; var initSimpleTable = function(){ SimpleTable.init({ key: '0AvMy3k6Dtn6bdFZZUTNFVVJPMDRqZElGSTk5VVBzWHc', cached: true, callback: createTable }); }; var createTable = function(data){ data.sort(function(a, b) { var textA = a.lastname.toUpperCase(); var textB = b.lastname.toUpperCase(); return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; }); var prevLetter = ""; var sections = []; var sectionHTML = []; var indexLinksHTML = []; $.each(data, function(index,value){ var letter = value.lastname.substring(0,1).toUpperCase(); if (letter!=prevLetter){ if(sectionHTML.length){ sections.push(formatSection(sectionHTML)); } var indexLine = '
'+ letter +'
'; sectionHTML = [indexLine]; prevLetter = letter; indexLinksHTML.push(''); } if(value.firstname) var name = value.lastname + ", " + value.firstname; else var name = value.lastname; if(value.url) name = '' + name + ''; var chapters = []; for(var prop in value){ var val = value[prop].toLowerCase(); if(val.indexOf("chapter") !== -1) chapters.push(val.replace("chapter ","")*1); if(val.indexOf("prologue") !== -1) chapters.push(-1); if(val.indexOf("epilogue") !== -1) chapters.push(999); } chapters.sort(function(a,b){return a-b}); var firstChapter = false; for(var i = 0; i < chapters.length; i++){ if(chapters[i]!=-1 && chapters[i]!=999 && !firstChapter){ chapters[i] = "ch. "+chapters[i]; firstChapter = true; } } var chapterHTML = chapters.join(", ").replace("-1","prologue").replace("999","epilogue"); var entry = '
' + name + ' ' + chapterHTML + '
' sectionHTML.push(entry); }); sections.push(formatSection(sectionHTML)); $("#index-container").append('
'+sections.join('
')+"
"); $("#index-links").html(indexLinksHTML.join("")); registerNavEvents(); }; function formatSection(section){ var count = section.length; var countPerColumn = Math.ceil(count/2); var column1 = ""; var column2 = ""; for(var i = 1; i < count; i++){ if(i <= countPerColumn) column1 += section[i]; else column2 += section[i]; } var sectionHTML = section[0] + '
' + column1 + '
' + column2 + '
'; //var sectionHTML = section.join(""); return sectionHTML; } function registerNavEvents(){ $(".index-link").click(function(){ var letter = $(this).attr("data-letter"); $(".index-section").hide(); $(".index-header[data-letter='"+letter+"']").parent().show(); }); } if($(".index-section").length) registerNavEvents(); else initSimpleTable(); });