$(document).ready(function() {
  // setup pagination
  $("div.pagination a").live("click", function () {
    $.ajax({
      type: "GET",
      url: $(this).attr("href"),
      success: function (t) {
        $("#results").html(t);
      }
    });
    return false;
  });
  
  // setup categories
  $("div#sidebar a.name").live("click", function () {
    $.ajax({
      type: "GET",
      url: $(this).attr("href"),
      success: function (t) {
        $("#results").html(t);
      }
    });
    return false;
  });
  
  //setup inline categories
  $("a.inlinecategory").live("click", function () {
    $.ajax({
      type: "GET",
      url: $(this).attr("href"),
      success: function (t) {
        $("#results").html(t);
      }
    });
    //activate the category in the menu
    $("div#categories > div").each(function () {
      $(this).find("a.rss").hide();
      $(this).removeClass("category_selected");
      $(this).addClass("category");
    });
    $("div#categories > div#category_"+$(this).attr("rel")).addClass("category_selected");
    $("div#categories > div#category_"+$(this).attr("rel")).find("a.rss").show();
    
    return false;
  });
  
  // setup search
  $("#search_button").click(function () {
    if ($("#search_text").val())
    {
      $.ajax({
        type: "GET",
        url: $("#search_form").attr("action"),
        data: "search_text="+escape($("#search_text").val()),
        success: function (t) {
          $("#results").html(t);
        }
      });
    }
    return false;
  });
  
  // hover on tweets
  $("div.tweet").live("mouseover", function () {
    $(this).removeClass("tweet");
    $(this).addClass("tweet_hover");
    $(this).find("div.actions").show();
  });
  
  $("div.tweet_hover").live("mouseout", function () {
    $(this).removeClass("tweet_hover");
    $(this).addClass("tweet");
    $(this).find("div.actions").hide();
  });
  
  // hover on categories
  $("div.category").live("mouseover", function () {
    $(this).removeClass("category");
    $(this).addClass("category_hover");
    $(this).find("a.rss").show();
  });
  
  $("div.category_hover").live("mouseout", function () {
    $(this).removeClass("category_hover");
    $(this).addClass("category");
    $(this).find("a.rss").hide();
  });
  
  $("div.category_hover").live("mousedown", function () {
    $(this).removeClass("category_hover");
    $(this).addClass("category_pressed");
  });
  $("div.category_pressed").live("mouseup", function () {
    $(this).removeClass("category_pressed");
    $("div#categories > div").each(function () {
      $(this).find("a.rss").hide();
      $(this).removeClass("category_selected");
      $(this).addClass("category");
    });
    $(this).find("a.rss").show();
    $(this).addClass("category_selected");
    $.ajax({
      type: "GET",
      url: $(this).find("a.name").attr("href"),
      success: function (t) {
        $("#results").html(t);
      }
    });
  });
  
  $("div.category_selected").live("mouseover", function () {
    $(this).removeClass("category_selected");
    $(this).addClass("category_selected_hover");
  });
  
  $("div.category_selected").live("mousemove", function () {
    $(this).removeClass("category_selected");
    $(this).addClass("category_selected_hover");
  });
  
  $("div.category_selected_hover").live("mouseout", function () {
    $(this).removeClass("category_selected_hover");
    $(this).addClass("category_selected");
  });
  
  $("div.category_selected_hover").live("mousedown", function () {
    $(this).addClass("category_selected_pressed");
  });
  
  $("div.category_selected_pressed").live("mouseup", function () {
    $(this).removeClass("category_selected_pressed");
    $(this).addClass("category_selected");
    $.ajax({
      type: "GET",
      url: $(this).find("a.name").attr("href"),
      success: function (t) {
        $("#results").html(t);
      }
    });
  });

  
  // intro button
  $("img#img_intro_button").mouseover(function () {
    $(this).attr("src", "/images/adauga_hover.png");
  });
  $("img#img_intro_button").mouseout(function () {
    $(this).attr("src", "/images/adauga_normal.png");
  });
  $("img#img_intro_button").mousedown(function () {
    $(this).attr("src", "/images/adauga_pressed.png");
  });
  $("img#img_intro_button").mouseup(function () {
    $(this).attr("src", "/images/adauga_normal.png");
  });
  
  // search icon
  $("#search_text").focus(function () {
    $("#search_icon").attr("src", "/images/search-on.png");
  });
  $("#search_text").blur(function () {
    $("#search_icon").attr("src", "/images/search.png");
  });
  
  // search button
  $("#search_button").mouseover(function () {
    $(this).attr("src", "/images/cauta-hover.png")
  });
  $("#search_button").mousedown(function () {
    $(this).attr("src", "/images/cauta-pressed.png")
  });
  $("#search_button").mouseout(function () {
    $(this).attr("src", "/images/cauta-normal.png")
  });
    
  //preload images
  $.preloadCssImages();
});