$(function(){
	$('div.gallery').lightCarousel({switcher:'.swicher'});
});
	jQuery.fn.lightCarousel = function(o){
	var o = jQuery.extend({
		stay_time: 2000,
		change_speed: 1100,
		step: 0, // step list
		activeClass: 'active',
		stopClass: 'stop',
		btnLeft: '.link-prev',
		btnRight: '.link-next',
		btnPause: 'a.pause',
		galleryBody: '>div.slideshow',
		galleryList: '>div.slideshow > ul',
		galleryListItem: '> li',
		switcherBtnBefore:'<span>',
		switcherBtnAfter:'</span>',
		slideNum:'span.swicher ',
		switcher: false,
		autoSlide: false
	},o);
	this.each(function(){
		var core = {
			t: jQuery(this),
			stepW: 0,
			m: 0,
			cloneElW:0,
			countEl: 0,
			countStep: 0,
			moveCountStep: -1,
			oldMoveCountStep: -1,
			switcher: [],
			st: '',
			f1: true,
			fAutoSlide: true,
			fPause: true,
			f4: true,
			moveList: function (f){
				if(this.st) clearTimeout(this.st);
				if(this.f1){
					this.f1 = false;
					if (this.moveCountStep > -1) {
						this.m = this.cloneElW +(this.moveCountStep * this.stepW);
					} else {
						if(f) {
							if(this.m + this.holdW > this.w + this.cloneElW * 2 - this.holdW){
								this.m -= this.w;
								this.listHold.css('left', - this.m);
							}
							this.m += this.stepW;
						} else {
							if(this.m - this.holdW < 0){
								this.m += this.w;
								this.listHold.css('left', - this.m);
							}
							this.m -= this.stepW;
						}
					}
					
					if (o.switcher) {
						if (this.moveCountStep == -1) {
							this.moveCountStep = Math.ceil((this.m -this.cloneElW)/this.stepW)
							if (this.moveCountStep < 0) {
								this.moveCountStep = this.countStep -1;
							} else if (this.moveCountStep>=this.countStep) {
								this.moveCountStep = this.moveCountStep - this.countStep;
							}
						}
						
						for (var j = 0; j < this.switcher.length; j++){
							if (this.oldMoveCountStep > -1) this.switcher[j].eq(this.oldMoveCountStep).removeClass(o.activeClass);
							this.switcher[j].eq(this.moveCountStep).addClass(o.activeClass);
						}
						this.oldMoveCountStep = this.moveCountStep;
					}
					
					
					this.listHold.animate({left: - this.m}, o.change_speed, function(){
						core.f1 = true;
					});
				}
				if(this.fAutoSlide){
					this.st = setTimeout(function(){
						core.moveList(core.f4);
					}, o.stay_time + o.change_speed);
				}
				this.moveCountStep = -1;
			},
			addSwitcher: function (){
				if (o.switcher) {
					this.t.find(o.switcher).each(function(){
						if ((jQuery(this).find('> ul > li a').length ) != core.countStep) {
							var switcher = '<ul>';
							for (var i = 0; i < core.countStep; i++){
								switcher += '<li><a href="#">'+o.switcherBtnBefore+(i+1)+o.switcherBtnAfter + '</a></li>';
							}
							switcher += '</ul>'
							jQuery(this).html(switcher);
						}
						core.switcher.push(jQuery(this).find('> ul > li a'));
						core.switcher[core.switcher.length - 1].eq(0).addClass(o.activeClass);
						//core.oldMoveCountStep = core.moveCountStep = 0;
						core.oldMoveCountStep = 0;
					});
					core.addSwitcherEvent();
				}
			},
			addSwitcherEvent: function (){
				for (var i = 0; i < this.switcher.length; i++){
					this.switcher[i].each(function(i){
						jQuery(this).click(function(){
							core.moveCountStep = i;
							core.moveList(core.f4);
							return false;
						});
					});
				}
			}
		};
		core.init = function () {
			this.btnLeft = this.t.find(o.btnLeft);
			this.parentBtnLeft = this.btnLeft.parent();
			this.btnRight = this.t.find(o.btnRight);
			this.parentBtnRight = this.btnRight.parent();
			this.btnPause = this.t.find(o.btnPause);
			this.parentBtnPause = this.btnPause.parent();
			this.holder = this.t.find(o.galleryBody);
			this.listHold = this.t.find(o.galleryList);
			this.holdW = this.holder.width();
			this.elW = this.listHold.find(o.galleryListItem).outerWidth();
			this.list = this.listHold.children();
			this.listCount = this.list.length;
			this.w = this.listCount * this.list.outerWidth();
			if (o.step == 0) {
				this.stepW = this.holdW; 
			} else {
				this.stepW = this.elW * o.step;
				if (this.stepW > this.holdW) {
					this.stepW = this.holdW;
				}
			}
			if (o.switcher) this.countStep = Math.ceil(this.w / this.stepW);
			if (this.countStep) this.addSwitcher();
			if(this.w > this.holdW){
				this.countEl = 1;
				while (1) {
					if (this.countEl*this.elW > this.holdW) {
						break;
					}
					this.countEl++;
				}
				this.listHold.append(this.list.clone()).append(this.list.clone());
				this.list = this.listHold.find('> li');
				for (var i = 0; i < this.list.length; i++){
					if ((i < (this.listCount - this.countEl)) || (i > (this.list.length - (this.listCount - this.countEl)-1))) {
						this.list.eq(i).remove();
					}
				}
				this.cloneElW = this.m = this.countEl*this.elW;
				this.listHold.css('left', -this.m);
				if (o.autoSlide) {
					if (!this.parentBtnPause.hasClass(o.stopClass)) {
						if (this.f4) {
							if (!this.parentBtnLeft.hasClass(o.activeClass)) {
								this.parentBtnLeft.addClass(o.activeClass);
							}
						} else {
							if (!this.parentBtnRight.hasClass(o.activeClass)) {
								this.parentBtnRight.addClass(o.activeClass);
							}
						}
						this.st = setTimeout(function(){
							core.moveList(core.f4);
						}, o.stay_time);
					} else {
						this.fPause = this.fAutoSlide = false;
					}
				} else {
					this.fPause = this.fAutoSlide = false;
					this.btnPause.hide();
				}
				this.btnLeft.click(function(){
					core.f4 = true;
					core.moveList(core.f4);
					if (core.fAutoSlide) {
						if (!core.parentBtnLeft.hasClass(o.activeClass)) {
							core.parentBtnLeft.addClass(o.activeClass);
							core.parentBtnLeft.removeClass(o.activeClass);
						}
					}
					return false;
				});
				this.btnRight.click(function(){
					core.f4 = false;
					core.moveList(core.f4);
					if (core.fAutoSlide) {
						if (!core.parentBtnRight.hasClass(o.activeClass)) {
							core.parentBtnRight.addClass(o.activeClass);
							core.parentBtnRight.removeClass(o.activeClass);
						}
					}
					return false;
				});
				this.listHold.mouseenter(function(){
					if (core.fPause) {
						core.fAutoSlide = false;
						if (core.st) {
							clearTimeout(core.st);
							if (!core.parentBtnPause.hasClass(o.stopClass)) {
								core.parentBtnPause.addClass(o.stopClass);
							}
						}
					}
				}).mouseleave(function(){
					if (core.fPause) {
						core.fAutoSlide = true;
						if (core.st) clearTimeout(core.st);
						core.parentBtnPause.removeClass(o.stopClass)
						core.st = setTimeout(function(){
							core.moveList(core.f4);
						}, o.stay_time + o.change_speed);
					}
				});
				this.btnPause.click(function(){
					if (core.st) clearTimeout(core.st);
					core.fPause = false;
					if (core.parentBtnPause.hasClass(o.stopClass)) {
						core.parentBtnPause.removeClass(o.stopClass);
						core.fPause = core.fAutoSlide = true;
						if (core.f4) {
							if (!core.parentBtnLeft.hasClass(o.activeClass)) {
								core.parentBtnLeft.addClass(o.activeClass);
							}
						} else {
							if (!core.parentBtnRight.hasClass(o.activeClass)) {
								core.parentBtnRight.addClass(o.activeClass);
							}
						}
						core.moveList(core.f4);
					} else {
						core.parentBtnPause.addClass(o.stopClass);
						core.fAutoSlide = false;
						core.parentBtnLeft.removeClass(o.activeClass);
						core.parentBtnRight.removeClass(o.activeClass);
					}
					return false;
				});
			} else {
				this.btnLeft.hide();
				this.btnRight.hide();
				this.btnPause.hide();
			}
		}
		core.init();
	});
}
