function _finish_cronologia() {
	$('#jquery-lightbox').remove();
	$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
	// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	$('embed, object, select').css({ 'visibility' : 'visible' });
	$('#Gallery3D_container').show();
}

function _show_image_cronologia(title, body) {
	$('#lightbox-loading').hide();
	$('#lightbox-image').fadeIn(function() {
		_show_image_data_cronologia(title, body);
		//_set_navigation();
	});
	//_preload_neighbor_images();
};
function _show_image_data_cronologia(title, body) {
	$('#lightbox-container-image-data-box').slideDown('fast');
	$('#lightbox-image-details-caption').hide();

	if ( title ) {
		$('#lightbox-image-details-caption').html(title+"<br/>"+body).show();
	}
}

function ___getPageSize_cronologia() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function ___getPageScroll_cronologia() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

function _resize_container_image_box_cronologia(intImageWidth,intImageHeight, title, body) {
	// Get current width and height
	var intCurrentWidth = $('#lightbox-container-image-box').width();
	var intCurrentHeight = $('#lightbox-container-image-box').height();
	// Get the width and height of the selected image plus the padding
	var intWidth = (intImageWidth + (10 * 2)); // Plus the image´s width and the left and right padding value
	var intHeight = (intImageHeight + (10 * 2)); // Plus the image´s height and the left and right padding value
	// Diferences
	var intDiffW = intCurrentWidth - intWidth;
	var intDiffH = intCurrentHeight - intHeight;
	// Perfomance the effect
	$('#lightbox-container-image-box').animate({ width: intWidth, height: intHeight },400,function() { _show_image_cronologia(title, body); });
	if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
		if ( $.browser.msie ) {
			___pause(250);
		} else {
			___pause(100);	
		}
	} 
	$('#lightbox-container-image-data-box').css({ width: intImageWidth });
	$('#lightbox-nav-btnPrev,#lightbox-nav-btnNext').css({ height: intImageHeight + (10 * 2) });
};

