// funzione richiamata dal flash al click su un codice prodotto
function goToProduct(index) {
  $('#slideshow').trigger('goto', [index]);
}

 // chiamta dal javascript per inviare un comando al filmato flash della barra superiore
function flashCallBack(newIndex) {
  if (document['navigationBar'].changeFlashSetting)
    document['navigationBar'].changeFlashSetting(newIndex);
} // end of flashCallBack


// funzioni per l'effetto show/hide dei vari div prodotti
$(document).ready(function() {
  var hideLinks = $(".hideL");
  var hideLinks2 = $(".hideL2");
  var hideBullets = $(".hideB");

  var showLinks = $(".showL");
  var showBullets = $(".showB");

  var docLinks = $(".docLink");
  var infoLinks = $(".infoLink");

  $(hideLinks).click(function(event) {
    event.preventDefault();
    var index = $(hideLinks).index(this);
    if (BrowserDetect.browser == "Safari") {
      $("#hide" + index).toggle("slow");
      $("#show" + index).toggle("slow");
    }
    else
      $("#hide" + index).toggle("slow", function() {$("#show" + index).toggle("slow");});
  });
  
  $(hideLinks2).click(function(event) {
    event.preventDefault();
    var index = $(hideLinks2).index(this);
    if (BrowserDetect.browser == "Safari") {
      $("#hide" + index).toggle("slow");
      $("#show" + index).toggle("slow");
    }
    else
      $("#hide" + index).toggle("slow", function() {$("#show" + index).toggle("slow");});
  });
  
  $(hideBullets).click(function(event) {
    event.preventDefault();
    var index = $(hideBullets).index(this);
    if (BrowserDetect.browser == "Safari") {
      $("#hide" + index).toggle("slow");
      $("#show" + index).toggle("slow");
    }
    else
      $("#hide" + index).toggle("slow", function() {$("#show" + index).toggle("slow");});
  });

  $(showLinks).click(function(event) {
    event.preventDefault();
    var index = $(showLinks).index(this);
    if (BrowserDetect.browser == "Safari") {
      $("#show" + index).toggle("slow");
      $("#hide" + index).toggle("slow");
    }
    else
      $("#show" + index).toggle("slow", function() {$("#hide" + index).toggle("slow");});
  });

  $(showBullets).click(function(event) {
    event.preventDefault();
    var index = $(showBullets).index(this);
    if (BrowserDetect.browser == "Safari") {
      $("#show" + index).toggle("slow");
      $("#hide" + index).toggle("slow");
    }
    else
      $("#show" + index).toggle("slow", function() {$("#hide" + index).toggle("slow");});
  });

  $(docLinks).click(function(event) {
    event.preventDefault();
    window.open($(this).attr('href'));
  });
  
  $(infoLinks).click(function(event) {
    event.preventDefault();
    window.location.href = $(this).attr('href');
  });


  jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
  };

  // Easing equation, borrowed from jQuery easing plugin
  // http://gsgd.co.uk/sandbox/jquery/easing/
  jQuery(function( $ ){
    $('#slideshow').serialScroll({
      items:'li',
      prev:'a.prevProduct',
      next:'a.nextProduct',
      navigation: '#navigation a.nav',
      offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
      start:0, //as we are centering it, start at the 2nd
      duration:1500,
      force:true,
      stop:true,
      lock:false,
      exclude: 0,
      cycle:false, //don't pull back once you reach the end
      easing:'easeOutQuart', //use this easing equation for a funny effect
      jump: true, //click on the images to scroll to them
      
      onBefore:function (e, elem, $pane, $items, pos) {
        flashCallBack(pos);
        
        // Codice che visualizza/nasconde le frecce avanti e indietro. Se viene tolto o commentato bisogna ricordarsi di eliminare il display=none
        // dallo style del div con id = "leftArrow" nel file product.php.
        if (pos == 0)
          $("#leftArrow").hide("slow");
        else
          $("#leftArrow").show("slow");
        
        if (pos == ($items.length - 1))
          $("#rightArrow").hide("slow");
        else {
          $("#rightArrow").show("slow");
        }

         //those arguments with a $ are jqueryfied, elem isn't.
        e.preventDefault();
        
        if (this.blur)
          this.blur();
      },
      onAfter:function(elem){
        //'this' is the element being scrolled ($pane) not jqueryfied
      }
    });
  });
  

});

