var Display =
{
	totalItens	: 0,
	delay		: 0,
	timerCount	: 0,
	
	cIndex		: null,
	xml			: null,
	
	intrlID		: null,
	imgOb		: null,
	tipo		: null,
	
	load		: function( tipo )
	{
		
		Display.tipo = tipo;
		
		jQuery.ajax({
			url		: '_services/destaque-lista.php',
			cache	: false,
			type	: 'POST',
			data:
			{
				idRegiao:idRegiao
			},
			dataType: 'xml',
			error	: function()
			{
				
			},
			success	: function(data)
			{
				//Montagem dos botões
				Display.xml = data;
				Display.totalItens = jQuery(data).find('destaque').length;
				
				/* Adicionado por Fernando - Centralizar os li's na TV */
				/*centraliza = "";
				centraliza = "-" + (158 * Display.totalItens) / 2 + "px";
				
				$(".abas-super-banner").css("margin-left",centraliza);
				$(".banner-bordas").css("margin-left",centraliza);
				*/
				ht = '';
				
				for(w = 0; w < Display.totalItens; w++)
				{
					var element = jQuery(data).find('destaque').get(w);
					
					//var element = jQuery(data).find('destaque').get(w);
					ht += '<li>';
					ht += 	'<a href="javascript:;">';
					ht += 	'</a>';
					ht += '</li>';
				}
				
				jQuery('.abas-super-banner').append(ht);
				
				jQuery('.abas-super-banner > li').each(function(w) {
					jQuery(this).find('a').bind('click', { index: w }, Display.setItem);
				});
				
				jQuery('.abas-super-banner > li:eq(0)').find('a').trigger('click');
			}
		});
	},
	setItem	: function( ev )
	{
		//cleaning old elements
		jQuery('.abas-super-banner > li').each(function(y)
		{
			var element = jQuery( Display.xml ).find('destaque').get(y);
			jQuery(this).find('a').removeClass('aba-home-banner-ativa');
			//jQuery(this).find('a').find('.cantodireito').removeClass('aba-home-banner-ativa');
			jQuery(this).find('a').find(".timer").remove();
			jQuery(this).find('a').html( jQuery(element).children('title').text() );
		});
		
		jQuery('#content-banner > img:eq(1)').remove();
		jQuery( ev.currentTarget ).addClass('aba-home-banner-ativa');
		//jQuery( ev.currentTarget ).find(".cantodireito").addClass('aba-home-banner-ativa');
		
		
		var xmlElement	= jQuery(Display.xml).find('destaque').get(ev.data.index);
		
		
		//set/reset new global values
		Display.cIndex		= ev.data.index;
		Display.delay		= parseInt(jQuery( xmlElement ).attr('tempo')) * 1000;
		Display.timerCount	= 0;
		
		//Se for imagem, usa o objeto Image nativo do javascript
		//if( jQuery(xmlElement).attr('tipo') == "IMAGEM" )
		//Se for mobile
		if( Display.tipo == 1 )
		{
			Display.imgOb			= new Image();
			
			Display.imgOb.src		= jQuery(xmlElement).children('imagem').text();
			Display.imgOb.onload	= Display.showImage;
			Display.imgOb.alt		= jQuery(xmlElement).children('title').text();
			
			jQuery('#content-banner').attr('title',jQuery( xmlElement ).attr('idDestaque'));
			Display.showImage();
		}
		else //Se não, apenas embeda o swf
		{
			jQuery('#content-banner').attr('title',jQuery( xmlElement ).attr('idDestaque'));	
			jQuery('#content-banner').html('<div id="content-embed"></div>');
			
			var strPath = jQuery(xmlElement).children('src').text();
			swfobject.embedSWF
			(
				"" + strPath,
				"content-embed",
				"730",
				"300",
				"9.0.0",
				null,
				null,
				{
					wmode	: "transparent"
				}
			);
			
			Display.showSWF();
			
			// Nesse momento vai chamar um AJAX para inserir uma visita no banner
			//alert( jQuery(xmlElement).attr('idDestaque') );
			jQuery.ajax({
				url		: '_services/AJAX-banner-visitas.php',
				cache	: false,
				type	: 'POST',
				data:
				{
					idDestaque:jQuery( xmlElement ).attr('idDestaque')
				},
				dataType: 'html',
				error	: function(x,y)
				{
				},
				success	: function(data)
				{
				}
			});
		}
		
		clearInterval(Display.intrlID);
		
		if( Display.totalItens > 1 ) {
			
			ht =  ' <div class="timer">';
			ht += '		<div class="fill-timer" style="width:1%;">&nbsp;</div>';
			ht += '		<div class="mask-timer">&nbsp;</div>';
			ht += '	</div>';
			
			var bolinhas 		= Math.ceil(jQuery( xmlElement ).attr('tempo') / 2 );
			var tamanhoTimer 	= Math.ceil(jQuery( xmlElement ).attr('tempo') / 2) * 9;
			
			if( bolinhas >= jQuery( xmlElement ).children('title').text().length )
			{				
				var resto 		= 	bolinhas - jQuery( xmlElement ).children('title').text().length;
				if(resto > 0)
				{
					tamanhoTimer 	=	(Math.ceil(jQuery( xmlElement ).attr('tempo') / 2) - 2 - resto) * 9;
				}
				else
				{
					tamanhoTimer 	=	(Math.ceil(jQuery( xmlElement ).attr('tempo') / 2) - 1) * 9;
				}
				bolinhas 		=	jQuery( xmlElement ).children('title').text().length - 2;
			}
			
			if( bolinhas + 1 == jQuery( xmlElement ).children('title').text().length )
			{
				bolinhas	=	jQuery( xmlElement ).children('title').text().length - 1;
				tamanhoTimer 	=	(Math.ceil(jQuery( xmlElement ).attr('tempo') / 2) - 1) * 9;
			}
			
			if( jQuery( xmlElement ).children('title').text().length  < 15 && jQuery( xmlElement ).children('title').text().length  > 10 )
			{
				if( bolinhas == 10){
					bolinhas = 8;
					tamanhoTimer 	=	(Math.ceil(jQuery( xmlElement ).attr('tempo') / 2) - 2) * 9;
				}
			}
			
			jQuery('.fill-timer').css('width','1%');
			jQuery( ev.currentTarget ).append(ht);
			
			jQuery('.timer').css('width',tamanhoTimer+'px');
			jQuery('.mask-timer').css('width',tamanhoTimer+'px');
			
			jQuery('.timer').css('marginLeft','-'+((tamanhoTimer/2)-7)+'px');
			
			jQuery('.mask-timer').css("backgroundImage","url('_images/timer-" + bolinhas + ".gif')");
			jQuery('.mask-timer').css("backgroundRepeat","no-repeat");
			//jQuery('.mask-timer').css('background-image', 'url(../img/icon/timer-'+jQuery( xmlElement ).attr('tempo')+'.gif)');
		}
	},
	showImage: function() {
		jQuery('#content-banner > img').hide();
		jQuery('#content-banner > object').hide();
		
		setTimeout(function()
		{
			jQuery('#content-banner').append(Display.imgOb);
			jQuery('#content-banner').css('display','none');
			
			jQuery('#content-banner').fadeIn();
			
			if( Display.totalItens > 1 )
			{
				clearInterval(Display.intrlID);
				Display.intrlID = setInterval(Display.progress,500);
			}
		},300);
	},
	showSWF	: function()
	{
		setTimeout(function()
		{
			jQuery('.borda-banner').css('cursor','pointer');
			jQuery('.borda-banner').bind('click',function(){
				document.location = 'destaque-contaclique.php?idDestaque='+jQuery('#content-banner').attr('title');
			});
			
			if( Display.totalItens > 1 )
			{
				Display.intrlID = setInterval(Display.progress,500);
			}
		},300);
	},
	progress: function(ev)
	{
		Display.timerCount += 500;
		
		var width = (((Display.timerCount+500)/Display.delay) * 100);
		
		jQuery('.fill-timer').css('width',(width) + '%');
		
		if( Display.timerCount >= Display.delay )
		{
			clearInterval(Display.intrlID);
			
			Display.cIndex ++;
			if( Display.cIndex > Display.totalItens-1 ) Display.cIndex = 0;
			
			jQuery('.abas-super-banner > li:eq(' + Display.cIndex + ')').find('a').trigger('click');
		}
	}
}

/*jQuery(function() {
	Display.load()
});*/
