// ============
// = domready =
// ============

var menuButtonSelected;
var targetZone;
var lastTargetZone;

var numbersArrowAni;

// scrollspy elements and offset
var scrollSpyElements;
var offsetForScrollSpy;

var delayBetweenScrollSpyed = 250;

window.addEvent('domready',function(){

	setLoader();

	// immediately scroll to top the window
	scrollDocument = new Fx.Scroll(window,{duration:200, transition:Fx.Transitions.Quint.easeInOut, wheelStops: true }).toTop();

	// trigger general functions
	socialButton();
	setMenu();
	setAutoMenuSwitch();
	setSquares();
	setInfoTwitter();
	setSendMail();
	setBollonziComparativi();
	setTheCloud();
	setCounter();
	configureMail();
	alternateTabRow();
	targetBlank();
	setServerAnimation();
	setScrollSpyElements();
	setScrollSpyAnimation();
	getTwitter();
});

window.addEvent('load',function() {

	scrollDocument = new Fx.Scroll(window,{duration:200, transition:Fx.Transitions.Quint.easeInOut, wheelStops: true }).toTop();
	hideLoader();
});

// ===================
// = website related =
// ===================

function setMenu() {

	// var declaring
	menuItems = $$('#menu a, #language a');
	menuRelatedItems = $$('#menu a');
	languageRelatedItems = $$('#language a');
	cleanOffset = $('bar').getSize().y;
	var menuButtonSelected;

	menuItems.each(function(item){
		// span effects creation
		var span = new Element('span', { text: item.get('text') });
		item.set('morph', { duration: 1000, transition: Fx.Transitions.Elastic.easeOut });
		item.set('text', '').grab(span, 'bottom').grab(span.clone().set('class', 'over'), 'bottom');
		item.getFirst().set('tween', { duration: 300, transition: Fx.Transitions.Expo.easeInOut });
		
		// roll over/out/click
		item.addEvents({
			'mouseenter': function(){ if( this!=menuButtonSelected && this.get('class')!='selectedLang' ) this.getElement('span').tween("margin-top",-16); },
			'mouseleave': function(){ if( this!=menuButtonSelected && this.get('class')!='selectedLang' ) this.getElement('span').tween("margin-top",0); },
			'click': onMenuItemClick.bind(item)
		});
	});
	
	function onMenuItemClick(e){
		// if the butt is a language selector exit
		if( this.get('id') == 'langIT' || this.get('id') == 'langEN' ) return;
		
		menuButtonSelected = this;
		
		//  prevent default actions
		e.stopPropagation();
		e.preventDefault();
		
		// page movements
		targetAnchor = this.get('href').substr(1);

		switch(targetAnchor)
		{
		case "top":
			new Fx.Scroll(window,{duration:2000,transition:Fx.Transitions.Quint.easeInOut}).toTop()
			break;
		case "contacts":
			new Fx.Scroll(window,{duration:2000,transition:Fx.Transitions.Quint.easeInOut}).toBottom()
			break;
		 default:
			new Fx.Scroll(window,{offset:{'y': - cleanOffset},duration:2000,transition:Fx.Transitions.Quint.easeInOut}).toElement(targetAnchor);
		};
	};
	
	// set init state w/ "EN" pressed
	menuLanguageSelected = $$('.selectedLang');
	menuLanguageSelected.set('class','selectedLang');
	menuLanguageSelected.getFirst().tween('margin-top',-16);
	
	//set initial state
	$('bar').setStyle('top',-87).set('tween',{ duration: 1000, transition: Fx.Transitions.Quint.easeOut, property: 'top' });
};

