var CONTENUTI;

window.addEvent('avvia_contenuti', function(){
//---------------------------------------------------------

//alert(CONTENUTI);
if(HB_DEBUG) alert('inizializzo i contenuti');

////////////////////////////////////////////////////////////////////
// OGGETTO CONTENUTI
//
// gestisce l'apparizione e la scomparsa dei contenuti
// gestisce lo scorrimento dei testi
////////////////////////////////////////////////////////////////////

var contentObject = new Class({

initialize: function(){
	
	// flag e variabili dell'oggetto
	this.ALL=$('contenuti');				// div che contiene tutti
	this.in_position=0;						// coordinate di arrivo in caso di apparizione
	this.out_position=245;					// coordinate di arrivo in caso di sparizione


	// effetto di transizione per la sparizione e apparizione
	this.fx_open=new Fx.Styles(this.ALL, {
		wait: false,
		duration: 1000,
		//transition: Fx.Transitions.Cubic.easeOut
		transition: Fx.Transitions.Expo.easeOut,
		onComplete: function(){
			HB_CONTENUTI='SI';
			if(HB_SLIDESHOW_LOCK){
				HB_SLIDESHOW_LOCK=false;
				clearTimeout(HB_SLIDESHOWINTERVAL);
			}
		}
	});

	this.fx_close=new Fx.Styles(this.ALL, {
		wait: false,
		duration: 600,
		//transition: Fx.Transitions.Cubic.easeOut
		transition: Fx.Transitions.Expo.easeOut,
		onComplete: function(){
			HB_CONTENUTI='NO';
			if((HB_GALLERY_LOCK)||(HB_SLIDESHOW_LOCK)){
				window.fireEvent('pronta');
			}
		}
	});

	this.fx_close.addEvent('onComplete',function(){
		if((HB_GALLERY_LOCK)||(HB_SLIDESHOW_LOCK)){
			window.fireEvent('nascondi_sfondo');
		}
	});
	this.fx_close.addEvent('onStart',function(){
		window.fireEvent('mostra_telecomando');
	});


}

});

// METODI
contentObject.implement({

	appear: function(){
		if((HB_CONTENUTI=='NO')&&(!HB_GALLERY_LOCK)){
			this.fx_open.start({'top': this.in_position});
			$('etichetta_contenuti').style.display='none';
			//HB_SLIDESHOW_LOCK=false;
			//clearInterval(HB_SLIDESHOWINTERVAL);
		}
	},

	hide: function(){
		if(HB_CONTENUTI=='SI'){
			this.fx_close.start({'top': this.out_position});
			$('etichetta_contenuti').style.display='block';
		}
		if(HB_SLIDESHOW_LOCK) window.fireEvent('nascondi_sfondo');
	},

	move: function(){
		if(HB_CONTENUTI=='NO') this.appear();
		else this.hide();
	}

});

/////////////////////////////////////////////////////////

$('chiudi_testi').addEvent('click',function(){
	CONTENUTI.hide();
});

/////////////////////////////////////////////////////////
//
// OGGETTO CHE GESTISCE LO SCORRIMENTO DEI TESTI
//
/////////////////////////////////////////////////////////

var textScrollObject = new Class({

initialize: function(contenitore,contenuto,gap){
	this.contenitore_id=contenitore;
	this.contenitore=$(this.contenitore_id).clientHeight;
	this.contenuto_id=contenuto;
	this.contenuto=$(this.contenuto_id).clientHeight;
	this.differenza=this.contenitore-this.contenuto;
	this.active=(this.differenza<0)?true:false;
	this.gap=gap;
	this.top=true;
	this.bottom=false;
	this.move=true;
	this.IntervalID=0;
}
});

// METODI
textScrollObject.implement({
	test: function(){
		//$('debug').innerHTML='TOP: '+this.top+'<br/>BOTTOM: '+this.bottom;;
		//alert(this.differenza);
	},

	moveUp: function(num){
		if(($(this.contenuto_id).offsetTop<0)&&(this.move)){
			var topY=$(this.contenuto_id).offsetTop;
			topY=topY+num;
			$(this.contenuto_id).style.top=topY+'px';
			this.top=false;
			this.bottom=false;
		} else {
			this.top=true;
		}
	},

	moveDown: function(num){
		if((this.calcBottom()>this.differenza)&&(this.move)){
			var topY=$(this.contenuto_id).offsetTop;
			topY=topY-num;
			$(this.contenuto_id).style.top=topY+'px';
			this.top=false;
			this.bottom=false;
		} else {
			this.bottom=true;
		}
	},

	calcBottom: function(){
		return $(this.contenuto_id).offsetTop+$(this.contenitore_id).offsetTop;
		//return $(this.contenitore_id).clientHeight-$(this.contenuto_id).clientHeight;
	},

	getIntervalID: function(num){
		this.IntervalID=num;
	}

});

/////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
// inizializzo lo scroller
var SCROLLER = new textScrollObject('contenitore_scroller_testo','scroller_testo');

$('button_up').addEvent('mouseenter',function(){
	if(!SCROLLER.top){
		this.src=HB_PATH+'images/testo_up_on.gif';
		this.style.cursor='pointer';
	} else {
		this.style.cursor='default';
	}
	element=SCROLLER;
	ID=setInterval('element.moveUp(1);',20);
	SCROLLER.getIntervalID(ID);
});

$('button_up').addEvent('mouseleave',function(){
	this.src=HB_PATH+'images/testo_up_off.gif';
	clearInterval(SCROLLER.IntervalID);
});

$('button_down').addEvent('mouseenter',function(){
	if(!SCROLLER.bottom) {
		this.src=HB_PATH+'images/testo_down_on.gif';
		this.style.cursor='pointer';
	} else{
		this.style.cursor='default';
	}
	element=SCROLLER;
	ID=setInterval('element.moveDown(1);',20);
	SCROLLER.getIntervalID(ID);
});

$('button_down').addEvent('mouseleave',function(){
	this.src=HB_PATH+'images/testo_down_off.gif';
	clearInterval(SCROLLER.IntervalID);
});

if (!SCROLLER.active) {
	$('button_up').style.display='none';
	$('button_down').style.display='none';
}

// MOUSE SCROLL

if (SCROLLER.active){
	$('scroller_testo').addEvent('mousewheel', function(event) {
		event = new Event(event);
		/* Mousewheel UP */
		if (event.wheel > 0) {
			if(!SCROLLER.top){
				element=SCROLLER;
				element.moveUp(10);
			}
		}
		/* Mousewheel DOWN*/
		else if (event.wheel < 0) {
			if(!SCROLLER.bottom){
				element=SCROLLER;
				element.moveDown(10);
			}
		}
	});
}
/////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
// inizializzo lo scroller per il LIGHTBOX
/**/
var LIGHTBOX_SCROLLER = new textScrollObject('lightbox_testo','lightbox_scroller_testo');

$('lightbox_button_up').addEvent('mouseenter',function(){
	if(!LIGHTBOX_SCROLLER.top){
		this.src=HB_PATH+'images/testo_up_on.gif';
		this.style.cursor='pointer';
	} else {
		this.style.cursor='default';
	}
	element=LIGHTBOX_SCROLLER;
	ID=setInterval('element.moveUp(2);',30);
	LIGHTBOX_SCROLLER.getIntervalID(ID);
});

$('lightbox_button_up').addEvent('mouseleave',function(){
	this.src=HB_PATH+'images/testo_up_off.gif';
	clearInterval(LIGHTBOX_SCROLLER.IntervalID);
});

$('lightbox_button_down').addEvent('mouseenter',function(){
	if(!LIGHTBOX_SCROLLER.bottom) {
		this.src=HB_PATH+'images/testo_down_on.gif';
		this.style.cursor='pointer';
	} else{
		this.style.cursor='default';
	}
	element=LIGHTBOX_SCROLLER;
	ID=setInterval('element.moveDown(2);',30);
	LIGHTBOX_SCROLLER.getIntervalID(ID);
});

$('lightbox_button_down').addEvent('mouseleave',function(){
	this.src=HB_PATH+'images/testo_down_off.gif';
	clearInterval(LIGHTBOX_SCROLLER.IntervalID);
});

if (!LIGHTBOX_SCROLLER.active) {
	$('lightbox_button_up').style.display='none';
	$('lightbox_button_down').style.display='none';
}

// MOUSE SCROLL
if (LIGHTBOX_SCROLLER.active){
	$('lightbox_scroller_testo').addEvent('mousewheel', function(event) {
		event = new Event(event);
		/* Mousewheel UP */
		if (event.wheel > 0) {
			if(!LIGHTBOX_SCROLLER.top){
				element=LIGHTBOX_SCROLLER;
				element.moveUp(15);
			}
		}
		/* Mousewheel DOWN*/
		else if (event.wheel < 0) {
			if(!LIGHTBOX_SCROLLER.bottom){
				element=LIGHTBOX_SCROLLER;
				element.moveDown(15);
			}
		}
	});
}

/////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////
// inizializzo l'oggetto CONTENUTI
CONTENUTI = new contentObject();

window.addEvent('mostra_contenuti',function(){
	if(HB_DEBUG) alert('mostra_contenuti');
	CONTENUTI.appear();
});

window.addEvent('new_img',function(){
	if(HB_DEBUG) alert('new_img');
	CONTENUTI.hide();
});

$('etichetta_contenuti').addEvent('click',function(){
	if(!HB_GALLERY_LOCK) CONTENUTI.appear();
});

window.addEvent('chiudi_contenuti',function(){
	CONTENUTI.hide();
});

/////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////
// LIGHTBOX

$('lightbox').style.height=window.getHeight()+'px';
$('sfondo_lightbox').style.height=window.getHeight()+'px';

function getFront(){
	$('lightbox').style.zIndex='100';
	$('lightbox').style.visibility='visible';
}

function getBack(){
	$('lightbox').style.zIndex='-1';
	$('sfondo_lightbox').style.zIndex='-2';
}

fx_chiudi=new Fx.Styles('sfondo_lightbox', {
	wait: false,
	duration: 1000,
	transition: Fx.Transitions.Expo.easeOut,
	onComplete: getBack
});

fx_apri=new Fx.Styles('sfondo_lightbox', {
	wait: false,
	duration: 1000,
	transition: Fx.Transitions.Expo.easeOut,
	onComplete: getFront
});

$('link_lightbox').addEvent('click',function(){
	$('lightbox').style.height=$('wrapper').clientHeight+'px';
	$('sfondo_lightbox').style.height=$('wrapper').clientHeight+'px';
	$('sfondo_lightbox').style.zIndex='99';
	fx_apri.start({'opacity': [0, 0.6]});
});

$('lightbox_chiudi').addEvent('click',function(){
	$('lightbox').style.visibility='hidden';
	fx_chiudi.start({'opacity': [0.6, 0]});
});

/////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// CONTROLLO SE ALL'INTERNO DEL LIGHTBOX SIA PRESENTE UN FORM
// IN CASO CI SIA, ALLORA LANCIO L'EVENTO CHE INIZIALIZZA IL FORM

if($('lightbox_scroller_testo').getElement('form')!=null) window.fireEvent('inizializza_form');

//---------------------------------------------------------
});