/**************************************************************
*
* CLASSE JAVASCRIPT RANDIMO
*
* CRIADO POR: CRISTIANO TAVELLA 
* DATA: 10/07/2010
* OBS:
* EDITADO POR: CRISTIANO TAVELLA
* DATA: 10/07/2010
* OBS:
*
*************************************************************/
function set(posicao, campo, value) {    
	eval("this."+campo+"["+posicao+"] = '"+value+"';");
} 

function get(posicao, campo){ 
	eval("var var_return =  this."+campo+"["+posicao+"];");
	return var_return;
}

function imovelClick(randimo) {
    var evtClick = randimo.evtClick;
    var fkempresa = randimo.fkempresa;

    $("." + randimo.id + "box_randimo").click(function () {
        eval(evtClick.replace(/\[pkimovel\]/g, $(this).attr('pkimovel')).replace(/\[fkempresa\]/g, fkempresa));
    });
}


function mudaRandimo(randimo) {
    var total = randimo.destaques + randimo.destaque_atual;

    for (var x = randimo.destaque_atual, y = 0; x < total; x++, y++) {
        $("#" + randimo.id + "pk_imovel_" + y).attr('pkimovel', randimo.pkimovel[x]);
        $("#" + randimo.id + "inf_referencia_" + y).html(randimo.inf_referencia[x]);
        $("#" + randimo.id + "inf_tipo_" + y).html(randimo.inf_tipo[x]);
        $("#" + randimo.id + "inf_bairro_" + y).html(randimo.inf_bairro[x]);

        var detalhes = "";
        if ($.trim(randimo.det_dormitorios[x]) != "")
            detalhes += detalhes == "" ? randimo.det_dormitorios[x] : " | " + randimo.det_dormitorios[x];
        if ($.trim(randimo.det_garagens[x]) != "")
            detalhes += detalhes == "" ? randimo.det_garagens[x] : " | " + randimo.det_garagens[x];
        if ($.trim(randimo.det_banheiros[x]) != "")
            detalhes += detalhes == "" ? randimo.det_banheiros[x] : " | " + randimo.det_banheiros[x];


        $("#" + randimo.id + "inf_detalhes_" + y).html(detalhes);

        $("#" + randimo.id + "inf_cidade_" + y).html(randimo.inf_cidade[x]);
        $("#" + randimo.id + "det_dormitorios_" + y).html(randimo.det_dormitorios[x]);
        $("#" + randimo.id + "det_suite_" + y).html(randimo.det_suite[x]);
        $("#" + randimo.id + "inf_valvenda_" + y).html(randimo.inf_valvenda[x]);
        $("#" + randimo.id + "inf_vallocacao_" + y).html(randimo.inf_vallocacao[x]);
        $("#" + randimo.id + "foto_" + y).attr("src", "http://fotos.centrina.com.br/" + randimo.fkempresa + "/mini_" + randimo.foto[x]);

        randimo.destaque_atual++;
    }

    if (randimo.destaque_atual >= randimo.destaques * (randimo.troca + 1))
        randimo.destaque_atual = 0;

    clearTimeout(timerTimeout);
    timerTimeout = setTimeout(this.id + 'randimo.print(' + this.id + 'randimo);', 1000 * this.tempo);
}

function voltaRandimo(randimo) {    
    randimo.destaque_atual = randimo.destaque_atual-5;

    if (randimo.destaque_atual < 0)
        randimo.destaque_atual = (this.destaques * (this.troca + 1)) - 1    


    for (var x = randimo.destaque_atual, y = 3; y >= 0; x--, y--) {
        $("#" + randimo.id + "pk_imovel_" + y).attr('pkimovel', randimo.pkimovel[x]);
        $("#" + randimo.id + "inf_referencia_" + y).html(randimo.inf_referencia[x]);
        $("#" + randimo.id + "inf_tipo_" + y).html(randimo.inf_tipo[x]);
        $("#" + randimo.id + "inf_bairro_" + y).html(randimo.inf_bairro[x]);

        var detalhes = "";
        if ($.trim(randimo.det_dormitorios[x]) != "")
            detalhes += detalhes == "" ? randimo.det_dormitorios[x] : " | " + randimo.det_dormitorios[x];
        if ($.trim(randimo.det_garagens[x]) != "")
            detalhes += detalhes == "" ? randimo.det_garagens[x] : " | " + randimo.det_garagens[x];
        if ($.trim(randimo.det_banheiros[x]) != "")
            detalhes += detalhes == "" ? randimo.det_banheiros[x] : " | " + randimo.det_banheiros[x];


        $("#" + randimo.id + "inf_detalhes_" + y).html(detalhes);

        $("#" + randimo.id + "inf_cidade_" + y).html(randimo.inf_cidade[x]);
        $("#" + randimo.id + "det_dormitorios_" + y).html(randimo.det_dormitorios[x]);
        $("#" + randimo.id + "det_suite_" + y).html(randimo.det_suite[x]);
        $("#" + randimo.id + "inf_valvenda_" + y).html(randimo.inf_valvenda[x]);
        $("#" + randimo.id + "inf_vallocacao_" + y).html(randimo.inf_vallocacao[x]);
        $("#" + randimo.id + "foto_" + y).attr("src", "http://fotos.centrina.com.br/" + randimo.fkempresa + "/mini_" + randimo.foto[x]);

        randimo.destaque_atual--;
    }
    randimo.destaque_atual = randimo.destaque_atual + 5;

    clearTimeout(timerTimeout);
    timerTimeout = setTimeout(this.id + 'randimo.print(' + this.id + 'randimo);', 1000 * this.tempo);
}

