window.ABS=window.ABS||{};jQuery(function(){ABS.Collection=ABS.Collection||{};ABS.Collection.TagList=Backbone.Collection.extend({model:ABS.Model.Tag,url:"/user/tags.json",addTagging:function(tagging){if(this.get(tagging.name)){this.get(tagging.name).get('taggings').push(tagging);}
else{this.add({id:tagging.name,taggings:[tagging]});}
return this.get(tagging.name);},getByChapter:function(osisId){var chap=ABS.common.osis.getComponents(osisId);var tags={};this.each(function(tag){var refs=tag.getReferences();_.each(refs,function(ref){var start=ABS.common.osis.getComponents(ref.start);if(start.book==chap.book&&start.chapter==chap.chapter){if(!(ref.start in tags)){tags[ref.start]=[];}
if(!_.include(tags[ref.start],tag)){tags[ref.start].push(tag);}}});});return tags;}});ABS.Collection.NoteList=Backbone.Collection.extend({model:ABS.Model.Note,url:"/user/notes.json",getByChapter:function(osisId){var chap=ABS.common.osis.getComponents(osisId);var notes={};this.each(function(note){note.get('references').each(function(ref){var start=ABS.common.osis.getComponents(ref.get('start'));if(start.book==chap.book&&start.chapter==chap.chapter){if(!(ref.start in notes)){notes[ref.get('start')]=[];}
notes[ref.get('start')].push(note);}});});return notes;}});ABS.Collection.TaggingList=Backbone.Collection.extend({model:ABS.Model.Tagging,url:"/user/taggings.json",initialize:function(){}});ABS.Collection.ReferenceList=Backbone.Collection.extend({model:ABS.Model.Reference});ABS.Collection.VersionList=Backbone.Collection.extend({model:ABS.Model.Version,url:"/versions.json",initialize:function(){this.sortVersions();this.bind('refresh',this.sortVersions);},sortVersions:function(){var getLang=function(ver){return ver.get('lang');},getLangAndAbbr=function(ver){return ver.get('lang')+ver.get('display_abbreviation');};this.sortedByLang=_.groupBy(this.sortBy(getLangAndAbbr),getLang);}});ABS.Collection.UserVersionList=Backbone.Collection.extend({model:ABS.Model.Version,url:"/user/versions.json",initialize:function(){},save:function(successFunction){var myVersionIDs=this.pluck("id");$.post(this.url,{'my_versions':myVersionIDs},successFunction,"json");}});ABS.Collection.SectionList=Backbone.Collection.extend({model:ABS.Model.Section});ABS.Collection.BookList=Backbone.Collection.extend({model:ABS.Model.Book,initialize:function(){this.bind('refresh',function(){this.bySection=_.groupBy(this.models,function(book){return book.get('testament');});});}});ABS.Collection.SearchResultsList=Backbone.Collection.extend({model:ABS.Model.SearchResults,initialize:function(){}});ABS.Collection.ChapterList=Backbone.Collection.extend({model:ABS.Model.Chapter,initialize:function(){this.trackPrimaryModel();},trackPrimaryModel:function(){this.bind('refresh',function(){this.primaryModel=this.at(0);});this.bind('add',function(){if(this.length===1){this.primaryModel=this.at(0);}});},comparator:function(chapter){return chapter.get('order');},getReferencesFromSpans:function(spans){var that=this;var refs=[];this.each(function(chapter){var chapterComps=ABS.common.osis.getComponents(chapter.id);_.each(spans,function(span){var startComps=ABS.common.osis.getComponents(span[0]);var endComps=ABS.common.osis.getComponents(span[1]);if(startComps.book===chapterComps.book&&startComps.chapter===chapterComps.chapter){var ref=chapter.get('display')+':'+startComps.verse;if(startComps.verse!==endComps.verse){ref+='-'+endComps.verse;}
refs.push(ref);}});});return refs;},getShortReferencesFromSpans:function(spans){var that=this;var refs=[];this.each(function(chapter){var chapterComps=ABS.common.osis.getComponents(chapter.id);_.each(spans,function(span){var startComps=ABS.common.osis.getComponents(span[0]);var endComps=ABS.common.osis.getComponents(span[1]);if(startComps.book===chapterComps.book&&startComps.chapter===chapterComps.chapter){var ref=startComps.book+' '+startComps.chapter+':'+startComps.verse;if(startComps.verse!==endComps.verse){ref+='-'+endComps.verse;}
refs.push(ref);}});});return refs;},addConsecutive:function(chapters){var that=this;chapters=_.filter(chapters,function(chap){return chap.id;});if(this.length===0){that.add(chapters);}
else{do{var matchFound=false;_.each(chapters,function(chapter){if(that.first().get('prevChapterId')===chapter.id||that.last().get('nextChapterId')===chapter.id){that.add(chapter);matchFound=true;}});}while(matchFound);}}});ABS.Collection.VerseList=Backbone.Collection.extend({model:ABS.Model.Verse});ABS.Collection.RelevancyVoteList=Backbone.Collection.extend({model:ABS.Model.RelevancyVote});ABS.Collection.DeveloperTools=Backbone.Collection.extend({model:ABS.Model.DeveloperTool,initialize:function(){this.add(ABS.Collection.DeveloperTools.types);}},{types:[{id:'widget',name:'Widget',path:'/share/widget'},{id:'highlighter',name:'Highlighter (Beta)',path:'/pages/highlighter'},{id:'api',name:'API',path:'/pages/api'}]});});
