window.addEvent('domready', function(){
	
	var grafica = function(){
		var head=document.getElement('#header .limit');
		var headSize=head.getSize();
		
		//Background
		var background = new Element('div',{ 
			id: 'background',
			styles: {
				'overflow' : 'hidden',
				'position' : 'absolute',
				'top' : 0,
				'left' : '50%',
				'margin-left' : -490,
				'width' : 980,
				'height': headSize.y
			}
		});
		background.inject(head,'before');
		
		var paper = Raphael(background, headSize.x, headSize.y);
		var ellisseTop = paper.ellipse(headSize.x/2, 0, 490, 200);
		ellisseTop.attr({ fill: "r(0.5, 0.5)#353535-#1F1F1F", stroke: "none" });
		
		if(Browser.ie6){
			var logo=document.getElement('#logo img');
			if(logo){
				var logoSize=logo.getSize();
				var logoPng= paper.image(logo.get('src'),((headSize.x/2)-(logoSize.x/2)), 0, logoSize.x, logoSize.y);
				logo.destroy();
			}			
		}
	};
	grafica.apply();
	
	var gallery=function(){
		var gallery=$('gallerySpalla');
		if(gallery ){
			gallery.getElements('a.antFoto').each(function(item){
				item.addEvent('click',function(event){
					event.stop();
					item.setStyle('opacity','0.4');
					var windowsSize=window.getSize();
					var windowsScrollSize=getScrollSize();
					var windowsScroll=window.getScroll();
					var coordinate=item.getCoordinates();
					var borderSize=2;
					
					var mask=$('mask');
					if(!mask){
						var mask=new Element('div',{ 
							id: 'mask',
							styles: {
								'background-color': '#000',
								'opacity': 0.8,
								'z-index' : 998,
								'overflow' : 'hidden',
								'position' : 'absolute',
								'top' : 0,
								'left' : 0,
								'width' : windowsScrollSize.x,
								'height': windowsScrollSize.y
							},
							events: {
								'click': function(){
									mask.setStyle('display','none');
									imgFocus.empty();
									imgFocus.setStyle('display','none');
								}
							}
						});
						mask.inject(document.getElement('body'),'bottom')	
					}
					
					var imgFocus=$('imgFocus');					
					if(!imgFocus){
						var imgFocus=new Element('div',{ 
							id: 'imgFocus',
							styles: {
								//'background-color': '#0f0',
								'z-index' : 999,
								'overflow' : 'hidden',
								'position' : 'absolute',
								'top' : 0,
								'left' : 0,
								'width' : 0,
								'height': 0,
								'border': '0px solid #C2AA4A'
							},
							events: {
								'click': function(){
									mask.setStyle('display','none');
									imgFocus.empty();
									imgFocus.setStyle('display','none');
								}
							}
						});
						imgFocus.inject(document.getElement('body'),'bottom')	
					}
					imgFocus.empty();
					imgFocus.setStyle('display','none');					
					
					var myImage = Asset.image( item.get('href'), {
					    id: 'myImage',
					    title: 'myImage',
					    styles: {
							//'background-color': '#0f0',
							'width' : '100%',
							'height': '100%'
						},
					    onLoad: function(){
					    	var imgWidth=myImage.get('width');
					    	var imgHeight=myImage.get('height');
					    	item.setStyle('opacity',1);
					    	mask.setStyle('display','block');
					    	myImage.inject(imgFocus);					    	
					    	imgFocus.setStyles({
					    		'display': 'block',
					    		'width':coordinate.width,
					    		'height':coordinate.height,
					    		'top':coordinate.top,
					    		'left':coordinate.left
					    	});
					    	imgFocus.set('morph', {
					    		duration: 'short', 
					    		transition: 'linear'
					    	});
					    	imgFocus.morph({
					    		height: imgHeight,
					    		width: imgWidth,
					    		top: windowsSize.y/2+windowsScroll.y-imgHeight/2-borderSize,
					    		left: windowsSize.x/2+windowsScroll.x-imgWidth/2-borderSize,
					    		'border-width' : borderSize
					    	});
					    }
					});

				});
			});
		}
	}.apply();
	
	window.addEvent('resize', function(){
		var mask=$('mask');
		if(mask){
			var windowsScrollSize=getScrollSize();
			var windowsScroll=window.getScroll();
			mask.setStyles({'width' : windowsScrollSize.x, 'height': windowsScrollSize.y });
		}
	});
	
	
	var galleryHead = function(){		
		var box=$('galleryHead');
		if(box){
			var photos=box.getElements('a.loadImg');
			var galleryPhoto=box.getElements('.foto');
			var active=0;
			var count=0;
			var lock=false;
			var periodicalRestart;
			
			if(galleryPhoto.length>1){
				galleryPhoto.each(function(item,i){
					if(i!=active){
						item.setStyle('opacity',0);
					}else item.addClass('active');
				});
				
				//Segnaposto
				var widthSegnaposto=16;
				var widthStroke=2;
				var totalWidth=widthSegnaposto+widthStroke;
				var marginSegnaposto=5;
				var arraySegnaposto=new Array();
				var widthCanvas=(totalWidth*galleryPhoto.length)+(marginSegnaposto*(galleryPhoto.length-1))+2;
				var heightCanvas=totalWidth+2;
				
				var segnapostoBox = new Element('div',{ 
					id: 'segnapostoBox',
					styles: {
						'overflow' : 'hidden',
						'position' : 'absolute',
						'bottom' : 10,
						'right' : 10,
						'width' : widthCanvas,
						'height': heightCanvas
					}
				});
				segnapostoBox.inject(box);			
				var paper = Raphael(segnapostoBox, widthCanvas, heightCanvas);
	
				var arraySegnaposto=new Array();
				galleryPhoto.each(function(item, index){
					x=(index*(totalWidth+marginSegnaposto))+(totalWidth/2+1);
					var pallino=paper.circle(x, totalWidth/2+1, widthSegnaposto/2);
					arraySegnaposto[index]=pallino;
					pallino.attr({ fill: "#DFDFDF", "stroke-width": widthStroke, stroke: "#FFF" });
					if(index==0) 
					pallino.attr({ fill: "#C2AA4A"});
					pallino.click(function () {
						if(index!=active){
							var selezionaImg= effetto.pass({numFoto: index});
		                    selezionaImg();
		                    periodical= clearInterval(periodical);
		                    if(periodicalRestart == undefined )
						        periodicalRestart = autoRestart.periodical(1000);
						}
	                }).mouseover(function () {
	                	if(this.attrs.fill!='#C2AA4A')
		                	this.animate({fill: '#000'}, 500);
		           	}).mouseout(function () {
		           		if(this.attrs.fill!='#C2AA4A')
		            		this.animate({fill: '#DFDFDF'}, 500);
		           	});
		           	
				});
				
				
				var myImages = Asset.images(photos.get('text'), {
				    onComplete: function(){
				        photos.each(function(item){
				        	var image = new Element('img', {src: item.get('text')});
							image.inject(item,'after');
							item.destroy();
				        });
				    }
				});
				
				var effetto = function(){
					var args= {};
					if(arguments.length==0)
						args.avanzamento=1;
					else{
						args=arguments[0];
						if(typeOf(args)!='object' || (args.avanzamento == undefined && args.numFoto == undefined))
							args.avanzamento=1;
					}
					
					if(lock==false){
		        		lock=true;
		        		
		        		arraySegnaposto[active].stop();
		        		arraySegnaposto[active].attr({ fill: "#DFDFDF"});
		        		
		        		var oldFoto=galleryPhoto[active];
		        		var oldFotoFX = new Fx.Tween(oldFoto, {
						    duration: 1000,
						    transition: 'linear',
						    link: 'cancel',
						    property: 'opacity'
						});
						
						if(args.avanzamento){
							if(args.avanzamento==1) active++;
							else active--;
						}else{
							active=args.numFoto.toInt()
						}
						active=active%galleryPhoto.length;
						if(active<0)
							active=galleryPhoto.length+active;
						
						arraySegnaposto[active].stop();
						arraySegnaposto[active].attr({ fill: "#C2AA4A"});	
			        	
			        	var newFoto = galleryPhoto[active];
			        	var newFotoFX = new Fx.Tween(newFoto, {
						    duration: 1000,
						    transition: 'linear',
						    link: 'cancel',
						    property: 'opacity',
						    onComplete: function(){
						    	lock=false;
						    }
						});
						oldFotoFX.start(0);
						oldFoto.removeClass('active');
						newFotoFX.start(1);
						newFoto.addClass('active');
			        }		
				};
				
				var autoRotation = function(){ effetto.apply();};
				var periodical = autoRotation.periodical(3000);
				
				var autoRestart = function(){
					count++
					if(count==24){
						//console.log('restart');
						periodical = autoRotation.periodical(3000);
						periodicalRestart = clearInterval(periodicalRestart);
						count=0;
					} 
				};	
			}
		}
	}.apply();
});
	
