var work=false;

function UpdateFavorites(action, type, object_id, id_link)
{
	if(work)
		return;
	work=true;

  $("#" +id_link).empty();
  $("#" +id_link).append("<img src='/img/line_2.gif'>");

  $.post('/favorite/',
  {action: action, type: type, object_id: object_id},
  function(data)
  {
	work=false;


    $("#" +id_link).empty();
    var $tmp = $("#" +id_link+ "_div").attr("innerHTML");
    $("#" +id_link+ "_div").empty();

    if (action == "add")
    {

      $("#" +id_link).append("Remove From Favorite");
      $("#" +id_link).attr("href", "javascript:UpdateFavorites( 'delete', '"+type+"', '"+object_id+"', '" +id_link+ "')");
      //$("#" +id_link).attr("class", "jointxt");
      //increase the number of people where this element is favorite
      var $tmp_int = parseInt($tmp) + 1;
      $("#" +id_link+ "_div").append($tmp_int);

      //if changes are in "Content" window and there are objects with the same id in "featured files" window
      if (type=='files'&& $("#"+"aHref_f"+object_id).text() && (id_link != "aHref_f"+object_id))
      {
        //then change text in "featured files" window also
        $("#"+"aHref_f"+object_id).empty();
        $("#"+"aHref_f"+object_id).append("Remove From Favorite");
        $("#"+"aHref_f"+object_id).attr("href", "javascript:UpdateFavorites( 'delete','"+type+"', '" +object_id+ "', '" +"aHref_f"+object_id+ "')");
        //$("#"+"aHref_f"+object_id).attr("class", "jointxt");
        $("#"+"aHref_f"+object_id+"_div").empty();
        $("#"+"aHref_f"+object_id+"_div").append($tmp_int);
      }


      //if changes are in "featured files" window and there are objects with the same id in "Content" window
	  //alert("#"+"aHref_"+object_id);
      if ( type=='files'&&$("#"+"aHref_"+object_id).text() && (id_link != "aHref_"+object_id))
      {

        //then change text in "Content" window also
        $("#"+"aHref_"+object_id).empty();
        $("#"+"aHref_"+object_id).append("Remove From Favorite");
        $("#"+"aHref_"+object_id).attr("href", "javascript:UpdateFavorites( 'delete', '"+type+"', '" +object_id+ "', '" +"aHref_"+object_id+ "')");
        //$("#"+"aHref_"+object_id).attr("class", "jointxt");
        $("#"+"aHref_"+object_id+"_div").empty();
        $("#"+"aHref_"+object_id+"_div").append($tmp_int);
      }
    }
    else
    {

      $("#" +id_link).append("Add To Favorite");
      $("#" +id_link).attr("href", "javascript:UpdateFavorites( 'add', '"+type+"', '"+object_id+"', '" +id_link+ "')");
      //$("#" +id_link).attr("class", "jointxt");
      //decrease the number of people where this element is favorite
      var $tmp_int = parseInt($tmp) - 1;
      if ($tmp_int == false) //if $tmp_int is equal to zero
      {
        $tmp_int = "0";
      }
      $("#" +id_link+ "_div").append($tmp_int);



      //if changes are in "Content" window and there are objects with the same id in "featured files" window
      if (type=='files'&& $("#"+"aHref_f"+object_id).text() && (id_link != "aHref_f"+object_id))
      {
        //then change text in "featured files" window also
        $("#"+"aHref_f"+object_id).empty();
        $("#"+"aHref_f"+object_id).append("Add To Favorite");
        $("#"+"aHref_f"+object_id).attr("href", "javascript:UpdateFavorites( 'add', '"+type+"', '" +object_id+ "', '" +"aHref_f"+object_id+ "')");
        //$("#"+"aHref_f"+object_id).attr("class", "jointxt");
        $("#"+"aHref_f"+object_id+"_div").empty();
        $("#"+"aHref_f"+object_id+"_div").append($tmp_int);
      }


      //if changes are in "featured files" window and there are objects with the same id in "Content" window
	/*  alert("#"+"aHref_"+object_id);
	  ("aHref_124" != "aHref_"+124)*/

      if ( type=='files'&&$("#"+"aHref_"+object_id).text() && (id_link != "aHref_"+object_id))
	  {

        //then change text in "Content" window also
        $("#"+"aHref_"+object_id).empty();
        $("#"+"aHref_"+object_id).append("Add To Favorite");
        $("#"+"aHref_"+object_id).attr("href", "javascript:UpdateFavorites( 'add', '"+type+"', '" +object_id+ "', '" +"aHref_"+object_id+ "')");
        //$("#"+"aHref_"+object_id).attr("class", "jointxt");
        $("#"+"aHref_"+object_id+"_div").empty();
        $("#"+"aHref_"+object_id+"_div").append($tmp_int);
      }
    }
  }
  );
}