function setAutoMenuSwitch() {

	items = $$('#solutions,#technologies,#pricing,#contacts');
	window.addEvent('scroll:pause(5)', spyMenu.bind(window));

	function spyMenu(){

		var windowScroll = window.getScroll().y + $('bar').getSize().y + 50;

		items.each(function(item, index){
			if(windowScroll >= item.getCoordinates().top && windowScroll <= item.getCoordinates().bottom){
				if(windowScroll >= $('container').getSize().y-window.getSize().y+120) targetZone = $('contacts');
				else targetZone = item;
				if( targetZone == lastTargetZone ) return
				else{
					lastTargetZone = targetZone;
					autoMenuSwitch(targetZone);
				}
			}
		});
	}
	
	function autoMenuSwitch(targetZone){
		menuRelatedItems.getElement('span').tween("margin-top",0);
		$$('#'+targetZone.get('id')+'Butt').getElement('span').tween("margin-top",-17);
		menuButtonSelected = $$('#'+targetZone.get('id')+'Butt');
	}
}

function setSquares() {

	theOthersSquares = $$('.square');
	theOthersSquares.set("morph",{ duration: 200, transition: Fx.Transitions.Back.easeInOut });
	theOthersSquares.set("tween",{ duration: 200, transition: Fx.Transitions.Back.easeInOut });
	theOthersSquares.getElement('.square-icon-animation').set('tween', { duration: 1000, transition: Fx.Transitions.Quint.easeOut, unit: "%" });

	theOthersSquares[0].store( 'othersSquares', $$('#square-2,#square-3,#square-4,#square-5') );
	theOthersSquares[1].store( 'othersSquares', $$('#square-1,#square-3,#square-4,#square-5') );
	theOthersSquares[2].store( 'othersSquares', $$('#square-1,#square-2,#square-4,#square-5') );
	theOthersSquares[3].store( 'othersSquares', $$('#square-1,#square-2,#square-3,#square-5') );
	theOthersSquares[4].store( 'othersSquares', $$('#square-1,#square-2,#square-3,#square-4') );
	theOthersSquares[0].store( 'startX', $('square-1').getPosition('container').x );
	theOthersSquares[1].store( 'startX', $('square-2').getPosition('container').x );
	theOthersSquares[2].store( 'startX', $('square-3').getPosition('container').x );
	theOthersSquares[3].store( 'startX', $('square-4').getPosition('container').x );
	theOthersSquares[4].store( 'startX', $('square-5').getPosition('container').x );


	theOthersSquares.each(function(item, index){
		item.addEvents({
			"mouseenter" : function(){
				this.morph({'border':'10px solid #373941','top':'0', 'left': this.retrieve('startX') - 10});
				this.getElement('.square-icon-animation').tween('background-position', '50% 100%');
				this.retrieve('othersSquares').fade(.2);
			},
			"mouseleave" : function(){
				this.morph({'border':'0 solid #373941','top':'10px', 'left': this.retrieve('startX')});
				this.getElement('.square-icon-animation').tween('background-position', '50% 0%');
				theOthersSquares.fade(1);
			}
		});
	});
}

function socialButton() {
	$$("#twitter-bar,#mail-footer,#twitter-footer").addEvents({
		'mouseenter':slideUpBg,
		'mouseleave':slideDownBg
	}).set("tween",{ duration: 400, transition: Fx.Transitions.Quint.easeOut, unit: "%"});
	
	function slideUpBg(){
		this.tween('background-position', "50% 0%");
	}
	function slideDownBg(){
		this.tween('background-position', "50% 100%");
	}
};

function setInfoTwitter() {
	$$('#info-twitter a, #location-follow-us').setStyle("opacity",.5);
	$$('#info-twitter a, #location-follow-us').set("tween",{ duration: 300, transition: Fx.Transitions.Quint.easeIn }).addEvents({
		"mouseenter": function(){ this.fade(1); },
		"mouseleave": function(){ this.fade(.5); },
	});
}

function setSendMail() {
	$('contact-form-submit').setStyle("opacity",.5);
	$('contact-form-submit').set("tween",{ duration: 200, transition: Fx.Transitions.Quint.easeIn }).addEvents({
		"mouseenter": function(){ this.fade(1); },
		"mouseleave": function(){ this.fade(.5); },
	});
}

