/**
 * Created by IntelliJ IDEA.
 * User: Bugi
 * Date: 13 вер 2010
 * Time: 13:16:06
 * To change this template use File | Settings | File Templates.
 */
var rotate_timer;
jQuery.fn.MainSlider = function()
{
	return this.each(function()
	{
    var conteiner = this;
    var li_width = $('li:first', conteiner).innerWidth();
    if($.browser.webkit)
    {
      // В хроме не может вытянуть ширину дива :(
      li_width = 540;
    }
    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'));

    rotate_slider(5000, conteiner);

    $('.slide-prev', conteiner).click(function()
    {
      ul_left = parseInt($('ul',conteiner).css('left'));
      if( !(ul_left >= 0) )
      {

        if(!isAnimate)
			  {
				  isAnimate = true;
				  $('ul', conteiner).animate({ left : ul_left + li_width   +'px' } , 1000, '', function()
          {
				  	isAnimate = false;
				  });
        }
      }
    });

    $('.slide-next', conteiner).click(function()
    {
      clearInterval(rotate_timer);
      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;
            });
          }
        }
      }
    });


	});
}
function rotate_slider(time_out, conteiner)
{

  rotate_timer = setTimeout(function()
  {
    $('.slide-next', conteiner).click();
    rotate_slider(time_out, conteiner);
  },time_out);
}
