// JavaScript Document

function initAll() {
	Shadowbox.init({
		skipSetup: true
	});
	
	initMenu();
	initGallery();
	initMoreOptions();
	initScrollbars();
}

function initScrollbars() {
	$('.colonna-destra .cronologico').wrapInner('<div id="scroll-container">');
	var s = $('#scroll-container');
	s.addClass('scroll-pane');
	
	s.hide();
	var h = $('#conts').height() - 100;
	
	s.css({
		height:	h+'px'
	});
	
	s.wrapInner('<div class="overview">').wrapInner('<div class="viewport">').prepend('<div class="scrollbar"><div class="track"><div class="thumb"></div></div></div>');
	
	s.fadeIn().tinyscrollbar();
	
	//$(window).resize( function(){ $('.scroll-pane').each(function(){ $(this).tinyscrollbar_update() }) } );

}

function initMenu() {
	// setta voce attuale del menu
	$('#main-menu a').each(function(){
				if ($.inArray($(this).attr('class'), $('body').attr('class').split(' ')) != -1) $(this).addClass('active');
			});
	// setta voce attuale del sotto menu
	$('.menu-sezione a').each(function(){
				if ($.inArray($(this).attr('class'), $('#conts').attr('class').split(' ')) != -1) $(this).addClass('active');
			});
	/*$('.menu-sezione a').each(function(){
				if ($(this).hasClass($('#conts').attr('class'))) a.addClass('active');
			});*/
}

function initGalleryZoom() {
	galleryParams = {
						small_width:330,
						small_height:210,
						big_width:920,
						big_height:585
					};
	if($('.gallery a').length != 0) {
		$('.gallery a').each(function(){
			var a = $(this);
			$(this).click(function(ev) {
				ev.preventDefault();
				var imd = a.parents('.gallery').siblings('.image-zoom');
				imd.html('<a href=""><img src="" /></a>');
				var im = imd.find('img');
				im.attr({'src': a.attr('href')});
				im.parent().attr('href', a.attr('rel'));
				if(a.attr('big-size')) {
						var s = a.attr('big-size').split('x');
						galleryParams.big_width = parseInt(s[0]);
						galleryParams.big_height = parseInt(s[1]);
				} else {
						galleryParams.big_width = 920;
						galleryParams.big_height = 585;
				}
				imd.zoom(galleryParams);
			});
		});
	}
	$('.image-zoom').zoom(galleryParams);
}

function initGallery() {
	var gals = $('.gallery');
	var n = 0;
	gals.each(function() {
		// galleria immagini
		$('a[target="metimg"], a:not([target])', $(this)).each(function(){
			Shadowbox.setup($(this), {
				gallery: "photo-gallery"+n
			});
		 });
		n++
		
		// galleria video e audio
		$('a[target="metjwp"]', $(this)).each(function(){
			Shadowbox.setup($(this), {
				player: 'flv',
				flv: ['flv', 'mp4'],
				width: 600,
				height: 400
			});
		});
		
		// galleria documenti
		$('a[target="metwin"]', $(this)).each(function(){
			$(this).attr('target', '_blank');
		});
	});
}

function initMoreOptions() {
	// DA RIMUOVERE QUANDO IL CALENDARIO E' ATTIVO
	// inizializza calendario
	/*
	calendario = $('#calendar').fullCalendar({
			firstDay: 1,
			monthNames: ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'],
			monthNamesShort: ['Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago', 'Set', 'Ott', 'Nov', 'Dic'],
			dayNames: ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'],
			dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab'],
			buttonText: {
							today:    'oggi',
							month:    'mese',
							week:     'settimana',
							day:      'giorno'
						},
	// put your options and callbacks here
	});

	$('#testInfo').click(function(){
		Shadowbox.open({
				content: '/inc/asp/info-calendar.asp?calDay=20101125',
				player: 'iframe',
				width: 600
		});
	 });
*/

	calendario = $('#calendar').fullCalendar({
			firstDay: 1,
			monthNames: ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'],
			monthNamesShort: ['Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago', 'Set', 'Ott', 'Nov', 'Dic'],
			dayNames: ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'],
			dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab'],
			buttonText: {
							today:    'oggi',
							month:    'mese',
							week:     'settimana',
							day:      'giorno'
						},
			events: '/inc/asp/json-events.asp',
	// put your options and callbacks here
			eventMouseover: function(event, jsEvent, view){
			},
			eventClick: function(event, jsEvent, view){
				jsEvent.preventDefault();
				Shadowbox.open({
						content: event.url,
						player: 'iframe',
						width: 600
					});
			}
	});


	/* Rounded corner sotto IE < 9  (eliminate per compatibilità) */
	if (!jQuery.support.leadingWhitespace && false) {
		$('.rounded').corner('keep');
		$('.rounded-top').corner('top keep');
		$('.rounded-right').corner('right keep');
	}
	
	if (!jQuery.support.leadingWhitespace) {
		DD_roundies.addRule('.rounded', '8px');
		DD_roundies.addRule('.rounded-top', '8px 8px 0px 0px');
		//DD_roundies.addRule('.rounded-right', '0px 12px 12px 0px');
		DD_roundies.addRule('.tassello', '0px 8px 8px 0px');
		DD_roundies.addRule('.rounded-bottom', '0px 0px 8px 8px');
		DD_roundies.addRule('.rounded-left', '8px 0px 0px 8px');
	}
	// */
	
	// init form newsletter
	$('form .newsletter, form .metsms').hide();
	$('p.titolo-news input:checkbox').click(function() {
			$(this).parent().next('div').slideToggle('normal');
	});
	
	$('div.blocco-info a.leggi-altro').each(function(){
		var a = $(this).attr('href');
		$(this).parents('.blocco-info').find('div.image').click(function(){
			location.href = a;
		}).css({'cursor':'pointer'});
	});
	
	Shadowbox.setup('a.info-box', {
					width: 600
	});
	
	if($('body').hasClass('met-ragazzi')) {
		$('ul.social-network .fb').attr('href','http://www.facebook.com/home.php?#!/profile.php?id=100001587144898&v=info');
	}
}

/* $('ID or class of the element containing the fading objects').innerfade({
	animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'),
	speed: Fadingspeed in milliseconds or keywords (slow, normal or fast)(Default: 'normal'),
	timeout: Time between the fades in milliseconds (Default: '2000'),
	type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'),
	containerheight: Height of the containing element in any css-height-value (Default: 'auto')
	runningclass: CSS-Class which the container get’s applied (Default: 'innerfade')
}); */
function homeFade(){
	$('#banner').innerfade({
		animationtype: 'fade',
		speed: 'slow',
		timeout: 6000,
		type: 'random_start'
	});
	
	var h = 0;
	$('#home-news-container .home-news').each(function(){
							h = Math.max(h,$(this).height());
					   });
//	$('#home-news-container').parent().height(h);
	$('#home-news-container').innerfade({
		animationtype: 'fade',
		speed: 'slow',
		timeout: 6000
	});
	$('#home-news-container').height(h);

}


$(document).ready(initAll);