// MooTools: the javascript framework.
// Load this file's selection again by visiting: http://mootools.net/more/417ab68a672f38ea1c04efab47af2ff7 
// Or build this file again with packager using: packager build More/Assets
/*
---
copyrights:
  - [MooTools](http://mootools.net)

licenses:
  - [MIT License](http://mootools.net/license.txt)
...
*/
MooTools.More={version:"1.3.1.1",build:"0292a3af1eea242b817fecf9daa127417d10d4ce"};var Asset={javascript:function(f,c){if(!c){c={};}var a=new Element("script",{src:f,type:"text/javascript"}),g=c.document||document,b=0,d=c.onload||c.onLoad;
var e=d?function(){if(++b==1){d.call(this);}}:function(){};delete c.onload;delete c.onLoad;delete c.document;return a.addEvents({load:e,readystatechange:function(){if(["loaded","complete"].contains(this.readyState)){e.call(this);
}}}).set(c).inject(g.head);},css:function(d,a){if(!a){a={};}var b=new Element("link",{rel:"stylesheet",media:"screen",type:"text/css",href:d});var c=a.onload||a.onLoad,e=a.document||document;
delete a.onload;delete a.onLoad;delete a.document;if(c){b.addEvent("load",c);}return b.set(a).inject(e.head);},image:function(c,b){if(!b){b={};}var d=new Image(),a=document.id(d)||new Element("img");
["load","abort","error"].each(function(e){var g="on"+e,f="on"+e.capitalize(),h=b[g]||b[f]||function(){};delete b[f];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(c,b){c=Array.from(c);var d=function(){},a=0;b=Object.merge({onComplete:d,onProgress:d,onError:d,properties:{}},b);return new Elements(c.map(function(f,e){return Asset.image(f,Object.append(b.properties,{onload:function(){a++;
b.onProgress.call(this,a,e,f);if(a==c.length){b.onComplete();}},onerror:function(){a++;b.onError.call(this,a,e,f);if(a==c.length){b.onComplete();}}}));
}));}};
