// JavaScript Document

//	if javascript is supported load immediately an additional css, just to avoid layout flickering...
new Asset.css(ABS+'/additional_style.css');



//	PHOTO DATA	//
var photos = {
	serramenti:
		[
			{
				url:	'images/thumb_box.png',
				title:	'Titolo'
			},
			{
				url:	'images/thumb_box.png',
				title:	'Titolo 2'
			},
			{
				url:	'images/thumb_box.png',
				title:	'Titolo 3'
			}
		],
	persiane:[]}	// MAH ...


var gallery = {
	
	init:function(e) {
	
		e = (e) ? e : 'general';
		//alert($type(e));
		
		this.photoContainer = $$('.photoGallery')[0];
		if ($type(this.photoContainer)) {
			this.allPhotos = $$('.photoGallery li a');
			
			this.serramenti = $$('.photoGallery .serramenti');
			this.persiane = $$('.photoGallery .persiane');
			this.verande = $$('.photoGallery .verande');
			this.zanzariere = $$('.photoGallery .zanzariere');
			this.avvolgibili = $$('.photoGallery .avvolgibili');
			this.porte = $$('.photoGallery .porte');
			this.cancelletti = $$('.photoGallery .cancelletti');
			
			this.displayThumbs(e)
		}
		return $chk(this.photoContainer);
	},
	hideContainer:function(e) {
		new Fx.Styles(this.photoContainer,{duration:200,wait:false, onComplete:this.displayThumbs.pass(e,this)}).start({'opacity':[1,0]});
	},
	showContainer:function() {
		new Fx.Styles(this.photoContainer,{duration:200,wait:false}).start({'opacity':[0,1]});
	},

	displayThumbs:function(e) {
		this.allPhotos.each(function(item) {
			if (item.hasClass(e)) {
				item.getParent().setStyle('display','block');
				item.getFirst().setProperty('title',e);
				item.setProperty('rel','lightbox['+e+']');
			} else {
				item.getParent().setStyle('display','none');
			}
		});
		this.showContainer();
	},
	show:function(e) {
		this.hideContainer(e);
	}
}












function isIE() {
	returnVal = (navigator.appName.indexOf("Microsoft")!=-1) ? true : false;
	return returnVal;}	//	RESTITUISCE TRUE SE IL CLIENT BROWSER E' INTERNET EXPLORER	// DEPRECATO.... UTILIZZARE PROPRIETA' WINDOW DI MOOTOOLS
function checkArrow(item) {
	
		item.properties=item.getSize();
													//
		if ((item.properties.scroll.y+item.properties.size.y)>=item.properties.scrollSize.y) item.getParent().getElementsBySelector('.goDown')[0].addClass('disabled').setStyle('opacity','.25');
		else item.getParent().getElementsBySelector('.goDown')[0].removeClass('disabled').setStyle('opacity','1');
		
		if ((item.properties.scroll.y)<=0) item.getParent().getElementsBySelector('.goUp')[0].addClass('disabled').setStyle('opacity','.25');
		else item.getParent().getElementsBySelector('.goUp')[0].removeClass('disabled').setStyle('opacity','1');
	}	//	GESTISCE LE FRECCE DEL CONTENT SCROLLER
	
function intro() {
/*		INTRO		*/
	/*$$('#wrapper').each(function(wrap) {
		wrap.iniFade = new Fx.Styles(wrap,{wait:false, duration:800, transistion:Fx.Transitions.quadOut});
		wrap.iniFade.start({'opacity':[0,1]});
	});	*/
	i=0;
	$$('.introFx').each(function(block) {
	block.setStyle('opacity',0);
	var openUp = new Fx.Styles(block,{duration:400, wait:false,transition:Fx.Transitions.sineOut});
	function openBlock() {
		var o = openUp.start({'opacity':[0,1]})
	}
	openBlock.delay(250*i);
	i++;
	
	});


/*		END INTRO ROUTINE	*/}
function resetProductList() {
		$$('.list_product p').each(function(item) {
			item.effect = new Fx.Styles(item, {	wait:false, duration:250 });
			if (item.getStyle('opacity') != 0) item.effect.start({'opacity':[1,0],'height':[item.getStyle('height').toInt(),0]});
		});
	}

