/*
	Copyright James Matteson 2011. All rights reserved.
*/
function jsDB(){
   var myStoryQueue = new function(){
      var oThis = this;
      this.items = [];
      this.get = function(){
         var stories = [];
         var i = 0;
         var storyIDs = '';
         
         while (i < oThis.items.length){
            if (i >= dataLayer.getMaxStoryIDs()){
               i = 0;
               stories.push(storyIDs);
               storyIDs = '';
            }
            
            if (storyIDs.length > 0){
               storyIDs += ',';
            }
            
            storyIDs += oThis.items[i].storyID;
            i++;  
         }
         
         if (storyIDs.length > 0){
            stories.push(storyIDs);
         }
         
         return stories;
      }
      this.add = function(storyID){
         if (!oThis.has(storyID)){
            oThis.items.push({
               'storyID': storyID,
               'dateAdded': dataLayer.getSQLiteDate()
            });
            myRead.add(storyID);
         }
      }
      this.remove = function(storyID){
         var i = oThis.indexOf(storyID);
         
         if (i >= 0){
            oThis.items.splice(i, 1);
         }
      }
      this.has = function(storyID){
         return (oThis.indexOf(storyID) >= 0);
      }
      this.indexOf = function(storyID){
         for(var i = 0, len = oThis.items.length; i < len; i++){
            if (oThis.items[i].storyID === storyID){
               return i;
            }
         }
         return -1;
      }
      this.empty = function(){
         oThis.items = [];
      }
   }
   var myRead = new function(){
      var oThis = this;
      this.items = [];
      this.add = function(storyID){
         if (!oThis.has(storyID)){
            oThis.items.push({
               'storyID': storyID,
               'dateAdded': dataLayer.getSQLiteDate()
            });
         }
      }
      this.has = function(storyID){
         for(var i = 0, len = oThis.items.length; i < len; i++){
            if (oThis.items[i].storyID === storyID){
               return true;
            }
         }
         return false;
      }
      this.empty = function(){
         oThis.items = [];
      }
   }
   var myDismissed = new function(){
      var oThis = this;
      this.items = [];
      this.add = function(storyID){
         if (!oThis.has(storyID)){
            oThis.items.push({
               'storyID': storyID,
               'dateAdded': dataLayer.getSQLiteDate()
            });
         }
      }
      this.remove = function(storyID){
         var i = oThis.indexOf(storyID);
         
         if (i >= 0){
            oThis.items.splice(i, 1);
         }
      }
      this.has = function(storyID){
         return (oThis.indexOf(storyID) >= 0);
      }
      this.indexOf = function(storyID){
         for(var i = 0, len = oThis.items.length; i < len; i++){
            if (oThis.items[i].storyID === storyID){
               return i;
            }
         }
         return -1;
      }
      this.empty = function(){
         oThis.items = [];
      }
   }
   var myDomainBlacklist = new function(){
      var oThis = this;
      this.items = [];
      this.get = function(){
         var domains = [];
         
         oThis.items.each(function(item){
            domains.push(dataLayer.createDomainBlacklist(item.storyID, item.dateAdded));
         });
                  
         return domains;
      }
      this.add = function(domain){
         var id = -1;
      
         domain = domain.strip().toLowerCase();
         
         if (domain.length > 0 && !oThis.has(domain)){
            oThis.items.push({
               'domain': domain,
               'dateAdded': dataLayer.getSQLiteDate()
            });
            id = oThis.items.length - 1;
         }
         
         return dataLayer.createDomainBlacklist(id, domain);
      }
      this.remove = function(id){
         if (id >= 0){
            oThis.items.splice(id, 1);
         }
      }
      this.has = function(domain){
         for(var i = 0, len = oThis.items.length; i < len; i++){
            if (oThis.items[i].domain === domain){
               return true;
            }
         }
         return false;
      }
      this.empty = function(){
         oThis.items = [];
      }
   }
   
   // private
   function getStoriesByOffset(storyHolder, lastRowID){
      var storyIDs = '';
      var i;
      var count = 0;
      var max = dataLayer.getMaxStoryIDs();
      
      if (lastRowID === null || lastRowID < 0){
         lastRowID = storyHolder.items.length;
      }
      
      i = lastRowID - 1;
      lastRowID = -1;
               
      while (i >= 0 && count < max){
         if (storyIDs.length > 0){
            storyIDs += ',';
         }
         
         storyIDs += storyHolder.items[i].storyID;
         lastRowID = i;
         i--;
         count++;
      }
      
      return { 'storyIDs': storyIDs, 'lastRowID': lastRowID };
   }

   /* overridden */

   // db functions
   this.init = function(){}
   this.emptyDB = function(){
      myStoryQueue.empty();
      myRead.empty();
      myDismissed.empty();
      myDomainBlacklist.empty();
   }
   
   // can do
   this.canDisplay = function(storyID, domain){
      if (myRead.has(storyID) || myDismissed.has(storyID) || myDomainBlacklist.has(domain)){
         return false;
      }else{
         return true;
      }
   }
  
   // story queue
   this.getQueue = function(){
      return myStoryQueue.get();
   }
   this.enqueue = function(storyID){
      myStoryQueue.add(storyID);
   }
   this.dequeue = function(storyID){
      myStoryQueue.remove();
   }
   this.getRead = function(lastRowID){
      return getStoriesByOffset(myRead, lastRowID);
   }
   
   // dismissed
   this.dismiss = function(storyID){
      myDismissed.add(storyID);
   }
   this.dismissToRead = function(storyID){
      myDismissed.remove(storyID);
      myStoryQueue.add(storyID);
   }
   this.getDismissed = function(lastRowID){
      return getStoriesByOffset(myDismissed, lastRowID);
   }
   
   // domain blacklist
   this.getDomainBlacklist = function(){
      return myDomainBlacklist.get();
   }
   this.addDomainBlacklist = function(domain){
      return myDomainBlacklist.add(domain);
   }
   this.removeDomainBlacklist = function(id){
      myDomainBlacklist.remove(id);
   }
   this.isDomainBlacklisted = function(domain){
      return myDomainBlacklist.has(domain);
   }
}
