var refreshID = false;
var ie6 = false;

if(!Array.indexOf) {
  Array.prototype.indexOf = function(obj) {
    for(var i=0; i<this.length; i++)
      if(this[i]==obj)
         return i;
    return -1;
  }
}

$(function() {
  // make the sidebar fixed in IE6 or below
  ie6 = $.browser.msie && parseInt($.browser.version) < 7;
  if(ie6)
    $(window).scroll(function() { windowResize(); });

  $("#bbfade").css({opacity: "0.75"}).click(function() { blackBoxClose(); });
  window.onresize = windowResize;

});

function addTask(above)
{
  blackBoxOpen("/task?op=add&above=" + above);
}

function editTask(id)
{
  blackBoxOpen("/task?op=mod&todo=" + id);
}

function blackBoxClose()
{
  $("#blackbox").css({display:"none"});
}

function blackBoxOpen(url)
{
  var fr = document.getElementById("bbframe").contentWindow.document.body;
  fr.innerHTML = "";
  fr.style.background = "white";
  blackBoxSize();
  $("#bbframe").attr({src: url});
  $("#blackbox").css({display: "block"});
}

function blackBoxSize()
{
  var bw = $(($.browser.msie && parseInt($.browser.version) < 7 ? window : document)).width();
  var bh = $(document).height();
  $("#bbfade").width(bw).height(bh);
  $("#blackbox").width(bw).height(bh);

  var ww = $(window).width() - Math.max(400, $("#bbpage").width());
  var wh = $(window).height() - Math.max(150, $("#bbpage").height());
  var wt = 0;
  if(ie6)
    wt = $(window).scrollTop();

  $("#bbpage").css({top: (wt ? wt : 0) + wh/2, left: ww/2});
}

function loadChart()
{
  if(!mini)
    $("#chart").html("<a href='/history'><img src='/chart?img=meter&rnd=" + Math.random() + "' width='132' height='70' /><img src='/chart?img=all&rnd=" + Math.random() + "' width='132' height='70' /></a>");
}

function loadTodos()
{
  var ot;

  $.get("/me-list", function(ret) {
    $("#todosbox").html(ret);
    $("#todos").sortable({
      axis: "y",
      handle: ".task",
      opacity: 0.5,
      scroll: true,
      start: function(e, ui) {
        ot = $("#todos").sortable("toArray");
      },
      update: function(e, ui) {
        var t = $("#todos").sortable("toArray");
        var tid = ui.item[0].id;
        $.post("/set/", { "order[]": t, id: tid, move: (t.indexOf(tid) - ot.indexOf(tid))}, function(ret) {
          if($("#" + t[0]).hasClass("head"))
          {
            loadTodos();
          }
        });
        if(!$("#" + t[0]).hasClass("head") && !$("#" + t[0]).hasClass("first"))
        {
          $("#todos .first").removeClass("first");
          $("#" + t[0]).addClass("first");
        }
        resetRefresh();
      }
    }).sortable("refresh").sortable("refreshPositions");
    blackBoxClose();
    loadChart();
  });
  resetRefresh();
}

function resetRefresh()
{
  if(refreshID)
    window.clearInterval(refreshID);
  refreshID = window.setTimeout(function() { loadTodos(); }, 5 * 60 * 1000);
}

function setQuirk(quirk, key, val)
{
  $(".quirk_" + quirk).removeClass("active");
  $("#" + quirk + "_" + key).addClass("active");
  $.get("/set/?quirk=" + quirk + "&value=" + val, function(ret) { loadChart(); });
}

function setStatus(id, st)
{
  $("#todo_" + id).hide("drop", { direction: (st=="Y" ? "left" : "right") }, 650);
  window.setTimeout(function() {
    $("#todo_" + id).remove();
    $.get("/set/?status=" + st + "&todo=" + id, function(ret) {
      loadTodos();
    });
  }, 700);
}

function windowResize()
{
  if($("#blackbox").css("display") == "block")
    blackBoxSize();

  if(ie6)
  {
    var top = $(window).scrollTop();
    $("#sidebar").css("top", (top ? top : 0));
  }
}