// JavaScript Document

var contactForm=null;

function init(){
	initHeaderMenu();
	initDynamicMenu();
	initContactForm();
}

function initHeaderMenu(){
	var productsDDMenu=new DropDownMenu("menu_products");
	var solutionsDDMenu=new DropDownMenu("menu_solutions");
}

function initDynamicMenu(){
	var dynamicMenu=new DynamicMenu("dyn_image");
}

function initContactForm(){
	if($id("contact_form")){
		var textarea=$id("textarea");
		contactForm=new Form("contact_form");
		contactForm.populate(true);
		var rdyStyle={
			backgroundColor:"#FFFFFF",
			border:"1px solid #E1E1E1"
		};
		var unrdyStyle={
			backgroundColor:"#ffd2a6",
			border:"1px solid #ff754b"
		};
		contactForm.setField(0, false, ValueConstraint.NAME, rdyStyle, unrdyStyle);
		contactForm.setField(1, false, ValueConstraint.EMAIL, rdyStyle, unrdyStyle);
		contactForm.setField(2, true, ValueConstraint.TEL_NUMBER, rdyStyle, unrdyStyle);
		var http=new HttpRequest("code/mailer.php");
		http.onResponse(function(e){
			if(e.responseText=="0"){
				$class(/form/).get(0).setBackgroundColor("#e1ffd5");
				alert("Messaggio correttamente inoltrato, verrai ricontattato al pił presto!");
			}
			else{
				alert("Vi sono problemi con il servizio, risolveremo al piu' presto.");
			}
		});
		contactForm.setHttpRequest(http);
		$id("form_submitter").onClick(function(e){
			if(textarea.value.trim().length==0){
				alert("Il messaggio non puo' essere vuoto.");
				return;
			}
			if(!contactForm.submit(true,true)){
				alert("Alcuni campi non sono stati compilati correttamente.");
			}
		});
	}
}

Class({
	public:{
		DropDownMenu:function(id, maxHeight){
			$(this).menuItem=$id(id);
			$(this).menu=$id($(this).menuItem.target);
			$(this).menu.setX($(this).menuItem.outerX());
			$(this).menu.setOpacity(0.9);
			$(this).maxHeight=maxHeight||$(this).menu.getChildNodes().size()*30+"px";
			$(this).ddMotionDown=new Interpolation($(this).menu, {
				height:["0px", $(this).maxHeight]
			}, Elastic.easeOut);
			$(this).ddMotionUp=new Interpolation($(this).menu, {
				height:[$(this).maxHeight, "0px"]
			},{
				easing:Pow(2).easeIn,
				numFrames:20,
				delay:20
			});
			$(this).ddMotionUp.onMotionComplete(function(e){
				$(this).menuItem.removeClass("hover");
			} ,this);
			$(this).overHandler=$(this).menuItem.onMouseOver(function(e){
				this.open();
			}, this);
			$(this).outHandler=$(this).menu.onMouseOut(function(e){
				e.stopPropagation();
				var node=e.toElement.nodeName.toUpperCase();
				var parents=$$(e.toElement).getParentNodes().sift({id:$(this).menu.id});
				if(parents.size()!=1&&e.toElement.id!=$(this).menu.id){
					this.close();
				}
			}, this);
			$(this).menuItem.onMouseOut(function(e){
				if(e.toElement.id==$(this).menu.id) return;
					this.close();
			}, this);
		},
		open:function(){
			$(this).overHandler.idle();
			$(this).outHandler.wakeup();
			$(this).menuItem.addClass("hover");
			$(this).ddMotionUp.stop();
			$(this).ddMotionDown.continueTo({ height:$(this).maxHeight });
			$(this).ddMotionDown.start();
		},
		close:function(){
			$(this).outHandler.idle();
			$(this).overHandler.wakeup();
			$(this).ddMotionDown.stop();
			$(this).menu.setHeight(0);
			$(this).menuItem.removeClass("hover");
		}
	}
});

Class({
	public:{
		DynamicMenu:function(id){
			$(this).dynImage=$id(id);
			if(!$(this).dynImage) return;
			$(this).current=0;
			$(this).url=URL.current();
			$(this).linkArray=["?page=fingerpay","?page=fingerpass","?page=fingercheck"];
			$(this).dynSelectors=$id("dynamic_tools").$class("dyn-selector",null,true);
			$(this).dynSelectors.get(0).addClass("current");
			$$($(this).dynImage.parentNode).href=$(this).url.hostname+$(this).linkArray[$(this).current];
			for(var i=0;i<$(this).dynSelectors.size();i++){
				(function(i, self){
					$(self).dynSelectors.get(i).onClick(function(e){
						e.preventDefault();
						this.set(i);
					}, self);
				})(i, this);
			}
			
			var arrows=$id("dynamic_tools").$class(/arr-/,null,true);
			var arrLeft=arrows.get(0);
			var arrRight=arrows.get(1);
			arrLeft.onClick(function(e){
				this.set(--$(this).current);
			}, this);
			arrRight.onClick(function(e){
				this.set(++$(this).current);
			}, this);
			$(this).timer=new Timer(6000, function() {
				this.display(++$(this).current);
			}, this);
			$(this).timer.start();
		},
		display:function(index){
			if(index>=3) index=0;
			else if(index<=-1) index=2;
			$(this).current=index;
			var delta=-index*240;
			$(this).dynSelectors.removeClass("current");
			$(this).dynSelectors.get(index).addClass("current");
			$(this).dynImage.interpolate({
				opacity:0
			},{
				numFrames:10
			}).onMotionComplete(function(e){
				$(this).dynImage.setCss("background-position","0px "+delta+"px");
				$(this).dynImage.interpolate({
					opacity:1
				});
				$$($(this).dynImage.parentNode).href=$(this).url.hostname+$(this).linkArray[$(this).current];
			}, this);
		},
		set:function(index){
			if(index>=3) index=0;
			else if(index<=-1) index=2;
			$(this).current=index;
			var delta=-index*240;
			$(this).dynSelectors.removeClass("current");
			$(this).dynSelectors.get(index).addClass("current");
			$(this).dynImage.setCss("background-position","0px "+delta+"px");
			$(this).dynImage.setOpacity(1);
			$$($(this).dynImage.parentNode).href=$(this).url.hostname+$(this).linkArray[$(this).current];
			$(this).timer.restart();
		}
	}
})

function changeLanguage(lang){
	var url=URL.current();
	url.querystring=url.querystring.replace(/(&*lang=.*)/, "");
	url.addQuery("lang",lang);
	url.locate();
}
