/*  Site Dinâmico 4.0
    www.cebinet.com.br
    silviosa@sswin.com.br
    Outubro/2009
    ====================== */

var SDI4={

// ******************************************
// Modo de execução: c# ou php
// ******************************************

    modo: 'cs',
    callback: null,

// ******************************************
// Métodos público
// ******************************************

    setModoCS: function(){ this.modo='cs'; },
    setModoPHP: function(){ this.modo='php'; },

    gerar: function(ancora, componente, parametros, callback){
        this[ancora] = new SDI4Componente({ "ancora": ancora, "componente": componente });
        if(typeof(parametros)!='object'){ parametros={}; }
        qs=this.obterQueryString();
        for(var p in qs){
            if(p!='pagina'){
                parametros[p]=qs[p];
            }
        }
        this[ancora].gerar(ancora, parametros);
        if(typeof(callback) == 'function'){
            this.callback = callback
        } else {
            this.callback = null;
        }

    },

    obterQueryString: function(){
        var qsElm=window.location.search.substr(1).split('&');
        var qs=new Object();
        for(i=0; i<qsElm.length; i++){ var el=qsElm[i].split('='); qs[el[0]]=el[1]; }
        return qs;
    },

    obterQueryStringParamento: function(p){
        var qs = this.obterQueryString();
        return qs[p];
    },

    popPicture: function(site, origem, pic){

        var script="sdi4/sdi4-recursos/sdi4.img.tamreal.php";
        script+='?nm=' + pic;
        script+='&img=' + site + origem + pic;

        var h=400;
        var w=600;
        var t=(screen.availHeight-h)/2;
        var l=(screen.availWidth-w)/2;
        var props='';
        props+="top=" + t +", ";
        props+="height=" + h + ", ";
        props+="left=" + l + ", ";
        props+="width=" + w + ", ";
        props+="toolbar=no, scrollbars=yes, resizable=yes";
        var popPic = window.open(script,'popPic', props);
        popPic.focus();
    },

    saibaMais: function(ancora, componente, il, ic){

        var tit=$('#'+ancora+"_"+il+"_"+ic+"_INFtit").html();

        var p={
            "ancora": ancora+'SM',
            "componente": componente,
            "titulo": tit,
            "width": $(window).width() * 0.80,
            "height": $(window).height() * 0.80,
            "center": 1
        };
        var sm=new SDI4PopUp(p);

        sm.gerar();

    },

    meses:[ 'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro']

};



// ******************************************
// Objeto: Componente
// ******************************************

function SDI4Componente(props){

// inicialização
    _defaults(this);
    if(props){ _construtor(this, props); };

// propriedades
    function _defaults(o){
        if(SDI4.modo=='cs'){ o.script = 'sdi4/sdi4-engine/gerador.aspx'; }
        else { o.script = 'sdi4/sdi4-engine-php/sdi4-gerador.php'; }
        o.ancora = 'areaDireita';
        o.componente = '(Não Informado)';
    };

// construtor
    function _construtor(o, ps){
        for(var p in ps){
            if(o[p]!=undefined){
                if(typeof(o[p])=='object'){ _construtor(o[p],ps[p]); }
                else { o[p]=ps[p]; }
            }
        }
    };

// métodos
    this.gerar = function(ancora, parametros){
        var t= '<img border="0" src="sdi4/sdi4-recursos/sdi4-loading.gif" />';
        this.mostrar(t);

        pData={ "componente": this.componente };
        for( var prm in parametros ){
            pData[prm]=parametros[prm];
        }

        $.ajax({
            url: this.script,
            data: pData,
            type: "POST",
            dataType: 'html',
            success: function(dados){ SDI4[ancora].mostrar(dados); },
            error: function(r, s, e){
                var m=(s?(s+' '):'')+(e?(e+' '):'')+"\n"+r.responseText;
                SDI4[ancora].mostrar(m);
            }
        });

    };

    this.mostrar = function(info){
        $('#'+this.ancora).html(info);
        if(typeof(SDI4.callback) == 'function'){
            SDI4.callback();
        }
    };

};


// ******************************************
// Objeto: Galeria - Método Tabela
// ******************************************

