(function($){$(function(){

	var current = 0;
	var currentClass = "active";
	
	var TIMER = function(){
		this.timer;
		this.loopFunc;
		this.delay;
	}
	
	TIMER.prototype = {
		start:function(func, time){
			if(this.timer == null){
				this.loopFunc = func;
				this.delay = time;
				this.timer = setInterval(this.loopFunc, this.delay);
			}
		},
		stop:function(){
			clearInterval(this.timer);
			this.timer = null;
		},
		restart:function(){
			clearInterval(this.timer);
			this.timer = setInterval(this.loopFunc, this.delay);
		}
	}
	
	var mainPhoto = $("#slider1 li");
	var totalPhoto = mainPhoto.length;
	
	var thumbnails = $("#thumbnails a");
	
	var nextBtn = $("#nextBtn");
	var prevBtn = $("#prevBtn");
	
	var timer = new TIMER();
	
	
	mainPhoto.hide();
	
	mainPhoto.eq(current).addClass(currentClass);
	mainPhoto.eq(current).css("z-index",1);
	mainPhoto.eq(current).fadeIn("slow");
	
	thumbnails.eq(current).addClass("pager-active");
	
	
	timer.start(loop, 5000);
	
	
	prevBtn.hover(function(){
		var $img = $(this).children("img");
		$img.attr("src", $img.attr("src").replace(".png","_on.png"));
	},function(){
		var $img = $(this).children("img");
		$img.attr("src", $img.attr("src").replace("_on.png",".png"));
	});
	
	nextBtn.hover(function(){
		var $img = $(this).children("img");
		$img.attr("src", $img.attr("src").replace(".png","_on.png"));
	},function(){
		var $img = $(this).children("img");
		$img.attr("src", $img.attr("src").replace("_on.png",".png"));
	});
	
	nextBtn.click(function(){
		switchImage("next");
		timer.stop();
		return false;
	});
	
	prevBtn.click(function(){
		switchImage("prev");
		timer.stop();
		return false;
	});
	
	thumbnails.each(function(){
		$(this).click(function(){
			var num = thumbnails.index($(this));
			if(current != num){
				switchImage(num + 1);
				timer.stop();
			}
			return false;
		});
	});
	
	function hiddenOtherPhoto(){
		$("#slider1 li").each(function(){
			if(!$(this).hasClass(currentClass)) $(this).hide();
		});
	}
	
	function loop(){
		switchImage("loop");
	}
	
	
	function switchImage(foward){
		var duration = "fast";
		
		mainPhoto.eq(current).removeClass(currentClass);
		thumbnails.eq(current).removeClass("pager-active");
		mainPhoto.eq(current).css("z-index", 0);
		
		switch(foward){
			case "next":
				current = (++current) % totalPhoto;
				break;
			case "prev":
				current = ((current - 1) < 0)? totalPhoto - 1 : current -= 1;
				break;
			case "loop":
				current = (++current) % totalPhoto;
				duration = 1200;
				break;
			default:
				current = foward - 1;
				break;
		}
		/*
		if(parseInt(foward)){
			current = foward - 1;
		}
		else if(foward){
			current = (++current) % totalPhoto;
		}
		else{
			current = ((current - 1) < 0)? totalPhoto - 1 : current -= 1;
		}
		*/
		mainPhoto.eq(current).hide();
		mainPhoto.eq(current).addClass(currentClass);
		mainPhoto.eq(current).css("z-index",1);
		mainPhoto.eq(current).fadeIn(duration, hiddenOtherPhoto);
		thumbnails.eq(current).addClass("pager-active");
	}
	
})})(jQuery);
