var animation = false;
var intervalID;
var timeInterval = 4000;//время (в милисикундах) между перемоткой кадров
/*gallery code!!!*/
var currentGalIm = 0;
/*gallery code!!!*/

function start()
{
   $('.news_show div:first').css({ display: "inline" });  
   /*gallery code!!!*/
   $('.gal_show div:first').css({ display: "inline" });  
   /*gallery code!!!*/
}

function activateButtons()
{
   $(".up_arrow").click(function(){      
      scrollUp();
   });
   $(".up_arrow").mouseenter(function(){
      $(this).addClass('up_arrow_act');
   }).mouseleave(function(){
      $(this).removeClass('up_arrow_act');
   });
   
   $(".dn_arrow").click(function(){      
      scrollDown(); 
   });    
   $(".dn_arrow").mouseenter(function(){
      $(this).addClass('dn_arrow_act');
   }).mouseleave(function(){
      $(this).removeClass('dn_arrow_act');
   });
   
   /*gallery code!!!*/
   $(".lt_arrow").mouseenter(function(){
      $(this).addClass('lt_arrow_act');
   }).mouseleave(function(){
      $(this).removeClass('lt_arrow_act');
   });
   
   $(".lt_arrow").click(function(){      
      scrollLeft(); 
   });
   
   $(".rt_arrow").mouseenter(function(){
      $(this).addClass('rt_arrow_act');
   }).mouseleave(function(){
      $(this).removeClass('rt_arrow_act');
   });
   
   $(".rt_arrow").click(function(){      
      scrollRight(); 
   });

   $(".gal_show div").click(function(){
      idd = $(this).attr('id').slice(2);
      if (idd != $(".gal_show div").length)
      {
		++idd;
		scrollRight(); 		
		showPicture(idd);
      }
   });
   
  $('.floating div').click(function(){
	  //alert($(this).attr('id').slice(4));
	  showPicture($(this).attr('id').slice(4));
   });
   
   //floating_hor для каждого дива активация по клику
   $(".navg_wr .floating_hor div").click(function(){
      //alert();
      showPicture($(this).attr('id').slice(4));
   });
   /*gallery code!!!*/   

   $(".top_news .nav_wr").mouseenter(function(){ /*modified*/
      deactivateCarousel();  
   }).mouseleave(function(){
      activateCarousel();   
   });
}                  

/*gallery code!!!*/
function activateGallery()
{
   $(".navg_wr .floating_hor #prev1").css({ border: '2px solid #ee9e07' });   
   var ll = $(".navg .floating_hor div").length;
   $(".navg .floating_hor").width(90*ll);
}

function scrollRight()
{     
   if(currentGalIm < ($(".navg_wr .floating_hor div").length - 7)) 
   {
      ++currentGalIm;   
      $(".navg_wr .floating_hor").animate({ marginLeft: '-=82' }, function(){
         
      });
   }
}

function scrollLeft()
{     
   if(currentGalIm > 0) 
   {
      --currentGalIm;
      $(".navg_wr .floating_hor").animate({ marginLeft: '+=82' }, function(){
         
      });
   }
}

function showPicture(num)
{
   $('.gal_show div').css({ display: "none" });
   $('.gal_show #im' + num).fadeIn("slow");
   $(".navg_wr .floating_hor div").css({ border: '2px solid white' });
   $(".navg_wr .floating_hor #prev" + num).css({ border: '2px solid #ee9e07' });
   
   $('.news_show div').css({ display: "none" });
   $('.news_show #im' + num).fadeIn("normal");   
}
/*gallery code!!!*/

function activateCarousel()
{      
   intervalID = setInterval(scrollDown, timeInterval);
}

function deactivateCarousel()
{
   clearInterval(intervalID);
}

function scrollUp()
{
   if(!animation)
   {
      animation = true;
      $('.floating').css({ marginTop: '-88' });
      replaceLastDivToStart(".floating");
      replaceLastDivToStart('.news_show');
      $('.news_show div').css({ display: "none" });       
      $('.news_show div:first').fadeIn("slow");
      $('.floating').animate({ marginTop: '0' }, function(){
         animation = false;
      });
   }   
}

function scrollDown()
{
   if(!animation)   
   {  
      animation = true;
      replaceFirstDivToEnd('.news_show');    
      $('.news_show div').css({ display: "none" });
      $('.news_show div:first').fadeIn("slow");
      $('.floating').animate({ marginTop: '-88' }, 500, function(){  
         replaceFirstDivToEnd('.floating');                  
         $('.floating').css({ marginTop: '0' });      
         animation = false;
      });
   }   
}

function replaceLastDivToStart(name)
{      
   $(name).prepend(('<div id="'+ $(name + " div:last").attr('id') +'">'+$(name + " div:last").html()+'</div>'));
   $(name + " div:last").remove();
   
   $('.floating div').click(function(){      
      showPicture($(this).attr('id').slice(4));
   });
}

function replaceFirstDivToEnd(name)
{
   $(name).append(('<div id="'+ $(name + " div:first").attr('id') +'">'+$(name + " div:first").html()+'</div>'));
   $(name + " div:first").remove();
   
   $('.floating div').click(function(){      
      showPicture($(this).attr('id').slice(4));
   });
}