function SDI4GaleriaTB(props){

// inicialização
    _defaults(this);
    if(props){ _construtor(this, props); };

// propriedades
    function _defaults(o){
        o.id = 0;
        o.ancora = 'ancora';
        o.origem = '';
        o.urlSite = '';
        o.path = '';
        o.reducao = 120;
        o.colunas = 4;
        o.estilo = "sd_gal_tabela";
    };

// construtor
    function _construtor(o, ps){
        for(var p in ps){
            if(o[p]!=undefined){
                if(typeof(o[p])=='object'){ _construtor(o[p],ps[p]); }
                else { o[p]=ps[p]; }
            }
        }
    };

// métodos
    this.gerar = function(arqs){

        // mapeamento
        var qtd=arqs.length;
        var ql = Math.ceil(qtd / this.colunas);
        var ix=0;
        var mapa = new Array(ql)
        for(il=0; il<ql; il++){
            mapa[il]=new Array(this.colunas);
            for(ic=0; ic<this.colunas; ic++){
                if(ix<arqs.length){
                    mapa[il][ic]=arqs[ix];
                } else {
                    mapa[il][ic]="";
                }
                ix++;
            }
        }

        // galeria
        if(this.estilo == ''){ this.estilo = "sd_gal_tabela"; }
        var t = "";
        t+='<table class="' + this.estilo + '">';
        t+="<tbody>"
        for(il=0;il<mapa.length;il++){
            t+='<tr class="' + this.estilo + '">';
            for(ic=0;ic<mapa[il].length;ic++){
                t+='<td id="'+ this.ancora + "_" + il + "_" + ic +'" class="' + this.estilo + '">';
                var pic=mapa[il][ic];
                if(pic==""){
                    t+="&nbsp;";
                } else {
                    var ir = "sdi4/sdi4-recursos/sdi4.img.reduzida.php";
                    ir+="?redu=" + this.reducao;
                    ir+="&img=" + this.path + this.origem + pic;
                    t+='<a href="javascript: SDI4.popPicture(\''+ this.urlSite + '\',\''+ this.origem + '\',\'' + pic + '\')">';
                    t+='<img class="' + this.estilo + '" src="' + ir + '" />';
                    t+='</a>';
                }
                t+="</td>";
            }
            t+="</tr>";
        }
        t+="</tbody>"
        t+="</table>";

        $("#"+this.ancora).html(t);

    };


};


// ******************************************
// Objeto: Galeria - Método Inline
// ******************************************

function SDI4GaleriaIL(props){

// inicialização
    _defaults(this);
    if(props){ _construtor(this, props); };

// propriedades
    function _defaults(o){
        o.id = 0;
        o.ancora = 'ancora';
        o.origem = '';
        o.urlSite = '';
        o.path = '';
        o.reducao = 40;
        o.colunas = 3;
        o.estilo = "sd_gal_inline";
        o.tamImagem = 300;
        o.sepImagem = 3;
        o.tamScrBar = 24;
        o.scriptReduzida = "sdi4/sdi4-recursos/sdi4.img.reduzida.php";
        o.scriptTamReal = "sdi4/sdi4-recursos/sdi4.img.tamreal.php";
    };

// construtor
    function _construtor(o, ps){
        for(var p in ps){
            if(o[p]!=undefined){
                if(typeof(o[p])=='object'){ _construtor(o[p],ps[p]); }
                else { o[p]=ps[p]; }
            }
        }
    };

// métodos
    this.gerar = function(arqs){

        // implementa o layout
        var t="";

        t+='<table class="sd_gal_inline">';
        t+='<tr class="sd_gal_inline">';
        t+='<td class="sd_gal_inline_menu">';

        // menu de imagens
        t+='<div class="sd_gal_inline_menu" id="gl_' + this.id + 'areaMenu">';
        var c=0;
        for(i=0;i<arqs.length;i++){
            pic=arqs[i];
            var ir = this.scriptReduzida;
            ir+="?redu=" + this.reducao;
            ir+="&img=" + this.path + this.origem + pic;
            ir+="&modo=crop";
            var it = this.scriptTamReal;
            t+='<img id="glImg_' + this.id + i + '" class="' + this.estilo + '" src="' + ir + '" />';
            c++;
            if(c>=this.colunas){ t+="<br />"; c=0; }
        }
        t+='</div>';

        t+='</td>';
        t+='<td class="sd_gal_inline_pic" id="gl_' + this.id + 'areaPic">';

        // imagem
        t+='<a href="">';
        t+='<img class="' + this.estilo + '" src="" title="Clique para ampliar" />';
        t+='</a>';

        // finaliza implementação do layout
        t+='</td>';
        t+='</tr>';
        t+='</table>';
        $("#"+this.ancora).html(t);

        // ajusta altura
        var alt=$('#gl_' + this.id + 'areaMenu').height();
        if(alt>this.tamImagem){ $('#gl_' + this.id + 'areaMenu').height(this.tamImagem); }
        var tamMenu = ((this.reducao+this.sepImagem)*this.colunas)+this.tamScrBar;
        $('#gl_' + this.id + 'areaMenu').width(tamMenu);
        $('#gl_' + this.id + 'areaPic').width(this.tamImagem);

        // associa eventos
        for(i=0;i<arqs.length;i++){
            var ip = this.scriptReduzida;
            ip+="?redu=" + this.tamImagem;
            ip+="&img=" + this.path + this.origem + arqs[i];
            ip+="&modo=resize";
            var it = 'javascript: SDI4.popPicture(';
            it+='"' + this.urlSite + '",';
            it+='"' + this.origem + '",';
            it+='"' + arqs[i] + '"';
            it+=')';
            $('#glImg_' + this.id + i).data('ip', ip);
            $('#glImg_' + this.id + i).data('it', it);
            $('#glImg_' + this.id + i).data('id', this.id);
            $('#glImg_' + this.id + i).click(function(){
                $('#gl_' + $(this).data('id') + 'areaPic img').attr('src', $(this).data('ip'));
                $('#gl_' + $(this).data('id') + 'areaPic a').attr('href', $(this).data('it'));
            });
            $('#glImg_' + this.id + 0).click();
        }

    };

};


