﻿// --------------- variáveis de apoio ----------------------------------------//
var programmer = null;
var qtdeBanners = 0;
var data = null;
var active = 0;

$(document).ready(function () {
   $.ajax({

      type: "GET",
      url: "/shared/xml/ucBannerHome.xml",
      cache: false,
      dataType: "xml",
      success: ParseResult,
      error: ParseError

   });

});


function ParseResult(dados) {

   qtdeBanners = $($(dados).find("banners")).find("banner").length;
   data = $(dados).find("banners").find("banner");
   ShowBanner();

}

function ParseError() {
}


function ShowBanner() {

   // variáveis de apoio
   var texto = $(data[active]).find("texto")[0].firstChild.nodeValue;
   var image = $(data[active]).find("imagem")[0].firstChild.nodeValue + "?" + Math.random();
   var url = $(data[active]).find("url")[0].firstChild.nodeValue;
   var tempo = 0;
   tempo = ($(data[active]).find("tempo").length > 0) ? $(data[active]).find("tempo")[0].text : 5;
   tempo = (tempo < 5) ? 5 : tempo; // não permite que o tempo seja inferior à 5 segundos

   // objetos
   var imgDesc = $("#imgDesc"); // container para a descrição do banner
   var txtDesc = $("#txtDesc"); // objeto para o texto dos banners
   var imgBack = $("#imgBack"); // container da imagem
   var HomeBanners = $("#HomeBanners");

   // troca do banner
   imgDesc.animate({ bottom: '-40px' }, 500, 'easeOutBounce', function () {
      txtDesc.html(texto);
      imgDesc.animate({ 'bottom': '0px' }, 500, 'easeOutBounce');
      imgBack.fadeOut(200, 'linear', function () {
         imgBack.css({ backgroundImage: "url('/shared/images/bannerdestaques/" + image + "')", backgroundRepeat: 'no-repeate' })
         imgBack.fadeIn(500, 'linear');
      });
      HomeBanners.unbind('click').click(function () { document.location.href = url; });
      // programando próximo banner
      active = (active < (qtdeBanners - 1)) ? active = active + 1 : 0;
      programmer = setTimeout(ShowBanner, tempo * 1000);

   });

}



