$(function() {
	slider = {
		list:[],
		current:0,
		left:0,
		nextBtn:null,
		prevBtn:null,
		init: function(listContainer,licount) {
			var lastLeft=0;
			this.listContainer = listContainer;
			this.licount = licount;
			
			var slideTo=0;
			
			$(this.listContainer+' li').each(function(i,a) {
				slider.list[i] = {
					left: (lastLeft)
				};
				lastLeft += a.clientWidth;
				if ($(a).hasClass('active')) slideTo=i;
			});
			this.count = this.list.length;
			slider.showImg(Math.floor((slideTo)/(this.licount-1))*(this.licount-1),true)
			
			//			$('#dotline a').click( function () {
			//				slider.showImg(this.rel); 
			//				return false;
			//			});
			return this;
		},
		initNext: function(selector) {
			this.nextBtn = $(selector).click(function() {
				if (!$(this).hasClass('inactive')) slider.next();
				return false;
			});
				
			return this;
		},
		initPrev: function(selector) {
			this.prevBtn = $(selector).click(function() {
				if (!$(this).hasClass('inactive')) slider.prev();
				return false;
			});
			return this;
		},
		auto: function() {
			this.intv = setInterval("slider.next()",7000);
		},
		showImg: function (id,noanimate) {
			if (id in this.list) {
				this.current = id;
				//if (id > Math.round(this.licount / 2)) id = id - Math.round(this.licount / 2)+1; else id = 0;
				this.left=id;
				console.log(id);
				
				$('#dotline a').removeClass('y');
				$('#dotline a:eq('+id+')').addClass('y');
				
				if (!noanimate)
					$(this.listContainer+' ul').animate(
					{
						marginLeft: -this.list[id].left
					},500);
				else $(this.listContainer+' ul').css({
					marginLeft:-this.list[id].left
				});
				
			///$('.dotline a')
			}
					
		},
		updateBtn: function() {
			// reset
			$(this.nextBtn).removeClass('inactive');
			$(this.prevBtn).removeClass('inactive');
					
			if (this.current == 0)
				$(this.prevBtn).addClass('inactive');
			if (this.current == this.count-1)
				$(this.nextBtn).addClass('inactive');
		},
		next: function (btn) {
			if (this.left < this.count-this.licount) {
				this.left = (this.left + this.licount - 1 );
				//if (this.left > this.count-this.licount+1) this.left = this.count - this.licount;
			}
			
			this.showImg(this.left);
			this.updateBtn();
		},
		prev: function (btn) {
			if (this.left > 0) {
				this.left = (this.left - this.licount + 1 );
				if (this.left < 0) this.left = 0;
			}
			
			this.showImg(this.left);
			this.updateBtn();
		}
	}
		
	slider.init('.gallery-block',6).initPrev('.main-gallery a.prev').initNext('.main-gallery a.next');
});
