
callingajax = 0;
linked = 0;
timerhome = null;
lasturl = "";
window.addEvent('domready', function() {
	
$$('.web3d').addEvent("click", function(e){
	e.stop();
	location.href='/web3d/';
});
new Asset.css('/inc/mediabox/mediaboxAdvWhite.css');
new Asset.css('/css/button-fix.css');

$('link').setStyle('display','none');
$('ulpo').set('rel','li_portfolio');
$$('li#li_portfolio ul li').set('rel','li_portfolio');
$$('a[rel=blank]').set('target','_blank');
			if (Browser.Engine.trident && Browser.Engine.version <= 5) {
			
			$$('span[class=back]').addEvent("click", function(e){
			
			e.stop();
			history.back();
		});
			
			}
});

function like_fb()
{
	
	if ($('fb_container')) {
	
		if (lasturl == "/home/") {
			lasturlfb = "";
		}
		else 
		{
			if (lasturl != "") {
				lasturlfb = lasturl;
			}
			else {
				lasturlfb = psURI.get('directory');
				
				if(lasturlfb=="/home/") lasturlfb = "";
			}
		}
		$('fb_container').empty();
		//alert(lasturlfb)

			var fblike = new Element('iframe', {
				'src': 'http://www.facebook.com/plugins/like.php?href=www.ccbc.it' + lasturlfb + '&layout=button_count&show_faces=false&width=150&action=like&font=trebuchet+ms&colorscheme=dark&height=65',
				'scrolling': 'no',
				'allowTransparency': 'true',
				'frameborder': '0',
				'style': 'height:0px'
			});
			$('fb_container').adopt(fblike)
			fban = new Fx.Tween(fblike, {duration: 500,transition: Fx.Transitions.linear})
			setTimeout("fban.start('height', '50px')", 3000);
	}
}

anima = function()
{
	if ($('foto') != null) {
		homeA = new Fx.Tween('sf' + b, {
			duration: 2000,
			transition: Fx.Transitions.linear
		}).addEvent('complete', function(e){
		
			if (b == 9) {
				b = 0;
				$$('div#foto img').setStyle('opacity', '1')
			}
			b++;
		});
		
		homeA.start('opacity', '0');
	}
}

function accordion_fixed(corrente)
{
	if(window.ie6) var heightValue='100%';
	else var heightValue='';
	if(corrente<0) corrente = -1; 
	window.addEvent('domready', function() {
		
		
	var myAccordion = new Accordion($$('.toggler'), $$('.element'), {
		   display: corrente,
		   alwaysHide: true,
		onActive: function(toggler){ toggler.addClass('active');},
		onComplete: function() {
		   var element=$(this.elements[this.previous]);
		   if(element && element.offsetHeight>0) element.setStyle('height', heightValue);         
		},
		onBackground: function(toggler){ toggler.removeClass('active');}
	});
	});
}



function invia(top){
	
$('div_expo').fade('out');

$('messaggio').morph({display: 'block', height: 200, 'margin-top': 100});


	var ajax = new Request.HTML({
		url: $('ajaxform').get("action"),
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
						$('messaggio').set('html',responseHTML);
		},
		onFailure: function(){
			$('messaggio').set('text', 'Richiesta fallita.');
		}		
	}).post($('ajaxform'));
		
}


	