function lightbox_cronologia(src, language, title, body)
{
	$('#Gallery3D_container').hide();
	$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><img id="lightbox-image"><div style="" id="lightbox-nav"><a href="#" id="lightbox-nav-btnPrev"></a><a href="#" id="lightbox-nav-btnNext"></a></div><div id="lightbox-loading"><a href="#" id="lightbox-loading-link"><img src="' + MgnlContext.settings.basePath + '/docroot/erg/images/lightbox-ico-loading.gif' + '"></a></div></div></div><div id="lightbox-container-image-data-box"><div id="lightbox-container-image-data"><div id="lightbox-image-details"><span id="lightbox-image-details-caption"></span><span id="lightbox-image-details-currentNumber"></span></div><div id="lightbox-secNav"><a href="#" id="lightbox-secNav-btnClose"><img src="' + MgnlContext.settings.basePath + '/docroot/erg/images/lightbox-btn-close_' + language + '.gif' + '"></a></div></div></div></div>');	
	// Get page sizes
	var arrPageSizes = ___getPageSize_cronologia();
	// Style overlay and show it
	$('#jquery-overlay').css({
		backgroundColor:	'#000000',
		opacity:			.5,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn();
	// Get page scroll
	var arrPageScroll = ___getPageScroll_cronologia();
	// Calculate top and left offset for the jquery-lightbox div object and show it
	$('#jquery-lightbox').css({
		top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
		left:	arrPageScroll[0]
	}).show();
	// Assigning click events in elements to close overlay
	$('#jquery-overlay,#jquery-lightbox').click(function() {
		_finish_cronologia();								
	});
	// Assign the _finish_cronologia function to lightbox-loading-link and lightbox-secNav-btnClose objects
	$('#lightbox-loading-link,#lightbox-secNav-btnClose').click(function() {
		_finish_cronologia();
		return false;
	});
	// If window was resized, calculate the new overlay dimensions
	$(window).resize(function() {
		// Get page sizes
		var arrPageSizes = ___getPageSize_cronologia();
		// Style overlay and show it
		$('#jquery-overlay').css({
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		});
		// Get page scroll
		var arrPageScroll = ___getPageScroll_cronologia();
		// Calculate top and left offset for the jquery-lightbox div object and show it
		$('#jquery-lightbox').css({
			top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
			left:	arrPageScroll[0]
		});
	});

	// Start
	$('#lightbox-loading').show();
	if ( false ) {
		$('#lightbox-image,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
	} else {
		// Hide some elements
		$('#lightbox-image,#lightbox-nav,#lightbox-nav-btnPrev,#lightbox-nav-btnNext,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
	}
	// Image preload process
	var objImagePreloader = new Image();
	objImagePreloader.title = title;
	objImagePreloader.body = body;
	objImagePreloader.onload = function() {
		$('#lightbox-image').attr('src',src);
		// Perfomance an effect in the image container resizing it
		_resize_container_image_box_cronologia(objImagePreloader.width,objImagePreloader.height,objImagePreloader.title,objImagePreloader.body);
		//	clear onLoad, IE behaves irratically with animated gifs otherwise
		objImagePreloader.onload=function(){};
	};
	objImagePreloader.src = src;
}

MgnlContext = new Object();
MgnlContext.settings = new Array();

function changeContrast(color, color_footer, color_calendar, color_tab_home, color_link, color_link_bt_ergpt, color_link_bt, color_strong, color_strong_hi, classe)
{
	var p = document.getElementsByTagName('body');
	for(n=0; n<p.length; n++)
	{
		p[n].style.color = color;
	}
	var a = document.getElementsByTagName('a');
	for(n=0; n<a.length; n++)
	{
		if(a[n].className != 'button' && a[n].className != 'button_no_dialog'){
			a[n].style.color = color_link;
		}
	}	
	var s = document.getElementsByTagName('strong');
	for(n=0; n<s.length; n++)
	{
		
		if(s[n].className == 'bold-green'){
			s[n].style.color = color_strong;
		}
	}	
	var div_hi = document.getElementsByTagName('div');
	for(n=0; n<div_hi.length; n++)
	{
		if( div_hi[n].className == 'highlight'){
			var s_hi = div_hi[n].getElementsByTagName('strong');
			for(i=0; i<s_hi.length; i++)
			{
				s_hi[i].style.color = color_strong_hi;
			}	
		}
		if( div_hi[n].className == 'contatto'){
			div_hi[n].style.color = color;
		}
		if( div_hi[n].className == 'main-info-body'){
			var pi = div_hi[n].getElementsByTagName('p');
			for(i=0; i<pi.length; i++)
			{
				pi[i].style.color = color_tab_home;
			}				
		}
		if( div_hi[n].className == 'legend'){
			div_hi[n].style.color = color_calendar;
		}				
	}	
	var p_hi = document.getElementsByTagName('p');
	for(n=0; n<p_hi.length; n++)
	{
		if( p_hi[n].className == 'highlight'){
			var s_hi = p_hi[n].getElementsByTagName('strong');
			for(i=0; i<s_hi.length; i++)
			{
				s_hi[i].style.color = color_strong_hi;
			}	
		}
	}	
	var submenu = document.getElementById('submenu');
	if(submenu!=null){
		var a_submenu = submenu.getElementsByTagName('a');
		for(n=0; n<a_submenu.length; n++)
		{
			a_submenu[n].style.color = '';
			a_submenu[n].className = classe;
		}	
	}	
	var a_menu = document.getElementById('menu').getElementsByTagName('a');
	for(n=0; n<a_menu.length; n++)
	{
		a_menu[n].style.color = '';
		a_menu[n].className = classe;
	}
	var t = document.getElementsByTagName('table');
	for(n=0; n<t.length; n++)
	{
		if( t[n].className == 'erg_calendar'){
			var a = t[n].getElementsByTagName('a');
			for(i=0; i<a.length; i++)
			{
				a[i].style.color = '';
			}	
		}
	}		
	var p_footer = document.getElementById('footer').getElementsByTagName('p');
	for(n=0; n<p_footer.length; n++)
	{
		p_footer[n].style.color = color_footer;
	}
	var ergpt = document.getElementById('ergpt_middle');
	if(ergpt!=null){
		var a_ergpt = ergpt.getElementsByTagName('a');
		for(n=0; n<a_ergpt.length; n++)
		{
			a_ergpt[n].style.color = color_link_bt_ergpt;
		}	
	}	
	var invest = document.getElementById('investitori');
	if(invest!=null){
		var a_invest = invest.getElementsByTagName('a');
		for(n=0; n<a_invest.length; n++)
		{
			a_invest[n].style.color = color_link_bt;
		}	
	}	
	var media = document.getElementById('media');
	if(media!=null){
		var a_media = media.getElementsByTagName('a');
		for(n=0; n<a_media.length; n++)
		{
			a_media[n].style.color = color_link_bt;
		}	
	}		
	return false;	
}
function changeFontSize(size)
{
	var p = document.getElementsByTagName('body');
	for(n=0; n<p.length; n++)
	{
		p[n].style.fontSize = size + 'px';
	}
	
	return false;
}

function toggleCard(cardId)
{
	var shouldDisplay = $('#'+cardId).hasClass('hidden');
	$('.card').each(function() {
		$(this).addClass("hidden");
	});
	if(shouldDisplay)
	{
		$('#'+cardId).removeClass("hidden");
	}
}

function fold(obj) {
	obj.parent().next().slideToggle();
	obj.parent().toggleClass("closed").toggleClass("open");
	var t = obj.attr("title");
	if(t.match("Show")) {
		obj.attr("title", t.replace("Show","Hide"));
	} else {
		obj.attr("title", t.replace("Hide","Show"));
	}
	return false;
}

$(document).ready(function() {
	$.fn.media.defaults.flvPlayer = MgnlContext.settings.basePath + '/docroot/erg/media/player.swf';
	if ($.browser.msie && ($.browser.version < 7)) {
		$('#menu li').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	};
	$('.media-right').media({
		caption: false,
		params: {allowfullscreen: true},
		flvPlayer: MgnlContext.settings.basePath + '/docroot/erg/media/player.swf'
	});
	$('.media').media({
		caption: false,
		params: {allowfullscreen: true},
		flvPlayer: MgnlContext.settings.basePath + '/docroot/erg/media/player.swf'
	});
	$('.media-simple').media({
		caption: false,
		params: {allowfullscreen: true, allowscriptaccess: 'always'}
	});
	$('.tabs').tabs();
	$('.faq h3').each(function() {
		$(this).addClass($(this).next().attr("class"));
		if($(this).next().hasClass("closed") || $(this).next().hasClass("open")) {
			var t = ($(this).next().hasClass("closed")) ? "Show" : "Hide";
			$(this).wrapInner('<a href="#" onclick="fold($(this));return false;" title="'+t+'"></a>');
		}
	});
});

