function apriModale() {
  $('select').hide();
  $('#modalBoxOverlay').show();
}

function chiudi3D() {
  window.close();
}

function chiudiModale() {
  $('select').show();
  $('#modalBoxOverlay, #finestraModale ').hide();
}

function mostraContenuto(data) {
  var offset = $('#finestraModale').outerHeight() / 2;
  
  if (typeof document.body.style.maxHeight == "undefined") {
    var viewportheight = document.documentElement.clientHeight //altezza area visibile
    var scrOfY = document.documentElement.scrollTop; //scroll verticale
    var topIe6 = scrOfY + (viewportheight / 2) - offset - 20;	//margin-top
    $('#finestraModale').css({'top': topIe6 + 'px'}).fadeIn();
  } else {
    // tutti gli altri
    $('#finestraModale').css({'top':'42%', 'margin-top':'-' + offset + 'px'}).fadeIn();
  }
  
  $('div#finestraModale .messaggio').html(data).css({'visibility':'visible'});
}

$(function(){
  
  $('#main-nav ul').hide();
  $('#main-nav li.current').find('ul').attr('data-current', true).show();
  
  $('#main-nav > li').hover(function() {
    if (!$(this).hasClass('current')) {
      $(this).addClass("current");
      $('#main-nav ul').hide();
      $(this).find('ul').fadeIn();
    }
  }, function() {
    $(this).removeClass("current");
  });
  
  $('#main-nav').hover(null, function() {
    $(this).find('ul').hide();
    $(this).find('ul[data-current=true]').show().parent().addClass('current');
  });
  
  // finestre modali
  $('a[rel="ajax"]').live('click', function(event){
    apriModale();
    var target = $(this).attr('href');
    $('div#finestraModale .messaggio').load(target, null, mostraContenuto);
    event.preventDefault();
  });

  // tracking spedizione
  $('a[rel="tracking"]').live('click', function(event){
    var target = $(this).attr('href');
    var windowLeft = (screen.width - 857) / 2;
    var windowTop = (screen.height - 590) / 2;
    window.open(target, 'popup', 'height=590, width=853, top=' + windowTop + ',left=' + windowLeft + ',scrollbars=no, resizable=0, menubar=0, toolbar=0, location=0, statusbar=0, fullscreen=0');
    event.preventDefault();
  });

  // popup
  $('a[rel="popup"]').live('click', function(event){
    var target = $(this).attr('href');
    var windowLeft = (screen.width - ($(this).attr('data-w') || 440)) / 2;
    var windowTop = (screen.height - ($(this).attr('data-h') || 440)) / 2;
    window.open(target, 'popup', 'height=' + (parseInt(($(this).attr('data-h')) || 440) + 10) + ', width=' + (parseInt(($(this).attr('data-w')) || 440) + 10) + ', top=' + windowTop + ',left=' + windowLeft + ',scrollbars=no, resizable=0, menubar=0, toolbar=0, location=0, statusbar=0, fullscreen=0');
    event.preventDefault();
  });
  
  
   // popup ciocco-mix
  $('a[rel="popup_ciocco"]').live('click', function(event){
    var target = $(this).attr('href');
    var windowLeft = (screen.width - ($(this).attr('data-w') || 905)) / 2;
    var windowTop = (screen.height - ($(this).attr('data-h') || 500)) / 2;
    window.open(target, 'popup', 'height=' + (parseInt(($(this).attr('data-h')) || 500) + 10) + ', width=' + (parseInt(($(this).attr('data-w')) || 905) + 10) + ', top=' + windowTop + ',left=' + windowLeft + ',scrollbars=no, resizable=0, menubar=0, toolbar=0, location=0, statusbar=0, fullscreen=0');
    event.preventDefault();
  });

});
