$(document).ready(function(){
    
    var scrolling = false;
    var scrollingNews = false;
    var timer;
    var current = $('#container-articleList div.item')[0];
    var currentNews = $('#container-news #news-category0 .newska')[0];

    $.timer(3000,function(){ if(!scrolling){listNext();}});
    $.timer(9000,function(){ if(!scrollingNews){listNextNews();}});

    function listNextNews()
    {
      offset = ($(currentNews).next().length != 0) ? $(currentNews).next()[0].offsetLeft : 0;
      scrollingNews = true;
      currentNews = ($(currentNews).next().length != 0) ? $(currentNews).next() : $(currentNews).parent().children()[0];
      $(currentNews).parents('#news-scroll').animate({
          left: -offset
          }, 1000, 'linear', function(){
          scrollingNews = false;
      });
    }

    function listNext()
    {
      offset = ($(current).next().length != 0) ? $(current).next()[0].offsetLeft : 0;
      scrolling = true;
      current = ($(current).next().length != 0) ? $(current).next() : $(current).parent().children()[0];
      act = $(current).children()[0];
      act = $(act).children()[0];
      act=$(act).attr('id');
      $('#container-articleList a[rel]').css('background-color','white');
      $('#container-articleList a[rel="'+act+'"]').css('background-color','#e6e6e6');
      $(current).parent().animate({
          left: -offset
          }, 1000, 'linear', function(){
          scrolling = false;
      });
    }


    $('.news-category').bind('mouseenter', function()
    {
      if(scrolling) $('#news-scroll').stop();
      scrollingNews = true;
    });

    $('#container-articleList p a').bind('mouseenter', function()
    {
      //if(!scrolling) {
      destination = $("#container-articleList img#"+this.rel).parent().parent();
      $('#container-articleList a[rel]').css('background-color','white');
      $(this).css('background-color','#e6e6e6');
      if(scrolling) $(destination).parent().stop();
      scrolling = true;
      offsetLeft = destination[0].offsetLeft;
      left = destination.parent()[0].offsetLeft;
      width = destination[0].width;
      current = destination;
      $(destination).parent().animate({
          left: -offsetLeft
      }, 100, 'linear', function(){
          scrolling = true;
      });
    });

    $('#container-articleList p a').bind('mouseleave', function()
    {
      scrolling = false;
    });

    $('.news-category').bind('mouseleave', function()
    {
      scrollingNews = false;
    });

    $('#container-articleList div.gallery div.galleryInner div.item').bind('mouseenter', function()
    {
	scrolling = true;	
    });
    
    $('#container-articleList div.gallery div.galleryInner div.item').bind('mouseleave', function()
    {
	scrolling = false;	
    });
    
});

