var Home = Class.create({
	initialize: function() {
		this.duration = 0.3;

		this.sectionRefresh = 10;
		this.sectionTimeout = null;
		this.initialSection = 1;
		this.lastSection = $$('.contenitore_sezione_home').length;
		this.currentSection = this.initialSection;
		
		this.initialOpacity = 0.15;
		this.finalOpacity = 0.7;
		
		this.eventsRefresh = 10;
		this.eventsDelay = 2;
		this.eventsTimeout = null;
		this.eventsInterval = null;
		this.maxDisplayedEvents = 2;
		this.initialTopEvents = parseInt($('contenitore_eventi_home').getStyle('top'));

		this.newsRefresh = 10;
		this.newsDelay = 4;
		this.newsTimeout = null;
		this.newsInterval = null;
		this.maxDisplayedNews = 2;
		this.initialTopNews = parseInt($('contenitore_news_home').getStyle('top'));
		
		this.observeFields();
		// this.observeDemos();
		this.observeDots();
		this.moveSections();
/*		this.scrollEvents();
		this.scrollNews();*/
	},
	
	observeFields: function() {
		$$('input').each(function(el) {
			Event.observe(el, 'focus', function() {
				if (!el.hasClassName('selezionato')) {
					el.addClassName('selezionato');
				}
			});
			Event.observe(el, 'blur', function() {
				if (el.hasClassName('selezionato')) {
					el.removeClassName('selezionato');
				}
			});
		})
	},
	
	observeDemos: function() {
		$$('div[id^="demo_home_"]').each(function(el) {
			var section = el.id.substring("demo_home_".length, el.id.length);
			var link = $("play_demo_" + section);
			var hovered = false;
			var activeAnimation = false;
			
			Event.observe(link, 'click', this.startDemo.bind(this, section));

			Event.observe(el, 'mouseover', function() {
				if (!hovered) {
					if (activeAnimation) {
						activeAnimation.cancel();
					}
					activeAnimation = new Effect.Opacity(link, {
						to: this.finalOpacity,
						duration: this.duration
					});
					hovered = true;
				}
			}.bind(this));
			Event.observe(el, 'mouseout', function() {
				if (hovered) {
					if (activeAnimation) {
						activeAnimation.cancel();
					}
					activeAnimation = new Effect.Opacity(link, {
						to: this.initialOpacity,
						duration: this.duration
					});
					hovered = false;
				}
			}.bind(this));
		}.bind(this));
	},
	
	observeDots: function() {
		$$('div[id^="punto_"]').each(function(el) {
			var section = el.id.substring("punto_".length, el.id.length);
			Event.observe(el, 'click', this.stopAndGoTo.bind(this, section));
		}.bind(this));
	},
	
	stopAndGoTo: function(section) {
		this.stopSections();
		this.goTo(section);
	},
	
	stopSections: function() {
		if (this.sectionTimeout) {
			clearTimeout(this.sectionTimeout);
		}
	},
	
	moveSections: function() {
		this.sectionTimeout = setTimeout(function() {
			var nextSection = this.currentSection + 1;
			if (this.currentSection == this.lastSection) {
				nextSection = this.initialSection;
			}
			this.goTo(nextSection);
			this.moveSections();
		}.bind(this), this.sectionRefresh * 1000);
	},
	
	goTo: function(section) {
		if (typeof(section) != "number") {
			section = parseInt(section);
		}
		var finalLeft = -1 * $('contenuto_home').getWidth() * (section - 1);
		
		new Effect.Morph('contenitore_sezioni_home', {
			style: {
				left: finalLeft + "px"
			},
			duration: this.duration
		});

		this.selectDot(section);
		this.currentSection = section;
	},
	
	selectDot: function(section) {
		var currentDot = $('punto_' + this.currentSection);
		if (currentDot.hasClassName('selezionato')) {
			currentDot.removeClassName('selezionato');
		}
		
		var selectedDot = $('punto_' + section);
		if (!selectedDot.hasClassName('selezionato')) {
			selectedDot.addClassName('selezionato');
		}
	},
	
	startDemo: function(section) {
		this.stopSections();
		this.stopEvents();
		this.stopNews();

		$('play_demo_' + section).hide();
		$('contenitore_demo_' + section).setStyle(
			'z-index:200;'/* +
			'-moz-box-shadow:0px 0px 30px 0px rgba(0, 0, 0, 1), 0px 0px 30px 0px rgba(255, 255, 255, 0.5);'*/
		);

		new Effect.Morph('contenitore_demo_' + section, {
			style: {
				width: '880px',
				height: '565px',
				left: '70px',
				top: '-380px'
			},
			duration: this.duration
		});
		new Effect.Morph('preview_' + section, {
			style: {
				width: '840px',
				height: '525px',
				left: '20px',
				top: '20px'
			},
			duration: this.duration
		});

		$('contenitore_demo_' + section).insert(
			'<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="840" height="525" codebase="http://www.apple.com/qtactivex/qtplugin.cab" id="object_demo_' + section + '">' +
			    '<param name="src" value="mov/demo_' + section + '.mov" />' +
			    '<param name="autostart" value="true" />' +
				'<param name="enablejavascript" value="true" />' +
				'<param name="postdomevents" value="true" />' +
			    '<param name="bgcolor" value="#000000" />' +
			    '<param name="controller" value="false" />' +
			    '<param name="showlogo" value="false" />' +
			    '<param name="loop" value="false" />' +
			    '<param name="volume" value="0" />' +
			    '<param name="cache" value="true" />' +
				'<embed src="mov/demo_' + section + '.mov" id="embed_demo_' + section + '" type="video/quicktime" width="840" height="525" autostart="true" enablejavascript="true" postdomevents="true" bgcolor="#000000" controller="false" showlogo="false" loop="false" volume="0" cache="true" pluginspage="http://www.apple.com/quicktime/download/" />' +
			'</object>' +
			'<a href="#" class="chiudi_demo" id="chiudi_demo_' + section + '">&nbsp;</a>'
		);
		
		var demo = $('embed_demo_' + section) ? $('embed_demo_' + section) : $('object_demo_' + section);
		Event.observe(demo, 'qt_canplaythrough', function() {
			demo.setStyle('visibility:visible');
		});
		Event.observe(demo, 'qt_ended', function() {
			setTimeout(this.stopDemo.bind(this, section), 2000);
		}.bind(this));
		Event.observe('chiudi_demo_' + section, 'click', this.stopDemo.bind(this, section));
	},
	
	stopDemo: function(section) {
		var demo = $('embed_demo_' + section) ? $('embed_demo_' + section) : $('object_demo_' + section);
		demo.Stop();
		demo.remove();
		$('chiudi_demo_' + section).remove();

		new Effect.Morph('contenitore_demo_' + section, {
			style: {
				width: '355px',
				height: '203px',
				left: '0px',
				top: '0px'
			},
			duration: this.duration
		});
		new Effect.Morph('preview_' + section, {
			style: {
				width: '355px',
				height: '203px',
				left: '0px',
				top: '0px'
			},
			duration: this.duration
		});

		$('contenitore_demo_' + section).setStyle(
			'z-index:100;'
		);
		$('play_demo_' + section).show();
		$('play_demo_' + section).setStyle({
			opacity: this.initialOpacity
		});

		this.moveSections();
	},
	
	scrollEvents: function() {
		var events = $$('div[id^="eventi_home_"]');
		if (events.length > this.maxDisplayedEvents) {
			var finalHeight = $$('#eventi_home h2')[0].getHeight() - parseInt($('eventi_home').getStyle('margin-bottom'));
			for (var i = 0; i < this.maxDisplayedEvents; i++) {
				finalHeight += events[i].getHeight();
			}
			$('eventi_home').setStyle({
				height: finalHeight + 'px'
			});
			
			var currentEvent = 0;
			this.eventsTimeout = setTimeout(function() {
				this.eventsInterval = setInterval(function() {
					currentEvent++;
					if (currentEvent >= events.length) {
						currentEvent = 0;
					}
					this.goToEvent(currentEvent, events);
				}.bind(this), this.eventsRefresh * 1000);
			}.bind(this), this.eventsDelay * 1000);
		}
	},
	
	stopEvents: function() {
		if (this.eventsTimeout) {
			clearTimeout(this.eventsTimeout);
		}
		if (this.eventsInterval) {
			clearInterval(this.eventsInterval);
		}
	},
	
	goToEvent: function(finalEvent, events) {
		var finalTop = this.initialTopEvents;
		for (var i = 0; i < finalEvent; i++) {
			finalTop -= events[i].getHeight();
		}
		finalTop += "px";
		new Effect.Morph('contenitore_eventi_home', {
			style: {
				top: finalTop
			},
			duration: this.duration
		});

		var finalHeight = $$('#eventi_home h2')[0].getHeight();
		finalHeight -= parseInt($('eventi_home').getStyle('margin-bottom'));
		for (var i = 0; i < this.maxDisplayedEvents; i++) {
			if (finalEvent + i == events.length) break;
			finalHeight += events[finalEvent + i].getHeight();
		}
		new Effect.Morph('eventi_home', {
			style: {
				height: finalHeight + 'px'
			},
			duration: this.duration
		});
	},
	
	scrollNews: function() {
		var news = $$('div[id^="news_home_"]');
		if (news.length > this.maxDisplayedNews) {
			var finalHeight = $$('#news_home h2')[0].getHeight() - parseInt($('news_home').getStyle('margin-bottom'));
			for (var i = 0; i < this.maxDisplayedNews; i++) {
				finalHeight += news[i].getHeight();
			}
			$('news_home').setStyle({
				height: finalHeight + 'px'
			});
			
			var currentNews = 0;
			this.newsTimeout = setTimeout(function() {
				this.newsInterval = setInterval(function() {
					currentNews++;
					if (currentNews >= news.length) {
						currentNews = 0;
					}
					this.goToNews(currentNews, news);
				}.bind(this), this.newsRefresh * 1000);
			}.bind(this), this.newsDelay * 1000);
		}
	},
	
	stopNews: function() {
		if (this.newsTimeout) {
			clearTimeout(this.newsTimeout);
		}
		if (this.newsInterval) {
			clearInterval(this.newsInterval);
		}
	},
	
	goToNews: function(finalNews, news) {
		var finalTop = this.initialTopNews;
		for (var i = 0; i < finalNews; i++) {
			finalTop -= news[i].getHeight();
		}
		finalTop += "px";
		new Effect.Morph('contenitore_news_home', {
			style: {
				top: finalTop
			},
			duration: this.duration
		});
		
		var finalHeight = $$('#news_home h2')[0].getHeight();
		finalHeight -= parseInt($('news_home').getStyle('margin-bottom'));
		for (var i = 0; i < this.maxDisplayedNews; i++) {
			if (finalNews + i == news.length) break;
			finalHeight += news[finalNews + i].getHeight();
		}
		new Effect.Morph('news_home', {
			style: {
				height: finalHeight + 'px'
			},
			duration: this.duration
		});
	}
});