/******************************************************************************************************************/
// POPUP IMAGES
/******************************************************************************************************************/

function popupImage(image){
	// ci manca la parte per riconoscere se l'immagine è in verticale o in orizzontale
	
	ww = $(window).width();
	wh = $(window).height();
	bh = $('BODY').height();
	
	$('BODY').prepend('<div id="popupImage-bg"><!-- sfondo --></div><div id="popupImage-stage"><div class="myImage"><img src="'+ image+'"/></div><span class="close">chiudi</span></div>');
	$('#popupImage-bg').fadeTo(0, 0.8);
	$('#popupImage-bg').css('width', ww);
	
	if (wh > bh) $('#popupImage-bg').css('height', wh);
	else $('#popupImage-bg').css('height', bh);
	
	$('#popupImage-stage IMG').hide();
	
	sw = $('#popupImage-stage').width();
	sh = $('#popupImage-stage').height();
	
	mleft = (ww - sw) / 2;
	//mtop = (bh - sh) / 2;
	mtop = 130;
	$('#popupImage-stage').css('left', mleft);
	$('#popupImage-stage').css('top', mtop);
	
	$('#popupImage-stage IMG').fadeIn('slow');
	
	$('#popupImage-bg, #popupImage-stage, #popupImage-stage .close').click(function(){
		$('#popupImage-bg,  #popupImage-stage').remove();
	});
}

/******************************************************************************************************************/
// NEWS TICKER FUNCTIONS
/******************************************************************************************************************/

function TickerHoverHandle(){
	$('.ticker .item').hover(function(){
		clearTimeout(TickerTimerId);
		index = $('.ticker .item').index(this);
	}, function(){
		TickerRun(index);
	});
}

function TickerShow(i){
	$('.ticker .item').hide();
	$('.ticker .item:eq('+ i +')').fadeIn(200);
}

function TickerRun(i){
	num = $('.ticker .item').size();
	if (num > 1) {
		TickerShow(i)
		i++;
		if (i == num) i = 0;
		TickerTimerId = window.setTimeout('TickerRun('+ i +')', 3000);
	}
}

/******************************************************************************************************************/
// BUNNER TICKER FUNCTIONS
/******************************************************************************************************************/

function BannerHoverHandle(){
	$('.banner .item').hover(function(){
		clearTimeout(BannerTimerId);
		index = $('.banner .item').index(this);
	}, function(){
		BannerRun(index);
	});
}

function BannerShow(i){
	$('.banner .item').hide();
	$('.banner .item:eq('+ i +')').show(600);
}

function BannerRun(i){
	num = $('.banner .item').size();
	if (num > 1) {
		BannerShow(i)
		i++;
		if (i == num) i = 0;
		BannerTimerId = window.setTimeout('BannerRun('+ i +')', 4000);
	}
}

/******************************************************************************************************************/
// SLIDER  FUNCTIONS
/******************************************************************************************************************/

function sliderHandle(){

	if ($('.scroll-box .text').height() > 241) {
		maxslide = $('.scroll-box .text').height() - 241;
		
		$('.scroll-box').append('<div class="nav"><span class="up"></span><span class="dwn"></span></div><div class="slider"><div class="ui-slider-handle"></div></div>');
		$('.scroll-box .text').wrapAll('<div class="scroll-box-viewport"></div>');
		
		$('.slider').slider({
			min: 0, 
			max: maxslide,
			slide: function(e, ui) {
				va = '-'+ui.value+'px';
				$('.scroll-box .text').css({top: va}); 
			}
		});
		
		$('.scroll-box').mousewheel(function(event, delta) {
			if (delta > 0) {
				$('.slider').slider('moveTo', '-=50');
			}
			else if (delta < 0) {
				$('.slider').slider('moveTo', '+=50');
			}
			event.stopPropagation();
			event.preventDefault();
		});
		
		$('.scroll-box .up').click(function(){
			$('.slider').slider('moveTo', '-=50');
		});
		
		$('.scroll-box .dwn').click(function(){
			$('.slider').slider('moveTo', '+=50');
		});
	}
}

/******************************************************************************************************************/
// COLLAPSE  FUNCTIONS
/******************************************************************************************************************/

function collapseHandle(){
	$('.collapse .photo, .collapse .content').hide();
	$('.collapse H1').attr('class','plus');
	
	$('.collapse H1').toggle(function(){
		$(this).parent().find('.photo, .content').show();
		$(this).attr('class','minus');
	}, function(){
		$(this).parent().find('.photo, .content').hide();
		$(this).attr('class','plus');
	});
}

/******************************************************************************************************************/
// ON DOCUMENT LOAD...
/******************************************************************************************************************/

$(function(){
	// News Ticker
	TickerRun(0);
	TickerHoverHandle();
	
	// BannerTicker
	BannerRun(0);
	BannerHoverHandle();
	
	// Slider handle
	sliderHandle();
	
	// Collapse handle
	collapseHandle();
	
	// Some Menu Hacks
	$('#nav LI UL').fadeTo(0, 0.95);
	
	$('#nav LI UL').each(function(){
		$(this).parent('LI').find('A').eq(0).click(function(){ return false; });
	});
});