
(function($){

$.fn.sweetPages = function(options){
	
	if(!options) options = {};
	
	/* Grab option*/
	var resultsPerPage 	= options.perPage || 4;	
	var maxWidth		= options.maxWidth || 400;
	var nextPage		= options.nextPage || 'Volgende';
	var prevPage		= options.prevPage || 'Vorige';
	var bottom_nav		= options.bottom_nav || true;
	var top_nav			= options.top_nav || false;
	
	
	/* Init */
	var ul 				= this;
	var li 				= ul.find('li');
	var current_page 	= 0;
	var maxHeight 		= 0;
	var totalWidth 		= 0;
	var total_pages		= 0;
	

	/* Style UL */
	// Setting the height of the ul to the height of the specified height:
	ul.width(maxWidth);

	
	li.each(function() {var el = $(this); el.data('height',el.outerHeight(true));});	/* Calculate the height of the li-elements */	
	var pagesNumber = Math.ceil(li.length / resultsPerPage);							/* Calculating the total number of pages */	
	if(pagesNumber < 2) return this;													/* no pagination needed */
	var swControls 	= $('<div class="swControls">');									/* initiate the control element */
	
	
	
	/* Split the pages into parts of the specified amount */	
	for(var i=0;i<pagesNumber;i++)
	{
		li.slice(i*resultsPerPage,(i+1)*resultsPerPage).wrapAll('<div class="swPage" />');
	}

		
	/* Add Extra Controls on top */
	if(top_nav)
	{
		var top_navigation = "<div class='swControls'><a href='' class='prev_button'>" + prevPage +"</a><a href='' class='next_button'>" + nextPage +"</a><div class='clear'></div></div><div class='clear'></div>";
			
		$('#main').prepend(top_navigation);
	}
	
	/* Add Extra Controls at bottom */
	if(bottom_nav)
	{
		swControls.append('<a href="" class="prev_button">' + prevPage +'</a>');
		swControls.append('<a href="" class="next_button">' + nextPage + '</a>');
		$('#main').append(swControls);
	}
	
	
	
	var swPage 			= ul.find('.swPage');
	swPage.each(function() {
		var elem 		= $(this);
		var tmpHeight 	= 0;
		
		elem.find('li').each(function(){tmpHeight+=$(this).data('height');});

		if(tmpHeight>maxHeight)
			maxHeight = tmpHeight;

		totalWidth+=elem.outerWidth();		
		elem.css('float','left').width(ul.width());
		
	});
	
	swPage.wrapAll('<div class="swSlider" />');
		
	var swSlider = ul.find('.swSlider');
	swSlider.append('<div class="clear" />').width(totalWidth);
	
	
	/* find buttons */
	var prev = $('#main').find('a.prev_button');
	var next = $('#main').find('a.next_button');
	
	prev.click(function(e){
		
		
		var new_left 		= 0;
		var current_left 	= $('div.swSlider').css('marginLeft');
		current_left 		= parseInt(current_left.replace('px',''));
		new_left 			= current_left + maxWidth;
				
				
		/* remove / attach 'active' */
		prev.addClass('active');
		next.removeClass('active');		
		
		
		if(new_left == maxWidth)
		{
			swSlider.stop();
		}
		else
		{
			current_page 		= current_page - 1;
			swSlider.stop().css({'margin-left':new_left}, 'fast');			
		}		
		
		e.preventDefault();
		
	});
	
	next.click(function(e)
	{		
		var max_width 		= -(pagesNumber * maxWidth);
		var current_left 	= $('div.swSlider').css('marginLeft');
		current_left 		= parseInt(current_left.replace('px',''));
		current_left		= current_left - maxWidth;
		
		/* remove / attach 'active' */
		next.addClass('active');
		prev.removeClass('active');

		if(current_left == max_width)
		{
			return false;
		}
		else
		{
			current_page	= current_page + 1;
			swSlider.stop().css({'margin-left':-(current_page * maxWidth)}, 'fast');
		}
		
		e.preventDefault();
		
	});

	prev.eq(0).addClass('active');
	
	return this;
	
}})(jQuery);
