var img_count = 0;
var actual = 0;
$(window).load(function(){
	imgs = $('#offer-images img');
	img_count = imgs.length;
	if (img_count==0) {
		$("#offer-images").html('');
		return;
	}
	imgs.css('margin-top', '0px');
	repairHeight($(imgs.get(0)));
	$('#offer-images')
		.createAppend(
			'div', {id : 'controls'},
				[
					'span', {id : 'counter'}, '[1/' + img_count + ']',
					'a', {id : 'next', href : '#'}, 'siguiente &raquo;'				]
		);
	if (img_count==1) {
		$('#offer-images a#next').remove();
	} else {
		$('#offer-images a#next').click(function(evt) {
			evt.preventDefault();
			$($('#offer-images img').get(actual)).css('display', 'none');
			actual = (actual + 1)%img_count;
			$('span', $(this).parent()).html('[' + (actual + 1) + '/' + img_count + ']');
			repairHeight($($('#offer-images img').get(actual)))
		});
	}
});

function repairHeight(img) {
	img_height = 157 - img.height();
	if (img_height<0) img_height = 0;
	img.css('display', 'block')
		.css('margin-top', img_height/2);
}
