window.addEvent('domready', function() {

	// Cambia colore per font e background menu principale

	var list = $$('#main_nav li a');
	list.each(function(element) {
		if (element.className != "kwick active") {
			var fx = new Fx.Styles(element, {duration:200, wait:false});

			element.addEvent('mouseenter', function(){
				fx.start({
					'background-color': '#626469',
					'color': '#e3e3e3'
				});
			});

			element.addEvent('mouseleave', function(){
				fx.start({
					'background-color': '#e3e3e3',
					'color': '#414141'
				});
			});
		
		}

	});

	// Animazione menu principale

	var szNormal = 102, szSmall  = 95, szFull   = 130;

	var kwicks = $$("#main_nav .kwick");
	var fx = new Fx.Elements(kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut});
	kwicks.each(function(kwick, i) {
		kwick.addEvent("mouseenter", function(event) {
			var o = {};
			o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
			kwicks.each(function(other, j) {
				if(i != j) {
					var w = other.getStyle("width").toInt();
					if(w != szSmall) o[j] = {width: [w, szSmall]};
				}
			});
			fx.start(o);
		});
	});
	
	// Animazione menu brochure
	
	var list = $$('#info_brochure li a');
	list.each(function(element) {
		if (element.className != "kwick active") {
			var fx = new Fx.Styles(element, {duration:200, wait:false});

			element.addEvent('mouseenter', function(){
				fx.start({
					'color': '#ec8223'
				});
			});

			element.addEvent('mouseleave', function(){
				fx.start({
					'color': '#505050'
				});
			});
		
		}

	});

	$("main_nav").addEvent("mouseleave", function(event) {
		var o = {};
		kwicks.each(function(kwick, i) {
			o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
		});
		fx.start(o);
	})


	// Margine sinistro menu di secondo livello

	var list = $$('#minicolumn_1 ul ul li a');
	list.each(function(element) {

		var fx = new Fx.Styles(element, {duration:200, wait:false});

		element.addEvent('mouseenter', function(){
			fx.start({
				'margin-left': -10,
				'color': '#ec8223'
			});
		});

		element.addEvent('mouseleave', function(){
			fx.start({
				'margin-left': -20,
				'color': '#414141'
			});
		});

	});

	// Colore menu extra
	
	var list = $$('#extra li a');
	list.each(function(element) {

		var fx = new Fx.Styles(element, {duration:200, wait:false});

		element.addEvent('mouseenter', function(){
			fx.start({
				'color': '#ec8223'
			});
		});

		element.addEvent('mouseleave', function(){
			fx.start({
				'color': '#414141'
			});
		});

	});
	

	// Scroll

	new SmoothScroll();
	
	// Risultati ricerca

	var list = $$('#search_results li em');
	list.each(function(element) {

		var fx = new Fx.Styles(element, {duration:1000, wait:false});

		fx.start({
			'backgroundColor': '#414141',
			'color': '#fff'
		});

	});
	
	// Reset colori e bg per animazione
	
	$$('ul#main_nav li a').each(function(e) {
		if (e.className != "kwick active") {
		e.setStyle('color', '#414141');
		e.setStyle('background-color', '#e3e3e3');
		}
	});
	
	$$('#extra li a').each(function(e) {
		e.setStyle('color', '#414141')
	});
	
	$$('#minicolumn_1 ul ul li a').each(function(e) {
		e.setStyle('color', '#414141')
	});
	
	// $('footer').setStyle('text-shadow', '#000 0 0 0');
	if ($('ajaxMovie')) {
		$('ajaxMovie').addEvent('click', function(e) {
			startingMovie(e, url);
		});
	}

});

function ajaxRequest(urlrequest){
	new Ajax(urlrequest, {
		method: 'get',
		update: $('log'),
		evalScripts: true,
		onComplete: function(){
			$('stop').addEvent('click', function(e) {
				closingMovie(function(){});
			});
			$('logcontainer').setStyle('background-image', 'url(http://dev.itacaconsulting.it/css/images/shadow.png)');
		}
	}).request();		
};

function startingMovie(e, url){
	new Element ( 'div' , {'id':'logcontainer'}).injectBefore('log');
	$('log').setStyle('position', 'absolute');
	var fx = new Fx.Styles($('log'), {
		duration:300,
		wait:true,
		transition: Fx.Transitions.Circ.easeIn,
		onComplete: function(){
			ajaxRequest(url);
		}
	});
	
	var fx2 = new Fx.Styles($('logcontainer'), {
		duration:400,
		wait:true
	});
	
	fx.start({
		'background-color': '#626469',
		'color': '#e3e3e3',
		'height': 285,
		'width': 570,
		'margin-top': -250,
		'margin-left': -100
	});

	fx2.start({
		'height': 285,
		'width': 570,
		'margin-top': -285,
		'margin-left': -135,
		'padding': 35
	});

}

function closingMovie(f){
	$('logcontainer').remove();
	$('log').setStyle('position', 'absolute');
	$('log').empty();
	var fx = new Fx.Styles($('log'), {
		duration:400, 
		wait:true,
		onComplete: f
	});
	
	fx.start({
		'background-color': '#fff',
		'color': '#e3e3e3',
		'height': 0,
		'width': 0,
		'margin-top': -38,
		'margin-left': 16
	});
	
}

function toggleMovie(url){
	$('logcontainer').remove();
	$('log').setStyle('position', 'absolute');
	$('log').empty();
	var fx = new Fx.Styles($('log'), {
		duration:400, 
		wait:true,
		onComplete: function(){
			startingMovie(this, url);
		}
	});
	
	fx.start({
		'background-color': '#fff',
		'color': '#e3e3e3',
		'height': 0,
		'width': 0,
		'margin-top': -38,
		'margin-left': 16
	});
}

function stopAndGo(url){
	toggleMovie(url);
}
