var Rotator=(function() {
   var init,play,stop,rotate,
       ii, delay=4000, aniTime=1000,
       oldPlayTitle
   
   init=function()
      {
      $('div#rotator ul li').css({opacity: 0.0})
      $('div#rotator ul li:first').css({opacity: 1.0})
      $('#btn_play').bind('click',function(){
         if(!ii) {
             play()
             $(this).css('backgroundImage','url(img/btn_stop.png)')
             }
         else {
             stop()
             $(this).css('backgroundImage','url(img/btn_play.png)')
             }
 
         })
      oldPlayTitle=$('#btn_play').get(0).title
      
      $('#btn_next').bind('click',function(){
         rotate(false)
         })
      $('#btn_prev').bind('click',function(){
         rotate(true)
         })
      }
   play=function()
      {
      Rotator.rotate(false)
      ii=setInterval('Rotator.rotate(false)',delay)
      $('#btn_play').get(0).title="остановить"
      
      }
   stop=function()
      {
      clearInterval(ii); ii=null
      $('#btn_play').get(0).title=oldPlayTitle
      }
      
   rotate=function(prev){	
   	var prev, cnext
   	var current = ($('div#rotator ul li.show') ?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));
   	cnext=prev ? current.prev() : current.next()
   	var next = ((cnext.length) ? ((cnext.hasClass('show')) ? $('div#rotator ul li:'+(prev ? 'last' : 'first')) :cnext) : $('div#rotator ul li:'+(prev ? 'last' : 'first')));	
    
   	// Расскомментируйте, чтобы показвать картинки в случайном порядке
   	// var sibs = current.siblings();
   	// var rndNum = Math.floor(Math.random() * sibs.length );
   	// var next = $( sibs[ rndNum ] );
    
   	next.css({opacity: 0.0})
   	.addClass('show')
   	.animate({opacity: 1.0}, aniTime);
    
   	current.animate({opacity: 0.0}, aniTime)
   	.removeClass('show');
   };
   
   return({
   init : init,
   play : play,
   rotate : rotate
   });
} ());



 
$(document).ready(function() {		
	// Запускаем слайдшоу
	Rotator.init()
});