var timerTimeout;

function print(randimo)
{
	if(this.destaque_atual == -1)
	{
		for(var x=0; x<this.destaques; x++)
		{
			$(this.destino).append(this.layout.replace(/\[x\]/g,x));
		}

        this.destaque_atual = 0;        
	}
	
	var total = this.destaques+this.destaque_atual;

	for (var x = this.destaque_atual, y = 0; x < total; x++, y++) {
	    $("#" + this.id + "pk_imovel_" + y).attr('pkimovel',this.pkimovel[x]);
	    $("#" + this.id + "inf_referencia_" + y).html(this.inf_referencia[x]);
		$("#"+this.id+"inf_tipo_"+y).html(this.inf_tipo[x]);
		$("#" + this.id + "inf_bairro_" + y).html(this.inf_bairro[x]);

		var detalhes = "";
		if ($.trim(this.det_dormitorios[x]) != "")
		    detalhes += detalhes == "" ? this.det_dormitorios[x] : " | " + this.det_dormitorios[x];
		if ($.trim(this.det_garagens[x]) != "")
		    detalhes += detalhes == "" ? this.det_garagens[x] : " | " + this.det_garagens[x];
		if ($.trim(this.det_banheiros[x]) != "")
		    detalhes += detalhes == "" ? this.det_banheiros[x] : " | " + this.det_banheiros[x];


		$("#" + this.id + "inf_detalhes_" + y).html(detalhes);

		$("#" + this.id + "inf_cidade_" + y).html(this.inf_cidade[x]);
		$("#" + this.id + "det_dormitorios_" + y).html(this.det_dormitorios[x]);
		$("#" + this.id + "det_suite_" + y).html(this.det_suite[x]);
		$("#" + this.id + "inf_valvenda_" + y).html(this.inf_valvenda[x]);
		$("#" + this.id + "inf_vallocacao_" + y).html(this.inf_vallocacao[x]);
		$("#"+this.id+"foto_"+y).attr("src","http://fotos.centrina.com.br/"+this.fkempresa+"/mini_"+this.foto[x]);
		
		this.destaque_atual++;
	}
	
	if(this.destaque_atual>=this.destaques*(this.troca+1))
		this.destaque_atual = 0;

	randimo.imovelClick(randimo);
	

	timerTimeout = setTimeout(this.id + 'randimo.print(' + this.id + 'randimo);', 1000 * this.tempo);
}

function Randimo(fkempresa, destaques, troca, tempo, destino, layout, evtClick, id) { 
    /*
		Campos de configuração do Randimo
	*/	
	//Código da empresa
	this.fkempresa = fkempresa; 
	//Número de destaques
	this.destaques = destaques;
	//Número de vezes que os destaques são trocados
	this.troca = troca;
	//Tempo em segundo que os destaques são trocados
	this.tempo = tempo;
	//destino no qual será imprimido o randimo
	this.destino = destino;
	//Id opcional antes dos nomes dos campos
	this.id = id;
	//destaques atuais
	this.destaque_atual = -1;
	// Envento click do randimo
	this.evtClick = evtClick;

	/*
		Métodos impressão do Randimo
	*/
	//layout do randimo
	this.layout = layout;
	this.print = print;
	this.imovelClick = imovelClick;
	this.mudaRandimo = mudaRandimo;
	this.voltaRandimo = voltaRandimo;
	
	/*
		Métodos GET e SET da classe
	*/
	this.set = set; 
	this.get = get;
	
	/*
		Campos da tabela
	*/
	this.pkimovel = new Array();
	this.inf_referencia = new Array();
	this.inf_tipo = new Array();
	this.inf_bairro = new Array();
	this.inf_cidade = new Array();
	this.det_dormitorios = new Array();
	this.det_garagens = new Array();
	this.det_banheiros= new Array();
	this.det_suite = new Array();
	this.inf_valvenda = new Array();
	this.inf_vallocacao = new Array();
	this.foto =  new Array();
	
}

