var Ecom = {
	init: function(){
		$(".add_to_cart").click(function(e){
			e.preventDefault();
			e.stopPropagation();
			this.blur();
			
			var printComicId = $(this).attr("id").substring(12);
			$.ajax({
				"type": "POST",
				"url": "/cart",
				"dataType": "html",
				"data": {"printComicId": printComicId, "method": "add"},
				"success": function(data){Fabler.showFadeMessage(data);},
				"error": function(data){Fabler.showFadeMessage("<div class='warnings'>Oops, something went wrong and we were unable to fulfill your request. Please try again in a few minutes</div>");}
			});
			
		});
		
		$(".buy").click(function(e){
			e.preventDefault();
			e.stopPropagation();
			this.blur();
			
			var printComicId = $(this).attr('id').substring(4);
			$.ajax({
				"type": "POST",
				"url": "/buy",
				"dataType": "html",
				"data": {"printComicId": printComicId},
				"success": function(response){
					window.location = response;
				},
				"error": function(response){
					if(response.status == '401')
						Fabler.showFadeMessage("<div class='warnings'>You must be logged in to purchase a comic.</div>");
					else
						Fabler.showFadeMessage("<div class='warnings'>Oops, something went wrong and we were unable to fulfill your request. Please try again in a few minutes</div>");
				}
			});
		});
		
		$(".subscribe").click(function(e){
			e.preventDefault();
			e.stopPropagation();
			this.blur();
			
			var seriesId = $(this).attr('id').substring(10);
			Ecom.addSubscription(seriesId);
		});
		
		$(".unsubscribe").click(function(e){
			e.preventDefault();
			e.stopPropagation();
			this.blur();
			
			var seriesId = $(this).attr('id').substring(10);
			Ecom.removeSubscription(seriesId);
		});
	},
	
	addSubscription: function(seriesId){
		$.ajax({
			"type": "POST",
			"url": "/a/subscription/series",
			"dataType": "html",
			"data": {"seriesId": seriesId, "a": "add"},
			"success": function(response){
				Fabler.showFadeMessage(response);
				$("#subscribe_" + seriesId).removeClass("subscribe").addClass("unsubscribe");
				
				$("#subscribe_" + seriesId).unbind("click");
				$("#subscribe_" + seriesId).click(function(e){
					e.preventDefault();
					e.stopPropagation();
					this.blur();
					Ecom.removeSubscription(seriesId);
				});
			},
			"error": function(response){
				if(response.status == '401')
					Fabler.showFadeMessage("<div class='warnings'>You must be logged in to subscribe to a comic series.</div>");
				else
					Fabler.showFadeMessage("<div class='warnings'>Oops, something went wrong and we were unable to fulfill your request. Please try again in a few minutes</div>");
			}
		});
	},
	
	removeSubscription: function(seriesId){
		$.ajax({
			"type": "POST",
			"url": "/a/subscription/series",
			"dataType": "html",
			"data": {"seriesId": seriesId, "a": "remove"},
			"success": function(response){
				Fabler.showFadeMessage(response);
				$("#subscribe_" + seriesId).removeClass("subscribe").addClass("unsubscribe");
				
				$("#subscribe_" + seriesId).unbind("click");
				$("#subscribe_" + seriesId).click(function(e){
					e.preventDefault();
					e.stopPropagation();
					this.blur();
					Ecom.addSubscription(seriesId);
				});
			},
			"error": function(response){
				if(response.status == '401')
					Fabler.showFadeMessage("<div class='warnings'>You must be logged in to subscribe to a comic series.</div>");
				else
					Fabler.showFadeMessage("<div class='warnings'>Oops, something went wrong and we were unable to fulfill your request. Please try again in a few minutes</div>");
			}
		});
	}
};

$(document).ready(function(){
	Ecom.init();
});
