(function($) {
	var interval;
	var min = 0;
	var current = 0;
	var max;
	var offset = 0;
	var elements = new Array;
	
	$.fn.slider = $.fn.Slider = function(options) {
		init = function(el) {
			$.each($('div.slider-image'), function(i,item) {
				elements[i] = $(item);
			});
			
			$('div#points').css({
				bottom: 10,
				left: '48%'
			});
			
			$('div#points a').first().addClass('active');
			
			max = elements.length - 1;
			
			$.slideTo();
			$.slide();
		};
		
		$.slide = function() {
			clearInterval(interval);
			interval = setInterval(function() { $.transition() }, 8000);
		};
		
		$.slideTo = function() {
			$('div#points a').click(function() {
				$('div#points a.active').removeClass('active');
				$(this).addClass('active');
				$.transitionTo($(this).attr('data-ref'));
			});
		};
		
		$.transition = function() {
			offset = $.get_next_offset();
			
			$('#slider-pane').animate({
				'left': offset
			}, 1000, function() {
				$.slide();
			});
		};
		
		$.transitionTo = function(position) {
			offset_width = parseInt($('div#slider').width());
			offset = '-'+(parseInt(position) * parseInt(offset_width));
			current = position;
			
			$('#slider-pane').animate({
				'left': offset
			}, 1000, function() {
				$.slide();
			});
		};
		
		$.get_next_offset = function() {
			offset_width = parseInt($('div#slider').width());
			if (current == max) {
				$('div#points a.active').removeClass('active');
				$('div#points a').first().addClass('active');
				current = 0;
				return '0';
			} else {
				current += 1;
				actual_offset = current * offset_width;
				$('div#points a.active').removeClass('active').next().addClass('active');
				return '-'+actual_offset;
			}
		};
		
		this.each (
			function() { init(this); }
		);
	};
})(jQuery);

