(function($)
{
	var pages;
	
    var speed;
    var navigation;
    var next;
    var prev;
    var loop;
	var updateURL;
    
    var animated;
    var currentPage;
	
$.fn.pagefade = function(opt)
{
    var defaults = {
		transition: 'fade',
        speed: 300,
        loop: false,
		updateURL: false
    };
	
	pages = $(this).children();
    
	var options = $.extend(defaults, opt);
    speed = options.speed;
    navigation = options.navigation;
    next = options.next;
    prev = options.prev;
    loop = options.loop;
	updateURL = options.updateURL;
	
	
	$(pages).each(function(){
		$(this).css({position: "absolute", top: 0, left: 0});
	});
	
	if(next && prev){
		$(next).click(function(){
			nextPage();
			return false;
		});
		$(prev).click(function(){
			prevPage();
			return false;
		});
	}
	
	if(navigation){
		$(navigation).each(function(){
			$(this).click(function(){
				changePage(parseInt($(this).attr('href')));
				return false;
			})
		});
	}
	
	changePage(0);
};

function nextPage()
{
	if(currentPage + 1 > $(pages).length - 1){
		changePage(0);
	}
	else{
		changePage(currentPage + 1);
	}
}

function prevPage()
{
	if(currentPage - 1 < 0){
		changePage($(pages).length - 1);
	}
	else{
		changePage(currentPage - 1);
	}
}

function changePage(page)
{
	if(page != currentPage){
		$(pages).each(function(){
			$(this).fadeOut(speed);
		});
		$(pages[page]).fadeIn(speed);;
		
		$(navigation).each(function(){
			if($(this).attr('href') == page){
				$(this).parent().addClass('selected');
			}
			else{
				$(this).parent().removeClass('selected');
			}
		});
		
		currentPage = page;
		if(updateURL){
			window.location.hash = $(pages[currentPage]).attr('id');
		}
	}
}

})(jQuery);