var validate = new Class({
	
	getOptions: function(){
		return {
			validateOnBlur: true,
			errorClass: 'error',
			errorMsgClass: 'errorMessage',
			dateFormat: 'dd/MM/yy',
			_onFail: $empty,
			_onSuccess: 'invia()',
			showErrorsInline: true,
			label: 'Please wait...',
			form2: false,
			replay: false,
			top_servizi: true
		};
	},

	initialize: function(form, options){
		this.setOptions(this.getOptions(), options);
		this.form = $(form);
		this.elements = this.form.getElements('.required');			
		
		this.list = [];
		
		this.elements.each(function(el,i){
			if(this.options.validateOnBlur){
				el.addEvent('blur', this.validate.bind(this, el));
			}
		}.bind(this));
		
		if(this.options.replay == false)
		{
		
			this.form.addEvent('submit', function(e){
				var event = new Event(e);
				var doSubmit = true;
				var classe = "required"
				this.elements.each(function(el, i){
	
					if (el.hasClass(classe)) {
						if (!this.validate(el)) {
							event.stop();
							doSubmit = false;
							this.list.include(el);
							$("errorMessage").set('html', errore_txt);
							$("errorMessage").set('style','display:block');
							//setTimeout(function() {new Fx.Scroll(window).toElement('form_ap');}, 1500); 
						}
						else {
							this.list.erase(el);
						}
					}
					
				}
.bind(this));
				
				if (doSubmit) {
					if (this.options._onSuccess) {
						event.stop();
						
						invia(this.options.top_servizi);
					//this.options._onSuccess(this.form);
					}
					else {
						this.form.getElement('input[type=submit]').setProperty('value', this.options.label);
					}
				}
				else {
					this.options._onFail(this.getList());
				}
				
				return false;
				
			}
.bind(this));
		}
		
	},
	getList: function(){
		var list = new Element('ul');
		this.list.each(function(el,i){
			if(el.title != ''){
			var li = new Element('li').inject(list);
			new Element('label').set({
				'for': el.id,
				'text': el.title
			}).inject(li);
			}
		});
		return list;
	},
	
	validate: function(el){
		var valid = true;
		this.clearMsg(el);
		
		switch(el.type){
			case 'text':
			case 'textarea':
			case 'select-one':
				if(el.value != ''){
					if(el.hasClass('email')){
						var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
						if(el.value.toUpperCase().match(regEmail)){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el);
							//this.setMsg(el, 'Please enter a valid email address');
						}
					}
					
					if(el.hasClass('number')){
						var regNum = /[-+]?[0-9]*\.?[0-9]+/;
						if(el.value.match(regNum)){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el);
						}
					}
					
					if(el.hasClass('postcode')){
						var regPC = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$/
						if(el.value.match(regPC)){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el, 'Please enter a valid postcode');
						}
					}
					
					if(el.hasClass('date')){
						var d = Date.parseExact(el.value, this.options.dateFormat);
						if(d != null){
							valid = true;
						}else{
							valid = false;
							this.setMsg(el, 'Please enter a valid date in the format: '+this.options.dateFormat.toLowerCase());
						}
					}
					
				}else{
					valid = false;
					this.setMsg(el);
				}
				break;
				
				
				
				case 'checkbox':

				if(!el.checked){
					valid = false;
					this.setMsg(el);	
					$(el.get("name")).addClass('error');	
				}else{
					valid = true;
					$(el.get("name")).removeClass('error');	
				}
				break;
				
				
				
			case 'radio':
				var rad = $A(this.form[el.name]);
				var ok = false;
				rad.each(function(e,i){
					if(e.checked){
						ok = true;
					}
				});
				if(!ok){
					valid = false;
					this.setMsg(rad.getLast(), '');
				}else{
					valid = true;
					this.clearMsg(rad.getLast());
				}
				break;
				
		}
		return valid;
	},
	
	setMsg: function(el, msg){
		if(msg == undefined){
			msg = el.title;
		}
		el.addClass("error");
	},
	
	clearMsg: function(el){
		el.removeClass("error");
	}
	
});

validate.implement(new Options);
validate.implement(new Events);


/*************************************************************/


	now = 2;
	var psURI = new URI();

function link_m(url)
{
			if (Browser.Engine.trident && Browser.Engine.version <= 5) {}
			else url = '#'+url;
			window.location = url;
}

function linka_a(t)
{
	
		$$('a[rel=blank]').set('target','_blank');
	if (t == 'init') {
		like_fb();
		$$('a[rel!=blank]').addEvent("click", function(e){
			
			if (this.className == "web3d") {
				window.location = this.href;
				return false;
			}
			
			if (this.id == "nolink") {
				return false;
			}
			
			if (Browser.Engine.trident && Browser.Engine.version <= 5) {			
			}
			else {
				e.stop();
				if (callingajax == 1) {
					return false;
					HTMLRequest.cancel();
					myFx.cancel();
					callingajax = 0;
				}
				psURI.set('fragment', this.get('href'));
				psURI.go();
			}
			
			});
	}
	else {
			$$('div#content a[rel!=blank]').addEvent("click", function(e){
			if (this.className == "web3d") {
				window.location = this.href;
				return false;
			}	

			if (this.get('rel')) {
				if(this.get('rel').match(/lightbox/i)) {return false;}
				}	
			
			if (Browser.Engine.trident && Browser.Engine.version <= 5) {
			
			}
			else {
			
				e.stop();
				
				
				
				if (callingajax == 1) {
				
					return false;
					HTMLRequest.cancel();
					myFx.cancel();
					callingajax = 0;
				}
				psURI.set('fragment', this.get('href'));
				psURI.go();
				
			}});
		$$('div#content span[class=back]').addEvent("click", function(e){
			e.stop();
			history.back();
		});
	}
}

