store = {
    setupLayout: function() {
	setTimeout(function() {
	    wHeight = $(window).height();
	    $('#container').css("min-height", wHeight+'px');
	}, 200);
    },

    productImages: function() {
	$('.imgatualprod').fancybox();

	$('#listaimgs > li > a:first').addClass('selecionado');

	$('#listaimgs > li > a').click(function() {
	    var elm = $(this);
	    var id = elm.attr('rel');
	    if(!elm.hasClass('selecionado')) {
		$('#listaimgs > li > a[class="selecionado"]').removeClass('selecionado');
		elm.addClass('selecionado');
		$('.imgatualprod').hide();
		$('#img-'+id).css('display', 'block');
	    }
	});
    },

    cartSubmit: function() {
	$('#btcomprar').click(function() {
	    $('#formcarrinho').submit();
	});
    },

    adjustRows: function() {
	var productsList = $('#produtoslista li.produtocont');
	var prevTop = 0;
	var currentRow = -1;
	var rows = [];
	var newTop;
	var elmSize;

	productsList.each(function(index, element) {
	    newTop = $(element).offset().top;

	    if((newTop != prevTop) || (index == 0)) {
		currentRow++;
		rows[currentRow] = {maxHeight:0, elements:[]};
	    }

	    elmSize = $(element).height();

	    rows[currentRow].maxHeight = (rows[currentRow].maxHeight < elmSize) ? elmSize : rows[currentRow].maxHeight;
	    rows[currentRow].elements.push($(element));

	    prevTop = newTop;
	});

	$.each(rows, function(index, obj) {
	    $.each(obj.elements, function(i, elm) {
		elm.height(obj.maxHeight);
	    });
	});
    }
}

$(document).ready(function() {
    store.setupLayout();
    store.productImages();
    store.cartSubmit();
    store.adjustRows();
});
