/*
 * primopiano.js 
 * Copyright (C) 2010 Provincia di Pesaro e Urbino  
 * Autore: Davide Principi <d.principi@provincia.ps.it>
 * 
 * $Id: primopiano.js 5544 2011-03-22 17:22:31Z davide $   
 */     

jQuery(document).ready(function($) {

  /* Disabilitazione effetti grafici per ie <= 6 */
  if($.browser.msie && parseInt($.browser.version.substr(0,1)) <= 6) {
    jQuery.fx.off = true;
  }  
  
  /**
   * BEGIN PrimoPiano -- Gestore della rotazione e dell'evidenziazione 
   * degli elementi in Primo Piano. 
   */  
  var primoPiano = function () {    
    var rootElement = $('ul.primoPiano').data('rotationInterval', 5000).data('currentIndex', -1).data('resetInterval', 100);
    var images = rootElement.find('li > img');
    var app = this;
    var opacityHigh = 0.9;
    var opacityLow = 0.5;
    var rotate = rootElement.hasClass('rotante');
    
    /* Associa ad ogni testo il proprio indice */
    rootElement.find('li').each(function(index, element) {
      $(element).find('div.text').data('index', index);        
    });
              
    /* Visualizza l'immagine all'indice dato */
    this.changeImage = function(nextIndex, fadeSpeed) { 
      var currentIndex = rootElement.data('currentIndex');   
      var currentImage = images.eq(currentIndex);
      var nextImage = images.eq(nextIndex);      
      var delta = nextIndex - currentIndex;
      
      if(fadeSpeed === undefined) fadeSpeed = 400;
      
      rootElement.data('currentIndex', nextIndex);
      
      // elimina eventuali dissolvenze pendenti:
      currentImage.stop(true, true);  
  
      if(delta > 0) {
          if(fadeSpeed > 0) {
            nextImage.fadeIn(fadeSpeed, function(){currentImage.hide()});                 
          } else {
            nextImage.show();
            currentImage.hide();
          }
      } else if (delta < 0) {
          nextImage.show();
          if(fadeSpeed > 0) {          
            currentImage.fadeOut(fadeSpeed);
          } else {
            currentImage.hide();
          }
      }                     
      
      if(delta != 0) {
          rootElement.find('div.text').each(function(index, element) {
            if(index === nextIndex) {            
                $(element).css('opacity', opacityHigh);         
            }
            else {          
                $(element).css('opacity', opacityLow);
            }           
          });        
      }
                  
      return this;      
    };
    
    this.startRotation = function () {                             
        rootElement.data('rotationTimer', setTimeout(function(){
            var i = (rootElement.data('currentIndex') + 1) % rootElement.children().size();
            app.changeImage(i, 1200);
            app.startRotation();            
        }, rootElement.data('rotationInterval')));                            
        return this; 
    };
    
    this.stopRotation = function () { 
        if(rootElement.data('rotationTimer') !== undefined)  {
            clearTimeout(rootElement.data('rotationTimer'));
        }
        return this; 
    };
    
    this.startReset = function () {
        if(rootElement.data('resetInterval') > 0) {
            rootElement.data('resetTimer', setTimeout(function(){
                app.changeImage(0, 1200);         
            }, rootElement.data('resetInterval')));    
        } else {
            app.changeImage(0, 1200);        
        }
        return this;
    };
    
    this.stopReset = function () {
        if(rootElement.data('resetTimer') !== undefined)  {
            clearTimeout(rootElement.data('resetTimer'));
        }
        return this;    
    };
    
    
    
    // Collega i gestori di evento del mouse in/out alle div.text
    $('ul.primoPiano div.text').hover(
      function (event) {
        $(this).parent().addClass('hover');
        if(rotate === true) {
            app.stopRotation();       
        } else {
            app.stopReset();
        }
        app.changeImage($(this).data('index'), false); 
      },
      function (event) {                          
        $(this).parent().removeClass('hover');
        if(rotate === true) {
            app.startRotation();
        } else {
            app.startReset();
        }
      }
    );
       
    /* Inizializza l'opacita' degli elementi */ 
    rootElement.find('div.text').each(function(index, element) {
      if(rotate && index > 0) {
        $(element).css('opacity', opacityLow);
      } else {
        $(element).css('opacity', opacityHigh);
      }       
    });    
        
    app.changeImage(0, false);    
        
    if(rotate === true) {
        app.startRotation();       
    }
  } ();
  /* END PrimoPiano */
  
  /* Sposta lo sfondo del primo elemento della lista */
  var altezzaTitolo = $('ul.primopiano.lista li.c0 .titolo').height() 
    + $('ul.primopiano.lista li.c0 .sottotitolo').height() + 1;
  $('ul.primopiano.lista li.c0[style]').css('background-position', 'center ' + altezzaTitolo + 'px');	
});

