$(document).ready(function() {
  // Add ajax parameters on rating link
  $(".rating > li a").attr("href", function() {
    return $(this).attr("href")+"&a=1";
   });
  // Init rating
  $(".rating > li a").click(function() {
    el = $(this);
    rating = getUrlParam(el.attr("href"), "r");
    el.parents("ul.rating").removeClass().addClass("rating star"+rating);
    $.get(el.attr("href"), function(rating) {
      if (rating==0) {
        el.parents("ul.rating").removeClass().addClass("rating star0");
      }
    });

    return false;
  });

  // Add ajax parameters on rating_byuser link
  $(".rating_byuser > li a").attr("href", function() {
    return $(this).attr("href")+"&a=1";
   });
  // Init rating
  $(".rating_byuser > li a").click(function() {
    el = $(this);
    design_num = getUrlParam(el.attr("href"), "num");
    rating = getUrlParam(el.attr("href"), "r");
    $.get(el.attr("href"), function(rating) {
      $("#rating-wrapper-"+design_num).html('<div class="rating_byuser star'+rating+'" ></div>');
    });

    return false;
  });

  // Add ajax parameters on watchlist link
  $("a#watchlist").attr("href", function() {
    return $(this).attr("href")+"&a=1";
   });
  // Init Watchlist
  $("a#watchlist").click(function() {
    el = $(this);
    el.html("Wait..");
    $.get(el.attr("href"), function(result) {
      if (result==0)
        el.html("Add to Watchlist");
      else
        el.html("Remove from Watchlist");
    });

    return false;
  });

  // Add ajax parameters on nominate link
  $("a.nominate").attr("href", function() {
    return $(this).attr("href")+"&a=1";
   });
  // Init Nominate Design
  $("a.nominate").click(function() {
    el = $(this);
    el.html("Wait..");
    $.get(el.attr("href"), function(result) {
      if (result==0) {
        el.html("Nominate");
        el.attr("title", "Nominate this design for winner candidates");
      } else {
        el.html("Denominate");
        el.attr("title", "Denominate this design from winner candidates");
      }
    });

    return false;
  });

  // Add ajax parameters on withdraw link
  $("a.withdraw").attr("href", function() {
    return $(this).attr("href")+"&a=1";
   });
  // Init Withdraw Design
  $("a.withdraw").click(function() {
    el = $(this);
    num= getUrlParam(el.attr("href"), "num");
    el.html("Wait..");
    $.get(el.attr("href"), function(result) {
      if (result==1)
        $(".thumbnails #design"+num).fadeOut("slow")
    });

    return false;
  });

  // Init image preview
  $(".thumbnails a").imgPreview();

})