function faccette_old(who,what)
{
	
	if ($(who + '_face')) {
		if (what == 'zero') {
			$(who + '_face').set('html', $(who + '_0').get('html'));
			return false;
		}
		next = $random(1, $(who).get('rel'));
		$(who + '_face').set('html', $(who + '_' + next).get('html'));
		setTimeout("faccette('" + who + "','zero')", $random(100, 500));
		setTimeout("faccette('" + who + "')", $random(2000, 4000))
	}
	else
	{
		return false;
	}
	
}

function faccette(who) {
	if ($(who + '_face')) {
		next = $random(1, $(who).get('rel'));
		$(who + '_face').set('html', $(who + '_' + next).get('html'));
		setTimeout("faccette_zero('" + who + "')", $random(100, 500));
	}
	else { return false; }
}

function faccette_zero(who) {
	if ($(who + '_face')) {
		$(who + '_face').set('html', $(who + '_0').get('html'));
		setTimeout("faccette('" + who + "')", $random(2000, 4000));
	}
	else { return false; }
}


function menu(dir)
{
	uurl = dir.split("/");
	if(uurl[1]==undefined) uurl[1] = 'home';
	$$('ul#menu li').removeClass("current");
	if ($('li_' + uurl[1])) {
		$('li_' + uurl[1]).addClass("current");
		padre = $('li_' + uurl[1]).get('rel'); 
		if (padre) $(padre).addClass("current");
		if ($('li_' + uurl[2])) $('li_' + uurl[2]).addClass("current");
	}
}

	window.addEvent("domready", function() {
	
	linka_a('init');
	
	g= 2;


	$('emailform').addEvent('focus', function(){
		if (this.get("value") == 'iscriviti@alla.newsletter')
		{	
			this.set('value', '');
		}
	});
	$('emailform').addEvent('blur', function(){
		if (this.get("value") == '') 
		{	
			this.set('value', 'iscriviti@alla.newsletter');
		}
	});
	
	//anima_logo()
/*
	function anima_logo()
	{
		
			myFxlo = new Fx.Morph('logobg', {duration: 5000, transition: Fx.Transitions.Cubic.easeOut}).addEvent(
		'complete', function(e){
			g = g<3 ? g+1 : 1;
			anima_logo();
		}
	);
			
			myFxlo.start('.color-'+g);
	}
*/
	if (!(Browser.Engine.trident && Browser.Engine.version <= 5)) {
		$('newsletter').addEvent("submit", function(event){
			event.stop();
			psURI.set('fragment', '/newsletter/');
			psURI.go();
		});
	} else {
		
		$('bottnew').addEvent("click", function(event){
			$('newsletter').submit();
		});
	}

	$$('ul#menu li ul').addEvent("mouseover", function() {
			$$('ul#menu li').removeClass("current");
			$(this.get('rel')).addClass('current')
		});
		
	$$('ul#menu li ul').addEvent("mouseout", function() {
			$$('ul#menu li').removeClass("current");
			dir = psURI.get('fragment')=="" ? "home" : psURI.get('fragment')
			menu(dir)
		});


dir = psURI.get('directory')=="/" ? "home": psURI.get('directory');

if (psURI.get('fragment') != "") {
			menu(psURI.get('fragment'))
			if(psURI.get('fragment').replace(/\//g,'')!=dir) link("avanti", psURI.get('fragment'))
	} else
	{
		menu(dir)
	}
		
	
		
		myFx = new Fx.Tween('controller', {duration: 500, transition: Fx.Transitions.Back.easeInOut}).addEvent(
		'complete', function(e){
			switch(now)
			{
				case 2: $('controller').adopt($('i1'), $('i2'), $('i3'));  break;
				case 3: $('controller').adopt($('i2'), $('i3'), $('i1'));  break;
				case 1: $('controller').adopt($('i3'), $('i1'), $('i2'));  break;
			}
			
			$('controller').setStyle('margin-left','-960px');
			
			switch(now)
			{
				case 2: $('i1').empty(); $('i3').empty();  break;
				case 3: $('i2').empty(); $('i1').empty();  break;
				case 1: $('i2').empty(); $('i3').empty();  break;
			}
			
			callingajax = 0;
		}
	);   
	})
	/*
	function link(direzione,page){
	
		$clear(timerhome);
		
		HTMLRequest = new Request.HTML({
			evalScripts: false,
			onRequest: function(){
				callingajax = 1;
			},
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){

				$('i2').set('html', responseHTML)
				eval(responseJavaScript);
				Mediabox.scanPage();
				linka_a('replay');
				actualurl = page;
			}
		});
		if(page[0]!="/") page = "/"+page;
		vars = page.split("/");
		phppage = vars[1]
		if(phppage==undefined) phppage = vars[0];
		variabili = {"pag":phppage,"par1":vars[2],"par2":vars[3],"par3":vars[4],"par4":vars[5],"par5":vars[6]};
		if (page == "newsletter") {
			variabili = {
				"email": $('emailform').value
			};
		}

		HTMLRequest.get("/pagine.php", variabili);
		
	}
	*/
	
	function link(direzione,page){
	
	$clear(timerhome);
	

	
	if(direzione=="avanti")
	{
		dim = '-1920px';
		
		next = now<3 ? now+1 : 1;
	}
	else
	{
		dim = '0px';
		next = now>1 ? now-1 : 3;
	}

	now = next;

		HTMLRequest = new Request.HTML({
			evalScripts: false,
			onRequest: function(){
				callingajax = 1;
			},
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){

				$('i' + next).set('html', responseHTML)
				eval(responseJavaScript);
				Mediabox.scanPage();
				myFx.start('margin-left', dim);
				linka_a('replay');
				lasturl = psURI.get('fragment');
				like_fb()
				pageTracker._trackPageview(page);
			}
		});
		if(page[0]!="/") page = "/"+page;
		vars = page.split("/");
		phppage = vars[1]
		if(phppage==undefined) phppage = vars[0];
		variabili = {"pag":phppage,"par1":vars[2],"par2":vars[3],"par3":vars[4],"par4":vars[5],"par5":vars[6]};
		if (phppage == "newsletter") {
			variabili = {
				"pag":phppage,"email": $('emailform').value
			};
		}

		HTMLRequest.get("/pagine.php", variabili);
		
	}
	

window.addEvent('hashload',function(data) {

	if (data.hash == "" && (Browser.Engine.gecko) && linked == 0) {
		return false;
	}
	else {
		if (data.hash == "" && psURI.get('directory') == "/") {  
			data.hash = "home";
		}
	}

	if (data.hash == "" && psURI.get('directory') != "") {
		if (psURI.get('directory') == "/") 
			data.hash = "home";
		if (psURI.get('directory') != "/") 
			datahash = psURI.get('directory');
	}
	else {
		if (psURI.get('directory') == "/" + data.hash) return false;
		datahash = "/" + data.hash
	}
	frag = datahash.split("/");
	fage = frag[1]
	if(fage==undefined) fage = frag[0];
	direction = fage =="home" ? "indietro":"avanti";
	menu(datahash)
	link(direction, datahash)
	linked = 1
});



/*
---
script: Fx.Spring.Demo.js
description: Fx.Spring Demo

authors:
  - Chi Wai Lau (http://tabqwerty.com)

license:
  - MIT-style license

requires:
  core/1.2.4:   '*'

...
*/
window.addEvent('domready', function() {
	new Fx.Spring.Demo();
});

Fx.Spring.Demo = new Class({
	initialize: function() {
		this.container = $('wrapper');
		this.mangosteen = $('logobg');

		this.xSpring = new Fx.Spring({
			'onMotion': this.xStep.bind(this)
		});

		this.ySpring = new Fx.Spring({
			'onMotion': this.yStep.bind(this)
		});

		this.timeFx = new Fx.Morph(this.mangosteen, {
			duration: 300,
			link: 'cancel'
		});

		this.onSpringFx = transition = 'Spring';
		



		this.container.addEvent('mousemove', this.mousemove.bind(this)); 
	},
	


	
	mousemove: function(e) {
		/*if (e.target.id == 'mangosteen-container') {*/
			if (this.onSpringFx) {
				this.xSpring.start(this.xSpring.get(), e.page.x);
				//this.ySpring.start(this.ySpring.get(), e.page.y);
			} else {
				this.timeFx.start({
					'top': e.page.y,
					'left': e.page.x
				});
			}
			e.stop();
		/*}*/
	},
	
	xStep: function(t) {
	
		this.mangosteen.setStyle('background-position', t);
	},
	
	yStep: function(t) {
		this.mangosteen.setStyle('top', t);
	}
});


