var x = 0;   
var y = 0;

var i = -1;
var completo = false;

var imagens_carregadas = [];

/* Function slide */
function slide () {
	new Asset.images(imagens, {
		   
		onProgress: function(i) {
	   
					if(imagens[i] != "undefined") {
						imagens_carregadas[y] = imagens[i];
						y++;
					}   
		completo = false;
		},

		onComplete: function() {
		troca();
		trocar();
		completo = true;
		}
	   
	});
}

/* Trocar */

function troca() {
    $("prev_btn").removeEvents();
    $("next_btn").removeEvents();
    $('foto_slide').effect('opacity', {duration: 500, transition: Fx.Transitions.linear}).start(0).chain(function(){
        $('foto_slide').setStyle('opacity',0);
       
        if(completo == true) { $('foto_slide').setProperty('src',imagens[x]); }
            else { $('foto_slide').setProperty('src',imagens_carregadas[x]); }
           
        $('foto_slide').effect('opacity', {duration: 500, transition: Fx.Transitions.linear,wait: 'true',
			onComplete: function(){
					$('legendas_cinema').setHTML(legendas[x]);
					$('legendas_cinema').setProperty("href", links[x]);
					$("prev_btn").addEvent('click',function(e){		
						e = new Event(e).stop();
						
						if(x == 0) {
							x = imagens.length-1; 
						}else { 
							x--; 
						}
						troca();
						tempo = -2;
					});
					$("next_btn").addEvent('click',function(e){		
							e = new Event(e).stop();
							
							if(x == imagens.length - 1) {
								x = 0; 
							}else { 
								x++; 
							}
							
							troca();
							tempo = -2;
					});
				}
		}).start(0,1);
    });
//	setTimeout('troca()',4000)   

}
var tempo = 0;
function trocar(){
	tempo++;
	if(tempo==3){
		if(x == imagens.length - 1) {
			x = 0; 
		}else { 
			x++; 
		}
		
		tempo=0;			
		troca();		
	}
	window.setTimeout('trocar()',3000);
}


/* On load  */

window.addEvent('domready', function() {
	slide();		

	$("prev_btn").addEvent('click',function(e){		
			e = new Event(e).stop();
			
			if(x == 0) {
				x = imagens.length-1; 
			}else { 
				x--; 
			}
			troca();
			tempo = -2;
	});
	$("next_btn").addEvent('click',function(e){		
			e = new Event(e).stop();
			
			if(x == imagens.length - 1) {
				x = 0; 
			}else { 
				x++; 
			}
			
			troca();
			tempo = -2;
	});

});

