$(document).ready(function () {
  $('#link-ministero').mouseenter(function() { $('#layer-ministero').show(); })
  $('#link-ministero').mouseleave(function() { $('#layer-ministero').hide(); })
  $('#link-carta').mouseenter(function() { $('#layer-carta').show(); })
  $('#link-carta').mouseleave(function() { $('#layer-carta').hide(); })
  $('#link-bosch').mouseenter(function() { $('#layer-bosch').show(); })
  $('#link-bosch').mouseleave(function() { $('#layer-bosch').hide(); })
  $('#link-bebe').mouseenter(function() { $('#layer-bebe').show(); })
  $('#link-bebe').mouseleave(function() { $('#layer-bebe').hide(); })
  
  if ($('label.radio').size() > 0)
    styleRadios();
  
  centerContent();
  footerHandler();
});

$(window).resize(function() {
  centerContent();
  footerHandler();
});

function centerContent() {
  var mt = ($(window).height()-$('.footer-container').height()-$('.header').height()-$('.mainContainer').height())/3;
  $('.mainContainer').css('margin-top',((mt > 0) ? mt : 0)+'px');
}

var fotpad = 50;
function footerHandler() {
  var space = ($(window).height()-$('.footer-container').height()-$('.header').height()-$('.mainContainer').height());
  // height management
  if (($(window).height()-$('.header').height()-$('.mainContainer').height()) < $('.footer-container').height()) {
    $('.footer-container').css('position','static');
    $('.header').css('marginTop','0px');
    $('.footer-container').css('paddingBottom', '0px')
  } else {
    $('.footer-container').css('position','fixed');
    $('.header').css('marginTop', space*0.2+'px');
    $('.footer-container').css('paddingBottom', space*0.2+'px')
  }
  // width management
  if (($(window).width() <= ($('.footer').width()+(fotpad*2)))) {
    var pad = Math.round((($(window).width())-$('.footer').width())/2);
    if (pad < 0) pad = 0;
    $('.footer').css({'paddingLeft': (pad)+'px','paddingRight': (pad)+'px'});
  } else {
    $('.footer').css({'paddingLeft': fotpad+'px','paddingRight': fotpad+'px'});
  }
}


function styleRadios() {
  $('label.radio').each(function() {
		if ($(this).children('input').attr('checked'))
			$(this).addClass('selected');
    $(this).click(function() {
			if (!$(this).hasClass('selected')) {
				$(this).parents('form').find('input[name="'+$(this).children('input').attr('name')+'"]').each(function() {
					$(this).parent().removeClass('selected');
				});
				$(this).addClass('selected');
				$(this).children('input').click();
			}
		});
	});
}

function $F(name) {
  var val;
  $('input[name="'+name+'"]').each(function() {
    if ($(this).attr('checked')) 
      val = $(this).val();
  });
  return val;
}

