jQuery(function($) {
	$.fn.simpleSlider = function() {
		var $slider = $(this),
			$imgs = $('.slider-images img', $slider),
			speed = 700,
			$pagination,
			visible_img_id,
			timer;
		
		function init() {
			visible_img_id = 0;
			$($imgs[visible_img_id]).show();
			generate_pagination();
			bind_pagination_handler();
			bind_img_mouse_handlers();
			start_timer();
		}
		
		function generate_pagination() {
			var links = [];
			for(var i = 0; i < $imgs.length; i++) {
				links.push('<a href="#" rel="' + i + '" class="small-corners">' + (i+1) + '</a>');
			}
			
			$pagination = $('<div id="slider-nav" />');
			$pagination.append(links.join(''));
			set_active_link(visible_img_id);
			$slider.append($pagination);
		}
		
		function bind_pagination_handler() {
			$pagination.children().click(function(e){
				var $this = $(this),
					curr_id = $this.index();
					
				if(curr_id == visible_img_id)
					return false;
				
				remove_timer();				
				slide_to_index(curr_id, true);
				start_timer();
				return false;
			});
		}
		
		function set_active_link(index) {
			$pagination.children()
						.eq(index).addClass('active')
						.siblings('a').removeClass('active');
		}
		
		function slide_to_index(index) {
			if(index == visible_img_id)
				return;
			
			$($imgs[visible_img_id]).fadeOut(speed);
			$($imgs[index]).fadeIn(speed);
			
			visible_img_id = index;
			set_active_link(visible_img_id);
		}
		
		function start_timer() {
			timer = setInterval(function(){
				var next_img_id = visible_img_id + 1;
				if(next_img_id == $imgs.length) {
					next_img_id = 0;
				}
				slide_to_index(next_img_id);
			}, 5000);
		}
		
		function remove_timer() {
			clearInterval(timer);
		}
		
		function bind_img_mouse_handlers() {
			$imgs.mouseenter(function(){
				remove_timer();
			});
			
			$imgs.mouseleave(function(){
				start_timer();
			});
		}
		
		init();
	}
});
