/** 
 * @projectDescription Painel de chamadas rotativo com botões para navegação
 *
 * @author  Roberto Robson (Bode) rnogueira@uolinc.com
 * @version 0.1.beta
 * @since   0.1.beta
 * 
 */
var UOLLib = window.UOLLib || {}

UOLLib.rotativoDeChamadas = {
  
  painel : null,
  itens : null,
  botoes : null,
  botoesTxts : {
    next : 'proximo',
    prev : 'anterior',
    stop : 'parar',
    play : 'executar'
  },
  ativado : 0,
  anterior : 0,
  proximo : 0,
  navMSIE : !!navigator.userAgent.match(/MSIE/g),
  timeOut : null,
  timer : 6,
  animando : false,
  
  init : function(args){
    this.painel = document.getElementById(args['id']);
    this.timer = args['timer'] || this.timer;
    this.botoesTxts = args['botoesTxts'] || this.botoesTxts;
    
    this.itens = this.painel.getElementsByTagName('li');
    this.setNavegacao(this.ativado);

    this.aplicarSombras();
    
    if (this.itens.length == 1)
      args['startOff'] = true;
    else
      this.montarBotoes();

    if(!args['startOff'])
      this.stopPlay();
    else
      this.setBtStopPlay('play');

  },
  
  events : {

    /**
     * Retorna false para os eventos padroes
     * @param {Object} e Evento
     */
    preventDefault : function(e){
      if(e.preventDefault) e.preventDefault();
      else e.returnValue = false; 
    }, 
    
    /**
     * Adicionar eventos
     * @param {Object} elem Elemento HTML
     * @param {String} event Evento (click, mouseover, mouseout, ...)
     * @param {Function} fun Função adicionada ao evento
     */
    add : function(elem,event,fun){
      if(document.attachEvent)
        elem.attachEvent('on'+event,fun);
      else if(document.addEventListener)
        elem.addEventListener(event,fun,true);
    }
  
  },
  
  stopPlay : function(replace){
    if ((!replace && this.timeOut == null) || (replace && this.timeOut != null)) {
      clearInterval(this.timeOut);
      this.timeOut = setInterval(function(){
        UOLLib.rotativoDeChamadas.navegar(1);
      }, UOLLib.rotativoDeChamadas.timer * 1000);
      this.setBtStopPlay('stop');
    } else {
      clearInterval(this.timeOut);
      this.timeOut = null;
      this.setBtStopPlay('play');
    }
  },
  
  setBtStopPlay : function(status){
    if (this.botoes != null) {
      var bt = this.botoes.getElementsByTagName('a')[1];
      bt.className = 'bt' + status;
      bt.innerHTML = this.botoesTxts[status];
    }
  },
  
  setNavegacao : function(i){
    this.ativado = (parseInt(i)+(this.ativado || this.itens.length))%this.itens.length;
    this.anterior = ((this.ativado || this.itens.length)-1)%this.itens.length;
    this.proximo = ((this.ativado || this.itens.length)+1)%this.itens.length;
  },
  
  navegar : function(to){
    var desativar = this.ativado;

    this.setNavegacao(to);
      
    this.itens[this.ativado].className=this.itens[this.ativado].className.replace(/desativado/g,'');
    this.itens[this.ativado].style.zIndex=3;
    this.itens[this.ativado].style.opacity=0;
    this.itens[this.ativado].style.filter = "Alpha(Opacity=0)";
    UOLLib.rotativoDeChamadas.animando = true;
    for(var i=1;i<=5;i++){
      var item = this.itens[this.ativado];
      setTimeout(function(y){
        return function(){
          if(UOLLib.rotativoDeChamadas.navMSIE)
            item.style.filter = "Alpha(Opacity="+(20 * y )+")";
          else
            item.style.opacity = .2 * y;
          if (y == 5) {
            if(UOLLib.rotativoDeChamadas.navMSIE)
              item.style.filter = "none";
            UOLLib.rotativoDeChamadas.itens[desativar].className = 'desativado';
            UOLLib.rotativoDeChamadas.animando = false;
          }
        }
      }(i),75*i)
    }
    this.itens[desativar].style.zIndex=2;
    this.writeBotoesHTML();
    if(this.timeOut==null)
      this.setBtStopPlay('play');
    else
      this.setBtStopPlay('stop');

  },

  montarBotoes : function(){
    this.botoes = document.createElement("div");
    this.botoes.className="botoesNavegacao";
    this.writeBotoesHTML();
    this.painel.appendChild(this.botoes);
  },
  
  aplicarSombras : function(){
    var sombra = document.createElement('div');
    sombra.className='sombra';
    
    for(var i=0;i<this.itens.length;i++){
      if(i>0)
        this.itens[i].className+=' desativado';
      this.itens[i].appendChild(sombra.cloneNode(true));
    }
  },
  
  writeBotoesHTML : function(){
    this.botoes.innerHTML="<a rel='-1' href='"+this.itens[this.anterior].getElementsByTagName('a')[0].href+"' class='anterior'>"+this.botoesTxts.prev+"</a>\
      <a rel='0' href='"+this.itens[this.ativado].getElementsByTagName('a')[0].href+"' class=''></a>\
      <a rel='+1' href='"+this.itens[this.proximo].getElementsByTagName('a')[0].href+"' class='proximo'>"+this.botoesTxts.next+"</a>";
    var aHrefs = this.botoes.getElementsByTagName('a');
    for(var i=0;i<aHrefs.length;i++){
      this.events.add(aHrefs[i],'click',
        function(e){
            UOLLib.rotativoDeChamadas.events.preventDefault(e);
            if(e.srcElement)
              e.target=e.srcElement;
            if (e.target.rel == 0) 
              UOLLib.rotativoDeChamadas.stopPlay();
            else if(UOLLib.rotativoDeChamadas.animando==false) {
              UOLLib.rotativoDeChamadas.navegar(e.target.rel);
              UOLLib.rotativoDeChamadas.stopPlay(true);
            }
        }
      );
    }
  }
  
}
