window.addEvent('domready',function() {
  /* settings */
  var showDuration = 3000;
  var container = $('slideshow-container');
  var images = container.getElements('li');
  var currentIndex = 0;
  var interval;
  
  /* new: create caption area */
  var captionDIV = new Element('div',{
    id: 'slideshow-container-caption'
  }).inject($('caption'));
  captionDIV.setStyle('opacity',0);
    
  /* new: starts the show */
  var start = function() { interval = show.periodical(showDuration); };
  var stop = function() { $clear(interval); };
  /* worker */
  var show = function(to) {	
	 images[currentIndex].setProperty('id','noactive');
	images[currentIndex].getElement('img').setProperty('src',images[currentIndex].getElement('img').getProperty('src').replace('.jpg','_grey.jpg'));
	images[currentIndex].tween('width',[528,91]);
    images[currentIndex = ($defined(to) ? to : (currentIndex < images.length - 1 ? currentIndex+1 : 0))].tween('width',[91,528]).getElement('img').setProperty('src',images[currentIndex].getElement('img').getProperty('src').replace('_grey',''));
    captionDIV.set('tween',{
      duration: 'short',
	  onComplete: function() {
        images[currentIndex].setProperty('id','active');
		captionDIV.set('tween',{
          onComplete: $empty
        }).tween('opacity',1);
        /* parse caption */
        var title = '';
        var captionText = '';
        if(images[currentIndex].getElement('img').get('alt')) {         
		  cap = images[currentIndex].getElement('img').get('alt').split('::');
		  mylink = images[currentIndex].getElement('img').get('rel');
		  title = cap[0];
          captionText = cap[1];
          captionDIV.set('html','<h3>' + title + '</h3>' + (captionText ? '<p>' + captionText + '</p>' : ''));
		  var captionLINK = $('url').setProperty('href',mylink);
        }
      }
    }).tween('opacity',0);
  }; 
 /* control: start/stop on mouseover/mouseout  container.addEvents({   mouseenter: function() { 	images.each(function(index,el) {show(index); });},mouseleave: function() { stop(); } });*/
  /* start once the page is finished loading */
  	window.addEvent('load',function(){ 
		if ($('default')) show(0); 
		if ($('installazioni')) show(1); 
		if ($('servizi')) show(2); 
		if ($('hp_servizio')) { start();}  
	});
});

window.addEvent('domready',function() {
	if ($('form_contatti')) {
		new FormCheck('form_contatti', {
				display : {
				closeTipsButton : 1,
				showErrors : 1
				}
		});
	} 
});
