var LIGHTBOX_SCROLLER;

window.addEvent('avvia_contenuti_standalone',function(){
//------------------------------------------------------
if(HB_DEBUG) alert('contenuti standalone');

/////////////////////////////////////////////////////////
//
// 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-40;	// tengo conto anche del padding
	this.contenuto_id=contenuto;
	this.contenuto=$(this.contenuto_id).clientHeight;
	this.differenza=this.contenitore-this.contenuto;
	if(window.ie) this.differenza = this.differenza-60;
	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;
	},

	reset: function(){
		$(this.contenuto_id).style.top='0px';
	}

});

$$('.hidden').each(function(element){
	element.style.display='none';
});

/*
var lightboxes=new Array();
var link_lightboxes=new Array();

$$('#contenuti_standalone a.link_lightbox').each(function(element){
	$('lightbox_scroller_testo').innerHTML=$(element.getProperty('rel')).innerHTML;
	element.suo_oggetto_lightbox=new textScrollObject('lightbox_testo','lightbox_scroller_testo');
	//lightboxes.push(new textScrollObject('lightbox_testo','lightbox_scroller_testo'));
});
*/
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);
});
*/
// MOUSE SCROLL
	$('lightbox_scroller_testo').addEvent('mousewheel', function(event) {
		event = new Event(event);
		/* Mousewheel UP */
		if (event.wheel > 0) {
			if((LIGHTBOX_SCROLLER.active)&&(!LIGHTBOX_SCROLLER.top)){
				element=LIGHTBOX_SCROLLER;
				element.moveUp(15);
			}
		}
		/* Mousewheel DOWN*/
		else if (event.wheel < 0) {
			if((LIGHTBOX_SCROLLER.active)&&(!LIGHTBOX_SCROLLER.bottom)){
				element=LIGHTBOX_SCROLLER;
				element.moveDown(15);
			}
		}
	});


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

/////////////////////////////////////////////////////////
// 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
});

$$('#contenuti_standalone a.link_lightbox').each(function(element){
	element.addEvent('click',function(){
		$('lightbox_scroller_testo').innerHTML=$(element.getProperty('rel')).innerHTML;
		LIGHTBOX_SCROLLER=new textScrollObject('lightbox_testo','lightbox_scroller_testo');
		LIGHTBOX_SCROLLER.reset();
		//alert(element.getProperty('rel'));
		$('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]});
		//---------------------------------
		// controlli per lightbox multipli
		//---------------------------------
		/*if (!LIGHTBOX_SCROLLER.active) {
			$('lightbox_button_up').style.display='none';
			$('lightbox_button_down').style.display='none';
		} else {
			$('lightbox_button_up').style.display='block';
			$('lightbox_button_down').style.display='block';
		}*/
	});
});

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

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

$('contenitore_contenuti').style.top='165px';
$('contenitore_contenuti').style.height='900px';

var apparizione_contenuti = $('contenuti_standalone').effect('top', {duration: 1000,
	transition: Fx.Transitions.Expo.easeOut,
	onComplete: function(){
		$('contenuti_standalone').style.overflow='auto';
	}
});

window.addEvent('mostra_contenuti',function(){
	apparizione_contenuti.start(0);
	$('contenuti_standalone').style.display='block';

});

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