﻿jQuery.fx.off = false;
AdRotators = {};
AdRotators.PreLoadImages = function(lista)
{
    if (lista.length <= 1) return;
    for (var i = 0; i <= 1; i++)
        AdRotators.PosLoadImages(lista, i);
}
AdRotators.PosLoadImages = function(lista, next)
{
    var cache = [];
    var cacheImage = document.createElement('img');
    cacheImage.src = window.detaqueRotativoHomeCaminhoImagens + '/' + lista[next].UrlArquivo;
    cache.push(cacheImage);

}

AdRotator = function(idImagem, idTitulo, idSubtitulo, idDescricao, idLink, idLink2, lista)
{
    AdRotators[idImagem] = this;
    this.RefreshTime = 20;
    this.idImagem = idImagem;
    this.idTitulo = idTitulo;
    this.idSubtitulo = idSubtitulo;
    this.idDescricao = idDescricao;
    this.idLink = idLink;
    this.idLink2 = idLink2;
    this.lista = lista;
    this.Pos = 0;
    this.Next = 0;
    this.PreLoad = false;
    this.Load = function()
    {
        this.Pos = (this.Pos + 1) % this.lista.length;
        //alert(this.lista[this.Pos].Titulo);
        var item = this.lista[this.Pos];

        //if ((this.idTitulo || '') != '') $Get(this.idTitulo).style.display = 'none';
        //if ((this.idSubtitulo || '') != '') $Get(this.idSubtitulo).style.display = 'none';
        //if ((this.idDescricao || '') != '') $Get(this.idDescricao).style.display = 'none';
        //if ((this.idLink || '') != '') $Get(this.idLink).style.display = 'none';
        if ((this.idTitulo || '') != '')
        {
            $Get(this.idTitulo).innerHTML = item.Titulo;
            $Get(this.idTitulo).style.display = (((item.Titulo || '') != '') ? '' : 'none');
        }
        if ((this.idSubtitulo || '') != '')
        {
            $Get(this.idSubtitulo).innerHTML = item.Subtitulo;
            $Get(this.idSubtitulo).style.display = (((item.Subtitulo || '') != '') ? '' : 'none');
        }
        if ((this.idDescricao || '') != '')
        {
            $Get(this.idDescricao).innerHTML = item.Descricao;
            $Get(this.idDescricao).style.display = (((item.Descricao || '') != '') ? '' : 'none');
        }
        if ((this.idLink || '') != '')
        {
            $Get(this.idLink).href = item.UrlDestaque;
            $Get(this.idLink).style.display = (((item.UrlDestaque || '') != '') ? '' : 'none');
        }
        if ((this.idImagem || '') != '')
        {
            if ($Get(this.idImagem).style.backgroundImage != 'url(' + window.detaqueRotativoHomeCaminhoImagens + '/' + item.UrlArquivo + ')')
            {
                $Get(this.idImagem).style.backgroundImage = '';
                $('#' + this.idImagem).css('backgroundImage', 'url(' + window.detaqueRotativoHomeCaminhoImagens + '/' + item.UrlArquivo + ')')
                //$Get(this.idImagem).style.backgroundImage = 'url(' + window.detaqueRotativoHomeCaminhoImagens + '/' + item.UrlArquivo + ')';
            }
        }
        if ((this.idLink2 || '') != '')
        {
            $Get(this.idLink2).href = item.UrlDestaque;
            $Get(this.idLink2).style.display = (((item.UrlDestaque || '') != '') ? '' : 'none');
        }
        
        if (this.PreLoad) return;
        this.Next = (this.Pos + 1) % this.lista.length;
        AdRotators.PosLoadImages(this.lista, this.Next);
        this.PreLoad = (this.Next == 0);

    }

    this.Timer = setInterval("AdRotators['" + this.idImagem + "'].Load(true);", this.RefreshTime * 1000);
}
