$(document).ready(
  function()
  {
    if ($('#newsreader-home').length > 0) {

      var imgs = new Array();
      $('.newsreader-img img', '#newsreader-home').each(
        function()
        {
          imgs[imgs.length] = $(this).attr('src');
          imgs[imgs.length] = $(this).parent().attr('rel');
        }
      );
      var imgslen = imgs.length;

      function loadImg(i) {
      	$.preload([imgs[i]],
        {
      		onFinish: function()
          {
            setTimeout(
              function()
              {
                if (i == imgslen - 1) startAll();
                else loadImg(i + 1);
              }, 30
            );
      		}
      	});
      }

      if (imgs.length > 0) loadImg(0);
      else startAll();
      function startAll()
      {
        $('.newsreader-category', '#newsreader-home').each(
          function()
          {
            $(this).show().clone().insertBefore($(this)).empty().css({
              'opacity': 0.5,
              'width': $(this).width(),
              'height': $(this).height()
            });

            $(this).css('background-color', 'transparent');
          }
        );

        var loop;
        var li = 0;
        var liw = $('li', '#newsreader-home-bottom').eq(0).outerWidth(true);
        var lin = $('li', '#newsreader-home-bottom').length;
        $('ul', '#newsreader-home-bottom').css('width', liw * lin);
        $('#newsreader-home-bottom').scrollLeft(0);
        $('li', '#newsreader-home-top').hover(
          function()
          {
            if (!$(this).hasClass('active')) $(this).addClass('hover');
            $('li', '#newsreader-home-thumbnails').eq($('li', '#newsreader-home-top').index($(this))).show();
          },
          function()
          {
            if (!$(this).hasClass('active')) $(this).removeClass('hover');
            $('li', '#newsreader-home-thumbnails').eq($('li', '#newsreader-home-top').index($(this))).hide();
          }
        ).click(
          function()
          {
            li = $('li', '#newsreader-home-top').index($(this));
            changeImg(li);
            stopPlay();
          }
        );
/*
        $('#newsreader-home-loading').before(
            '<ul id="newsreader-home-controls">'
//           + '<li id="newsreader-home-controls-prev">Precedente</li>'
//           + '<li id="newsreader-home-controls-next">Successiva</li>'
          + '<li id="newsreader-home-controls-play" style="display: none;">Play</li>'
          + '<li id="newsreader-home-controls-pause">Pause</li>'
          + '</ul>'
        );
*/
        $('li', '#newsreader-home-controls').hover(
          function()
          {
            $(this).addClass('hover');
          },
          function()
          {
            $(this).removeClass('hover');
          }
        );

        function startPlay()
        {
          loop = setInterval(
            function()
            {
              li = (li < lin - 1) ? li + 1 : 0;
              changeImg(li);
            }, 4000
          );

          $('#newsreader-home-controls-play').fadeOut(200,
            function()
            {
              $('#newsreader-home-controls-pause').fadeIn(200);
            }
          );
        }

        function stopPlay()
        {
          clearInterval(loop);
          $('#newsreader-home-controls-pause').fadeOut(200,
            function()
            {
              $('#newsreader-home-controls-play').fadeIn(200);
            }
          );
        }

//         $('#newsreader-home-controls-prev').click(
//           function()
//           {
//             stopPlay();
//             li = (li > 0) ? li - 1 : lin - 1;
//             $('li', '#newsreader-home-top').eq(li).click();
//           }
//         );

//         $('#newsreader-home-controls-next').click(
//           function()
//           {
//             stopPlay();
//             li = (li < lin - 1) ? li + 1 : 0;
//             $('li', '#newsreader-home-top').eq(li).click();
//           }
//         );

        $('#newsreader-home-controls-play').click(
          function()
          {
            li = (li < lin - 1) ? li + 1 : 0;
            $('li', '#newsreader-home-top').eq(li).click();
            startPlay();
          }
        );

        $('#newsreader-home-controls-pause').click(stopPlay);
        function changeImg(n)
        {
          $('li', '#newsreader-home-top').eq(n).addClass('active').removeClass('hover');
          $('li:not(:eq(' + n + '))', '#newsreader-home-top').removeClass('active');
          $('#newsreader-home-bottom').animate({'scrollLeft': liw * n}, 800);
        }

//         $('.newsreader-category', '#newsreader-home').each(
//           function(i)
//           {
//             $(this).before('<div class="newsreader-category-wrapper"></div>');
//             $('.newsreader-category-wrapper', '#newsreader-home').eq(i).css({
//               'opacity': 0.3,
//               'width': $(this).outerWidth(),
//               'height': $(this).outerHeight()
//             });
//           }
//         );

//        $('#newsreader-home-loading').before('<div class="newsreader-arrows-wrapper newsreader-next-car-wrapper"><span></span></div>');
//        $('#newsreader-home-loading').before('<div class="newsreader-arrows-wrapper newsreader-prev-car-wrapper"><span></span></div>');
//        $('#newsreader-home-loading').before('<div class="newsreader-arrows newsreader-next-car" title="Successivo"><span></span></div>');
//        $('#newsreader-home-loading').before('<div class="newsreader-arrows newsreader-prev-car" title="Precedente"><span></span></div>');
        $('.newsreader-arrows-wrapper', '#newsreader-home').css('opacity', 0.1);
        $('.newsreader-prev-car', '#newsreader-home').hover(
          function()
          {
            $(this).addClass('newsreader-prev-car-hover');
          },
          function()
          {
            $(this).removeClass('newsreader-prev-car-hover');
          }
        ).click(
          function()
          {
            stopPlay();
            li = (li > 0) ? li - 1 : lin - 1;
            $('li', '#newsreader-home-top').eq(li).click();
          }
        );

        $('.newsreader-next-car', '#newsreader-home').hover(
          function()
          {
            $(this).addClass('newsreader-next-car-hover');
          },
          function()
          {
            $(this).removeClass('newsreader-next-car-hover');
          }
        ).click(
          function()
          {
            stopPlay();
            li = (li < lin - 1) ? li + 1 : 0;
            $('li', '#newsreader-home-top').eq(li).click();
          }
        );

        $('#newsreader-home-loading').before('<ul id="newsreader-home-thumbnails"></ul>');
        $('.newsreader-img a', '#newsreader-home').each(
          function(i)
          {
            var left = $('li', '#newsreader-home-top').eq(i).position().left;
            var title = $(this).attr('title');
            $('#newsreader-home-thumbnails').append('<li style="left: ' + left + 'px"><img src="' + $(this).attr('rel') + '" alt="' + title + '" title="' + title + '" /></li>');
          }
        );

        $('#newsreader-home-loading').fadeOut(500, startPlay);
      }
    }
  }
);
