(function($) {
	
	var self = this, $self = $(this);
	
	function CartButton(root, el){
		var self = this, $self = $(this);
		var id = el.attr("rel");
		
		$.extend(this, {
				
			click: function(i, e) {
				$.ajax({
					async: false,
					type: "GET",
					url: "/services/bag/add.asp?item="+id+"&quantity=1",
					cache: false,
					success: this.requestSuccess,
					error: this.requestError,
					timeout: 0
				});
			},

			done: function(i,e){
				root.trigger("update");
				el.after("<a href=\"/bag/\" class=\"order\">Заказать</a>").FadeIn(500);
			},
			
			requestSuccess: function(data, status){
				el.fadeOut(500, function(){
					e = $.Event();
					e.type = "done";
					$self.trigger(e);			
				});
			},
			
			requestError: function(httpobj, status, ex){
				alert("Error occured!")
			}
			
		})
	}
	
	function CartItem(root,el){
		var self = this, $self = $(this), value;
		
		$.extend(this, {
		
			remove: function(i,e){
				//if(confirm("Really delete?"))
				$.ajax({
					async: false,
					type: "GET",
					url: "/services/bag/remove.asp?item="+$(e.currentTarget).attr("rel"),
					cache: false,
					success: this.removeSuccess,
					timeout: 0
				});
				el.fadeOut(200, function(){
					e = $.Event();
					e.type = "afterRemove";
					$self.trigger(e);			
				});
			},
			
			removeSuccess: function(data, status){
				root.trigger("update");
			},
			
			afterRemove: function(){
				if(el.siblings("tr").length == 0){
					el.closest("tbody").append("<tr><td colspan=\"5\" class=\"empty\">Ваша корзина пуста</td></tr>");
					$("#orderbox").hide();
				}
				el.remove();
			},
			
			updateSuccess: function(data, status){
				var jData = $(data);
				el.find("#item-sum").text(jData.find("subtotals").text());
				root.trigger("update");
			},
			
			beforePress: function(i,e){
				if((e.keyCode>=48 && e.keyCode<=57 && !e.shiftKey && !e.altKey && !e.ctrlKey) || (e.keyCode>=35 && e.keyCode<=40) || e.keyCode==8 || e.keyCode==46 ){
					value = e.currentTarget.value;
				}
				else{
					e.preventDefault();
					e.stopPropagation();
					return false;
				}
			},
			
			afterPress: function(i,e){
				if(((e.keyCode>=48 && e.keyCode<=57) /*|| (e.keyCode>=35 && e.keyCode<=40)*/|| e.keyCode==8 || e.keyCode==46) && !e.shiftKey && !e.altKey && !e.ctrlKey){
					if(e.currentTarget.value == "" || isNaN(parseInt(e.currentTarget.value))){
						e.currentTarget.value = value;
						return false;
					}
					if(e.currentTarget.value != value){
						$.ajax({
							async: false,
							type: "GET",
							url: "/services/bag/update.asp?item="+e.currentTarget.name+"&quantity="+e.currentTarget.value,
							cache: false,
							success: this.updateSuccess,
							timeout: 0,
							dataType: "xml"
						});
					}
				}
				else{
					e.preventDefault();
					e.stopPropagation();
					return false;
				}
			}
			
		})
	}
	
	$.fn.smartcart = function(buttons) {
		$(buttons).each(function(i) {
			var el = new CartButton($self, $(this));
			$(this).bind("click", function(e) {
					el.click(i, e);
					return false;
				});
		});
		
		$("table#bag tbody tr").each(function(i){
			var el = new CartItem($self, $(this));
			$(this).find("a[rel]").bind("click", function(e){
				el.remove(i,e);
				return false;
			});
			$(this).find(":input[type='text']").bind("keydown", function(e){
				return el.beforePress(i,e);
			});
			$(this).find(":input[type='text']").bind("keyup", function(e){
				return el.afterPress(i,e);
			});
		});
		return this;
	};
		
	$.extend(this, {
	
		update: function(){
			$.ajax({
				async: false,
				type: "GET",
				url: "/services/bag/count.asp",
				cache: false,
				success: this.updateSuccess,
				timeout: 0,
				dataType: "xml"
			});
		},
		
		updateSuccess: function(xmlData, status){
			var jData = $(xmlData);
			$("#basket #count").text(jData.find("count").text());
			$("#sum").text(jData.find("subtotals").text());
			$("#totals").text(jData.find("subtotals").text());
		}
		
	});

}) (jQuery); 

