$(document).ready(function() {
	
	// Apertura in nuova finestra
	$(".tBlank").click(function() {
		var url = $(this).attr('href');
		window.open(url);
		return false;
    });
	
	// Gestone gallery di immagini
	if ($("div.iCmsPagImageGallery").length > 0) {
		var timeFade = 1000;
		var idImage = null;
		var statusObj = $(this).find("li.status");
		var status = statusObj.html();
		var imgGalleryVisible = 1;
		var imgGalleryTot = parseInt(status.substring(status.indexOf('/') + 1, status.length));
				
		$("div.iCmsPagImageGallery ul span.prev, div.iCmsPagImageGallery ul span.next").click(function() {
			effect = false;
			idImage = imgGalleryVisible;
			if ($(this).hasClass('next')) {
				if (imgGalleryVisible < imgGalleryTot) {
					imgGalleryVisible++;
					effect = true;
				}
			} else {
				if (imgGalleryVisible > 1) {
					imgGalleryVisible--;
					effect = true;
				}
			}			
			
			if (effect) {				
				$("div.image.gallery img.img" + idImage).fadeOut(timeFade);
				$("div.image.gallery img.img" + imgGalleryVisible).fadeIn(timeFade);
				$("div.iCmsPagImageGallery ul li.status").html(imgGalleryVisible + ' / ' + imgGalleryTot);
			}
		});
	}
	
	// Overlay
	if ($(".fancybox").length > 0) {
		$(".fancybox, .fancybox.nl").fancybox({
			  'width'			   : 530
			, 'height'			   : 465
			, 'autoScale'		   : false
			, 'overlayOpacity'	   : 0.5
			, 'overlayColor'	   : '#000'
			, 'hideOnOverlayClick' : false
			, 'titleShow'          : false
			, 'centerOnScroll'	   : true
		});
		$(".fancybox.cnt").fancybox({
			  'width'			   : 530
			, 'height'			   : 465
			, 'autoScale'		   : false
			, 'overlayOpacity'	   : 0.5
			, 'overlayColor'	   : '#000'
			, 'hideOnOverlayClick' : false
			, 'titleShow'          : false
			, 'centerOnScroll'	   : true
		});
		$(".fancybox.cv").fancybox({
			  'width'			   : 835
			, 'height'			   : 469
			, 'autoScale'		   : false
			, 'overlayOpacity'	   : 0.5
			, 'overlayColor'	   : '#000'
			, 'hideOnOverlayClick' : false
			, 'titleShow'          : false
			, 'centerOnScroll'	   : true
		});
	}
	
	// Gestione altre lingue
	othersLangInput = $("input.iText.lang");
	$("div.otherLanguages input").click(function() {
		if (!$(this).is(":checked")) {
			othersLangInput.addClass("hidden");			
		} else {
			othersLangInput.removeClass("hidden");			
		}
	});
	
	// Gestione hover allegati pagina (codice etico e condizioni generali)
	$("ul.attachlist").hover(
		function() {},
		function() {
			$(this).children('li.open-close').addClass('hidden');
		}
	)
	$("ul.attachlist li.active").hover(
		function() {
			$(this).parent('ul').children('li.open-close').removeClass('hidden');
		},
		function() {}
	)
	
	// Gestione credits
	var creditObj = $(".credits");
	var ulCreditObj = $("ul.detail-credits");
	var timeFideCredits = 700;
	if (creditObj.length > 0) {	
		creditObj.click(function() {
			if (ulCreditObj.is(":visible")) {
				ulCreditObj.fadeOut(timeFideCredits)
			} else {
				ulCreditObj.fadeIn(timeFideCredits)
			}
		});
	}
	
	// Gestione scroll
	if ($("div.colTextLink").length > 0) {		
		showHideScoll();		
	}
	
	// Gestione flash	
	if ($("div#myContent").length > 0) {
		lang = $("#linguaFlash").html();
		var flashvars = {
			'lingua' : lang
		};
		var params = {
			'wmode'  : 'transparent'			
		};
		var attributes = {};
		swfobject.embedSWF("/flash/map.swf", "myContent", "794", "355", "9.0.0", "", flashvars, params, attributes);
	}
	
});

/** Funzione per la gestione del testo del flash **/
function setText(section) {		
	textarea = $('.textarea');			
	if (! textarea.hasClass('contacts')) {			
		textarea.addClass('contacts');
	}
				
	$.ajax({
		type: "POST",
		url: "/ajax-contacts/",
		data: {section : section},
		dataType: "html",
    	async: false,				
		success: function(xhtml) {
			$('.textarea').html(xhtml);
			showHideScoll();
		}
	});
}

/** Funzione che in base all'altezza valuta se visuallizare lo scoll o meno */
function showHideScoll() {
	textareaHeight = $(".textarea").height();		
	heightBox = $("#box").height();
	if (textareaHeight > heightBox) {
		$("#scroll-management").show();
		dsSC(0, 20, (heightBox - 30), 'dsvTrack', 'dsvUp', 'dsvDown', 'dsvDrag', 'box');
	} else {
		$("#scroll-management").hide();
	}
}
