var flyerX = 0;

Event.observe(window, 'load', function() {
	slideshow.start();
});

document.observe("dom:loaded", function() {
	slideshow.init();
	slideshow.fixflyers();
});

slideshow = {
	timer: 3000,
	playing: true,
	position: 1,
	totalslides: 0,
	mover: false,
	init: function(){
		var totalslides = 0;
		$$('#flyer_panner .slide').each(function(s){
			totalslides++;
		});
		slideshow.totalslides = totalslides;
	},
	start: function(){
		setTimeout(function(){
			if(slideshow.playing == true){
				slideshow.next();
				slideshow.start();
			}
		}, slideshow.timer);
	},
	restart: function(){
		
	},
	next: function(){
		slideshow.position = slideshow.position + 1;
		if((slideshow.position + 1) > slideshow.totalslides){
			flyerX = 0;
			slideshow.position = 1;
			var dur = .5;
		}else{
			flyerX = flyerX - 220;
			var dur = 3.0;
		}
		slideshow.mover = $('flyer_panner_inner').morph('margin-left: '+flyerX+'px',{duration: dur, transition: Effect.Transitions.linear});
	},
	prev: function(){
		flyerX = flyerX + 220;
		$('flyer_panner_inner').morph('margin-left: '+flyerX+'px',{duration: .5});
	},
	fixflyers: function(){
		$$('#flyer_panner .slide img').each(function(s){
			var w = s.width;
			var h = s.height;
			var maxw = 190;
			var maxh = 168;
			if(w > maxw){
				s.width = maxw;
				ratio = maxw / w;
				s.height = h * ratio;
			}
			if(s.height > maxh){
				s.height = maxh;
				ratio = maxh / h;
				s.width = w * ratio;
			}
			s.style.display='block';
		});
	}
}

