
//prendo un DIV e gli aggiungo gli eventi per trasformarlo in un pulsante
function setCursor(element){
	 element.addEvents({
	 'mouseover':  function(e){
						this.setStyle('cursor', 'pointer');
					},
	  'mouseout':   function(e){
						this.setStyle('cursor', 'default');
					}
	  })
}

//tolglie gli eventi da pulsante che abbiamo aggiunto prima al DIV
function unSetCursor(element){
	element.removeEvents('mouseover');
	element.removeEvents('mouseout');
	element.setStyle('cursor', 'default');
}


//
// Fade away the DIV containing the form errors
//
function fadeErrors() {

	if( $( 'formerrors' ) != undefined ) {

		// Set slide and styles effects
		var fxOut = new Fx.Morph( $( 'formerrors' ), { duration: 750, wait: false } );

		fxOut.start( {
			'opacity': 0.0
		} );

	}

	if( $( 'formerrorspromo' ) != undefined ) {

		// Set slide and styles effects
		var fxOut = new Fx.Morph( $( 'formerrorspromo' ), { duration: 750, wait: false } );

		fxOut.start( {
			'opacity': 0.0
		} );

	}

};

//
// Called from Flash AS or from JS
// Load the page with AJAX request
//
function loadPage( pageURI, h, reqMethod, reqData ) {

	// Set slide and styles effects
	var fxOut = new Fx.Morph( $( 'thepage' ), { duration: 150, transition: Fx.Transitions.Quad.easeOut, wait: true } );
	var fxIn = new Fx.Morph( $( 'thepage' ), { duration: 1250, transition: Fx.Transitions.Quad.easeIn, wait: false } );
	var fxH1 = new Fx.Morph( $( 'contentarea' ), { duration: 750, transition: Fx.Transitions.Quad.easeInOut, wait: false } );
	var fxH2 = new Fx.Morph( $( 'thepage' ), { duration: 750, transition: Fx.Transitions.Quad.easeInOut, wait: false } );

	fxOut.start( {
		'opacity': [ 1.0, 0.0 ]
	} );

	function myFadeIn() {

		fxIn.start( {
			'opacity': [ 0.0, 1.0 ]
		} );


		// if we have the button with that name...
		if( $( 'invia_richiesta' ) != undefined ) {
			$( 'fpromo' ).addEvent( 'submit', function( evnt ) {
					evnt.stop();

					// call itself recursively to handle form submit
					loadPage( pageURI, h, 'post', $( 'fpromo' ) );

				} );

			if( $( 'formerrorspromo' ) != undefined ) {

				var fxInErr = new Fx.Morph( $( 'formerrorspromo' ), { duration: 750,
																	  transition: Fx.Transitions.Quad.easeInOut,
																	  wait: false } ).set( { opacity: 0.8 } );

			}

		}

		// if we have the button with that name...
		if( $( 'invia_richiesta' ) != undefined ) {
			$( 'fcontatti' ).addEvent( 'submit', function( evnt ) {
					evnt.stop();

					// call itself recursively to handle form submit
					loadPage( pageURI, h, 'post', $( 'fcontatti' ) );

				} );

			if( $( 'formerrors' ) != undefined ) {

				var fxInErr = new Fx.Morph( $( 'formerrors' ), { duration: 750,
																 transition: Fx.Transitions.Quad.easeInOut,
																 wait: false } ).set( { opacity: 0.8 } );

			}

		}

		// if we have an element with that name...
		if( $( 'imgdisplay' ) != undefined ) {

			window.addEvent( 'domready', function() {
				var portfolio = new multiBox( 'portfolio', {
						overlay: new overlay(),
						path: '../img/portfolio/',
						descClassName: 'portfolioDesc' } );
			} );

		}

		// if we have at least a mooSimpleSlide element...
		if( $$( 'div.imgal' ).length > 0 ) {

			var i = 0;

			$$( 'div.imgal' ).each( function( e, idx ) {
				// Find the primary element class stripping " imgal"
				var eClass = e.get( 'class' );
				eClass = eClass.substr( 0, eClass.length - 6 );

				// Find all IMG contained in that very element
				var imgs = $$( '#' + e.get( 'id' ) + '.' + eClass + ' img' );

				// Starting up the mooSimpleSlide thing...
				var mySlideshow = new mooSimpleSlide( imgs, { period: 5000 + i * 25 } );
				mySlideshow.displayImage();
				i++;
			} );

		}

	}


	function myLoadHTML() {
		if( reqMethod == 'post' ) {
			// handle the AJAX request if the method is POST
			reqData = formToQueryString( reqData );
			$( 'thepage' ).empty().set( 'load', { method: reqMethod,
												  url: pageURI,
												  data: reqData,
												  evalScript: true } );
			$( 'thepage' ).load();
		} else {
			// handle the AJAX request if the method is GET
			$( 'thepage' ).empty().set( 'load', { method: reqMethod, evalScript: true } );
			$( 'thepage' ).load( pageURI ).get( reqData );
		}

	}

	myLoadHTML.delay( 150, $( 'thepage' ) );

	fxH1.start( {
		'height': h
	} );

	fxH2.start( {
		'height': h
	} );

	myFadeIn.delay( 1500, $( 'thepage' ) );

};



function moveHeadText(img, textOriginalLeft, textWidth){
	$(img).setStyle('left', textOriginalLeft);
	var myEffect = new Fx.Morph(img, {duration: 50000, transition: Fx.Transitions.linear, wait: false});
	myEffect.start({
		'left': (-1) * textWidth //Morphs the left from the current
	});
}

function startText(img,textOriginalLeft,textWidth){
	moveHeadText(img,textOriginalLeft,textWidth);
	checkHeadTextPosition(img,textOriginalLeft,textWidth);
}

function checkHeadTextPosition(img, textOriginalLeft, textWidth){
	if( $(img).getStyle('left').toInt() <= -1*textWidth ){
			setTimeout("startText('"+img+"',"+textOriginalLeft+","+textWidth+")",20000);
	}else{
		setTimeout("checkHeadTextPosition('"+img+"',"+textOriginalLeft+","+textWidth+")",1000);
	}
}

window.addEvent('domready',function(e){
	if( $( 'head_text_img1' ) != undefined ) {
		var textOriginalLeft = $('head_text_img1').getStyle('left').toInt();
		var textWidth = $('head_text_img1').getStyle('width').toInt();

		startText('head_text_img1',textOriginalLeft,textWidth);
		setTimeout("startText('head_text_img2',"+textOriginalLeft+","+textWidth+")",35000);
	}

	var box = new multiBox( {
			mbClass: '.mb',
			container: $( document.body ),
			useOverlay: true,
			descClassName: 'multiBoxDesc' } );
	
if( $( 'home' ) != undefined ) {
	var boxAuto = new multiBox( {
			mbClass: '.mb_auto',
			container: $( document.body ),
			useOverlay: true,
			maxSize: { w: 850, h: 225 },
			descClassName: 'multiBoxDesc',
			offset: { x: 85, y: 0 },
			autoOpen: 1			
	} );
}
})
