// This is called by the queue if the total digg count is greater than the number registered in the db
function updateDiggs()
{
  var latestDigg = 0, params;
  currentUserId = checkAddUser(currentUser);
  latestDigg = getLatestDigg();
  params = {
    endPoint : "/users/"+currentUser+"/diggs",
    sort : "date-asc",
    count : 100
  };
  if (latestDigg > 0)
  {
    params.min_date = latestDigg+1;
  }
  diggRequestComplete = function (obj)
  {
    expectedTotal[currentUserId] = obj.total > expectedTotal[currentUserId] || expectedTotal[currentUserId] == undefined ? obj.total : expectedTotal[currentUserId];
    var i;
    for (i in obj.diggs)
    {
      // sends them all to the WorkerPool for insertion as 'new' items
      // by sending to the WorkerPool thread, the UI is less prone to lock up
      // var uds = checkAddUser(obj.diggs[i].user) + "," + obj.diggs[i].date + "," + obj.diggs[i].story;
      // hmm.. they should theoretically all be dugg by the same user, yes? why was i checking for every item?
      var uds = currentUserId + "," + obj.diggs[i].date + "," + obj.diggs[i].story;
      workerPool.sendMessage("new:"+String(obj.diggs[i].story), storyChildId);
      workerPool.sendMessage(uds, diggChildId);
      workerTasks += 2;
      // one goes to the story thread (create empty, uncached story item)
      // the other registers a digg for that story id for the current user
    }
    if (obj.total > obj.diggs.length)
    {
      callQueue.push([updateDiggs,[]]);
    }
    callQueue.push([updateStories,[]]);
  }
  diggRequest(params);
}

// this is called once at the beginning to pull the 'total' variable
function getTotalDiggs()
{
  var params;
  currentUserId = checkAddUser(currentUser);
  params = {
    endPoint : "/users/"+currentUser+"/diggs",
    count : 0
  };
  diggRequestComplete = function (obj)
  {
    myTotalDiggs = obj.total;
  }
  diggRequest(params);
}

function diggChildInit()
{
  // this "Child" is a WorkerPool object and runs in a separate thread from the UI
  // need to make the db connection from this thread, since global objects are not shared
  db = google.gears.factory.create('beta.database', '1.0');
  db.open('diggoraclev1');
  gearsWorkerPool.onmessage = diggChildHandler;
}

function diggChildHandler (msg, sender)
{
  gearsWorkerPool.sendMessage("start|"+msg, sender);
  var info = msg.split(","), q, exists;
  q = "SELECT count(*) as mycount FROM diggs WHERE userid = " + info[0] + " AND storyid = " + info[2];
  exists = sqlToObj(q);
  if (exists[0].mycount == 0)
  {
    q = "INSERT INTO diggs (userid, diggdate, storyid) VALUES (" + info[0] + ", " + info[1] + ", " + info[2] + ")";
    tmp = sqlToObj(q);
  }
  gearsWorkerPool.sendMessage("end|"+msg, sender);
  gearsWorkerPool.sendMessage("done", sender);
}

// this helps with queuing up past diggs for the one-time sync
// it is also used for checking for new diggs going forward
// two birds with one stone
function getLatestDigg()
{
  var latestDigg = 0, arrDiggs;
  arrDiggs = sqlToObj("SELECT MAX(diggdate) as latestDigg FROM diggs WHERE userid = "+currentUserId);
  if (arrDiggs[0].latestDigg != undefined)
  {
    latestDigg = arrDiggs[0].latestDigg;
  }
  return latestDigg;
}