/**
 * User: Bugi
 * Date: 13 вер 2010
 * Time: 13:16:06
 */
var rotate_timer;
jQuery.fn.PhotoSlider = function()
{
	return this.each(function()
	{
    var conteiner = this;
    rotate_slider(5000, conteiner);
    
	});
}
function rotate_slider(time_out, conteiner)
{

  rotate_timer = setTimeout(function()
  {
    //$('.slide-next', conteiner).click();
    slide_next(conteiner);
    rotate_slider(time_out, conteiner);
  },time_out);
}
function slide_next(conteiner)
{
  var li_width = $('li:first', conteiner).innerWidth();
  var ul_width = $('li', conteiner).length * li_width;
  $('ul', conteiner).css('width', ul_width);
  var isAnimate = false;
  var ul_left = parseInt($('ul',conteiner).css('left'));

  ul_left = parseInt($('ul',conteiner).css('left'));
  if(!isAnimate)
  {
    if( (-ul_left + li_width) != ul_width )
    {
      isAnimate = true;
      $('ul', conteiner).animate({ left : ul_left - li_width   +'px' } , 1000, '', function()
      {
        isAnimate = false;
      });
    }
    else
    {
      var last_li = $('ul li:last', conteiner).clone();
      $('ul',conteiner).prepend(last_li);
      $('ul',conteiner).css('left', 0);
      ul_left = parseInt($('ul',conteiner).css('left'));
      if(!isAnimate)
      {
        isAnimate = true;
        $('ul', conteiner).animate({ left : ul_left - li_width   +'px' } , 1000, '', function()
        {
          last_li.remove();
          $('ul', conteiner).css('left', 0);
          isAnimate = false;
        });
      }
    }
  }
}
