(function($){
	$.fn.slider = function(options) {
		var defaults = {
			ViewerWidth: '564px',
			Timer: 5000,
			SlideSpeed: 500,
			LeftControlClass: 'clickLeft',
			RightControlClass: 'clickRight',
			ViewerClass: 'viewer',
			FadeControls: true,
			TitleClass: 'title'
		};
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var obj = $(this);
			
			var viewer = obj.find('.'+options.ViewerClass);
			var dots = obj.find('li');
			var left = obj.find('.'+options.LeftControlClass);
			var right = obj.find('.'+options.RightControlClass);
			var title = obj.find('.'+options.TitleClass);
			var count = 1;
			var animation = false;
			
			Init();
			timersStart();
			hoverState();
			DotsInit();
			ArrowsInit();
			keyNavigation();
			
			function ArrowsInit() {
				left.click(function(){
					if (animation == false) {
						counter(-1);
						slideRight()
					}
				});
				right.click(function(){
					if (animation == false) {
						counter(1);
						slideLeft();
					}
				});
			}
			
			function Init() {
				var img = obj.find('li:nth-child('+count+')').children().attr('src');
				var imgtitle = obj.find('li:nth-child('+count+')').children().attr('title');
				title.html(imgtitle);
				viewer.html('<img src="'+img+'" class="current" />');
				setDot()
			}
			
			function counter(value) {
				count = (count + value);
				if (count > dots.size()) {
					count = 1;
				};
				if (count < 1) {
					count = dots.size();
				}
			}
			
			function setDot() {
				obj.find('li').removeClass('active');
				obj.find('li:nth-child('+count+')').addClass('active');
			}

			function slideLeft() {
				animation = true;
				var img = obj.find('li:nth-child('+count+')').children().attr('src');
				var imgtitle = obj.find('li:nth-child('+count+')').children().attr('title');
				viewer.append('<img src="'+img+'" class="inward" />');
				viewer.find('.inward').css({'left':options.ViewerWidth});
				$('.current').animate({
					'left':'-'+options.ViewerWidth
				},options.SlideSpeed);
				$('.inward').animate(
					{
						'left':'0px'
					},
					options.SlideSpeed, 
					function(){
						$('.current').remove();
						$('.inward').removeClass('inward').addClass('current');
						setDot();
						title.html(imgtitle);
						animation = false;
					}
				);
			}
			
			function slideRight() {
				animation = true;
				var img = obj.find('li:nth-child('+count+')').children().attr('src');
				var imgtitle = obj.find('li:nth-child('+count+')').children().attr('title');
				viewer.append('<img src="'+img+'" class="inward" />');
				viewer.find('.inward').css({'left':'-'+options.ViewerWidth});
				$('.current').animate({
					'left':options.ViewerWidth
				},options.SlideSpeed);
				$('.inward').animate(
					{
						'left':'0px'
					},
					options.SlideSpeed, 
					function(){
						$('.current').remove();
						$('.inward').removeClass('inward').addClass('current');
						setDot();
						title.html(imgtitle);
						animation = false;
					}
				);
			}
				
			function hoverState() {
				obj.hover(
					function(){
						timersStop();
						
					},
					function(){
						timersStart();
						
					}
				);
			}
			
			function showControls() {
				dots.stopTime().animate({'opacity':'1'}, 500)
				left.stopTime().animate({'opacity':'1'}, 500)
				right.stopTime().animate({'opacity':'1'}, 500)
			}
			
			function hideControls() {
				if (options.FadeControls == true ) {
					dots.stopTime().animate({'opacity':'0.75'}, 500).oneTime(options.Timer, function(){
						$(this).animate({'opacity':'0.5'}, 500)
					});
					left.stopTime().animate({'opacity':'0.5'}, 500).oneTime(options.Timer, function(){
						$(this).animate({'opacity':'0'}, 500)
					});
					right.stopTime().animate({'opacity':'0.5'}, 500).oneTime(options.Timer, function(){
						$(this).animate({'opacity':'0'}, 500)
					});
				}
			}
			
			function timersStart() {
				hideControls();				
				if (dots.size() > 1) {
					obj.everyTime(options.Timer, function(i) {
						counter(1);
						slideLeft();
					});
				}
			}
			
			function timersStop(){
				showControls();
				obj.stopTime();
			}
			
			function DotsInit() {
				dots.bind('click',function(){
					if (animation == false) {
						var num = ($(this).index()) + 1
						if (num > count) {
							count = num
							slideLeft()
						}
						if (num < count) {
							count = num
							slideRight()
						}
					}
				});
			}
			
			function keyNavigation() {
				$(window).keyup(function(event){
					if (animation == false) {
						var item = obj.find('.'+options.ActiveImageClass);
						if (event.keyCode == 37) { // Left Key
							// Move left
							timersStop();
							counter(-1);
							slideRight();
							
						} else if (event.keyCode == 39) { // Right Key
							// Move Right
							timersStop();
							counter(1);
							slideLeft();
						}
					}
				})
			}
		});
	};
})(jQuery);