function setBollonziComparativi() {

	titlesInBollonz = $$('#comparative .bollonzo h4, #comparative .bollonzo h5, #comparative .bollonzo h6').set('fade', { duration: 400, transition: Fx.Transitions.Back.easeInOut }).setStyle('opacity',0);

	// draw canvas
	bolCanWhite = new Bollonzo($('bollonzo-background-white'),194,194,'#fff',82);
	bolCanWhite.draw();
	
	bolCanBlu = new Bollonzo($('bollonzo-background-blu'),194,194,'#6790a4',82);
	bolCanBlu.draw();

	bolCanRed = new Bollonzo($('bollonzo-background-red'),194,194,'#e83e3e',82);
	bolCanRed.draw();

	bollonziComparativi = $$('.bollonzo');
	bollonziComparativi[0].store('tds', $$('#comparative-table div>p:nth-of-type(3),#comparative-table div>p:nth-of-type(4)'));
	bollonziComparativi[0].store('theCanvas', bolCanWhite);
	bollonziComparativi[1].store('tds', $$('#comparative-table div>p:nth-of-type(2),#comparative-table div>p:nth-of-type(4)'));
	bollonziComparativi[1].store('theCanvas', bolCanBlu);
	bollonziComparativi[2].store('tds', $$('#comparative-table div>p:nth-of-type(2),#comparative-table div>p:nth-of-type(3)'));
	bollonziComparativi[2].store('theCanvas', bolCanRed);
	
	// interaction canvas
	bollonziComparativi.each(function(item,index) {
		
		item.set('tween',{duration: 300,transition: Fx.Transitions.Expo.easeOut}).addEvents({
			"mouseenter": function(){
				this.retrieve('tds').each(function(tdsToDelay,index){ tdsToDelay.fade.delay(index,tdsToDelay,.2); });
				expandBollonzo(this.retrieve('theCanvas'));
			},
			"mouseleave": function(){
				this.retrieve('tds').each(function(tdsToDelay,index){ tdsToDelay.fade.delay(index,tdsToDelay,1); });
				collapseBollonzo(this.retrieve('theCanvas'));
			}
		});
	});

	function expandBollonzo(theBollonzo) {
		theBollonzo.animate(95,.5);
	}
	
	function collapseBollonzo(theBollonzo) {
		theBollonzo.animate(82,.5);
	};
}

function targetBlank() {
	$$("a[href^='http']").addEvent("click", function(e){ e.preventDefault(); window.open(this.get("href"), "_blank"); });
}

