CatalogCart = function () {
	this.url = '';
	this.items = [];
}

CatalogCart.prototype.init = function () {
	var controller = this;
	var eids = $("input[name=id]");
	
	for (var i = 0; i < this.items.length; i++) {
 		this.handleItem(this.items[i], i);
	}
	
	$("input[name=enabled-delivery]").change(function () { controller.handleDelivery() });
	
	jQuery.get(this.url, { }, function (data) { controller.onCount(data) });
}

CatalogCart.prototype.makeSum = function () {
	var sum   = 0;
	var total = 0;
	
	var esum = $("#total-price");
		
	for (var i = 0; i < cart.items.length; i++) {
		var enabled = $("input[name=enabled-" + cart.items[i].id + "]")[0].checked;
		if (enabled) {
			sum += cart.items[i].price * cart.items[i].count;
			total += cart.items[i].count;
		}
	}
	
	esum.text(sum + "р.");
	cart.onCount(total);
}

CatalogCart.prototype.handleItem = function (item, index) {
	var id    = item.id;
	var price = item.price;
	
	var ecount  = $("input[name=count-" + id + "]");
	var eenabled = $("input[name=enabled-" + id + "]");
	var eprice  = $("#per-item-" + id);
	var etotal  = $("#total-" + id);
	
	var makeprice = function () {
		var count   = cart.items[index].count = parseInt(ecount.val());
		var enabled = eenabled[0].checked
		
		if (count && enabled) {
			if (count > 1) { etotal.show();
			} else { etotal.hide(); }
			
			etotal.text("(" + (price * count) + ' р.)');
			cart.set(id, count);
		} else {
			etotal.text("");
			cart.set(id, 0);
		}
		
		cart.makeSum();
	}
	
	ecount.keyup(makeprice);
 	eenabled.change(makeprice);
 	
	makeprice();
}

CatalogCart.prototype.handleDelivery = function () {
	this.makeSum();
}

CatalogCart.prototype.add = function (itemid, clicker) {
	var controller = this;
	
	jQuery.get(this.url, { 'add' : itemid }, function (data) { controller.onCount(data) });
	
	if (clicker) {
		$(clicker).text("Добавить еще");
	}
}

CatalogCart.prototype.set = function (itemid, count) {
	var controller = this;
	
	jQuery.get(this.url, { 'set' : itemid, 'count' : count }, function (data) { controller.onCount(data) });
}

CatalogCart.prototype.onCount = function (data) {
	var text = '';
	
	$('.cart-link').show();
	$('.cart-link a').removeClass("disabled");
	$('.cart-link a')[0].onclick = null;
	
	if (data == 0) {
		text = 'Нет товаров';
		
		$('.cart-link a').addClass("disabled");
		$('.cart-link a')[0].onclick = function () { return false; }
		
//		$('#header .cart-link').hide();
	} else if (data == 11 || data == 12) {
		text = 'В корзине ' + data + ' товаров';
	} else if (data % 10 == 1) {
		text = 'В корзине ' + data + ' товар';
	} else if (data % 10 == 2 || data % 10 == 3) {
		text = 'В корзине ' + data + ' товара';
	} else {
		text = 'В корзине ' + data + ' товаров';
	}
	
	$('.cart-link a').text(text);
}

var cart = new CatalogCart();

CatalogFilter = function (id) {
	this.id   = id;
	this.data = [];
}

CatalogFilter.prototype.init = function () {
	var controller = this;
	var container  = $("#" + this.id);
	
	for (var i in this.data) {
		var wdata = this.data[i];
		var widget = this.createWidget(wdata);
		
		container.append(widget);
	}
	
	this.askCount();
	
	$("#filter .available .only").click(function () { controller.setAvailable('1') });
	$("#filter .available .all").click(function () { controller.setAvailable('0') });
	
	this.setAvailable($("#filter input[name=available]").val());
	
	$("#filter input").change(function () { controller.askCount() });
}

CatalogFilter.prototype.createWidget = function (wdata) {
	var widget = $("#" + this.id + "-type-" + wdata['type']).clone();
	
	widget.code = wdata.code;
	$(".label", widget).text(wdata.name);
	
	if (wdata.type == 0) {
		$(".from", widget).attr("name", wdata.code + '-from');
		$(".from", widget).val(wdata.from);
		
		$(".to", widget).attr("name", wdata.code + '-to');
		$(".to", widget).val(wdata.to);
	}
	
	if (wdata.type == 1) {
		for (var i in wdata.choices) {
			var repeat = $(".repeat > *", widget).clone();
			
			$("input", repeat).attr("name", wdata.code);
			$("input", repeat).val(wdata.choices[i]);
			$(".value", repeat).text(wdata.choices[i])
			
			for (var y in wdata.selected) {
				if (wdata.choices[i] == wdata.selected[y]) {
					$("input", repeat).attr("checked", true);
					$(widget).removeClass("collapsed");
					$(widget).addClass("expanded");
					break;
				}
			}
			
			$(".widgets", widget).append(repeat);
		}
		
		$(".repeat", widget).remove();
	}
	
	if (wdata.type == 3) {
		$("input", widget).attr("name", wdata.code);
		$("input", widget).attr("checked", wdata.checked);
	}
	
	if (wdata.type == 4) {
		$("input", widget).attr("name", wdata.code);
		
		if (wdata.value == "1") { var selected = "yes"; }
		else if (wdata.value == "0") { var selected = "no"; }
		else { var selected = "nomatter"; }
		
		$("input." + selected, widget).attr("checked", "checked");
	}
	
	return widget;
}

CatalogFilter.prototype.setAvailable = function (value) {
	$("#filter input[name=available]").val(value);
	$("#filter .available .js-link").removeClass("selected");
	
	if (value == "1") {
		$("#filter .available .only").addClass("selected");
	} else {
		$("#filter .available .all").addClass("selected");
	}
	
	this.askCount();
}

CatalogFilter.prototype.askCount = function () {
	var counturl = this.counturl;
	
	var onget = function (count) {
		var lastpart = '';
		
		if (count == 0) {
			$("#filter .submit").val("Нет результатов");
			$("#filter .submit").attr("disabled", "disabled");
			return;
		} else {
			$("#filter .submit").attr("disabled", "");
		}
		
		if (count % 10 == 1) {
			lastpart = "результат";
		} else if (count % 10 >= 2 && count % 10 <= 3) {
			lastpart = "результата";
		} else {
			lastpart = "результатов";
		}
		
		$("#filter .submit").val("Показать " + count + " " + lastpart);
	}
	
	var data = $("#filter form").serialize();
	jQuery.get(counturl + "?" + data, "", onget);
}
