praFrente = true;
rodarTimeout=0;

proximo = function(duration) {
	if ($('swap').effect && !praFrente) $('swap').effect.cancel();
	praFrente = true;
	duration = duration || 0.8;
	marginLeft = parseInt($('swap').style.marginLeft.replace('px',''));
	if (!$('swap').style.marginLeft) marginLeft = 0;
	marginLeft = marginLeft/250;
	margin = - 250 * parseInt(-marginLeft + 1);
	$('swap').effect = new Effect.Morph('swap',{style:{marginLeft:''+margin+'px'}, duration:duration, afterFinish: function() {
    	if(margin <= -250 * parseInt($('swap').childElements().length  - 1)) {
			margin = 0;
			$('swap').style.marginLeft = margin + "px";
		}
    }});
}
anterior = function(duration) {
	if ($('swap').effect && praFrente) $('swap').effect.cancel();
	praFrente = false;
	duration = duration || 0.8;
	marginLeft = parseInt($('swap').style.marginLeft.replace('px',''));
	marginLeft = marginLeft/250;
	margin = - 250 * parseInt(-marginLeft - 1);
	if(margin > 0) {
		margin = -250 * parseInt($('swap').childElements().length  - 1);
		$('swap').style.marginLeft = margin + "px";
	}	
	$('swap').effect = new Effect.Morph('swap',{style:{marginLeft:''+margin+'px'}, duration:duration});
}
irPara = function(posicao, duration) {
	if ($('swap').effect && praFrente) $('swap').effect.cancel();
	duration = duration || 0.8;
	marginLeft = parseInt($('swap').style.marginLeft.replace('px',''));
	margin = - 250 * parseInt(posicao - 1);
	$('swap').effect = new Effect.Morph('swap',{style:{marginLeft:''+margin+'px'}, duration:duration});
	parar();
}
parar = function() {
	clearTimeout(rodarTimeout);
	rodarTimeout=setTimeout(rodar, 10000);
}
rodar = function() {
	if (praFrente)
		proximo();
	else
		anterior();
	rodarTimeout=setTimeout(rodar, 10000);
};

Event.observe(window, "load", function() {
$$('#swap div .e').each(function(elem){
	elem.onclick = function(){anterior();};
});
$$('#swap div .d').each(function(elem){
	elem.onclick = function(){proximo();};
});
rodarTimeout=setTimeout(rodar, 5000);
});

