(function($){
	$.fn.preSlider = function(options){
	
	   var defaults = {
	   	elPerPage: 1,
	   	elWidth: 500,
	   	speed: 400,
	   	mode: 'fade',
	   	element: 'li',
	   	prevArrow: $('.prev'),
	   	nextArrow: $('.next')
	   };
	   
	   var options = $.extend(defaults, options); 
	   
	   return this.each(function() {  
	   	
	   		var obj = $(this);
	   		var elements = $(options.element, this);
	   		var ul = $("ul", this);
	 
	   		var elNumber = elements.length;
	   		var speed = options.speed;
	   		var pages = Math.ceil(elNumber / options.elPerPage);
			var currentPage = 0;
			
			var prev = options.prevArrow;
			var next = options.nextArrow;
			
			//images off
			if(options.mode == 'fade') 
			{
				elements.css({ opacity: 0 });
				elements.first().css({ opacity: 1 }).addClass('active');
			}
			
			//horizontal
			if(options.mode == 'horizontal') 
			{
				ul.css({ 'width' : 10000 });
				$("li", this).addClass('float-left');
			}
			
			//left arrow off
			prev.addClass('hidden');
			
			//right arrow off
			if(elNumber <= options.elPerPage)
				next.addClass('hidden');
				
	   		next.click(function(e) {
				e.preventDefault();
				
				if(currentPage < pages -1)
				{ 
					currentPage++;
					if(options.mode == 'fade') 
					{
						$('.active', obj).animate({ 'opacity': 0 }, speed, 'swing', function() {
							prev.removeClass('hidden');
							ul.css({ 'marginTop': -(options.elPerPage * options.elWidth * currentPage) });
						
							nextEl = $('.active', obj).parent().next().children().addClass('active');
							$('.active:first', obj).removeClass('active');
							
							nextEl.animate({ 'opacity': 1 }, speed, 'swing');
						});
					}
					else
					{
						prev.removeClass('hidden');
						if(options.mode == 'vertical')
							ul.animate({ 'marginTop': -(options.elPerPage * options.elWidth * currentPage) }, speed, 'swing');
						else
							ul.animate({ 'marginLeft': -(options.elPerPage * options.elWidth * currentPage) }, speed, 'swing');
							
					}
					if(currentPage == (pages-1))
						$(this).addClass('hidden');
				}
			});
			
			prev.click(function(e) {
				e.preventDefault();
				if(currentPage > 0)
				{
					currentPage--;
					if(options.mode == 'fade') 
					{
						$('.active', obj).animate({ 'opacity': 0 }, speed, 'swing', function() {
							next.removeClass('hidden');
							ul.css({ 'marginTop': -(options.elPerPage * options.elWidth * currentPage) });
							
							prevEl = $('.active', obj).parent().prev().children().addClass('active');
							$('.active:last', obj).removeClass('active');
							
							prevEl.animate({ 'opacity': 1 }, speed, 'swing');
						});	
					}
					else
					{
						next.removeClass('hidden');
						if(options.mode == 'vertical')
							ul.animate({ marginTop: -(options.elPerPage * options.elWidth * currentPage) }, speed, 'swing');
						else
							ul.animate({ marginLeft: -(options.elPerPage * options.elWidth * currentPage) }, speed, 'swing');
					}
					if(currentPage == 0)
						$(this).addClass('hidden');
				}
			});

	   });
	
	};
})(jQuery);

