//Per evitare chiusura del carosello al click cul navi
var navi = false;
var primo = true;
var altS;
var larS;
var aImm = [];
var TempTT = -1;
/*Preview del prodotto */
function preview(id){
  if(!jQuery('#caroselloLungo'+id).is(':visible'))
    jQuery("#"+id).show();
}

function previewClose(id){
  jQuery("#"+id).hide();
}

/* Gestione dei tooltip */
function tooltipOn(id){

    if(TempTT > -1){
      jQuery("#nascostaTooltip"+TempTT).hide();
      TempTT = id;
    }else{
      TempTT = id;
    }
    jQuery("#nascostaTooltip"+id).css({
      'position':'absolute',
      'left': jQuery('.posizione'+id).offset().left - jQuery('#contentContainer').offset().left - 31 + 'px',
      'top':jQuery('.posizione'+id).offset().top - 34 + 'px',
      'z-index':'300'
      });
    jQuery("#nascostaTooltip"+id).hover(function(e){
      
    },
    function(e){
      jQuery("#nascostaTooltip"+id).hide();
      
    }
    );
    
    jQuery("#nascostaTooltip"+id).show();
}

function tooltipOff(id){
    jQuery("#nascostaTooltip"+id).hover(function(){},
    function(e){
      jQuery("#nascostaTooltip"+id).delay(200).hide();
      e.stopPropagation();
    });
}

/* gestione della navigazione */

function navigazioneProdotto(a){
  if(jQuery('#caroselloLungo'+a).is(':visible')){
    if(!navi){
      jQuery('#padreCarosello'+a).removeClass('functionSide');
      jQuery('#padreCarosello'+a).removeClass('active');
      jQuery('#caroselloLungo'+a).fadeOut();
    }else{
      navi = false;
    }
  }else{
    jQuery('#padreCarosello'+a).addClass('functionSide');

    jQuery('#padreCarosello'+a).addClass('active');

    jQuery("#"+a).hide();
    jQuery('#caroselloLungo'+a).fadeIn();

    jQuery('li.bufferSide').each(function(){
      if(jQuery(this).attr('id') != 'padreCarosello'+a){
        jQuery(this).removeClass('functionSide');
        jQuery(this).removeClass('active');
      }

    });

    jQuery('.navigazioneProdotti').each(function(){
      if(jQuery(this).attr('id') != 'caroselloLungo'+a){
        jQuery(this).fadeOut();
      }
    });

    jQuery('.bufferSide').each(function(){
      if(jQuery(this).attr('id') != 'padreCarosello'+a){
        jQuery(this).css({'background-color' : '#e8e8e8'});
      }
    });
  }
}

function sfondoDiv(alt, lar){

  var Wid = jQuery('body').width();
  
  jQuery('#sfondo').css({
      'width':Wid,
      'height':jQuery(window).height()-25,
      'z-index':'1',
      'overflow':'hidden'
    });

//   alert('moltiplico ' + Wid + ' per ' + alt + ' e poi lo divido per '+ lar);
  var altD = ((Wid * alt) / lar)-35;
  jQuery('#sfondo > img').attr('width',Wid);
  
  jQuery('#sfondo > img').attr('height',altD);

}
  
function resizer(){
  //Margine laterale
  if(jQuery('body')){
    var Wid = jQuery('body').width();
    Wid = Wid - 240;
    //Setto i margini
    jQuery('.navigazioneProdotti').css({
      'width' :  '928px'
       });
  }
}

jQuery(window).load(function () {
  //jQuery('#contenitoreSvanireFoto').animate('background-image':'none', function(){jQuery('.singolaFotoSvanire').fadeIn(1000)});
  jQuery('.singolaFotoSvanire').fadeIn(1000);
  jQuery('#contenitoreSvanireFoto').css({'background-image':'none'});
//   if(primo && jQuery('#sfondo > img')){
//     altS = jQuery('#sfondo > img').height();
//     larS = jQuery('#sfondo > img').width();
//
//     primo = false;
//   }
//   if(jQuery('#sfondo > img'))
//     sfondoDiv(altS, larS);

  var altezzaConfronto = 0;
  jQuery('.singolaFotoSvanire').each(function(){
    aImm.push(jQuery(this));
    if(altezzaConfronto < jQuery(this).height())
      altezzaConfronto = jQuery(this).height();
    jQuery(this).hide();
    //jQuery('#contenitoreSvanireFoto').css({'height': altezzaConfronto+'px'})

  });

  if(aImm.length > 0)
    aImm[aImm.length -1].show();

  var ik = aImm.length;
  if(aImm.length > 1){
    setInterval(function() {
    if( ik <  aImm.length){
      aImm[ik].fadeIn(3000);
      if(ik > 0)
        aImm[ik-1].fadeOut(3000);
      ik++;
    }else if(ik == aImm.length){
      aImm[ik-1].fadeOut(3000);
      aImm[0].fadeIn(3000);
      ik = 1;
    }
    }, 6000);
  }

});

jQuery(document).ready(function(){

  resizer();

  //jQuery('#productNavigation').css({'height':jQuery(document).height() -108 - jQuery('#footer').height()  +'px'});
  jQuery(window).resize(function(){
    resizer();
    if(jQuery('#sfondo > img'))
      sfondoDiv(altS, larS);
    //jQuery('#productNavigation').css({'height':jQuery(document).height() -108 - jQuery('#footer').height()  +'px'});
  });

/* Bottone per far apparire la form di ricerca */

jQuery('.bottonePerForm').click(function(){
  jQuery('#ricercaForm').toggle();
  if(jQuery('#ricercaForm').is(':visible'))
    jQuery('#ricerca').addClass('formRicercaVisibile');
  else
    jQuery('#ricerca').removeClass('formRicercaVisibile');
});

jQuery('.browse').click(function(){
  //Evito il chiudersi del carosello al click cul navi
  navi = true;
});
  var buf = 0;
  jQuery('.singolaFotoSvanire').each(function(){
    if( buf > 0 )
     jQuery(this).hide();
    buf++;
  });
  
});

  
