﻿
$.fn.shopCartItemCounter = function(options){
	
	var productId = $(this).attr('data-productId');
	var _this  = $(this);
	var updateUrl = options.updateUrl;
	var removeUrl = options.removeUrl;
	var updateSelector = options.updateSelector;
	$('.item-remove').hover(function(e){
		$(this).css('cursor', 'pointer');
	},
	function(e){
		$(this).css('cursor', 'default');
	});
	$('.item-remove').click(function(e){
		$.ajax({
				type: "POST",
				url: removeUrl,
				data: {productId : productId},
				dataType: "html",
				success: function (data) {
					$(updateSelector).html(data);
				}
			});
	});
	$('.item-counter-quantity:first', _this).keyup(function(e){
		if(isNaN($(this).val())){
			$(this).val(1);
		} 
		if(parseInt($(this).val()) < 1){
			$(this).val(1);
		}
		if($(this).val() == null || $(this).val() == ''){
			$(this).val(1);
		}
		$('.item-cost', _this).html(parseFloat($('.item-price', _this).html()) * parseInt($(this).val()));
		var isUpdateAllowed = $(_this).attr('data-isUpdateAllowed');
		if(isUpdateAllowed){
			$(_this).attr('data-isUpdateAllowed', false);
			$.ajax({
				type: "POST",
				url: updateUrl,
				data: {productId : productId, quantity : quantity},
				dataType: "html",
				success: function (data) {
					$(updateSelector).html(data);
					$(_this).attr('data-isUpdateAllowed', true);
				}
			});
        }
	});	

}