function setServerAnimation() {

	$$('#professional .server-tassello > div,#advanced .server-tassello > div,#business .server-tassello > div').set('tween',{ duration: 300, transition: Fx.Transitions.Back.easeInOut, property: 'background-position' });

	$$('.server-name').set('tween',{ duration: 800, transition: Fx.Transitions.Quint.easeOut });
	$$('.server-price').set('tween',{ duration: 1000, transition: Fx.Transitions.Quint.easeOut });
	$$('.server-feature').set('tween',{ duration: 1200, transition: Fx.Transitions.Quint.easeOut });

	$$('.server-name-detail').set('tween',{ duration: 800, transition: Fx.Transitions.Quint.easeOut });
	$$('.server-price-detail').set('tween',{ duration: 1100, transition: Fx.Transitions.Quint.easeOut });

	$$('.server-description').set('tween',{ duration: 500, transition: Fx.Transitions.Back.easeInOut });
	$$('.server-description p').set('tween',{ duration: 300, transition: Fx.Transitions.Back.easeInOut, property: 'color' });

	$$('#professional,#advanced,#business').addEvents({
		'mouseenter': serverAnimationOver,
		'mouseleave': serverAnimationOut,
		'mousedown': serverAnimationClick
	});
	
	
	$$('.server-name-detail').setStyle('bottom',-300);
	$$('.server-price-detail').setStyle('bottom',-500);
	
	
	function serverAnimationOver(e) {

		serverContainer = this.get('id');
		server = this.getFirst().getFirst();
		server.tween('0 32');
		$$('#'+serverContainer+' .server-description').tween('backgroundColor','#373941');
		$$('#'+serverContainer+' .server-description .optimal-for').tween('color','#fff');
		$$('#'+serverContainer+' .server-description .optimal-for-what').tween('color','#6690A3');

		$$('#'+serverContainer+' .server-name').tween('bottom',-300);
		$$('#'+serverContainer+' .server-price').tween('bottom',-500);
		$$('#'+serverContainer+' .server-feature').tween('bottom',-700);

		$$('#'+serverContainer+' .server-name-detail').tween('bottom',45);
		$$('#'+serverContainer+' .server-price-detail').tween('bottom',20);

	}

	function serverAnimationOut() {

		serverContainer = this.get('id');
		server = this.getFirst().getFirst();
		server.tween('0 0');
		$$('#'+serverContainer+' .server-description').tween('backgroundColor','#151619');
		$$('#'+serverContainer+' .server-description p').tween('color','#000');
		$$('#'+serverContainer+' .server-description .optimal-for').tween('color','#919299');
		$$('#'+serverContainer+' .server-description .optimal-for-what').tween('color','#eaeaeb');

		$$('#'+serverContainer+' .server-name').tween('bottom',82);
		$$('#'+serverContainer+' .server-price').tween('bottom',58);
		$$('#'+serverContainer+' .server-feature').tween('bottom',19);

		$$('#'+serverContainer+' .server-name-detail').tween('bottom',-300);
		$$('#'+serverContainer+' .server-price-detail').tween('bottom',-500);

	}

	function serverAnimationClick() {

		new Fx.Scroll(window,{offset:{'y': - cleanOffset},duration:2000,transition:Fx.Transitions.Quint.easeInOut}).toElement('pricing');
	}
}

function configureMail() {

	var contactForm = $('contact-form');
	new Element('div', { id: 'contact-form-ok', text: 'Ok! Message was sent correctly!' }).fade('hide').inject(contactForm, 'bottom');

	contactLabels = $$('#contact-name, #contact-email, #contact-message');
	contactFields = $$('#contact-name input, #contact-email input, #contact-message textarea');
	contactErrors = $$('.contact-error');
	contactErrors.each(function(error, index){

		error.fade('hide');
		error.how = 2;
		error.set('morph', {
			duration: 500,
			onComplete: function(){ error.morph({ top: error.how*=-1 }); }
		});
		error.getFirst().fade('hide');
		error.addEvent('mouseover', error.fade.bind(error.getFirst(), 1));
		error.addEvent('mouseout', error.fade.bind(error.getFirst(), 0));
	});

	contactForm.addEvent('submit', function(){ return false; });
	contactForm.addEvent('submit', function(){

		var inValid = new Array();
		contactErrors.fade('0');

		contactFields.each(function(item, index){

			if(!item.get('value').trim() || (item.get('name') == 'email' && !/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(item.get('value').trim())))
				inValid.push(item);
		});

		if(inValid.length){

			var fx = new Fx.Tween($('contact-form'), { property: 'marginLeft', duration: 50 });
			fx.start(0, -12).chain(function(){ fx.start(-12, 12); }, function(){ fx.start(12, 0); });

			inValid.each(function(field, index){

				var error = field.getNext().fade(1);
				error.get('morph').cancel();
				(function(){ error.morph({ top: error.how*=-1 }); }).delay(index * 121);
			});
		}
		else {

			var wait = 4000;

			$('contact-form-submit').fade(0);
			(function(){ $('contact-form-submit').fade(1); }).delay(wait + 400);
			
			contactLabels.each(function(label, index){
				(function(){ new Fx.Morph(label, { duration: 500 }).start({ top: 15, opacity: 0}); }).delay(index * 100);
				(function(){ new Fx.Morph(label, { duration: 500 }).start({ top: [-15, 0], opacity: 1}); }).delay(index * 100 + wait);
			});
			
			(function(){ contactFields.set('value', ''); }).delay(1000);

			new Request.HTML({ url: this.get('action') }).post(this);

			(function(){ new Fx.Morph($('contact-form-ok'), { duration: 500 }).start({ opacity: 1, marginTop: [-15, 0] }); }).delay(800);
			(function(){ new Fx.Morph($('contact-form-ok'), { duration: 500 }).start({ opacity: 0, marginTop: [0, 15] }); }).delay(4000);
		}

		return false;
	});
};

