$(document).ready(function() {

	init_slider_controls = function() {
		$('.g_next').one('click', function() {
			// disable slider controls
			$('.g_next, .g_prev').unbind('click').bind('click', function() { return false; });

			// get next image width
			var active_image_width = $('li:eq(1)', '#gallery').outerWidth(true);

			// animate
			$('ul', '#gallery').stop().animate({ left: '-=' + active_image_width }, 500, 'easeOutExpo', function() {
				// swap first image
				var current_offset = parseInt($('ul', '#gallery').css('left').slice(0, -2));
				$('li:last', '#gallery').after($('li:first', '#gallery'));
				$('ul', '#gallery').css({ left: (current_offset + $('li:last', '#gallery').outerWidth(true)) });

				// reactivate slider controls
				init_slider_controls();
			});
			return false;
		});

		$('.g_prev').one('click', function() {
			// disable slider controls
			$('.g_next, .g_prev').unbind('click').bind('click', function() { return false; });

			// swap last image
			var current_offset = parseInt($('ul', '#gallery').css('left').slice(0, -2));
			$('li:first', '#gallery').before($('li:last', '#gallery'));
			$('ul', '#gallery').css({ left: (current_offset - $('li:first', '#gallery').outerWidth(true)) });

			// get previous image width
			var active_image_width = $('li:eq(1)', '#gallery').outerWidth(true);
			
			// animate
			$('ul', '#gallery').stop().animate({ left: '+=' + active_image_width }, 500, 'easeOutExpo', function() {
				// reactivate slider controls
				init_slider_controls();
			});
			
			return false;
		});
	};

	init_slider = function() {
		var active_image_width = $('li:first', '#gallery').outerWidth(true);
		$('ul', '#gallery').stop().animate({ left: (358 - active_image_width) });

		init_slider_controls();
	};
	
	$(window).bind('load', function() {

	init_slider();
});
});