window.onDomReady(function() {
	gallery.init();
	
	if ($E("#slideshow")) {
		if ($("slideshow").hasClass('home')) {
			var so = new SWFObject("image_loader.swf", "infall_slideshow", "426", "280", "8", "#fff");
			so.addParam("wmode", "transparent");
			so.write("slideshow");
		}
	}
	
//	Disabling all # linked a tags
	$$('a').each(function(item) {
		if (item.href.charAt(item.href.length-1)=="#") {
			item.onclick=function() {
				this.blur();
				return false;
			}
		}
	});
	// end
						   
						   
	/*	STEP 1	: hiding wrapper	*/
	/*$$('#wrapper').each(function(wrap) {
		wrap.iniFade = new Fx.Styles(wrap,{wait:false, duration:800, transistion:Fx.Transitions.quadOut});
		wrap.iniFade.start({'opacity':[0,1]});
	});*/
	
	/*	STEP 2	:	*/
	
	if (window.ie6) {
		$$('#navBar_1 li' ,'#navBar_2 li' ).each(function(li) {
			//alert (li.getSize().size.y);
			li.setStyle('height',14);
		});
		/*$$('label').each(function(l) {
			l.onmouseover = function() {l.setStyle('background','#eee')};
			l.onmouseout = function() {l.setStyle('background','#fff')};
		});*/
		
	}	//	IE HACK
	$$('#preventivo a').each(function(a) {
		a.setStyle('opacity',.75);
		a.FX = new Fx.Styles(a,{wait:false, duration:250});
		a.addEvent('mouseover',function() {
			a.FX.start({'opacity':[.65,1]});
		});
		a.addEvent('mouseout',function() {
			a.FX.start({'opacity':[1,.65]});
		});		
	});
	$$('#navBar_1 a').each(function(a) {
		if (!a.hasClass('notActive')) {
			a.bg_color_start = a.getStyle('background-color');
			a.color_start = a.getStyle('color');			
			a.bg_color_end = '#006';
			a.color_end = '#fff';
			a.FX = new Fx.Styles(a,{wait:false, duration:250,transition:Fx.Transitions.sineOut});
			a.addEvent('mouseover',function() {
				a.FX.start({'background-color':[a.bg_color_start,a.bg_color_end],'color':[a.color_start, a.color_end], 'padding-left':[0,8]});
			});
			a.addEvent('mouseout',function() {
				a.FX.start({'background-color':[a.bg_color_end,a.bg_color_start],'color':[a.color_end, a.color_start],'padding-left':[8,0]});
			});
		} else {
			a.onclick = function(){ this.blur(); return false; };
		}
	});	//	GESTIONE BEHAVIOUR	NAVBAR_1
	$$('#navBar_2 a','#partners a').each(function(a) {
		a.FX = new Fx.Styles(a,{wait:false, duration:250, transition:Fx.Transitions.sineOut});
		a.ini_pad = a.getStyle('padding-left').toInt();
		a.end_pad = a.ini_pad+5;
		a.addEvent('mouseover',function() {
				a.FX.start({'padding-left':[a.ini_pad,a.end_pad]});
			});
			a.addEvent('mouseout',function() {
				a.FX.start({'padding-left':[a.end_pad,a.ini_pad]});	
			});
	
	});	//	GESTIONE BEHAVIOUR	NAVBAR_2
	$$('#navBar_2 li ul').each(function(item) {
		item.setStyle('display','block');
		item.finalPadding = item.getStyle('padding');
		item.finalHeight = item.getStyle('height').toInt();
		item.setStyles({'height':0,'padding':0,'opacity':0});
		
		item.FX = new Fx.Styles(item,{wait:false,duration:400,transition:Fx.Transitions.sineOut});
		//alert(item.getParent().getParent().getFirst().getFirst().getTag())
		item.getParent().addEvent('mouseenter',function() {
			//alert(item.getParent().getTag())
			//if (item.getStyle('height').toInt() == 0) 
			item.FX.start({'height':[0,item.finalHeight],'opacity':[0,1]});
			//else 
		});
		item.getParent().addEvent('mouseleave',function() {
			item.FX.start({'height':[item.finalHeight,0],'opacity':[1,0]});
		});
	});
	/*	GESTIONE CONTENT SCROLLER	*/
	$$('.notScrollable .goUp', '.notScrollable .goDown').each(function(item) {
		item.remove();
	});
	$$('.scrollable').each(function(item) {
		checkArrow(item);
		item.scrollFx = new Fx.Scroll(item,{wait:false, 
											duration:400, 
											transition:Fx.Transitions.sineOut,
											onComplete:function() {checkArrow(item);}
	});
		
		//	DOWN ARROW
		
		item.getParent().getElementsBySelector('.goDown').each(function(down){
			down.onclick = function() {
				if (!down.hasClass('disabled')) {
					down.amountScroll= item.getSize().scroll.y + item.getSize().size.y
					item.scrollFx.scrollTo(0,down.amountScroll);
					//alert(down.amountScroll);
				}
			down.blur();
			return false;
			};	//end event attached
		});

		//	UP ARROW
		//alert($$('.goUp').length);
		item.getParent().getElementsBySelector('.goUp').each(function(up){
			up.onclick = function() {
				if (!up.hasClass('disabled')) {
					up.amountScroll= item.getSize().scroll.y - item.getSize().size.y
					item.scrollFx.scrollTo(0,up.amountScroll);
				}
			up.blur();
			return false;
			};	//end event attached
		});
		
	});	//end each	
	
	$$('.scrollToId').each(function(a) {
		if ($(a.rel)) {
			a.scrollFx = new Fx.Scroll($(a.rel).getParent(),{wait:false, 
											duration:400, 
											transition:Fx.Transitions.sineOut,
											onComplete:function() {checkArrow($(a.rel).getParent());}
										});
			a.addEvent('click',function() {
				a.scrollFx.scrollTo(0, $(a.rel).getCoordinates().top-234)
			});
		}
	});
	$$('.scrollToTop').each(function(a) {
			a.scrollFx = new Fx.Scroll(a.getParent(),{wait:false, 
											duration:500, 
											transition:Fx.Transitions.sineOut,
											onComplete:function() {checkArrow(a.getParent());}
										});
			a.addEvent('click',function() {
				a.scrollFx.scrollTo(0, 0)
			});
	});
	
	/*	FINE GESTIONE CONTENT SCROLLER	*/
	$$('.list_product h3').each(function(item) {
		//	init	//
		//item.getParent().setStyle('background-color','#eee');
		if (!item.getNext().hasClass('this_area')) {
			item.getNext().setStyles({'display':'block','opacity':0});
		} else {
			gallery.show(item.getProperty('title').toLowerCase());			//	All'apertura della pagina verifica che sia stata aperta
																			//  un prodotto specifico e nel caso 
																			//	filtra le immagini della gallery
		}
		item.fHeight = item.getNext().getStyle('height');
		if (!item.getNext().hasClass('this_area')) item.getNext().setStyles({'height':'0px'})		
		item.effect = new Fx.Styles(item.getNext(), {wait:false, duration:250});
		item.onclick = function() {
			if (item.getNext().getStyle('opacity') == 0) {
				resetProductList();
				item.effect.start({'opacity':[0,1],'height':[0,item.fHeight]});
				gallery.show(item.getProperty('title').toLowerCase());
			}
			else item.effect.start({'opacity':[1,0],'height':[item.fHeight,0]});
			
				
		}
	});
	$$('p.this_area').each(function(i) {
		//window.scrollTo(i.getPosition().x,i.getPosition().y);
		new Fx.Scroll(window,{duration:800,transition:Fx.Transitions.quadInOut}).toElement(i);
	});
			
	if (window.ie6) {
		$$('.text').each(function(i) {
			i.onfocus = function() { i.addClass('hover');}
			i.onblur = function() { i.removeClass('hover');}
		}); //	GESTION ON FOCUS ELEMENT FORM
	}
	var myTips = new Tips($$('.toolTipImg'), {
							  onShow:function(tip) {new Fx.Styles(tip,{wait:false,duration:250}).start({'opacity':[0,.75]});},
							  onHide:function(tip) {new Fx.Styles(tip,{wait:false,duration:250}).start({'opacity':[.75,0]});},
							  offsets: {
								  x:-75,
								  y:20
							  }
	});	// GESTIONE TOOLTIPS
	
	$$('.materiale').each(function(item) {
		var materiale = {'Alluminio':['Verniciato', 'Anodizzato', 'Alluminio legno'],'PVC':['Bianco','Rivestito tipo legno']};
		
		item.addEvent('change',function() {
			select = new Element('select',{'name':'tipo_materiale','id':'tipo_materiale','class':'text half'});
			materiale[item.value].each(function(e) {
				o = new Element('option',{'value':e}).appendText(e);
				select.adopt(o);
			});
			$('tipo_materiale').replaceWith(select);
			$('tipo_materiale').focus();
		});
	});
	
	 $$('a._blank').each(function(a) {
		a.rel = a.href;
		a.href="#";
		a.addEvent('click',function() {
			window.open(a.rel,'InfallNewWindow');
		});
	 });

	if ($$('form .submit')[0]) {
		submitBtn = $$('form .submit')[0];
		
		// DISATTIVO IL SUBMIT BUTTON [DA ATTIVARE TRAMITE CONSENSO DATI PERSONALI] //
		submitBtn.setProperty('disabled','disabled')
		submitBtn.setStyle('opacity',.5)
		
		submitBtn.onclick = function() {
			returnVal = true;
			$$('.requiredField').each(function(item) {
				if (item.value.length==0) {
					item.getParent().addClass('error');
					if (!window.ie6) item.getParent().setStyles({'background-color':'#ffffef'});	// ONE MORE IE6 HACK >:-|
					var myTips = new Tips(item.getParent(), {
							  onShow:function(tip) {new Fx.Styles(tip,{wait:false,duration:250}).start({'opacity':[0,.75]});},
							  onHide:function(tip) {new Fx.Styles(tip,{wait:false,duration:250}).start({'opacity':[.75,0]});},
							  offsets: {
								  x:-75,
								  y:20
							  }
						});					
					returnVal=false;
				}
			});
			if (!returnVal) {new Fx.Scroll(window).scrollTo(0,$('content').getPosition().y)}
			
			return returnVal;
		}
	}

	if ($('disable_submit_onclick')) {
		$('disable_submit_onclick').onclick = function() {
			if (this.checked) {
				submitBtn = $$('.submit')[0];
				submitBtn.setProperty('disabled','disabled');
				submitBtn.setStyle('opacity',.5);
			}
		}
	}
	if ($('abilitate_submit_onclick')) {
		$('abilitate_submit_onclick').onclick = function() {
			if (this.checked) {
				submitBtn = $$('.submit')[0];
				submitBtn.removeProperty('disabled');
				submitBtn.setStyle('opacity',1);
			}
		}	
	}



});
window.addEvent('load',function() {
	intro();
});

