$(document).ready(function () {

  // hide these elements from the get-go
  $("#changelog").hide();

  // bind our event listeners
  $("#psp-hacks-archives").hover(function () {
    $("#archives-list").toggle("slow");
  });
  $("#changelog-toggle").click(function () {
    $("#changelog").slideToggle();
  }); 

  $("#filter-legend").click(function () {
    $("#news-filter").slideToggle();
  });

  $("#news-filter input:checkbox[name=all]").click(function () {
    if (this.checked) {
      $("#news-filter input:checkbox[name!=score]")
        .attr("checked", true)
        .attr("disabled", "disabled");
      $("#news-filter-button").attr("disabled", "disabled");
      $.post("/index.php", { ac: 1, ac_func: 'ac_categories', ac_options: 0 }, function () {
        window.location.reload();
      });
    }
    else {
      $(".news-filter-column input:checkbox,#news-filter-button").removeAttr("disabled");
    }
  });

  $("#news-filter-button").click(function () {
    var ac_cats = [];
    $(this).attr("disabled", "disabled");
    $(".news-filter-column input:checkbox").attr("disabled", "disabled");
    $(".news-filter-column input:checked").each(function () {
      ac_cats.push(this.value);
    });
    $.post("/index.php", { ac: 1, ac_func: "ac_categories", 'ac_options[]': ac_cats }, function () {
      window.location.reload();
    });
  });

  $("#news-filter input:checkbox[name=score]").click(function () {
    var score = this.checked ? 'score' : 0;
    $(this).attr("disabled", "disabled");
    $.post("/index.php", { ac: 1, ac_func: "ac_sort", ac_options: score }, function () {
      window.location.reload();
    });
  });
 
  $(".hide-theme,.hide-theme-x").click(function() {
    $("#filter-legend").trigger("click");
    $(".news-filter-column input:checkbox,#news-filter-button").removeAttr("disabled");
    $("#news-filter input:checkbox[name=all],.news-filter-column input:checkbox[value=58]").removeAttr("checked");
    $(".news-filter-column input:checkbox[value!=58]").attr("checked", true);
    $("#news-filter-button").trigger("click");
  });

  $(".news-filter-column input:checkbox").click(function () {
    ac_checked(true);
  });
        
  // do the real deal
  ac_init();
});

function ac_checked(trigger) {
  var count   = $(".news-filter-column input:checkbox").length;
  var checked = $(".news-filter-column input:checked").length;
  if (count > 0 && count == checked) {
    $("#news-filter input:checkbox[name=all]").attr("checked", true).removeAttr("disabled");
    $(".news-filter-column input:checkbox,#news-filter-button").attr("disabled", "disabled");
    if (trigger) {
      $("#news-filter input:checkbox[name=all]").trigger("click");
    }
    return true;
  }
  else {
    $("#news-filter input:checkbox[name=all]").removeAttr("checked");
    return false;
  }
}

/**
 * initialize content filter & post scores
 */
function ac_init() {
  $("#news-filter input:checkbox[name=score]").removeAttr("disabled");
  if ($.cookie("ac_sort") == "score") {
    $("#news-filter input:checkbox[name=score]")
      .attr("checked", true);
  }
  else {
    $("#news-filter input:checkbox[name=score]")
      .removeAttr("checked");
  }
  
  if ($.cookie("ac_cats")) {
    $("#news-filter-button,.news-filter-column input:checkbox").removeAttr("disabled");
    $.each($.cookie("ac_cats").split("+"), function (k, v) {
      $("#news-filter input:checkbox[value=" + v + "]").attr("checked", true);
    });
  }
  else {
    $("#news-filter input:checkbox[name=all],.news-filter-column input:checkbox")
      .attr("checked", true);
    $("#news-filter input:checkbox[name=all]")
      .removeAttr("disabled");
    $(".news-filter-column input:checkbox,#news-filter-button")
      .attr("disabled", "disabled");
  }

  if ($("#news-filter input:checkbox[name=all]").attr("checked")) {
    $("#news-filter-button").attr("disabled", "disabled");
  }

  if (ac_checked(false)) {
    $.post("/index.php", { ac: 1, ac_func: 'ac_categories', ac_options: 0 });
  }

  setTimeout('$("#news-filter").slideUp()', 2000);

  var posts = [];
  $(".post").each(function (i) {
    posts.push($(this).attr("id").match(/\d+$/));
    if (i == ($(".post").length - 1)) {
      $.get("/index.php", { ac: 1, ac_func: "get_scores", "posts[]": posts }, function (data) {
        $.each($.evalJSON(data), function (k, v) {
          $("#post-" + k + " .posttext").append("<img rel='" + k + "' src='/dh.php?num=" + v + "' class='dh-score' />");
        });
        $(".dh-score").click(function () {
          if ($.inArray($(this).attr("rel"), $.cookie("ac_rated_posts").split("+")) == -1) {
            $(this).fadeOut();
            $.post("/index.php", { ac: 1, ac_func: "update_score", post_id: $(this).attr("rel") }, function (score) {
              score = score.split("+");
              $(".dh-score[rel=" + score[1] + "]").attr("src", "/dh.php?num=" + score[0]);
            });
            $(this).fadeIn();
          }
        });
      });
    }
  });
}
