// var slideshow_image;

/* Registra gli eventi una volta che la pagina è caricata */
Event.observe(window, 'load', function() {
	var menuitems = $$('img.rollover_image');
	menuitems.each(
		function(menuitem) {
       		menuitem.observe('mouseover', rolloverOn);  
		    menuitem.observe('mouseout', rolloverOff);  	   
    	} 
	);  
	
	// var slideshow_image = $('immagine_slideshow');
	// slideshow_image.observe('mouseover', stopSlideshow());
	// slideshow_image.observe('mouseout', runSlideshow());
	    
});



function rolloverOn(event) {
  var element = event.element();
  var imgsrc = element.src;
  var pos = imgsrc.lastIndexOf("_off");
  if (pos != -1) {
	  element.src = imgsrc.substr(0, pos) + "_on.jpg";	
  }
}

function rolloverOff(event) {
  var element = event.element();	
  var imgsrc = element.src;
  var pos = imgsrc.lastIndexOf("_on");
  if (pos != -1) {
	  element.src = imgsrc.substr(0, pos) + "_off.jpg";	
  }
}


var TheSlideshow = Class.create({
	initialize: function(image_count, interval, image_id) {
		this.image_count = image_count;
		if (image_count == 0) { return; }
		this.image_id = image_id;
		slideshow_image = $(image_id);
		
		this.slideshow_image = new Image();
		this.slideshow_image = slideshow_image;
		
		this.slideshow_image_src = slideshow_image.src;
		this.image_basename = this.slideshow_image_src.substr(0, this.slideshow_image_src.length - 6);	
		this.current_image = 1;
		this.interval = interval;
		this.timer = setInterval(this.update_image.bind(this), this.interval);
	},

	update_image: function() {
		this.current_image++;		
		if (this.current_image > this.image_count) {
			this.current_image = 1;
		}
		if (this.current_image < 10) {
			this.slideshow_image.src = this.image_basename + "0" + this.current_image + ".jpg";
		} else {
			this.slideshow_image.src = this.image_basename + this.current_image + ".jpg";
		}	
	},
	
	stop: function() {
		// DEBUG alert ('stop');
		if (!this.timer) return;
    	clearInterval(this.timer);
    	this.timer = null;
	},
	
	start: function() {
		// DEBUG alert ('start');
		this.timer = setInterval(this.update_image.bind(this), this.interval);
	}
});

var slideshow;

function startSlideshow(image_count, interval, image_id){
	// slideshow = new TheSlideshow(image_count, interval, image_id);
	new TheSlideshow(image_count, interval, image_id);
}

function runSlideshow() {
	slideshow.start();
}

function stopSlideshow() {
	slideshow.stop();
}



/*
var album = { 
  startup: function() { 
    new PeriodicalExecuter(album.cycle, 5) // change image every 5 seconds 
  }, 
  cycle: function() { 
    new Effect.Fade('image', { // the id of the <DIV> containing the photos 
      duration: 1, 
      fps: 50, 
      afterFinish: function() { 
        new Ajax.Updater('image','/album/next', { // URL for next <IMG> tag 
          asynchronous: true, 
          onSuccess: function() { 
            new Effect.Appear('image', {
              duration: 1,
              fps: 50,
              queue:'end'
            })
          } 
        }) 
      } 
    }) 
  } 
} 
 
window.onload = album.startup
*/

