/**
 * @requires jQuery
 * @date 2009-11-29
 */

function catalogSort(sortEl, productsEl) {
	productsEl = $(productsEl);

	var
		itemsEls = productsEl.find('.item'),
		sortEls = $(sortEl).find('.item');

	var
		bySeries = [],
		byNew = [],
		byAsc = [],
		byDesc = [],
		byAlphabet = [];

	fillBySeries();
	fillByNew();
	fillByAsc();
	fillByDesc();
	fillByAlphabet();

	sortEls.each(function() {
		var el = $(this);

		el.find('.pseudo').click(function() {
			if (el.hasClass('selected')) return;

			var sortType = getType(el, 'type_');

			switch (sortType) {
				case 'series':
					sortBySeries();
					break;
				case 'new':
					sortByNew();
					break;
				case 'asc':
					sortByAsc();
					break;
				case 'desc':
					sortByDesc();
					break;
				case 'alphabet':
					sortByAlphabet();
					break;
			}

			sortEls.filter('.selected').removeClass('selected');
			el.addClass('selected');
		});
	});

	function fillBySeries() {
		productsEl.find('.series_header').each(function() {
			bySeries.push({
				headerEl: $(this),
				items: []
			});
		});

		itemsEls.each(function() {
			var number = $(this).prevAll('.series_header').length - 1;

			if (-1 < number) {
				bySeries[number].items.push($(this));
			}
		});
	}

	function sortBySeries() {
		for (var i = 0; i < bySeries.length; i++) {
			bySeries[i].headerEl.appendTo(productsEl);

			for (var j = 0; j < bySeries[i].items.length; j++) {
				bySeries[i].items[j].appendTo(productsEl);
			}
		}

		productsEl.removeClass('without_series');
	}

	function fillByNew() {
		var items = [];

		itemsEls.each(function() {
			var el = $(this);
			el.find('.new_price').length ? byNew.push(el) : items.push(el);
		});

		for (var i = 0; i < items.length; i++) {
			byNew.push(items[i]);
		}
	}

	function sortByNew() {
		productsEl.addClass('without_series');

		for (var i = 0; i < byNew.length; i++) {
			byNew[i].appendTo(productsEl);
		}
	}

	function fillByAsc() {
		var sorts = [];

		itemsEls.each(function(index) {
			var el = $(this);
			var price = -1;
			var priceEl = el.find('.modification_selected .new_price .sort_field');

			if (priceEl.length) {
				price = getPrice(priceEl.text());
			} else {
				priceEl = el.find('.modification_selected .sale_price .sort_field');

				if (priceEl.length) {
					price = getPrice(priceEl.text());
				} else {
					priceEl = el.find('.modification_selected .price .sort_field');

					if (priceEl.length) {
						price = getPrice(priceEl.text());
					}
				}
			}

			sorts.push({
				price: price,
				el: el
			});
		});

		sorts.sort(function (a, b) {
			if (0 > a.price) return true;
			if (0 > b.price) return false;

			return a.price - b.price;
		});

		for (var i = 0; i < sorts.length; i++) {
			byAsc.push(sorts[i].el);
		}
	}

	function sortByAsc() {
		productsEl.addClass('without_series');

		for (var i = 0; i < byAsc.length; i++) {
			byAsc[i].appendTo(productsEl);
		}
	}

	function fillByDesc() {
		var sorts = [];

		itemsEls.each(function(index) {
			var el = $(this);
			var price = -1;
			var priceEl = el.find('.modification_selected .new_price .sort_field');

			if (priceEl.length) {
				price = getPrice(priceEl.text());
			} else {
				priceEl = el.find('.modification_selected .sale_price .sort_field');

				if (priceEl.length) {
					price = getPrice(priceEl.text());
				} else {
					priceEl = el.find('.modification_selected .price .sort_field');

					if (priceEl.length) {
						price = getPrice(priceEl.text());
					}
				}
			}

			sorts.push({
				price: price,
				el: el
			});
		});

		sorts.sort(function (a, b) {
			return b.price - a.price;
		});

		for (var i = 0; i < sorts.length; i++) {
			byDesc.push(sorts[i].el);
		}
	}

	function sortByDesc() {
		productsEl.addClass('without_series');

		for (var i = 0; i < byDesc.length; i++) {
			byDesc[i].appendTo(productsEl);
		}
	}

	function fillByAlphabet() {
		var sorts = [];

		itemsEls.each(function(index) {
			var el = $(this);

			sorts.push({
				name: $.trim(el.find('.name').eq(0).text()),
				el: el
			});
		});

		sorts.sort(function (a, b) {
			return a.name > b.name;
		});

		for (var i = 0; i < sorts.length; i++) {
			byAlphabet.push(sorts[i].el);
		}
	}

	function sortByAlphabet() {
		productsEl.addClass('without_series');

		for (var i = 0; i < byAlphabet.length; i++) {
			byAlphabet[i].appendTo(productsEl);
		}
	}

	function getPrice(priceStr) {
		var price = priceStr.replace(' ', '');

		return parseFloat(price.replace(',', '.'));
	}

	function getType(el, prefix) {
		var classes = $(el).attr('class').split(' ');

		for (var i = 0; i < classes.length; i++) {
			if (prefix == classes[i].substr(0, prefix.length)) {
				return classes[i].substr(prefix.length);
			}
		}

		return false;
	}
}

$(function() {
	catalogSort('#content .sortby', '#content .products_list');
});
