//$(function(){
//  var intervallo = 8000;
//  var newsContainer = $(".newsContainer");
//  newsContainer.each(function(index){
//    $(this).scrollable({mousewheel: true, circular: true}).autoscroll({interval: intervallo});
//  });
//});
/**
 * Plugin jQuery galleria personalizzata TelematicaItalia s.r.l.
 *
 * @author lbravi
 */
var lf_performing_animation = new Array;
(function($) {
  $.fn.listFader = function(options) {
    $machedElements = this.length;
    // iterate and reformat each matched element
    return this.each(function(indexElement) {
      // $this = $(this);
      lf_performing_animation[indexElement] = false;
      
      var self = {};
        // build main options before element iteration
        self.index = indexElement;
        self.opts = $.extend({}, $.fn.listFader.defaults, options);
        self.mainContainer = $(this);
        self.mainContainer.addClass('lf_main_container');
        self.toFadeList = self.mainContainer.children();
        self.numOfContent = self.toFadeList.length;
        self.totalContent = 0;
        self.singleWidth = 0;
        self.toFadeList.hide();
        self.toFadeList.eq(0).show();
        self.autoPlayInterval = null;
            
      // build element specific options
      // var o = $.meta ? $.extend({}, self.opts, $this.data()) : self.opts;  
      
      if(self.opts.tooltips){
        self.toFadeList.each(function (index, singleElement) {
          var tooltipText = $(singleElement).find('img').attr('title');
          $(singleElement).find('img').removeAttr('title'); 
          $(singleElement).append('<div class="ui-listFader-tooltips" style="display:none; position:relative; bottom:26px; margin: 0 auto;"><div>'+tooltipText+'</div></div>');
        });
      }
      
      /*
      var button_width = 0;
      if(self.opts.prev != ''){
        self.mainContainer.prepend('<div class="tr_scroller_button '+self.opts.prev+'" style="float:left;">&nbsp;</div>');
        button_width += self.mainContainer.children('div.'+self.opts.prev).outerWidth(true) + ((self.opts.next != '')? (self.scrollerPadding/2) : self.scrollerPadding);
        self.scroller.css('float', 'left');
      }
      if(self.opts.next != ''){
        self.mainContainer.append('<div class="tr_scroller_button '+self.opts.next+'"style="float:right;">&nbsp;</div>');
        button_width += self.mainContainer.children('div.'+self.opts.next).outerWidth(true) + ((self.opts.prev != '')? (self.scrollerPadding/2) : self.scrollerPadding);
        self.scroller.css('float', 'left');
      }
      
      self.sliderWidth -= button_width;
      self.scroller.width(self.sliderWidth);
      */
     
      var callbackFunctionName = self.opts.fadeOutCallback;
      var callback = ((callbackFunctionName != null && typeof functionName == "function")?callbackFunctionName:null);

      /*
      if(self.opts.prev != '' || self.opts.next != ''){  // ATTIVO I CONTROLLI LATERALI
        self.mainContainer.append('<div style="clear:both;"></div>');
        self.controls = self.mainContainer.children('div.tr_scroller_button');
        self.controls.each(function(){
          $(this).hover(
            function(){
              if(self.opts.autoscroll)
                clearInterval(autoPlayInterval);
            },
            function(e){
              if(self.opts.autoscroll){
                clearInterval(autoPlayInterval);
                var apFunction3 = function(){
                  $.fn.listFader.autoplay.call(e.target, self, callback);
                };
                autoPlayInterval = setInterval(apFunction3, self.opts.autoPlaySpeed);
              }
            }
          );
          $(this).click(function(e){
            if($(this).is('.'+self.opts.next))
              $.fn.listFader.fadeToNext(self, e, callback);
            else
              $.fn.listFader.fadeToPrev(self, e, callback);
          });
        });
      }*/
      
      if(self.opts.autoPlay){ // ATTIVO L'AUTOPLAY
        var apFunction = function(){
          $.fn.listFader.autoplay.call(null, self, callback);
        };
        setTimeout(function(){
          self.autoPlayInterval = setInterval(apFunction, self.opts.autoPlaySpeed);
        }, (self.opts.autoPlaySpeed/$machedElements)*self.index);
      }

      if(self.opts.nextOnClick){ // ATTIVO IL FADE SUL CLICK
        self.toFadeList.click(
          function(e){
            $.fn.listFader.nextonclick.call(null, self, e, callback);
          });
      }
      
      if(self.opts.pauseOnHover){ // ATTIVO LA PAUSA ON HOVER
        self.mainContainer.hover(
          function(e){
            if(self.opts.autoPlay){
              clearInterval(self.autoPlayInterval);
            }
          },
          function(e){
            if(self.opts.autoPlay){
              clearInterval(self.autoPlayInterval);
              var apFunction2 = function(){
                $.fn.listFader.autoplay.call(e.target, self, callback);
              };
              self.autoPlayInterval = setInterval(apFunction2, self.opts.autoPlaySpeed);
            }
          }
        );
      }
        
      if(self.opts.tooltips){ // ATTIVO I TOOLTIPS
        self.toFadeList.hover(
          function() {
            $(this).children('.ui-listFader-tooltips').css('display', 'block');
          },
          function() {
            $(this).children('.ui-listFader-tooltips').css('display', 'none');
          }
        );
      }
    });
  };
  //
  // private function for debugging
  //
  function debug($obj) {
    if (window.console && window.console.log)
      window.console.log($obj);
  };
  //
  // define and expose our autoplay function
  //
  $.fn.listFader.autoplay = function(self, callback){
    if(self.opts.direction == 'next')
      $.fn.listFader.fadeToNext(self, {autoPlay:true}, callback);
    else if(self.opts.direction == 'prev')
      $.fn.listFader.fadeToPrev(self, {autoPlay:true}, callback);
  }
  
  $.fn.listFader.fadeToNext = function(self, event, callback){
    $.fn.listFader.runFade('next', self, event, callback);
  }
  
  $.fn.listFader.fadeToPrev = function(self, event, callback){
    $.fn.listFader.runFade('prev', self, event, callback);
  }

  $.fn.listFader.nextonclick = function(self, event, callback){
    if(self.opts.direction == 'next')
      $.fn.listFader.fadeToNext(self, event, callback);
    else if(self.opts.direction == 'prev')
      $.fn.listFader.fadeToPrev(self, event, callback);
  }
    
  $.fn.listFader.runFade = function(direction, self, event, callback){
    var precedente = self.mainContainer.children(':visible');

    var index = null;

    if(direction == 'next')
      index = precedente.index()+1;
    else if(direction == 'prev')
      index = precedente.index()-1;
    
    if(index >= self.numOfContent)
      index = 0;
    if(index < 0)
      index = self.numOfContent-1;
    
    var element = self.toFadeList.eq(index);
    
    if(lf_performing_animation[self.index]) return;
      
    lf_performing_animation[self.index] = true;
    
    //$(self.mainContainer).stop().clearQueue().queue(function(){
      precedente.stop().fadeOut(self.opts.fadeOutSpeed);
      element.stop().delay(self.opts.fadeOutSpeed).fadeIn(self.opts.fadeInSpeed, function(){
        if(callback != null){
          try {
            callback.call(null, event, element);
          }
          catch(err) {
            debug(callback);
            debug(err);
          }
        }
        lf_performing_animation[self.index] = false;
      });
    //}).dequeue();
    
  }
  
  //
  // plugin defaults option
  //
  $.fn.listFader.defaults = {
    fadeOutSpeed:  1000,
    fadeInSpeed:  1000,
    pauseOnHover: true,
    autoPlay: false,
    autoPlaySpeed: 5000,
    nextOnClick: false,
    fadeOutCallback: null,
    direction: 'prev',
    prev: '',
    next: '',
    tooltips: false
  };
//
// end of closure
//
})(jQuery);
