window.addEvent('domready', function(){
	
	var tabs = function(){
		var boxTabs=$('boxTabs');
		if(boxTabs){
			var selectedTab=null;
			var selectedMenu=null;
			var height=0;
			var size=0;			
			boxTabs.addClass('ajax');
			
			boxTabs.getElements('.tabs').each(function(item,i){
				if(i==0){
					selectedTab=item;
					size=item.getSize().y;
					height=size;
				}else item.setStyle('opacity',0);
				var title=item.getElement('h3.title');
				if(title) title.destroy();
				//size=item.getSize().y;
				//if(size>height) height=size;
			});
			boxTabs.setStyle('height',height);
			
			var menuTab=$('menuTab');
			menuTab.getElements('a').each(function(item,i){
				if(i==0){
					item.addClass('active');
					selectedMenu=item;
				}
				item.addEvent('click',function(e){
					e.stop();
					var background= $('background');
					selectedMenu.removeClass('active');
					selectedMenu=item;
					item.addClass('active');
					
					selectedTab.setStyle('opacity',0);
					selectedTab=$(item.get('href').replace('#',''));
					selectedTab.setStyle('opacity',1);
					
					var pattern = /#(.+)/;
					var stringa = item.get('href');
					var result = stringa.match(pattern);
					if(result){
						var box=$(result[1]);
						if(box){
							size=$(box).getSize().y;
							if(background){
								background.tween('height',background.getStyle('height').toInt()+(size-height));
							}
							boxTabs.tween('height',size);
							height=size;
						}
					}
				});
			});
		}
	};
	tabs.apply();
	
	var grafica = function(){
				
		var windowsSize=window.getScrollSize();
		var windowsScroll=window.getScroll();
		var container=$('container').getCoordinates();
		var headSize=$('header').getSize();
		var footerSize=$('footer').getCoordinates();
		
		
		if(windowsSize.x>1018)
			var width=windowsSize.x;
		else
			var width=1018;
		//Background
		var background = new Element('div',{ 
			id: 'background',
			styles: {
				'z-index' : -1,
				'overflow' : 'hidden',
				'position' : 'absolute',
				'top' : 0,
				'left' : 0,
				'width' : width,
				'height': windowsSize.y
			}
		});
		background.inject(document.getElement('body'));
		
		
		var footerheight=footerSize.height+130;
		var footerBottom=0;
		if(windowsScroll.y==0){
			footerheight=windowsSize.y-container.bottom+402;
		}
		var background2 = new Element('div',{ 
			id: 'background2',
			styles: {
				'overflow' : 'hidden',
				'position' : 'absolute',
				'bottom' : footerBottom,
				'left' : 0,
				'width' : '100%',
				'height': footerheight
			}
		});
		background2.inject(background);
		
		var paper = Raphael(background, width, windowsSize.y);
		var gradienteTop = paper.rect(0,0,width,headSize.y);
		gradienteTop.attr({ fill: "270-#aaaaaa-#e8e8e8", stroke: "none" });
		var radiceTop= paper.image(PATH_IMAGE_TEMPLATE+'/radice-promos.png',((windowsSize.x/2)-670), 0, 382, 402);
		
		var paper2 = Raphael(background2, width, footerheight);
		var gradienteBottom = paper2.rect(0,0,width,footerheight);
		gradienteBottom.attr({ fill: "90-#aaaaaa-#e8e8e8", stroke: "none" });
		
		var xBottom=((windowsSize.x)/2)+235;
		if(windowsScroll.x>0) xBottom=(980/2)+235;
		var radiceBottom=paper2.image(PATH_IMAGE_TEMPLATE+'/radice-promos.png',xBottom, -50, 382, 402);
		
		window.addEvent('resize', function(){
			background.setStyle('display', 'none');
			windowsSize=window.getScrollSize();
			container=$('container').getCoordinates();
			if(windowsSize.x>1018) width=windowsSize.x;
			else width=1018;
			background.setStyles({'display': 'block', 'width': width, 'height': windowsSize.y });
			paper.setSize(width,windowsSize.y);
			paper2.setSize(width,windowsSize.y);
			
			gradienteTop.attr({width: width});
			
			gradienteBottom.attr({width: width});			
			
			xBottom=((windowsSize.x)/2)+235;
			if(windowsScroll.x>0) xBottom=(980/2)+235;	
			radiceBottom.attr({ x: xBottom });
			
			radiceTop.attr({ x: ((windowsSize.x/2)-670)});
		});
	};
	grafica.apply();
	
		
	var labelMenuSelect = function(){
		var labelMenuSelect=document.getElement('#menuMain .active a');
		if(labelMenuSelect){
			var size=labelMenuSelect.getSize();			
			
			var background = new Element('div',{ 
			id: 'bkgMenuActive',
				styles: {
					'overflow' : 'hidden',
					'position' : 'absolute',
					'top' : 0,
					'left' : 0,
					'width' : size.x,
					'height': size.y
				}
			});
			background.inject(labelMenuSelect.getParent('li'),'top');
			
			var paper = Raphael(background, size.x, size.y);
			var radiceTop= paper.image(PATH_IMAGE_TEMPLATE+'/label-menu.png', 14, 0, 266, 22);
			labelMenuSelect.setStyle('background-color','transparent');
			//var scritta = paper.print(100, 10, "TEST", paper.getFont("Helvetica-CondensedLight",100), 14);
		}
		
	};
	labelMenuSelect.apply();
	
	var galleryHead = function(){		
		var box=$('galleryHead');
		if(box){
			var photos=box.getElements('span.loadImg');
			var galleryPhoto=box.getElements('.foto');
			var active=0;
			var lock=false;
			var count=0;
			var periodicalRestart;
			
			var labelGallery = new Element('div',{	id: 'labelGallery'});
			labelGallery.inject(box);
			var paper = Raphael(labelGallery, 358, 62);
			var labelgrande= paper.image(PATH_IMAGE_TEMPLATE+'/label-grande.png', 0, 0, 358, 62);
			
			galleryPhoto.each(function(item,i){
				if(i!=active) item.setStyle('opacity',0);
				else{
					desc=item.getElement('.desc');
					if(desc && desc.get('html').trim()!=''){
						desc.clone().inject(labelGallery);
						labelGallery.setStyle('display','block');
					}  
				}
			});
			
			var myImages = Asset.images(photos.get('text'), {
			    onComplete: function(){
			        photos.each(function(item){
			        	var image = new Element('img', {
						    src: item.get('text'),
						    width: 680,
						    height: 365						    
						});
						image.inject(item,'after');
						item.destroy();						
			        });
			    }
			});
			var effetto = function(avanzamento){
				if(lock==false){
	        		lock=true;
	        		/*var oldFoto = new Fx.Tween(galleryPhoto[active.abs()], {
					    duration: 1000,
					    transition: 'linear',
					    link: 'cancel',
					    property: 'opacity'
					});*/
					var oldFoto = galleryPhoto[active.abs()];
					
					if(avanzamento==1) active++;
					else active--;
		        	active=active%galleryPhoto.length;
		        	var newFoto = new Fx.Tween(galleryPhoto[active.abs()], {
					    duration: 1000,
					    transition: 'linear',
					    link: 'cancel',
					    property: 'opacity',
					    onComplete: function(){
					    	oldFoto.setStyle('opacity',0);
					    	lock=false;
					    }
					});
					
					
					desc=labelGallery.getElement('.desc')
					if(desc) desc.destroy();
					
					desc=galleryPhoto[active.abs()].getElement('.desc');					
					if(desc && desc.get('html').trim()!=''){
						desc.clone().inject(labelGallery);
						labelGallery.setStyle('display','block');
					}else{
						labelGallery.setStyle('display','none');
					}
						
					
					//oldFoto.start(0);
					newFoto.start(1);
		        }		
			};
			
			var autoRotation = function(){ effetto.apply(1);};
			var periodical = autoRotation.periodical(5000);
			
			
			
			
			/*var bkgDescGallery=box.getElements('.desc');
			
			if(bkgDescGallery){
				box.addClass('ajax');
				bkgDescGallery.each(function(item){
						
					var background = new Element('div',{ 
						'class': 'bkgDescGallery',
						styles: {
							'overflow' : 'hidden',
							'position' : 'absolute',
							'top' : 0,
							'left' : 0,
							'width' : size.x,
							'height': size.y
						}
					});
					background.inject(item,'top');
					var paper = Raphael(background, size.x, size.y);
					var radiceTop= paper.image(PATH_IMAGE_TEMPLATE+'/label-grande.png', 0, 0, 358, 62);
				});
			}*/
		}
	}.apply();
	
	
	var gallery=function(){
		var gallery=$('gallery') || $('boxImg');
		if(gallery ){
			var allPhoto=gallery.getElements('a.img');
			allPhoto.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 indexPhoto=allPhoto.indexOf(item);
										
					var mask=$('mask');
					if(!mask){
						var mask=new Element('div',{ 
							id: 'mask',
							styles: {
								'background-color': '#000',
								'opacity': 0.4,
								'z-index' : 998,
								'overflow' : 'hidden',
								'position' : 'absolute',
								'top' : 0,
								'left' : 0,
								'width' : windowsScrollSize.x,
								'height': windowsScrollSize.y
							},
							events: {
								'click': function(){
									mask.setStyle('display','none');
									var imgAttuale=imgFocus.getElement('img');
									if(imgAttuale) imgAttuale.destroy();
									imgFocus.setStyle('display','none');
								}
							}
						});
						mask.inject(document.getElement('body'),'bottom')	
					}
					
					var imgFocus=$('imgFocus');					
					if(!imgFocus){
						var imgFocus=new Element('div',{ id: 'imgFocus'});
						imgFocus.inject(document.getElement('body'),'bottom');
						imgFocus.set('morph', {
				    		duration: 'short', 
				    		transition: 'linear',
				    		onComplete: function(){
				    		}
			    		});	
			    		
			    		if(allPhoto.length>1){
			    			var indietroFocus=new Element('div',{ 
								'class' : 'freccia indietro',
								events: {
									'click': function(){
										myImage.setStyle('opacity',0.4);
										indexPhoto=(indexPhoto-1)%(allPhoto.length);
										if(indexPhoto<0) indexPhoto=allPhoto.length-1;
										var myImageLoad = Asset.image( allPhoto[indexPhoto], {
										    onLoad: function(){
										    	var imgAttuale=imgFocus.getElement('img');
												if(imgAttuale){
													imgAttuale.set('src',myImageLoad.get('src'));
										    		imgAttuale.setStyle('opacity',1);	
												}
										    }
										});
									}
								}
				    		});
				    		indietroFocus.inject(imgFocus);
				    		
				    		var avantiFocus=new Element('div',{
								'class' : 'freccia avanti',
								events: {
									'click': function(){
										myImage.setStyle('opacity',0.4);
										indexPhoto=(indexPhoto+1)%(allPhoto.length);
										var myImageLoad = Asset.image( allPhoto[indexPhoto], {
										    onLoad: function(){
										    	var imgAttuale=imgFocus.getElement('img');
												if(imgAttuale){
													imgAttuale.set('src',myImageLoad.get('src'));
										    		imgAttuale.setStyle('opacity',1);	
												}
										    }
										});
									}
								}
				    		});
				    		avantiFocus.inject(imgFocus);
			    		}			    		
			    		
			    		var closeFocus=new Element('div',{
							'class' : 'close',
							events: {
								'click': function(){
									mask.setStyle('display','none');
									var imgAttuale=imgFocus.getElement('img');
									if(imgAttuale) imgAttuale.destroy();
									imgFocus.setStyle('display','none');
								}
							}
			    		});
			    		closeFocus.inject(imgFocus);
			    		
					}
					var imgAttuale=imgFocus.getElement('img');
					if(imgAttuale) imgAttuale.destroy();
					imgFocus.setStyle('display','none');

					var myImage = Asset.image( item.get('href'), {
					    id: 'myImage',
					    title: 'myImage',
					    styles: {
							'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.morph({
					    		height: imgHeight,
					    		width: imgWidth,
					    		top: windowsSize.y/2+windowsScroll.y-imgHeight/2-10,
					    		left: windowsSize.x/2+windowsScroll.x-imgWidth/2-10
					    	});
					    }
					});

				});
			});
		}
	}.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();}}}));
}));}};
