/*
	Copyright James Matteson 2011. All rights reserved.
*/
var myStories = new storyManager();

function storyManager(){
   var oThis = this;
   var stories = [];
   var storyIDs = [];
   var storyIndex = 0;
      
   /* private */
   function updateStoryCount(){
      document.fire('update:count', { 'count': stories.length });
   }
   function showStory(){
      document.fire('show:story', { 'story': oThis.getStory() });
   }
      
   /* public */
   this.prevStory = function(){
      if (storyIndex > 0)
         storyIndex--;
      else
         storyIndex = stories.length - 1;
      
      showStory();
   }
   this.nextStory = function(){
      if (storyIndex < stories.length - 1)
         storyIndex++;
      else
         storyIndex = 0;
      
      showStory();
   }
   this.removeStory = function(){
      dataLayer.dequeue(stories[storyIndex].id);
      storyIDs[stories[storyIndex].id] = null;
      stories.splice(storyIndex, 1);
      updateStoryCount();
      
      if (storyIndex >= stories.length){
         storyIndex = stories.length - 1;
      }
      
      if (storyIndex < 0){
         storyIndex = 0;
         document.fire('stories:empty');
      }else{
         showStory();
      }
   }
   this.addStory = function(story){
      if (storyIDs[story.id] != null) return; // Prevent dups
      stories.push(story);
      storyIDs[story.id] = 1;
      updateStoryCount();
   }
   this.getStory = function(){
      return stories[storyIndex];
   }
   this.getStoryIndex = function(){
      return storyIndex;
   }
   this.getStoryCount = function(){
      return stories.length;
   }
}
