$().ready(function() {

  if($('.lightbox, .lightbox_inside').size() > 0) {

    $('.lightbox_inside').each(function() {
     $(this).lightBox({
      imageLoading:			main_host+'images/lightbox/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
      imageBtnPrev:			main_host+'images/lightbox/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
      imageBtnNext:			main_host+'images/lightbox/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
      imageBtnClose:		main_host+'images/lightbox/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
      imageBlank:				main_host+'images/lightbox/lightbox-blank.gif'			// (string) Path and the name of a blank image (one pixel)
      });
    });

    $('.lightbox').lightBox({
      imageLoading:			main_host+'images/lightbox/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
      imageBtnPrev:			main_host+'images/lightbox/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
      imageBtnNext:			main_host+'images/lightbox/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
      imageBtnClose:		main_host+'images/lightbox/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
      imageBlank:				main_host+'images/lightbox/lightbox-blank.gif'			// (string) Path and the name of a blank image (one pixel)
      });

  }

  $('.youtubevideo').bind('click', function() {
    url = $(this).attr('href');
    title = $(this).attr('title');
    code_pieces = url.split('=');
    code = code_pieces[1];
    showvideo(title, '&nbsp;');
    var params = { allowScriptAccess: "always" };
    var atts = { id: "youtubeplayer" };
    video_code = $('td:eq(1)', $(this).parent()).html();
    swfobject.embedSWF('http://www.youtube.com/v/'+code+'&enablejsapi=1&playerapiid=ytplayer',
                       "youtubeplayer", "425", "364", "8", null, null, params, atts);
    return false;
  });




  $container_immagini = $('.container_immagini');


  if($container_immagini.size() > 0) {

    $scroller = $('#scroller');
    thumb_height = $('.container_immagini #immagini img:first').height();
    thumb_width = $('.container_immagini #immagini img:first').width();
    var passo = thumb_width*2;
    numero_immagini = $('.container_immagini #immagini img').size();
    lunghezza_contenitore = ((5+thumb_width)*numero_immagini)-5;

    $container_immagini.css({'height':thumb_height+'px'});
    if(lunghezza_contenitore <= ($container_immagini.width()-66)) {
      scroller_width = $container_immagini.width()-66;
    } else {
      $('#prev_img, #next_img').css({'display':'block'});
      scroller_width = $container_immagini.width()-66;
      $('#immagini').css({'position':'relative', 'width':lunghezza_contenitore+'px'});
      $('#prev_img img').css({'position':'relative', 'top':((thumb_height/2)-8)+'px', 'display':'block', 'left':(($('#prev_img').width()/2)-12)+'px'});
      $('#next_img img').css({'position':'relative', 'top':((thumb_height/2)-8)+'px', 'display':'block', 'left':(($('#next_img').width()/2)-12)+'px'});
      $('#prev_img, #next_img').css({'cursor':'pointer', 'height': thumb_height+'px', 'width':'28px', 'border':'1px solid #fff'});

      $('#prev_img').bind('click', function() {
        posizione_attuale = parseInt($('#immagini').css('left').replace('px', ''));
        log(posizione_attuale);
        if(posizione_attuale == 0 || (-1*posizione_attuale) < passo) {
          new_pos = 0;
        } else {
          new_pos = posizione_attuale + passo;
        }
        $('#immagini').animate({
            left: new_pos+'px'
        }, 500, 'swing' );
      });

      $('#next_img').bind('click', function() {
        max_left = -1*(lunghezza_contenitore - scroller_width);
        posizione_attuale = parseInt($('#immagini').css('left').replace('px', ''));
        log('Posizione attuale :'+posizione_attuale);
        if(posizione_attuale == max_left || (posizione_attuale-passo) <= max_left) {
          new_pos = max_left;
        } else {
          new_pos = posizione_attuale - passo;
        }


        $('#immagini').animate({
            left: new_pos+'px'
        }, 500 , 'swing');

      });

    }

    $scroller.css({'position':'relative',
                   'width':scroller_width+'px',
                   'height':thumb_height,
                   'overflow':'hidden'});


  }

  $container = $('.container_docs');

  if($container.size() > 0) {

    $scroller = $('#scroller_docs');
    thumb_docs_height = $('.container_docs #docs img:first').height();
    thumb_docs_width = $('.container_docs #docs img:first').width();
    var passo_docs = thumb_docs_width*2;
    numero_immagini = $('.container_docs #docs img').size();
    lunghezza_contenitore_docs = ((5+thumb_docs_width)*numero_immagini)-5;

    $container.css({'height':thumb_docs_height+'px'});
    if(lunghezza_contenitore_docs <= ($container.width()-66)) {
      scroller_docs_width = $container.width()-66;
    } else {
      $('#prev_doc, #next_doc').css({'display':'block'});
      scroller_docs_width = $container.width()-66;
      $('#docs').css({'position':'relative', 'width':lunghezza_contenitore_docs+'px'});

      $('#prev_doc img').css({'position':'relative', 'top':((thumb_docs_height/2)-8)+'px', 'display':'block', 'left':(($('#prev_doc').width()/2)-12)+'px'});
      $('#next_doc img').css({'position':'relative', 'top':((thumb_docs_height/2)-8)+'px', 'display':'block', 'left':(($('#next_doc').width()/2)-12)+'px'});
      $('#prev_doc, #next_doc').css({'cursor':'pointer', 'height': thumb_docs_height+'px', 'width':'28px', 'border':'1px solid #fff'});

      $('#prev_doc').bind('click', function() {
        posizione_attuale = parseInt($('#docs').css('left').replace('px', ''));
        log(posizione_attuale);
        if(posizione_attuale == 0 || (-1*posizione_attuale) < passo_docs) {
          new_pos = 0;
        } else {
          new_pos = posizione_attuale + passo_docs;
        }
        $('#docs').animate({
            left: new_pos+'px'
        }, 500, 'swing' );
      });

      $('#next_doc').bind('click', function() {
        max_left = -1*(lunghezza_contenitore_docs - scroller_width);
        posizione_attuale = parseInt($('#docs').css('left').replace('px', ''));
        log('Posizione attuale :'+posizione_attuale);
        if(posizione_attuale == max_left || (posizione_attuale-passo) <= max_left) {
          new_pos = max_left;
        } else {
          new_pos = posizione_attuale - passo_docs;
        }
        $('#docs').animate({
            left: new_pos+'px'
        }, 500 , 'swing');
      });

    }

    $scroller.css({'position':'relative',
                   'width':scroller_docs_width+'px',
                   'height':thumb_docs_height,
                   'overflow':'hidden'});
  }

  $containerv = $('.container_video');

  if($containerv.size() > 0) {

    $scroller = $('#scroller_video');
    thumb_video_height = $('.container_video #videos img:first').height();
    thumb_video_width = $('.container_video #videos img:first').width();
    var passo_video = thumb_docs_width*2;
    numero_immagini = $('.container_video #videos img').size();
    lunghezza_contenitore_video = ((5+thumb_video_width)*numero_immagini)-5;

    $containerv.css({'height':thumb_video_height+'px'});
    if(lunghezza_contenitore_video <= ($containerv.width()-66)) {
      scroller_video_width = $containerv.width()-66;
    } else {
      $('#prev_doc, #next_doc').css({'display':'block'});
      scroller_video_width = $containerv.width()-66;
      $('#videos').css({'position':'relative', 'width':lunghezza_contenitore_video+'px'});


      $('#prev_video img').css({'position':'relative', 'top':((thumb_video_height/2)-8)+'px', 'display':'block', 'left':(($('#prev_video').width()/2)-12)+'px'});
      $('#next_video img').css({'position':'relative', 'top':((thumb_video_height/2)-8)+'px', 'display':'block', 'left':(($('#next_video').width()/2)-12)+'px'});
      $('#prev_video, #next_video').css({'cursor':'pointer', 'height': thumb_video_height+'px', 'width':'28px', 'border':'1px solid #fff'});

      $('#prev_video').bind('click', function() {
        posizione_attuale = parseInt($('#videos').css('left').replace('px', ''));
        log(posizione_attuale);
        if(posizione_attuale == 0 || (-1*posizione_attuale) < passo_video) {
          new_pos = 0;
        } else {
          new_pos = posizione_attuale + passo_video;
        }
        $('#videos').animate({
            left: new_pos+'px'
        }, 500, 'swing' );
      });

      $('#next_video').bind('click', function() {
        max_left = -1*(lunghezza_contenitore_video - scroller_video_width);
        posizione_attuale = parseInt($('#docs').css('left').replace('px', ''));
        log('Posizione attuale :'+posizione_attuale);
        if(posizione_attuale == max_left || (posizione_attuale-passo) <= max_left) {
          new_pos = max_left;
        } else {
          new_pos = posizione_attuale - passo_docs;
        }
        $('#videos').animate({
            left: new_pos+'px'
        }, 500 , 'swing');
      });

    }

    $scroller.css({'position':'relative',
                   'width':scroller_video_width+'px',
                   'height':thumb_video_height,
                   'overflow':'hidden'});


  }

});

function showvideo(title, contenuto) {
  $('<div id="youtubeplayer">&nbsp;</div>').dialog({
    title: title,
    modal : true,
    overlay: {
      backgroundColor: '#000',
      opacity: 0.5
    },
    width:'425px',
    height:'388px',
    close:function() {
      $(this).dialog('destroy').remove();
    }
  });

}

var ytplayer;
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("youtubeplayer");
  playVideo();
}
function playVideo() {
  ytplayer.playVideo();
}
