// JavaScript Document
var rotatorio;
var destaques;

$(document).ready( function(){	
	getRotatorio();
	getDestaquesSecao();
	window.setInterval("rodar()", 5000);
	window.setInterval("rodarDestaques()", 25000);
});

function rodar(){
	$('#rotatorio img').hide();
	$('#comentarioRotatorio').hide();
	$('#nomeRotatorio').hide();

	var url = "<a href='"+rotatorio.url[ rotatorio.contador ]+"'>";

	$('#imgRotatorio').attr('href', rotatorio.url[ rotatorio.contador ]);
	$('#rotatorio img').attr('src', './'+rotatorio.image[ rotatorio.contador ]);
	$('#rotatorio img').attr('alt', rotatorio.nome[ rotatorio.contador ]);		
	$('#nomeRotatorio').html( url + rotatorio.nome[ rotatorio.contador ] + "</a>");	
	$('#comentarioRotatorio').html( url + rotatorio.comentario[ rotatorio.contador ] +"</a>");		
	
	$('#rotatorio img').animate({
	  opacity: 'show'
	}, "slow");
	$('#comentarioRotatorio').animate({
	  opacity: 'show'
	}, "slow");		
	$('#nomeRotatorio').animate({
	  opacity: 'show'
	}, "slow");
	
	rotatorio.contador = rotatorio.contador + 1;
	if( rotatorio.contador == 5 )
		rotatorio.contador = 0;
}

function getRotatorio(){
	$.post('../bin/ControlerUser.php', 
		   {action:'getRotatorio', lingua:1}, 
		   function( data ){				
		   		//alert( data );
				//$('#rotatorio').html( data );
				rotatorio = eval("(" + data + ")");						
				rodar();
			}
	);
}

function getDestaquesSecao(){
	$.post('../bin/ControlerUser.php', 
		   {action:'getDestaquesSecao', idSecao:$('#idSecao').val()}, 
		   function( data ){				
				destaques = eval("(" + data + ")");	
				rodarDestaques();
			}
	);	
}

function rodarDestaques(){
	$('.listDestaque a').hide();
	//$('.listDestaque div').hide();
	var url = "";
	//$('#imgRotatorio').attr('href', "../html/detalhesProduto.php?id="+rotatorio.id[ rotatorio.contador ])
	//$('#rotatorio img').attr('src', "../"+rotatorio.image[ rotatorio.contador ]);
	//$('#rotatorio img').attr('alt', rotatorio.nome[ rotatorio.contador ]);		
	//$('#nomeRotatorio').html( url + rotatorio.nome[ rotatorio.contador ] + "</a>");	
	//$('#comentarioRotatorio').html( url + rotatorio.comentario[ rotatorio.contador ] +"</a>");		
	$('.listDestaque').each( function( i ){
		url = destaques.url[ destaques.contador ];									  
									  
		$(this).children('a').children('.imgDestaque').attr('src', '../Templates/'+destaques.image[ destaques.contador ]);
		$(this).children('a').children('.imgDestaque').attr('alt', destaques.nome[ destaques.contador ]);	
		$(this).children('div').html( '<a href="'+url+'">'+destaques.nome[ destaques.contador ] +'</a>');
		$(this).children('a').attr('href', url);
		
		destaques.contador = destaques.contador + 1;
		if( destaques.contador == 26 )
			destaques.contador = 0;
		
	});
	
	$('.listDestaque a').animate({
	  opacity: 'show'
	}, "slow");
	//$('.listDestaque div').animate({
	  //opacity: 'show'
	//}, "slow");		
}