function alternateTabRow() {
	$$('#comparative-table div:nth-child(even)').addClass("even");
	$$('#comparative-table div:nth-child(odd)').addClass("odd");
}

function setCounter() {

	counterSingle = $$('.single-counter');
	counterSwapIndex = 0;
	counterChosen = counterSingle[counterSwapIndex].get('id');
	if(counterSingle.length == 1)return;

	numbersArrow = $('numbers-arrow');
	numbersArrow.set('tween',{ duration: 600, transition: Fx.Transitions.Linear, property: 'top' });

	initCounter();
	floatArrowAni();

	function initCounter() {

		counterArrow = $$('#'+counterChosen+' .counter-arrow');
		counterNumber = $$('#'+counterChosen+' .counter-number');
		counterArrow.set('tween',{ duration: 100, transition: Fx.Transitions.Linear, property: 'top' });
		counterNumber.set('tween',{ duration: 100, transition: Fx.Transitions.Linear, property: 'top' });

		counterService = $$('#'+counterChosen+' .counter-service');
		counterName = $$('#'+counterChosen+' .counter-name');
		counterService.set('tween',{ duration: 200, transition: Fx.Transitions.Linear, property: 'top' });
		counterName.set('tween',{ duration: 200, transition: Fx.Transitions.Linear, property: 'top' });

	}

	function floatArrowAni() {

		numbersArrow.set('tween',{ duration: 600, transition: Fx.Transitions.Linear, property: 'top' });
		numbersArrow.get('tween').start(8).chain(
			function(){ numbersArrow.get('tween').start(0); },
			function(){ numbersArrow.get('tween').start(8); },
			function(){ numbersArrow.get('tween').start(0); },
			function(){ numbersArrow.get('tween').start(8); },
			function(){ numbersArrow.get('tween').start(0); },
			function(){ numbersArrow.get('tween').start(8); },
			function(){ numbersArrow.set('tween', { duration: 200 }).get('tween').start(-5); },
			function(){ pullElements(); }
		);
	}

	function pullElements() {

		numbersArrow.get('tween').start(8);

		counterArrow.tween(39);
		counterNumber.tween(19);
		counterService.tween(27);
		counterName.tween(23);

		(function(){ pushElements(); }).delay(300);
	}
	
	function pushElements() {

		numbersArrow.set('tween', { transition: Fx.Transitions.Back.easeInOut }).get('tween').start(50);

		counterArrow.set('tween',{ transition: Fx.Transitions.Back.easeInOut }).tween(124);
		counterNumber.set('tween',{ transition: Fx.Transitions.Back.easeInOut }).tween(116);

		counterService.set('tween',{ transition: Fx.Transitions.Back.easeInOut }).tween(124);
		counterName.set('tween',{ transition: Fx.Transitions.Back.easeInOut }).tween(124);

		(function(){ pullNewElements(); }).delay(400);
	}

	function pullNewElements() {
		resetOldElements();

		numbersArrow.tween(0);

		if(counterSwapIndex == (counterSingle.length -1)) counterSwapIndex = 0;
		else counterSwapIndex++;
		counterChosen =  counterSingle[counterSwapIndex].get('id');

		initCounter();

		setNewElements();
		counterArrow.set('tween',{ duration: 300, transition: Fx.Transitions.Quint.easeInOut }).tween(52);
		counterNumber.set('tween',{ duration: 300, transition: Fx.Transitions.Quint.easeInOut }).tween(24);
		counterService.set('tween',{ duration: 300, transition: Fx.Transitions.Quint.easeInOut }).tween.delay(400,counterService,52);
		counterName.set('tween',{ duration: 300, transition: Fx.Transitions.Quint.easeInOut }).tween.delay(400,counterName,48);

		(function(){ floatArrowAni(); }).delay(400);
	}

	function resetOldElements() {

		counterName.setStyle('top',48);
		counterArrow.setStyle('top',52);
		counterNumber.setStyle('top',24);
		counterService.setStyle('top',52);
		
		$(counterChosen).setStyle('top',121);
	}
	
	function setNewElements() {

		counterName.setStyle('top',124);
		counterArrow.setStyle('top',124);
		counterNumber.setStyle('top',116);
		counterService.setStyle('top',124);
		
		$(counterChosen).setStyle('top',0);
	}
}

