/**
 * @author Pedro Henrique Menez Xudré {pedroxudre ON gmail WITH com}
 */

var ie = /msie 6/i.test(window.navigator.userAgent);

var Destaque = function (dTela, dVelocidade)
{
    var foto, titulo;
    try
    {
        if (/^string$/i.test(typeof(dTela)))
        {
            this.tela = dTela;
            
            this.velocidade = !isNaN(dVelocidade) ? dVelocidade : 3;
        }
        else
        {
            throw new Error('Tipo de tela inválido.');
        }
    }
    catch (e)
    {
        
    }
};

Destaque.prototype =
{
    materias: new Array(),
    tela: new String(),
    materiaAtual: new Number(),
    velocidade: new Number(), // em segundos
    
    __controle: null,
    
    adicionar: function (aTitulo, aFoto, aLink)
    {
        try
        {
            this.materias.push({titulo:aTitulo, foto:aFoto, link:aLink});
        }
        catch (e)
        {
            
        }
    },
    
    anterior: function ()
    {
        this.ir(this.materiaAtual-1);
    },
    
    ir: function (numero)
    {
        var tela, foto, titulo, link, controle;
        
        try
        {
            this.materiaAtual = /^number$/i.test(typeof(numero)) ? numero : 0;
            
            if (this.materiaAtual >= this.materias.length)
            {
                this.materiaAtual = 0;
            }
            else if (this.materiaAtual < 0)
            {
                this.materiaAtual = this.materias.length - 1;
            }
            
            if (ie)
            {
                tela = document.getElementById(this.tela);
                foto = document.getElementById(this.tela + '_foto');
                titulo = document.getElementById(this.tela + '_titulo');
                link = document.getElementById(this.tela + '_link');
                
                controle = document.getElementById(this.tela + '_controle');
                controle = controle.getElementsByTagName('a');
                if (link.href != null)
                {
                    link.href = this.materias[this.materiaAtual].link;
                }
                else
                {
                    link.setAttribute('href', this.materias[this.materiaAtual].link);
                }
                
                var thisDestaque = this;
                foto.onclick = function ()
                {
                    window.location = thisDestaque.materias[thisDestaque.materiaAtual].link;
                };
                
                for (var i=0; i<controle.length; i++)
                {
                    if (i == this.materiaAtual && !/foco/i.test(controle[i].className))
                    {
                        controle[i].className += ' foco';
                    }
                    else if (/foco/i.test(controle[i].className))
                    {
                        controle[i].className = String(controle[i].className).split('foco').join('');
                    }
                }
            }
            else
            {
                tela = $('#' + this.tela);
                foto = $('#' + this.tela + '_foto');
                titulo = $('#' + this.tela + '_titulo');
                link = $('#' + this.tela + '_link');
                controle = $('#' + this.tela + '_controle > a');
                
                link.attr('href', this.materias[this.materiaAtual].link);
                
                for (var i=0; i<controle.get().length; i++)
                {
                    if (i == this.materiaAtual)
                    {
                        $(controle[i]).addClass('foco');
                    }
                    else
                    {
                        $(controle[i]).removeClass('foco');
                    }
                }
            }
            
            var tmpFoto   = this.materias[this.materiaAtual].foto;
            var tmpTitulo = this.materias[this.materiaAtual].titulo;
            
            if (ie)
            {
                titulo.innerHTML = tmpTitulo;
                titulo.style.top = (tela.clientHeight - titulo.offsetHeight) + 'px';
                foto.setAttribute('src', tmpFoto);
            }
            else
            {
                titulo.fadeOut(.5e3, function ()
                {
                    foto.fadeOut(.5e3, function ()
                    {
                        titulo.text(tmpTitulo);
                        titulo.css('top', (tela.innerHeight() - titulo.outerHeight()) + 'px');
                        foto.attr('src', tmpFoto);
                    });
                });
                
                foto.load(function ()
                {
                    foto.fadeIn(1.5e3, function ()
                    {
                        titulo.fadeIn(1e3);
                    });
                });
            }
        }
        catch (e)
        {
            
        }
    },
    
    iniciar: function (numero)
    {
        try
        {
            if (!/^number$/i.test(typeof numero))
            {
                numero = 0;
            }
            else
            {
                
            }
            
            if (this.__controle)
            {
                clearInterval(this.__controle);
            }
            
            var obj = this;
            
            var proxima = function ()
            {
                obj.proxima();
            };
            
            var vel = (this.velocidade + 3) * 1e3;
            
            this.__controle = setInterval(proxima, vel);
            
            this.ir(numero);
        }
        catch (e)
        {
            
        }
    },
    
    proxima: function ()
    {
        this.ir(this.materiaAtual+1);
    }
};
