function mycarousel_itemLoadCallback(carousel, state)
{
    // Check if the requested items already exist
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }

    jQuery.get(
        'ajax/imoveis_carrossel.php',
        {
            first: carousel.first,
            last: carousel.last
        },
        function(xml) {
            mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
        },
        'xml'
    );
};

function mycarousel_itemAddCallback(carousel, first, last, xml)
{
    // Set the size of the carousel
    carousel.size(parseInt(jQuery('total', xml).text()));

    var cidade;
	var bairro;
    var tipo;
    var metragem;
    var dormitorios;
    var anuncio;
    var foto;
    var id;
    var imovel;
    var id_cliente;
    var financeiro;

    jQuery("imovel", xml).each(function(i) {
	    cidade = jQuery("l1", this).text();
    	bairro = jQuery("l2", this).text();
	    tipo = jQuery("l3", this).text();
	    metragem = jQuery("l4", this).text();
	    dormitorios = jQuery("l5", this).text();
	    suites = jQuery("l6", this).text();
	    preco = jQuery("l7", this).text();
	    anuncio = jQuery("l8", this).text();
	    foto = jQuery("l9", this).text();
	    id = jQuery("l10", this).text();
	    id_cliente = jQuery("l11", this).text();
	    financeiro = jQuery("l12", this).text();
    	carousel.add(first + i, mycarousel_getItemHTML(cidade, bairro, tipo, metragem, dormitorios, suites, preco, anuncio, foto, id, id_cliente, financeiro));
    });
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(l1,l2,l3,l4,l5,l6,l7,l8,l9,l10, l11, l12) {
	var item_carrossel = '<div class="slides-item">';
    item_carrossel += '<a href="desc-simples.php?id_imovel='+ l10 +'" title="Saiba Mais">';
	item_carrossel += '<img src="inc/image.php?caminho='+ escape(l9) + '&width=240&height=180" alt="Item" title="Foto do Imóvel" style="width: 240px; height: 180px; padding-left: 10px; padding-top: 10px" />'
	item_carrossel += '<h1>' + l1 + ' - ' + l2 + '</h1>';
    item_carrossel += '<ul>';

    if (l11 > 0) {
		item_carrossel += '    <li><label>Refer&ecirc;ncia: </label>' + l11 + '</li>';
	}
    
    if (l5 > 0) {
		item_carrossel += '    <li><label>Dormit&oacute;rios: </label>' + l5; 

		if (l6 > 0) {
			item_carrossel += ' (' + l6;
			if (l6 > 1) {
				item_carrossel += ' suites';
			} else {
				item_carrossel += ' suite';
			}
			item_carrossel += ')';
		}
		
		item_carrossel += '</li>';
	}
	if (l7 > '') {			
    	item_carrossel += '    <li><label>Pre&ccedil;o: </label>R$ ' + l7 + '</li>';
	}
	if (l12 > '') {			
    	item_carrossel += '    <li><div class="financeiro_oferta">' + l12 + '</div></li>';
	}
	item_carrossel += '</ul>';
    item_carrossel += '<img src="img/bt/saiba-mais.jpg" alt="Saiba Mais" title="Saiba Mais" width="86px" height="25px" class="saiba-mais" />';
    item_carrossel += '</a>';
    item_carrossel += '</div>';
	return item_carrossel;
};
