$(document).ready(function(){
	$('div.gallery').gallery({
		autoRotation: 5000,
		listOfSlides: '.gallery-holder > ul > li',
		switcher: '.swicher>li',
		autoRotation: 4000
	});
});
(function($) {
	$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
	
	function Gallery(context, options) { this.init(context, options); };
	
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				duration: 700,
				listOfSlides: 'ul > li',
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				switcher: false,
				autoRotation: false,
				event: 'click'
			}, options || {});
			this.context = $(context);
			this.els = this.context.find(this.options.listOfSlides);
			this.width = this.els.outerWidth();
			this.nextBtn = this.context.find(this.options.nextBtn);
			this.prevBtn = this.context.find(this.options.prevBtn);
			this.count = this.els.index(this.els.filter(':last'));
			this.move = true;
			
			this.active = 0;
			if (this.active == 0) this.prev = this.count;
			else this.prev = this.active-1;
			
			this.initEvent(this, this.nextBtn, this.prevBtn, true);
			this.initEvent(this, this.prevBtn, this.nextBtn, false);
			
			this.els.css({marginLeft:-this.width}).eq(this.active).addClass('active').css({marginLeft:0});
			
			if (this.options.switcher) {
				this.switcher = this.context.find(this.options.switcher);
				this.switcher.removeClass('active').eq(this.active).addClass('active');
				this.initEventSwitcher(this, this.switcher);
			}
			if (this.options.autoRotation) this.runTimer(this);
		},
		calcPrev: function(){
			if (this.active == 0) this.prev = this.count;
			else this.prev = this.active-1;
		},
		scrollElement: function($this,side){
			if(this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer($this);
			$this.move = false;
			if (side) {
				$this.els.removeClass('active').eq($this.active).addClass('active');
				this.els.eq(this.active).css({
					marginLeft:this.width
				}).animate({
					marginLeft:0
				},{
					queue:false,
					duration:$this.options.duration,
					complete:function(){
						$this.els.eq($this.prev).animate({
							marginLeft:-$this.width
						},{
							queue:false,
							duration:0,
							complete:function(){$this.move = true;}
						});
					}
				});
			} else {
				$this.els.removeClass('active').eq($this.active).addClass('active');
				this.els.eq(this.active).css({
					marginLeft: -this.width
				}).animate({
					marginLeft:0
				},{
					queue:false,
					duration:$this.options.duration,
					complete:function(){
						$this.els.eq($this.prev).animate({
							marginLeft:$this.width
						},{
							queue:false,
							duration:0,
							complete:function(){$this.move = true;}
						});
					}
				});
			}
		},
		initEvent: function($this, addEventEl, addDisClass, dir){
			addEventEl.bind($this.options.event, function(){
				if ($this.move) $this.toPrepare(dir);
				return false;
			});
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.prev = $this.active;
				$this.active = $this.switcher.index($(this));

				$this.switcher.removeClass('active').eq($this.active).addClass('active');

				if ($this.active > $this.prev) {
					$this.scrollElement($this,true);
				}
				if ($this.active < $this.prev) {
					$this.scrollElement($this,false);
				}
				return false;
			});
		},
		runTimer: function($this){
			if(this._t) clearTimeout(this._t);
			this._t = setInterval(function(){
				$this.toPrepare(true);
			}, this.options.autoRotation);
		},
		toPrepare: function(side){
			if (side) {
				if (this.active == this.count) this.active = 0;
				else this.active++;
				
				if (this.active == 0) this.prev = this.count;
				else this.prev = this.active-1;
			}
			else {
				if (this.active == 0) this.active = this.count;
				else this.active--;

				if (this.active == this.count) this.prev = 0;
				else this.prev = this.active+1;
			}
			this.scrollElement(this,side);
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
		}
	}
}(jQuery));
