$().ready(function () {
	var c = jQuery('<center><div id="loader"><center><img src="img/loading.gif" alt="loading..." /></center></div></center>').css({
		position: "absolute",
		top: "250px",
		left: "40%"
	}).appendTo("body").hide();
	jQuery().ajaxStart(function () {
		c.show()
	}).ajaxStop(function () {
		c.hide()
	}).ajaxError(function (a, b, e) {
		throw e;
	});
	$("#sort i").hover(function () {
		$("a:first", this).addClass("selected");
		$(this).addClass("selected")
	},
	function () {
		if ($(this).hasClass("protected")) {} else {
			$("a:first", this).removeClass("selected");
			$(this).removeClass("selected")
		}
	})
});
function sorted() {
	var a = "";
	var b = "";
	$('#sorted').empty();
	$(".selected").each(function () {
		a += $(this).attr("id") + ",";
		if ($(this).attr("val") == "br") {
			b = $(this).attr("id")
		}
	
	});
	
	$("#hiddenresult").load("http://www.e-papa.ru/sort.php", {
		id: a,
		id_cat: ci,
		dirr: rd,
		brend: b
	},
	initPagination)

}
$(".cond:not(.protected)").live("click", function () {
	$("#pre i").removeClass("selected protected");
	$("#pre i").html("");
	var a = $(this).attr("val");
	$(".cond[val*='" + a + "']").removeClass("selected protected");
	$(".cond[val*='" + a + "']").parent().removeClass("selected protected");
	$("#allbrand").css({
		"display": "none"
	});
	$(this).addClass("selected protected");
	$(this).parent().addClass("selected protected");
	sorted();
	
	//return false
});
$("#un_sort img").live("click", function () {
	var a = $(this).attr("id");
	$(".cond[id*='" + a + "']").removeClass("selected protected");
	$(".cond[id*='" + a + "']").parent().removeClass("selected protected");
	sorted()
});
$("#all").live("click", function () {
	$("#allbrand").css({
		"display": "block"
	})
});
$("#allbrand i").live("click", function () {
	$("#pre i").html($(this).html());
	$("#pre i").addClass("selected protected");
	$("#pre i").find("a").addClass("selected protected");
	var a = $("#pre i").find("a").attr("val");
	$(".cond[val*='" + a + "']").removeClass("selected protected");
	$(".cond[val*='" + a + "']").parent().removeClass("selected protected");
	$("#allbrand").slideUp("fast");
	sorted()
});
$("#close").live("click", function () {
	$("#allbrand").css({
		"display": "none"
	})
});
function handlePaginationClick(b, c) {
	var d = $('#hiddenresult div.result:eq(' + b + ')').clone();
	$('#sorted').empty().append(d);
	$("#sorted .pict a").each(function () {
		var a = $(this).parent().attr("val");
		if (a == "") {
			a = "def.gif"
		}
		$(this).parent().css('background-image', 'url(img/loading-micro.gif)');
		$(this).html("<img src='inc/mini_im_goods/" + a + "'>")
	});
	return false
}
function initPagination() {
	var a = $('#hiddenresult div.result').length;
	$("#Pagination").pagination(a, {
		num_edge_entries: 2,
		num_display_entries: 8,
		items_per_page: 1,
		callback: handlePaginationClick
	})
}