function setScrollSpyElements() {

	if(Browser.Platform.name == 'ios'){ offsetForScrollSpy = -300; } else { offsetForScrollSpy = 200; };

	scrollSpyElements = $$('#solutions','#claim','#claim h2','#claim h3',
		'.server-tassello','.server-description','#bollonzo-best-value',
		'#cloud-title h2','#cloud-title h3','#cloud-title h4',
		'#plans h2','#plans h3','#squares',
		'#info-presentation','#info-presentation h2','#info-presentation p',
		'#info-twitter','#info-twitter h2','#info-twitter p','#info-twitter span','#info-twitter a',
		'#comparative-table','#comparative h2','#comparative h3',
		'#location h2','#location h3','#location p','#location a',
		'#contact-name','#contact-email','#contact-email','#contact-message','#contact-form-submit',
		'#footer','#footer p','#hosted-by','#twitter-footer','#mail-footer'
	).set('fade', { duration: 400, transition: Fx.Transitions.Back.easeInOut }).setStyle('opacity',0);
	
}

function setScrollSpy() {

	window.addEvent('scroll', scrollSpy.bind(window,[offsetForScrollSpy]));
	scrollSpy();
	scrollSpyAnimation();
	
	function scrollSpy() {

		var windowScroll = window.getScroll().y + window.getSize().y - offsetForScrollSpy;
		if(!scrollSpyElements.length) window.removeEvent('scroll', scrollSpy);

		scrollSpyAnimation(windowScroll);

		var elements = new Array();

		Array.clone(scrollSpyElements).each(function(item, index){
			if(windowScroll >= item.getPosition().y || (window.getScrollSize().y - windowScroll) == offsetForScrollSpy){
				elements.push(item);
				scrollSpyElements = scrollSpyElements.erase(item);
			}
		});

		elements.each(function(item, index){
			(function(){ item.fade(1); }).delay(index * delayBetweenScrollSpyed);
		});
	}
}

