(function($) {
	$.fn.subscribeToCategory = function(id, currentlySubscribed, shouldConfirm, subscribeCallback) {

		var cb = function(isSubscribed) {
			if (subscribeCallback)
				subscribeCallback.call(this, isSubscribed);
		}

		var removeCategory = function() {
			var self = this;
			services.subscriptions.removeCategory(id, function(data) {
				if(data.status == true) {
					self.html("Subscribe");
					self.data("subscribed", !self.data("subscribed"));
					cb.call(self, false);
				} else if (data.action != "") {
					window.location.href = data.action;
				} else {
					alert("Your subscribe/unsubscribe request couldn't be fullfilled at the moment. Please try again later");					    
				}
			});
		}

		var addCategory = function() {
			var self = this;
			services.subscriptions.addCategory(id, function(data) {
				if (data.status == true) {
					self.html("Unsubscribe");
					self.data("subscribed", !self.data("subscribed"));
					cb.call(self, true);
				} else if(data.action != "") {
					window.location.href = data.action;
				} else {
					alert("Your subscribe/unsubscribe request couldn't be fullfilled at the moment. Please try again later");					    						
				}
			});
		}

		return this.each(function() {
			var $this = $(this);
			$this.data("subscribed", currentlySubscribed);

			$this.bind("click", function() {
				if (!$this.data("subscribed")) {
					addCategory.call($this);
				} else {
					if (shouldConfirm) {
						if (confirm("Are you sure you want to unsubscribe from this category?"))
							removeCategory.call($this);	
					} else {
						removeCategory.call($this);
					}
				}
				return false;
			});
		});
	}

	$.fn.subscribeToMember = function(id, currentlySubscribed, shouldConfirm, subscribeCallback) {

		var cb = function(isSubscribed) {
			if (subscribeCallback)
				subscribeCallback.call(this, isSubscribed);
		}

		var addMember = function() {
			var self = this;
			services.subscriptions.addMember(id, function(data) {
				if (data.status == true) {
					self.html("Unsubscribe");
					self.data("subscribed", !self.data("subscribed"));					    
					cb.call(self, true);
				} else if (data.action != "") {
					window.location.href = data.action;
				} else {
					alert("Your subscribe/unsubscribe request couldn't be fullfilled at the moment. Please try again later");					    
				}
			});
		}

		var removeMember = function() {
			var self = this;
			services.subscriptions.removeMember(id, function(data) {
				if (data.status == true) {
					self.html("Subscribe");
					self.data("subscribed", !self.data("subscribed"));
					cb.call(self, false);
				} else if (data.action != "") {
					window.location.href = data.action;
				} else {
					alert("Your subscribe/unsubscribe request couldn't be fullfilled at the moment. Please try again later");
				}
			});
		}

		return this.each(function() {
			var $this = $(this);
			$this.data("subscribed", currentlySubscribed);

			$this.bind("click", function() {
				if (!$this.data("subscribed")) {
					addMember.call($this);
				} else {
					if (shouldConfirm) {
						if (confirm("Are you sure you want to unsubscribe from this member?"))
							removeMember.call($this);	
					} else {
						removeMember.call($this);
					}
				}
				return false;
			});
		});
	}

}(jQuery))
