//MooTools More, <http://mootools.net/more>. Copyright (c) 2006-2009 Aaron Newton <http://clientcide.com/>, Valerio Proietti <http://mad4milk.net> & the MooTools team <http://mootools.net/developers>, MIT Style License.

MooTools.More={version:"1.2.4.4",build:"6f6057dc645fdb7547689183b2311063bd653ddf"};var Asset={javascript:function(f,d){d=$extend({onload:$empty,document:document,check:$lambda(true)},d);
if(d.onLoad){d.onload=d.onLoad;}var b=new Element("script",{src:f,type:"text/javascript"});var e=d.onload.bind(b),a=d.check,g=d.document;delete d.onload;
delete d.check;delete d.document;b.addEvents({load:e,readystatechange:function(){if(["loaded","complete"].contains(this.readyState)){e();}}}).set(d);if(Browser.Engine.webkit419){var c=(function(){if(!$try(a)){return;
}$clear(c);e();}).periodical(50);}return b.inject(g.head);},css:function(b,a){return new Element("link",$merge({rel:"stylesheet",media:"screen",type:"text/css",href:b},a)).inject(document.head);
},image:function(c,b){b=$merge({onload:$empty,onabort:$empty,onerror:$empty},b);var d=new Image();var a=document.id(d)||new Element("img");["load","abort","error"].each(function(e){var g="on"+e;
var f=e.capitalize();if(b["on"+f]){b[g]=b["on"+f];}var h=b[g];delete b[g];d[g]=function(){if(!d){return;}if(!a.parentNode){a.width=d.width;a.height=d.height;
}d=d.onload=d.onabort=d.onerror=null;h.delay(1,a,a);a.fireEvent(e,a,1);};});d.src=a.src=c;if(d&&d.complete){d.onload.delay(1);}return a.set(b);},images:function(d,c){c=$merge({onComplete:$empty,onProgress:$empty,onError:$empty,properties:{}},c);
d=$splat(d);var a=[];var b=0;return new Elements(d.map(function(e){return Asset.image(e,$extend(c.properties,{onload:function(){c.onProgress.call(this,b,d.indexOf(e));
b++;if(b==d.length){c.onComplete();}},onerror:function(){c.onError.call(this,b,d.indexOf(e));b++;if(b==d.length){c.onComplete();}}}));}));}};

window.addEvent('domready', function(){
	var Slider = new Class({
    	initialize: function(container){
			if($(container)){
				var actualI=null;
				var actualObject=this;
		        this.container = $(container);
				this.element= [];
				var count=this.container.getElements('li').length;
				var spostamento=0;
				if(this.container.getElement('li.active')) spostamento=300;
				this.container.getElements('li').each(function(item,i){
					if(item.hasClass('active')) spostamento=0;
					item.setStyles({position:'absolute', top:0, right:(98*(count-1-i))+spostamento});
					var fx = new Fx.Morph(item, {'duration': 800, 'fps':100, 'link': 'cancel','transition':Fx.Transitions.linear});
					actualObject.element[i]={'element':item,'fx': fx};
					item.addEvent('mouseover', function(){
						if(actualI!=i){
							actualI=i;
							this.addClass('over');
							actualObject.animation(i);							
						}
					});
					item.addEvent('mouseleave', function(){this.removeClass('over');});
				});
				this.container.getElements('ul').addEvent('mouseleave', function(){
					actualI=null;					
					actualObject.animation(null);				
				});
			}
	    },
		animation: function(iselect){
			var count=this.element.length;
			var spostamento=0;
			var width=98;
			for(var i=count-1; i>=0; i--){
				width=98;
				if(iselect==null && this.element[i].element.hasClass('active'))width=398;
				if(iselect==i) width=398;
				
				this.element[i].fx.start({'right':spostamento,'width':width});
				
				if(iselect==null && this.element[i].element.hasClass('active')) spostamento+=300;
				if(iselect==i) spostamento+=300;
				spostamento+=98;
			}
		}
	});
	var mySlider = new Slider('menuSlider');
	
	var formContatti=function (){
		var form=''
		if($('formContatti')) var form=$('formContatti');
		if($('formDistributori')) var form=$('formDistributori');
		if(form!=''){	
			var ragineSociale=form.getElement('#labelRagioneSociale');
			var NomeCognome=form.getElement('#labelNomeCognome');
			var TipoCliente=form.getElement('#TipologiaCliente');
			if(TipoCliente.get('value')=='Privato'){
				NomeCognome.setStyle('display','inline');
				ragineSociale.setStyle('display','none');
			}else{
				ragineSociale.setStyle('display','inline');
				NomeCognome.setStyle('display','none');
			}
			TipoCliente.addEvent('change',function(){
				if(TipoCliente.get('value')=='Privato'){
					NomeCognome.setStyle('display','inline');
					ragineSociale.setStyle('display','none');
				}else{
					ragineSociale.setStyle('display','inline');
					NomeCognome.setStyle('display','none');
				}
			});			
		}
	};
	formContatti.run();
	
	var menuTendina=function (){
		if($('menuTendina')){
			var object=$('menuTendina');
			object.addEvent('mouseenter',function(){ this.addClass('hover'); });
			object.addEvent('mouseleave',function(){ this.removeClass('hover'); });
		}
	}
	menuTendina.run();
	
	var jsGallery=function (){
		if($('gallery')){
			var gallery=$('gallery');
			var bigPhoto=gallery.getElement('.bigFoto img');
			var antPhoto=gallery.getElements('.antFoto a');
			var desc=gallery.getElements('.bigFoto .titoloImg');
			antPhoto.each(function(item,i){
				if(i==0) item.addClass('active');
				item.addEvent('click',function(e){
					new Event(e).stop();
					var myImage = new Asset.image(item.getProperty('href'), {id: 'myImage',	title: 'myImage'});
					myImage.replaces(bigPhoto);
					bigPhoto=myImage;
					antPhoto.each(function(item){item.removeClass('active');});
					item.addClass('active');
					desc.set('text',item.getElement('.desc').get('text'));					
				});
			});
		}	
	};
	jsGallery.run();
	
	var slider = function(){		
		var box=$('slider');
		if(box){
			var photos=box.getElements('.foto');
			var daCaricare=box.getElements('a.loadImg');
			var count=0;
			if(photos.length>1){
				
				photos.each(function(item,i){
					if(i==count) item.addClass('active');
					else item.setStyle('left',660);
				});
				
				var myImages = Asset.images(daCaricare.get('text'), {
				    onComplete: function(){
				        daCaricare.each(function(item,i){
				        	var image = new Element('img', {src: item.get('text') });
							image.inject(item,'after');
							item.destroy();
				        });
				        var periodical = undefined;
						var autoRotation = function(){
		                    if(periodical == undefined ){
		                    	periodical = autoRotation.periodical(5000);
		                    }
		                    effetto.apply();
						}
						autoRotation.delay(3000);
				    }
				});
				
				var effetto = function(){
	        		var oldFoto=photos[count];
	        		count++;
	        		count=count%photos.length;
					var newFoto = photos[count];
					
	        		var newFotoFX = new Fx.Tween(newFoto, {
					    duration: 1000,
					    transition: Fx.Transitions.Quint.easeOut,
					    link: 'cancel',
					    property: 'left',
					    onComplete: function(){
					    	oldFoto.setStyle('left',660);
					    }
					});
					newFoto.addClass('active');
					oldFoto.removeClass('active');
					newFotoFX.start(660,0);
				};
			}
		}
	}.apply();
});