// ******************************************
// Objeto: Informativos
// ******************************************

function SDI4Info(props){

// inicialização
    _defaults(this);
    if(props){ _construtor(this, props); };

// propriedades
    function _defaults(o){
        if(SDI4.modo=='cs'){ o.script = 'sdi4/sdi4-engine/gerador.aspx'; }
        else { o.script = 'sdi4/sdi4-engine-php/sdi4-gerador.php'; }
        o.id = 0;
        o.ancora = 'ancora';
        o.origem = '';
        o.urlSite = '';
        o.conteudo = '';
        o.estilo ="sdi_info";
        o.modo = 'ultimas';
        o.img_origem = 'sdi4/sdi4-arquivos/';
        o.grupo = {
            informacao_grupo_id: 0,
            nome:'Informações',
            modelo: "padrao",
            colunas: 3,
            titulo_posicao: 'A', // D = dentro; A = acima
            imagem_posicao: 'X',
            saiba_mais_posicao: 'D', // D = dentro; B = abaixo
            tipo_saiba_mais: 'X',
            tipo_mais_informacoes: 'X',
            chamada_saiba_mais: 'pagina.html?pagina=',
            modo_saiba_mais: 'popup'
        };
        o.mostrarBotaoVolta = false;
        o.slider = {
            imgHeight: 9999999,
            imgWidth: 9999999
        }
    };


// construtor
    function _construtor(o, ps){
        for(var p in ps){
            if(o[p]!=undefined){
                if(typeof(o[p])=='object'){ _construtor(o[p],ps[p]); }
                else { o[p]=ps[p]; }
            }
        }
    };

// métodos
    this.gerar = function(obj){
        if(this.modo=='todas'){
            var pData={ acao: 'info', opcao: 'lerMeses', informacao_grupo_id: obj.grupo.informacao_grupo_id };
            $.ajax({
                url: this.script,
                data: pData,
                type: "POST",
                dataType: 'json',
                success: function(dados){ obj.gerarTodas(dados); },
                error: function(r, s, e){
                    var m=(s?(s+' '):'')+(e?(e+' '):'')+"\n"+r.responseText;
                    $('#'+obj.ancora).html('<span class="sd_efeito_vermelho">' + m + '</span>');
                }
            });

        } else {
            var pData={ acao: 'info', opcao: 'lerUltimas', informacao_grupo_id: obj.grupo.informacao_grupo_id };
            $.ajax({
                url: this.script,
                data: pData,
                type: "POST",
                dataType: 'json',
                success: function(dados){ obj.gerarUltimas(dados); },
                error: function(r, s, e){
                    var m=(s?(s+' '):'')+(e?(e+' '):'')+"\n"+r.responseText;
                    $('#'+obj.ancora).html('<span class="sd_efeito_vermelho">' + m + '</span>');
                }
            });
        }

    };

    this.gerarMes = function(){

        var sel=$('#'+this.ancora+'_selMeses').val();
        var obj=SDI4['inf'+this.id];
        this.gerarMesCarregar(obj,sel);

    };

    this.gerarMesCarregar = function(obj, sel){

        var wSel=sel.split('_');
        var pData={
            acao: 'info',
            opcao: 'lerMes',
            informacao_grupo_id: this.grupo.informacao_grupo_id,
            'mes': wSel[1],
            'ano':wSel[0]
        };
        $.ajax({
            url: this.script,
            data: pData,
            type: "POST",
            dataType: 'json',
            success: function(dados){ obj.gerarMesMostrar(dados); },
            error: function(r, s, e){
                var m=(s?(s+' '):'')+(e?(e+' '):'')+"\n"+r.responseText;
                $('#'+obj.ancora).html('<span class="sd_efeito_vermelho">' + m + '</span>');
            }
        });

    };

    this.gerarMesMostrar = function(dados){


        // preparação
        var t="";

        // verificar retorno
        if(dados.rc!='ok'){
            t='<span class="sd_efeito_vermelho">'+dados.msg+'</span>';
            $("#"+this.ancora).html(t);
            return null;
        }

        // não há notícias
        if(dados.rs.length==0){
            t+='<tr>';
            t+='<td class="' + this.estilo + '_todas">';
            t+='Não há informativos registrados neste mês.';
            t+='</td>';
            t+='</tr>';
            $('#'+this.ancora+'_mes_area').empty();
            $('#'+this.ancora+'_mes_area').html(t);
            return null;

        }

        // mapeamento das colunas de informações
        var qtd=dados.rs.length;
        var ql = Math.ceil(qtd / this.grupo.colunas);
        var ix=0;
        var mapa = new Array(ql)
        for(il=0; il<ql; il++){
            mapa[il]=new Array(this.grupo.colunas);
            for(ic=0; ic<this.grupo.colunas; ic++){
                if(ix<dados.rs.length){ mapa[il][ic]=ix; }
                else { mapa[il][ic]=(-1); }
                ix++;
            }
        }

        // apresentação das informações
        t+='<table class="' + this.estilo + '_ultimas">';
        t+="<tbody>"

        // linhas do mapa
        for(il=0;il<mapa.length;il++){
            t+='<tr class="' + this.estilo + '_ultimas">';

            // coluna do mapa
            for(ic=0;ic<mapa[il].length;ic++){
                t+='<td id="'+ this.ancora + "_" + il + "_" + ic +'" class="' + this.estilo + '_ultimas">';
                var info=mapa[il][ic];

                // célula vazia
                if(info==(-1)){
                    t+="&nbsp;";

                // célula com conteúdo
                } else {
                    var reg=dados.rs[info];
                    var img=false;
                    if(reg.imagem){ img=this.img_origem+reg.imagem; }
                    var img_pos=reg.imagem_posicao;
                    if(img_pos=='X'){ img_pos=this.grupo.imagem_posicao; }
                    var lnk="javascript: SDI4.saibaMais('"+this.ancora+"', '"+reg.componente+"', "+il+", "+ic+");";

                    var lnkTexto="";
                    if(this.grupo.tipo_saiba_mais=='V') { lnkTexto="Veja +"; }
                    if(this.grupo.tipo_saiba_mais=='P') { lnkTexto=reg.saiba_mais_info; }
                    if(lnkTexto==""){ lnkTexto="Saiba mais ..."; }

                    t+='<table>';
                    t+='<tbody>';

                    // título acima
                    if(this.grupo.titulo_posicao=="A"){
                        t+='<tr><td class="'+this.estilo+'_ultimas_titulo">';
                        t+='<span id="'+ this.ancora + '_' + il + "_" + ic +'_INFtit">'+reg.titulo+'</span>';
                        t+='</td></tr>';
                    }

                    // corpo do informativo
                    t+='<tr>';
                    t+='<td>';

                        t+='<table>';
                        t+='<tbody>';

                        // imagem acima
                        if(img && img_pos=="A"){
                            t+='<tr><td class="'+this.estilo+'_ultimas_imgacima"><img src="'+img+'" /></td></tr>';
                        }

                        // área do informativo
                        t+='<tr>';

                        // imagem à esquerda
                        if(img && img_pos=="E"){
                            t+='<td class="'+this.estilo+'_ultimas_imgesq"><img src="'+img+'" /></td>';
                        }

                        t+='<td>';

                            // área interna do informativo
                            t+='<table>';
                            t+='<tbody>';

                            // título dentro
                            if(this.grupo.titulo_posicao=="D"){
                                t+='<tr><td class="'+this.estilo+'_ultimas_titulo">';
                                if(this.grupo.tipo_saiba_mais=='T'){ t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">' }
                                t+='<span id="'+ this.ancora + '_' + il + "_" + ic +'_INFtit">'+reg.titulo+'</span>';
                                if(this.grupo.tipo_saiba_mais=='T'){ t+='</a>' }

                                t+='</td></tr>';
                            }

                            // resumo
                            t+='<tr>';
                            t+='<td class="'+this.estilo+'_ultimas_resumo">';
                            // imagem junto
                            if(img && img_pos=="J"){
                                t+='<img src="'+img+'" class="'+this.estilo+'_ultimas_imgjunto" />';
                            }
                            // texto
                            if(this.grupo.tipo_saiba_mais=='R'){ t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">' }
                            t+=reg.resumo;
                            if(this.grupo.tipo_saiba_mais=='R'){ t+='</a>' }
                            t+='</td>';
                            t+='</tr>';

                            // link dentro
                            if(this.grupo.saiba_mais_posicao=='D' &&
                               reg.componente_id!='' &&
                               this.grupo.tipo_saiba_mais!='R' &&
                               this.grupo.tipo_saiba_mais!='T' &&
                               this.grupo.tipo_saiba_mais!='X'){
                                t+='<tr><td class="'+this.estilo+'_ultimas_lnk_veja">';
                                t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">'+lnkTexto+'</a>';
                                t+='</td></tr>';
                            }

                            t+='</tbody>';
                            t+='</table>';

                            t+='</td>';

                        // imagem à direita
                        if(img && img_pos=="D"){
                            t+='<td><img src="'+img+'" /></td>';
                        }

                        t+='</tr>';

                        // imagem abaixo
                        if(img && img_pos=="B"){
                            t+='<tr><td class="'+this.estilo+'_ultimas_imgabaixo"><img src="'+img+'" /></td>';
                        }

                        t+='</tr>';
                        t+='</tbody>';
                        t+='</table>';

                    t+='</td>';
                    t+='</tr>';

                    // link abaixo
                    if(this.grupo.saiba_mais_posicao=='B' &&
                       reg.componente_id &&
                       this.grupo.tipo_saiba_mais!='R' &&
                       this.grupo.tipo_saiba_mais!='T' &&
                       this.grupo.tipo_saiba_mais!='X'){

                        t+='<tr><td class="'+this.estilo+'_ultimas_lnk_veja">';
                        t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">'+lnkTexto+'</a>';
                        t+='</td></tr>';
                    }

                    t+='</tbody>';
                    t+='</table>';
                }
                t+="</td>";
            }
            t+="</tr>";
        }

        t+="</tbody>";
        t+="</table>";

        // área do saiba mais
		t+="<div id=\"" + this.ancora + "SM\" style=\"display:none;\">";
        t+="<div id=\"" + this.ancora + "SMTitulo\">titulo</div>";
        t+="<div id=\"" + this.ancora + "SMConteudo\">conteudo</div>";
        t+="</div>";
        t+="<div>";

        // apresenta
        $('#'+this.ancora+'_mes_area').empty();
        $('#'+this.ancora+'_mes_area').html(t);

    };

    this.gerarTodas = function(dados){

        // meses
        var estemes=new Date();
        var mespassado=new Date(estemes.getFullYear(), estemes.getMonth()-1, estemes.getDate());

        var meses=new Array();
        meses[0]={ "ano": estemes.getFullYear(), "mes": estemes.getMonth()+1, "nome": "Este mês" };
        meses[1]={ "ano": mespassado.getFullYear(), "mes": mespassado.getMonth()+1, "nome": "Mês passado" };
        for(i=0;i<dados.rs.length;i++){
            var ok=true;
            var reg=dados.rs[i];
            if(parseInt(reg.ano)==estemes.getFullYear() && parseInt(reg.mes)==(estemes.getMonth()+1)){ ok=false; }
            if(parseInt(reg.ano)==mespassado.getFullYear() && parseInt(reg.mes)==(mespassado.getMonth()+1)){ ok=false; }
            if(ok){
                var m=SDI4.meses[parseInt(reg.mes)-1];
                m+='/'+reg.ano;

                meses[meses.length]={ "ano": parseInt(reg.ano), "mes": parseInt(reg.mes), "nome": m };
            }
        }

        var lnk='javascript:';
        lnk+="SDI4.inf"+this.id+".gerarMes()";

        var mesesSelect='';
        mesesSelect+='<select id="'+this.ancora+'_selMeses" onchange="'+lnk+'">';
        for(i=0;i<meses.length;i++){
            mesesSelect+='<option value="'+meses[i].ano+'_'+meses[i].mes+'">'+meses[i].nome+'</option>';
        }
        mesesSelect+='</select>';

        // link da volta
        var rot='SDI4.inf'+this.id;
        var lnk='javascript:';
        lnk+=rot+'.modo=\'ultimas\';';
        lnk+=rot+'.gerar('+rot+');'

        // preparação
        var t="";
        t+='<table class="' + this.estilo + '_todas">';

        t+='<thead>';
        t+='<tr>';
        t+='<td class="' + this.estilo + '_todas_titulo">';
        t+=this.grupo.nome+'&nbsp;';
        t+=mesesSelect;
        t+='</td>';
        t+='</tr>';
        t+='</thead>';

        t+='<tbody id="'+this.ancora+'_mes_area"></tbody>';

        if(this.mostrarBotaoVolta){
            t+='<tfoot>';
            t+='<tr>';
            t+='<td class="'+this.estilo+'_todas_lnk_ultimas">';
            t+='<a class="'+this.estilo+'_todas_lnk_ultimas" href="'+lnk+'">Volta</a>';
            t+='</td>';
            t+='</tr>';
            t+='</tfoot>';
        }
        t+='</table>';

        // apresentação
        $("#"+this.ancora).empty();
        $("#"+this.ancora).html(t);
        $("#"+this.ancora).removeClass(this.estilo+'_ultimas');
        $("#"+this.ancora).addClass(this.estilo+'_todas');

        this.gerarMes();

        $.scrollTo('#'+this.ancora, 500, { offset:{ top:-30 } });

    };

    this.gerarUltimas = function(dados){

        // verificar retorno
        if(dados.rc!='ok'){
            t='<span class="sd_efeito_vermelho">'+dados.msg+'</span>';
            $("#"+this.ancora).html(t);
            return null;
        }

        // escolhe modelo
        if(this.grupo.modelo == 'slideshow'){
            this.gerarUltimasSlideShow(dados);
        } else {
            this.gerarUltimasPadrao(dados);
        }
    }

    this.gerarUltimasPadrao = function(dados){

        // preparação
        var t="";

        // mapeamento das colunas de informações
        var qtd=dados.rs.length;
        var ql = Math.ceil(qtd / this.grupo.colunas);
        var ix=0;
        var mapa = new Array(ql)
        for(il=0; il<ql; il++){
            mapa[il]=new Array(this.grupo.colunas);
            for(ic=0; ic<this.grupo.colunas; ic++){
                if(ix<dados.rs.length){ mapa[il][ic]=ix; }
                else { mapa[il][ic]=(-1); }
                ix++;
            }
        }

        // apresentação das informações
        t+='<table class="' + this.estilo + '_ultimas">';
        t+="<tbody>"

        // linhas do mapa
        for(il=0;il<mapa.length;il++){
            t+='<tr class="' + this.estilo + '_ultimas">';

            // coluna do mapa
            for(ic=0;ic<mapa[il].length;ic++){

                t+='<td id="'+ this.ancora + "_" + il + "_" + ic +'" class="' + this.estilo + '_ultimas">';
                var info=mapa[il][ic];

                // célula vazia
                if(info==(-1)){
                    t+="&nbsp;";

                // célula com conteúdo
                } else {
                    var reg=dados.rs[info];
                    var img=false;
                    if(reg.imagem){ img=this.img_origem+reg.imagem; }

                    var img_pos=reg.imagem_posicao;
                    if(img_pos=='X'){ img_pos=this.grupo.imagem_posicao; }
                    if(this.grupo.modo_saiba_mais == 'popup'){
                    	var lnk="javascript: SDI4.saibaMais('"+this.ancora+"', '"+reg.componente+"', "+il+", "+ic+");";
                    } else {
                        var lnk = this.grupo.chamada_saiba_mais + reg.componente;
                    }
                    var lnkTexto="";
                    if(this.grupo.tipo_saiba_mais=='V') { lnkTexto="Veja +"; }
                    if(this.grupo.tipo_saiba_mais=='P') { lnkTexto=reg.saiba_mais_info; }
                    if(lnkTexto==""){ lnkTexto="Saiba mais ..."; }

                    t+='<table>';
                    t+='<tbody>';

                    // título acima
                    if(this.grupo.titulo_posicao=="A"){
                        t+='<tr><td class="'+this.estilo+'_ultimas_titulo">';
                        if(this.grupo.tipo_saiba_mais=='T' && reg.componente_id!=''){ t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">' }
                        t+='<span id="'+ this.ancora + '_' + il + "_" + ic +'_INFtit">'+reg.titulo+'</span>';
                        if(this.grupo.tipo_saiba_mais=='T' && reg.componente_id!=''){ t+='</a>' }
                        t+='</td></tr>';
                    }

                    // corpo do informativo
                    t+='<tr>';
                    t+='<td>';

                        t+='<table>';
                        t+='<tbody>';

                        // imagem acima
                        if(img && img_pos=="A"){
                            t+='<tr><td class="'+this.estilo+'_ultimas_imgacima">';
                            if(reg.componente_id!=''){ t+='<a href="'+lnk+'">' }
                            t+='<img src="'+img+'" />';
                            if(reg.componente_id!=''){ t+='</a>' }
                            t+='</td></tr>';
                        }

                        // área do informativo
                        t+='<tr>';

                        // imagem à esquerda
                        if(img && img_pos=="E"){
                            t+='<td class="'+this.estilo+'_ultimas_imgesq">';
                            if(reg.componente_id!=''){ t+='<a href="'+lnk+'">' }
                            t+='<img src="'+img+'" />';
                            if(reg.componente_id!=''){ t+='</a>' }
                            t+='</td>';
                        }

                        t+='<td>';

                            // área interna do informativo
                            t+='<table>';
                            t+='<tbody>';

                            // título dentro
                            if(this.grupo.titulo_posicao=="D"){
                                t+='<tr><td class="'+this.estilo+'_ultimas_titulo">';
                                if(this.grupo.tipo_saiba_mais=='T'){ t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">' }
                                t+='<span id="'+ this.ancora + '_' + il + "_" + ic +'_INFtit">'+reg.titulo+'</span>';
                                if(this.grupo.tipo_saiba_mais=='T'){ t+='</a>' }

                                t+='</td></tr>';
                            }

                            // resumo
                            t+='<tr>';
                            if(this.grupo.titulo_posicao=="L"){
                                t+='<td class="'+this.estilo+'_ultimas_titulo">';
                                if(this.grupo.tipo_saiba_mais=='T'){ t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">' }
                                t+='<span id="'+ this.ancora + '_' + il + "_" + ic +'_INFtit">'+reg.titulo+'</span>';
                                if(this.grupo.tipo_saiba_mais=='T'){ t+='</a>' }
                                t+='</td>';
                            }
                            t+='<td class="'+this.estilo+'_ultimas_resumo">';
                            // imagem junto
                            if(img && img_pos=="J"){
                                if(reg.componente_id!=''){ t+='<a href="'+lnk+'">' }
                                t+='<img src="'+img+'" class="'+this.estilo+'_ultimas_imgjunto" />';
                                if(reg.componente_id!=''){ t+='</a>' }
                            }
                            // texto
                            if(this.grupo.tipo_saiba_mais=='R'  && reg.componente_id!=''){ t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">' }
                            t+=reg.resumo;
                            if(this.grupo.tipo_saiba_mais=='R' && reg.componente_id!=''){ t+='</a>' }
                            t+='</td>';
                            t+='</tr>';

                            // link dentro
                            if(this.grupo.saiba_mais_posicao=='D' &&
                               reg.componente_id!='' &&
                               this.grupo.tipo_saiba_mais!='R' &&
                               this.grupo.tipo_saiba_mais!='T' &&
                               this.grupo.tipo_saiba_mais!='X'){
                                t+='<tr><td class="'+this.estilo+'_ultimas_lnk_veja">';
                                t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">'+lnkTexto+'</a>';
                                t+='</td></tr>';
                            }

                            t+='</tbody>';
                            t+='</table>';

                            t+='</td>';

                        // imagem à direita
                        if(img && img_pos=="D"){
                            t+='<td>';
                            if(reg.componente_id!=''){ t+='<a href="'+lnk+'">' }
                            t+='<img src="'+img+'" />';
                            if(reg.componente_id!=''){ t+='</a>' }
                            t+='</td>';
                        }

                        t+='</tr>';

                        // imagem abaixo
                        if(img && img_pos=="B"){
                            t+='<tr><td class="'+this.estilo+'_ultimas_imgabaixo">';
                            if(reg.componente_id!=''){ t+='<a href="'+lnk+'">' }
                            t+='<img src="'+img+'" />';
                            if(reg.componente_id!=''){ t+='</a>' }
                            t+='</td>';
                        }

                        t+='</tr>';
                        t+='</tbody>';
                        t+='</table>';

                    t+='</td>';
                    t+='</tr>';

                    // link abaixo
                    if(this.grupo.saiba_mais_posicao=='B' &&
                       reg.componente_id &&
                       this.grupo.tipo_saiba_mais!='R' &&
                       this.grupo.tipo_saiba_mais!='T' &&
                       this.grupo.tipo_saiba_mais!='X'){

                        t+='<tr><td class="'+this.estilo+'_ultimas_lnk_veja">';
                        t+='<a class="'+this.estilo+'_ultimas_lnk_veja" href="'+lnk+'">'+lnkTexto+'</a>';
                        t+='</td></tr>';
                    }

                    t+='</tbody>';
                    t+='</table>';
                }
                t+="</td>";
            }
            t+="</tr>";
        }

        t+="</tbody>";

        // link para todas as informações
        var lnkNome='';
        if(this.grupo.tipo_mais_informacoes=="O"){
            lnkNome+='Outros '+this.grupo.nome;
        } else if(this.grupo.tipo_mais_informacoes=="A"){
            lnkNome+='Outras '+this.grupo.nome;
        } else {
            lnkNome+='Mais '+this.grupo.nome;
        }
        var rot='SDI4.inf'+this.id;
        var lnk='javascript:';
        lnk+=rot+'.modo=\'todas\';';
        lnk+=rot+'.mostrarBotaoVolta=true;';
        lnk+=rot+'.gerar('+rot+');'

        if(this.grupo.tipo_mais_informacoes!="X"){
            t+='<tfoot>';
            t+='<tr>';
            t+='<td class="'+this.estilo+'_ultimas_lnk_todas" colspan="'+this.grupo.colunas+'">';
            t+='<a class="'+this.estilo+'_ultimas_lnk_todas" href="'+lnk+'">'+lnkNome+'</a>';
            t+='</td>';
            t+='</tr>';
            t+='</tfoot>';
        }

        t+="</table>";

        // área do saiba mais
		t+="<div id=\"" + this.ancora + "SM\" style=\"display:none;\">";
        t+="<div id=\"" + this.ancora + "SMTitulo\">titulo</div>";
        t+="<div id=\"" + this.ancora + "SMConteudo\">conteudo</div>";
        t+="</div>";
        t+="<div>";


        // apresentação
        $("#"+this.ancora).empty();
        $("#"+this.ancora).html(t);
        $("#"+this.ancora).removeClass(this.estilo+'_todas');
        $("#"+this.ancora).addClass(this.estilo+'_ultimas');

    };

    this.gerarUltimasSlideShow = function(dados){

        // preparação
        var t = '';

        for( var i = 0; i < dados.rs.length; i++){
            var r = dados.rs[i];
            var lnk = '#';
            var target = '';
            if(r.resumo != ''){
//                lnk = this.grupo.chamada_saiba_mais + r.componente;
                lnk = r.resumo;
                target = ' target="_blank"';
            }
            t += '<a href="' + lnk + '"' + target + '">';
            t += '<img id="' + this.ancora + 'sdiinfoslider' + i + '" src="' + this.img_origem + r.imagem + '" />';
            t += '<span><span style="font-weight:bold;">' + r.titulo + '</span><br />' + r.resumo + '</span>';
            t += '</a>';

        }

        // apresentação
        $("#"+this.ancora).empty();
        $("#"+this.ancora).html(t);

        // configurar slider
        this.slider.imgWidth = 9999999;
        this.slider.imgHeight = 9999999;

//        for( var i = 0; i < dados.rs.length; i++){
//            var w = $('#sdiinfoslider' + i).width();
//            if(this.slider.imgWidth > w){
//                this.slider.imgWidth = w;
//            }
//            var h = $('#sdiinfoslider' + i).height();
//            if(this.slider.imgHeight > h){
//                this.slider.imgHeight = h;
//            }
//        }


        
        if(this.grupo.nome == 'Banner 1'){

            $("#"+this.ancora).coinslider({
                width:  723,
                height: 144,
                delay: 15000,
                opacity: 0.3
            });

        } else if(this.grupo.nome == 'Banner 2'){
 
            $("#"+this.ancora).coinslider({
                width:  807,
                height: 134,
                delay: 13000,
                opaciy: 0.3,
                effect: 'swirl'
            });
        
        } else {

            $("#"+this.ancora).coinslider({
                width:  810,
                height: 160,
                delay: 6000 + (Math.floor(Math.random()*5) + 1) * 1000
            });
        
        }

    }
};

// ******************************************
// Objeto: PopUp
// ******************************************

function SDI4PopUp(props){

// inicialização
    _defaults(this);
    if(props){ _construtor(this, props); };

// propriedades
    function _defaults(o){
        o.id = 0;
        o.ancora = 'ancora';
        o.origem = '';
        o.urlSite = '';
        o.titulo = '';
        o.componente = '';
        o.width = '400';
        o.height = '300';
        o.center = '1'; // 1 = centralizer; 0 = usar top e left
        o.top = '';
        o.left = '';
        o.resize = '1';
        o.scroll = '1';

    };


// construtor
    function _construtor(o, ps){
        for(var p in ps){
            if(o[p]!=undefined){
                if(typeof(o[p])=='object'){ _construtor(o[p],ps[p]); }
                else { o[p]=ps[p]; }
            }
        }
    };

// métodos
    this.gerar = function(){

        // calcula o posicionamento
        var top=this.top; if(!top){ top='0'; }
        var left=this.left; if(!left){ left='0'; }

        if(this.center=='1'){

            var wlarg=$(window).width();
            var walt=$(window).height();
            top=((walt-this.height)/2) + 0;
            left=(wlarg-this.width)/2;
        }

        // obtém conteudo do popup
//        $("#"+this.ancora).hide();
        SDI4.gerar(this.ancora + 'Conteudo', this.componente);

        // preparação
        var t="";

        // barra de titulo
        t+='<div class="sd_pp_titulo">' + this.titulo + '</div>';
        t+='<div id="pp' + this.ancora + 'Close" class="sd_pp_close ui-state-default">';
        t+='<span class="ui-icon ui-icon-closethick"></span>'
        t+='</div>';


        // cria o popup
        $("#"+this.ancora+'Conteudo')
            .css('overflow','auto')
            .css('height', (this.height-32)+'px')
            .css('clear', 'both')
            .css('padding', '0px 10px');
        $("#"+this.ancora+"Titulo")
            .html(t)
            .addClass('sd_pp_titarea');

        // configura ações
        $('#pp' + this.ancora + 'Close')
            .data('ancora', this.ancora)
            .click(function(){ $("#"+$(this).data('ancora')).hide(); })
            .hover(function(){ $(this).addClass('ui-state-hover')},
                   function(){$(this).removeClass('ui-state-hover')});

        // configura o popup
        $("#"+this.ancora)
            .addClass("ui-widget-content")
            .css('position', 'fixed')
            .css('z-index', '9999')
            .css('top', top+'px')
            .css('left', left+'px')
            .css('width', this.width + 'px')
            .css('height', this.height + 'px');

        // apresenta o popup
        $("#"+this.ancora)
            .draggable({
                opacity: 0.75,
                handle: '.sd_pp_titarea'
            })
            .resizable({
                resize: function(event,ui){
                    var h=ui.size.height-32;
                    var id=$(this).attr('id');
                    $('#'+id+'Conteudo').height(h);
                }
            })
            .show();

    };

};

