// JavaScript immagine in rotazione

promo = {
	init:function(){
		var numscure = 1;
		$('.promozione_offerta img.scure').each(function(){
			$(this).attr('id', 'imagescure-'+numscure);
			numscure++;
		});
		var numchiare = 1;
		$('.promozione_offerta img.chiare').each(function(){
			$(this).attr('id', 'imagechiare-'+numchiare);
			numchiare++;
		});
		var numtitolo = 1;
		$('.promozione_offerta span.promozione_nome').each(function(){
			$(this).attr('id', 'promozione_nome-'+numtitolo);
			numtitolo++;
		});
		var numvai = 1;
		$('.promozione_offerta span.promozione_vai').each(function(){
			$(this).attr('id', 'promozione_vai-'+numvai);
			numvai++;
		});
		$('#imagescure-1').addClass('current');
		$('#imagescure-1').fadeOut('slow');
		$('#promozione_nome-1').css('background-image', 'url(img/titolo_promozione_back_current.png)');
		$('#promozione_vai-1').css('background-image', 'url(img/promozione_vai_back_current.png)');
		$('#imagechiare-1').css('background', '#8D8E8D');
		promo.current = 1;
		promo.lastImage = $('.promozione_offerta img.scure:last').attr('id').split('-')[1];	
		promo.prepNextSlide();			
	},
	prepNextSlide:function(doFade){
		if(promo.current == promo.lastImage){
			promo.nextImg = '#imagescure-1';
			promo.next 	= 1;
		} else {
			promo.next 	= parseInt(promo.current)+1;			
			promo.nextImg = '#imagescure-'+promo.next;
		}		
		promo.getRotatorSpeed();
	},
	getRotatorSpeed:function(){
		if($('#imagescure-'+promo.current).attr('class')) {
			promo.speed = parseInt($('#imagescure-'+promo.current).attr('class').split('-')[1] );	
		}
		if(promo.speed)
			promo.rotatorSpeed = promo.speed*1000;
		else
			promo.rotatorSpeed = 5000;		
		promo.timer = setTimeout(function(){ promo.fadeIt(); }, promo.rotatorSpeed);
	},
	fadeIt:function(){	
		$('#imagescure-'+promo.current).fadeIn('slow').removeClass('current');
		$('#promozione_nome-'+promo.current).css('background-image', 'url(img/titolo_promozione_back.png)');
		$('#promozione_vai-'+promo.current).css('background-image', 'url(img/promozione_vai_back.png)');
		$('#imagechiare-'+promo.current).css('background', '#5A5757');
		$(promo.nextImg).addClass('current');
		$(promo.nextImg).fadeOut('slow');
		$('#promozione_nome-'+promo.next).css('background-image', 'url(img/titolo_promozione_back_current.png)');
		$('#promozione_vai-'+promo.next).css('background-image', 'url(img/promozione_vai_back_current.png)');
		$('#imagechiare-'+promo.next).css('background', '#8D8E8D');
		promo.current = promo.next;
		promo.prepNextSlide();
	},
}
$(document).ready(function(){	
	promo.init();
});

