function FavoriteButton(divObj, imgPath) {
  var fb = this;
  
  this.divObj = divObj;
  this.imgObj = $(divObj).find(".favorite_button");
  this.imgOut = this.imgObj.src;
  this.imgPath = imgPath;
  this.favNoFav = function() { return this.imgPath + "fav_nofav.gif" };
  this.favYesFav = function() { return this.imgPath + "fav_yesfav.gif" };
  this.favHover = function() { return this.imgPath + "fav_hover.gif" };
  this.favActive = function() { return this.imgPath + "fav_active.gif" };
  this.active = false;
  
  this.autoSetButton = function() {
    if (fb.active) {
        fb.imgOut = fb.favYesFav();
      } else {
        fb.imgOut = fb.favNoFav();
      }
      $(fb.imgObj).attr("src", fb.imgOut);
  };
  
  this.setButton = function() {
  	var metAll = $(fb.imgObj).attr('meta').split("|");
	fb.brandId = metAll[0];
	fb.listId = metAll[1];
	if (metAll[2] == "True") fb.active = true;
    $(fb.imgObj).hover(function() {
      fb.imgOut = $(this).attr("src");
      $(this).attr("src", fb.favHover());
    }, function() {
      fb.autoSetButton();
    }).mousedown(function() {
      $(this).attr("src", fb.favActive());
      fb.active = !fb.active;
    }).mouseup(function() {
      $(this).attr("src", fb.favHover());
    }).click(function() {
      var operation = -1;
      if (fb.active == true)
        operation = 1;  // Add
      else
        operation = 3;  // Remove
      $.ajax({
        type: "POST",
        url: "/listener/v4.1/json/favorite/",
        dataType: 'json',
        data: {
          b: fb.brandId ? fb.brandId : -1,
          l: fb.listId ? fb.listId : -1,
          o: operation,
          w: 1
        },
        success: function(result) {
          // Parse the results and re-set the button if it's a failure
          if (result.favorite == true) {
            $(fb.divObj).find(".favorites_notification_added").show().animate({
              opacity: 0.0
            }, 3000, function() {
              $(this).hide().css("opacity", 1);
            })
          } else {
          	$(fb.divObj).find(".favorites_notification_removed").show().animate({
          		opacity: 0.0
          	}, 3000, function() {
          		$(this).hide().css("opacity", 1);
          	})
          }
        },
        error: function() {
          // Re-set the button
          fb.active = !fb.active;
          fb.autoSetButton();
        }
      });
    });
  };
  
  this.checkFavorite = function() {
    $.ajax({
      type: "GET",
      url: "/listener/v4.1/json/favorite/",
      dataType: 'json',
      data: {
        b: brandid ? brandid : -1,
        l: listid ? listid : -1,
        o: 4,
        w: 1
      },
      success: function(result) {
        if (result[0] == true) {
          fb.active = true;
          fb.autoSetButton();
        }
      }
    })
  };
}

function clearHistory() {
	$.ajax({
		type: "POST",
		url: "/listener/v4.1/json/recent/clear/",
		data: {
			w: 1
		},
		success: function(result) {
			window.location.reload()
		}
	});
}

