/**
 * Functions to parse onPageLoad
 */

$(document)
		.ready(
				function() {
					// Round red-header items
					$("h2.red-header").wrap(
							'<div class="rounded-top red-header"><div class="cl"><div class="cr">'
									+ '</div></div></div>');
					// Round menu block item
					$("div.menu div.inner").wrap(
							'<div class="rounded-bottom grey-white block"><div class="cl"><div class="cr">'
									+ '</div></div></div>');
					// Round discount block
					$("div.redHaderBlock div.inner").wrap(
							'<div class="rounded-bottom grey-white"><div class="cl"><div class="cr">'
									+ '</div></div></div>');
					// Round grey white blocks
					$('div.grey-white-block').wrap(
							'<div class="rounded grey-white block"><div class="cl"><div class="cr">'
									+ '</div></div></div>');
					$('div.grey-block').wrap(
							'<div class="rounded grey-block"><div class="cl"><div class="cr">'
									+ '</div></div></div>');
					// Round block
					$('div.rounded')
							.prepend(
									'<div class="hd">'
											+ '<div class="hl"></div><div class="hr"></div></div>')
							.append(
									'<div class="ft">'
											+ '<div class="fl">&nbsp;</div><div class="fr">&nbsp;</div></div>');
					// Round bottom part
					$('div.rounded-bottom')
							.append(
									'<div class="ft">'
											+ '<div class="fl">&nbsp;</div><div class="fr">&nbsp;</div></div>');
					// Round top part
					$('div.rounded-top')
							.prepend(
									'<div class="hd">'
											+ '<div class="hl"></div><div class="hr"></div></div>');

					$('input.autocomplete-off').attr('autocomplete', 'off');
				});
				
function reCountItems(goodsList) {
	goodsList.each(function(index) {
		var items = $(this).find('.item');
		var placeWidth = $(this).innerWidth();
		var itemWidth = items.first().innerWidth();
		var itemsOnRow = Math.round(placeWidth / itemWidth);
		var rows = Math.ceil(items.length / itemsOnRow);
		items.each(function(index) {

			var modulo = index % itemsOnRow;
			var remainingRows = Math.floor((items.length - index - 1)
					/ itemsOnRow);
			$(this).removeClass('last lastRow');
			if (modulo == (itemsOnRow - 1)) {
				$(this).addClass('last');
			}
			if (remainingRows == 0) {
				$(this).addClass('lastRow');
			}

		});
		setHeight(items);
	});

}
function setHeight(items) {
	var maxHeight = 0;
	items.each(function() {
		if (maxHeight < $(this).innerHeight()) {
			maxHeight = $(this).innerHeight();
		}
	});
	items.each(function() {
		$(this).height(maxHeight);
	});
}