function setScrollSpyAnimation() {

	bolCanWhite.animate(.1,0);
	bolCanBlu.animate(.1,0);
	bolCanRed.animate(.1,0);

	$('the-cloud-cdn').set('morph',{ duration: 200, transition: Fx.Transitions.Quint.easeOut }).setStyles({ 'opacity': 0, 'top': 275 });
	$('the-cloud-storage-engines').set('morph',{ duration: 200, transition: Fx.Transitions.Quint.easeOut }).setStyles({ 'opacity': 0, 'top': 354 });
	$('the-cloud-technologies').set('morph',{ duration: 600, transition: Fx.Transitions.Quint.easeOut }).setStyles({ 'opacity': 0, 'top': 336 });
	$('the-cloud-cloud').set('morph',{ duration: 600, transition: Fx.Transitions.Quint.easeOut }).setStyles({ 'opacity': 0, 'top': 437 });
	$('the-cloud-text').set('tween',{ duration: 600, transition: Fx.Transitions.Quint.easeOut }).setStyles({ 'opacity': 0 });
	$$('.the-cloud-icon').set('tween',{ duration: 600, transition: Fx.Transitions.Quint.easeOut }).setStyles({ 'opacity': 0 });
	$('the-cloud-blup-sx').set('tween',{ duration: 600, transition: Fx.Transitions.Quint.easeOut }).setStyles({ 'opacity': 0 });
	$('the-cloud-blup-dx').set('tween',{ duration: 600, transition: Fx.Transitions.Quint.easeOut }).setStyles({ 'opacity': 0 });

	$('cloud-title-arrow').setStyle('top',128).set('tween',{ duration: 200, transition: Fx.Transitions.Quint.easeOut, property: 'top' });
}

function scrollSpyAnimation(windowScrollAnimation) {
	
	if(windowScrollAnimation >= $('cloud-title-arrow').getPosition().y || (window.getScrollSize().y - windowScrollAnimation) == 400) $('cloud-title-arrow').tween(160);
	if(windowScrollAnimation >= $('comparative').getPosition().y || (window.getScrollSize().y - windowScrollAnimation) == 400) {
		bolCanWhite.animate(82,.3);
		(function(){ bolCanBlu.animate(82,.3); }).delay(200);
		(function(){ bolCanRed.animate(82,.3); }).delay(300);
		(function(){ titlesInBollonz.fade(1); }).delay(1000);
	}
	if(windowScrollAnimation >= $('the-cloud').getPosition().y || (window.getScrollSize().y - windowScrollAnimation) == 400) {

		(function() {

			(function(){ $('the-cloud-cloud').morph({'opacity': 1, 'top': 397 }); }).delay(600);

			(function(){ $$('.the-cloud-icon').each(function(item,index) { item.tween.delay(index*100,item,['opacity',1]); }); }).delay(600);
			(function(){ $('the-cloud-text').tween('opacity', 1); }).delay(1200);
			(function(){ $('the-cloud-cdn').morph({'opacity': 1, 'top': 255 }); }).delay(1200);

			(function(){ $('the-cloud-blup-sx').tween('opacity', 1); }).delay(1200);
			(function(){ $('the-cloud-blup-dx').tween('opacity', 1); }).delay(1400);

			(function(){ $('the-cloud-technologies').morph({'opacity': 1, 'top': 326 }); }).delay(1600);
			(function(){ $('the-cloud-storage-engines').morph({'opacity': 1, 'top': 334 }); }).delay(1800);
		}).delay(1000);
	}
}

function setTheCloud() {

	// draw canvas
	canvasTheCloudBollo1 = new Bollonzo($('the-cloud-bollo-1'),120,120,'#6690a3',50);
	canvasTheCloudBollo1.draw();
	canvasTheCloudBollo2 = new Bollonzo($('the-cloud-bollo-2'),120,120,'#6690a3',50);
	canvasTheCloudBollo2.draw();
	canvasTheCloudBollo3 = new Bollonzo($('the-cloud-bollo-3'),120,120,'#6690a3',50);
	canvasTheCloudBollo3.draw();
	canvasTheCloudBollo4 = new Bollonzo($('the-cloud-bollo-4'),120,120,'#6690a3',50);
	canvasTheCloudBollo4.draw();
	canvasTheCloudBollo5 = new Bollonzo($('the-cloud-bollo-5'),120,120,'#6690a3',50);
	canvasTheCloudBollo5.draw();
	canvasTheCloudBollo6 = new Bollonzo($('the-cloud-bollo-6'),120,120,'#6690a3',50);
	canvasTheCloudBollo6.draw();

	bollonziTheCloud = $$('.the-cloud-icon');
	bollonziTheCloud[0].store('theCanvas', canvasTheCloudBollo1);
	bollonziTheCloud[1].store('theCanvas', canvasTheCloudBollo2);
	bollonziTheCloud[2].store('theCanvas', canvasTheCloudBollo3);
	bollonziTheCloud[3].store('theCanvas', canvasTheCloudBollo4);
	bollonziTheCloud[4].store('theCanvas', canvasTheCloudBollo5);
	bollonziTheCloud[5].store('theCanvas', canvasTheCloudBollo6);

	bollonziTheCloud.each(function(item,index) {
		
		item.set('tween',{duration: 300,transition: Fx.Transitions.Expo.easeOut}).addEvents({
			"mouseenter": function(){ expandBollonzo(this.retrieve('theCanvas')); },
			"mouseleave": function(){ collapseBollonzo(this.retrieve('theCanvas')); }
		});
	});

	function expandBollonzo(theBollonzo) {

		theBollonzo.animate(60,.5);
	}
	
	function collapseBollonzo(theBollonzo) {

		theBollonzo.animate(50,.5);
	};
}

