function gallery_slide(up) {
	// Grab necessary data
	var ol = $('#gallery ol');
	var items = $('li',ol);
	var width = ol.width();
	var unit = width/items.length;
	var left = parseInt(ol.css('margin-left').replace('px',''));
	
	// Calculate target
	var target = round_nearest(up ? left - unit : left + unit, unit);
	
	// Jump to first or last item at the end of the line
	if (target < width * -1 + unit) {
		target = 0;
	} else if (target > 0) {
		target = width * -1 + unit;
	}
	
	// Go!
	ol.stop().animate({marginLeft: target + 'px'}, 750, "easeInOutQuart");
}

// http://www.talkincode.com/javascript-round-to-nearest-number-1101.html
function round_nearest(num, acc){
    if ( acc < 0 ) {
        num *= acc;
        num = Math.round(num);
        num /= acc;
        return num;
    } else {
        num /= acc;
        num = Math.round(num);
        num *= acc;
        return num;
    }
}

$(document).ready(function(){
	$('#gallery ul a').attr('href','#').click(function(){
		gallery_slide($(this).parent().hasClass('next'));
		return false;
	});
});