
$(document).ready(function(){
    
    //DEVE ESSER LA PRIMA COSA CHE FA, ALTRIMENTI DA PROBLEMI CON IE8
    if ($('video').length) {

        $("video").VideoJS();

        myPlayer = $("video")[0].player;
        myPlayer.onEnded(function(){

            myPlayer.exitFullScreen();
            videoTerminato();

        });

    }
 
    
    if ( $('#scroller_elenco_video').length && $('#scroller_elenco_video ul.thumbnail_video').length ) {

        $('#scroller_elenco_video')
        .after('<div id="pagination_scroller_video">')
        .cycle({
            fx:         'scrollHorz',
            speed:      1000, 
            timeout:    15000,
            pause:      1,
            continuous: 0,
            easing:     'easeInOutExpo',
            next:       '#elenco_video_next_button', 
            prev:       '#elenco_video_prev_button',
            slideExpr:  'ul',
            pager:      '#pagination_scroller_video',
            cleartypeNoBg: true /* altrimenti il bg in IE non viene trasparente */
        });

    }
    
    if ( ($('ul.lista_categorie li').length>3) ) {          //if ( $('ul.lista_categorie').length ) {          
        
        var slider = $('ul.lista_categorie').bxSlider({
            auto: true,
            autoHover: true,
            mode: 'vertical',
            controls: false,
            displaySlideQty: 3,
            moveSlideQty: 1
        });
        
        $('#categorie_prev_button').click(function(){
            slider.goToPreviousSlide();
            return false;
        });
          
       $('#categorie_next_button').click(function(){
            slider.goToNextSlide();
            return false;
        });

        /*$('ul.lista_categorie')
        .cycle({
            fx:         'scrollVert',
            speed:      500, 
            timeout:    4000,
            pause:      1,
            continuous: 0,
            rev:        1,
            easing:     '',
            next:       '#categorie_next_button', 
            prev:       '#categorie_prev_button',
            slideExpr:  'li'
        });*/
        
    }
    
    
    var topVideoAnimationInProgress = false;
    
    $("ul#selettore_periodo_video_piuvisti a").click(function(){
      
      if (!topVideoAnimationInProgress) {
          
          topVideoAnimationInProgress = true;
          
          var currentData = $(this).attr("data");

          $("ul#selettore_periodo_video_piuvisti a").not($(this)).removeClass("current");
          
          $(this).addClass("current");

          $("ul.video_piuvisti:visible").slideUp(300,function(){

              $("ul.video_piuvisti." + currentData).slideDown(300,function(){
                  
                  topVideoAnimationInProgress = false;
                  
              });      

          }); 
      
      }
      
    });
    

   /*if ($('video').length) {
   
        $("video").VideoJS();

        myPlayer = $("video")[0].player;
        //myPlayer.play();
        myPlayer.onEnded(function(){

            myPlayer.exitFullScreen();
            videoTerminato();

        });
    
   }*/
   
   
    $("a.apri_mappa").click(function(){
      
        $("#popup_mappa").show();
        $('#iframe_mappa')[0].contentWindow.initialize();

        return false;
      
    });
    
    $("a.chiudi_mappa").click(function(){

       $("#popup_mappa").hide();

       return false;

    });
    
    
    $("a.apri_embed").click(function(){
      
        $("#popup_embed").show();

        return false;
      
    });
    
    $("a.chiudi_embed").click(function(){

       $("#popup_embed").hide();

       return false;

    });
   
    
   
   
   $("a.invia_ad_amico").click(function(){
      
      var indirizzo = $(this).attr("href");

      window.open(indirizzo,'InviaAdAmico','statusbar=0,width=460,height=420,top=10,left=10,location=no');

      return false;
      
   });
   
   
   $("a.tasto_cerca").click(function(){
       
       $form = $(this).parent().find("form");
       
       $form.toggle();
       
   })
    

});

function videoTerminato() {
    
    //alert("Fine!");
    
    $("#popup_termine_video").fadeIn("fast",function(){
       
       $("#video_player").css("visibility","hidden");
       
    });
    
}