function setLoader() {

	loaderSp = new Element('div', {
		id: 'loaderSpinner',
		style: 'position: absolute; top: 27px; left: 27px; display: block; width: 64px; height: 64px; opacity: 0;'
	}).inject($('loaderLogo'));

	var opts = {
		lines: 20, // The number of lines to draw
		length: 0, // The length of each line
		width: 7, // The line thickness
		radius: 25, // The radius of the inner circle
		color: '#999', // #rbg or #rrggbb
		speed: 2.2, // Rounds per second
		trail: 50, // Afterglow percentage
		shadow: false // Whether to render a shadow
	};
	var target = $('loaderSpinner');
	var spinner = new Spinner(opts).spin(target);

	$('loader').set('tween',{ duration: 600, transition: Fx.Transitions.Expo.easeOut, property: 'opacity' });
	$('loaderLogo').set('tween',{ duration: 1000, transition: Fx.Transitions.Expo.easeOut, property: 'opacity' });
	$('loaderSpinner').set('tween',{ duration: 1000, transition: Fx.Transitions.Expo.easeOut, property: 'opacity' });

	showloader();
	preventSillyUsers();
}

function showloader() {

	$('loaderLogo').tween.delay(1000,$('loaderLogo'),[1]);
	$('loaderSpinner').tween.delay(1500,$('loaderSpinner'),[1]);
}

function hideLoader() {

	(function() {
		$('loaderLogo').tween(0);
		$('loaderSpinner').tween(0);
		$('loader').tween.delay(1000,$('loader'),[0]);
	}).delay(4000);
	
	(function() {
		reenableSillyUsers();
		$('bar').tween(0);
		setScrollSpy();
	}).delay(5500);
	
}

function preventSillyUsers() {

	window.addEvent('mousewheel',preventSillyWheel);
}

function preventSillyWheel(e) {

	e.preventDefault();
	e.stopPropagation();
};

function reenableSillyUsers() {

	window.removeEvent('mousewheel',preventSillyWheel);
}

function getTwitter(){

	var twitterJSONP = new Request.JSONP({ url: 'http://twitter.com/status/user_timeline/mediaparker.json?count=1&callback=setTwitter' }).send();
}
function setTwitter(data){

	$$('#info-twitter > p > em').set('html', data[0].text);
	$$('#info-twitter > span').set('html', parseTwitterDate(data[0].created_at));

	function parseTwitterDate(text) {

		var newtext = text.replace(/(\d{1,2}[:]\d{2}[:]\d{2}) (.*)/, '$2 $1');

		newtext = newtext.replace(/(\+\S+) (.*)/, '$2 $1')
		var date = new Date(Date.parse(newtext)).toLocaleDateString();
		var time = new Date(Date.parse(newtext)).toLocaleTimeString();

		return date;
	}
}

