function refGalLeft() {
	var big = $('.g_big');
	$(big).removeAttr("style");
	
	if ($(big).prev('div').length > 0) {
		var next = $(big).prev('div');
	}
	else {
		var next = $(".g_container").children(":last");
	}
		
	if ($(big).next('div').length > 0) {
		var last = $(big).next('div');
		if ($(last).next('div').length > 0) {
			var after_last = $(last).next('div');
		}
		else {
			var after_last = $(".g_container").children(":first");
		}
	}
	else {
		var last = $(".g_container").children(":first");
		var after_last = $(last).next('div');
	}	
	refGalRotate(big,next,last,after_last);
}

function refGalRight() {
	var big = $('.g_big');
	$(big).removeAttr("style");	
	
	if ($(big).next('div').length > 0) {
		var next = $(big).next('div');
	}
	else {
		var next = $(".g_container").children(":first");
	}	
	if ($(big).prev('div').length > 0) {
		var last = $(big).prev('div');
		if ($(last).prev('div').length > 0) {
			var after_last = $(last).prev('div');
		}
		else {
			var after_last = $(".g_container").children(":last");
		}
	}
	else {
		var last = $(".g_container").children(":last");
		var after_last = $(last).prev('div');
	}
	
	refGalRotate(big,next,last,after_last,true);
}

function refGalRotate(big,next,last,after_last,flip) {	
	$(next).removeAttr("style");
	$(last).removeAttr("style");
	$(after_last).removeAttr("style");
	var left_l = $(big).css('left');	
	var top_l = $(big).css('top');
	var width_l = $(big).css('width');	
	var height_l = $(big).css('height');		
	var left_s = $(next).css('left');
	var top_s = $(next).css('top');
	var width_s = $(next).css('width');
	var height_s = $(next).css('height');
	$(next).fadeOut(600);
	
	$(big).animate({
		left:left_s,
		top:top_s,
		height:height_s,
		width:width_s
	},600,'swing',function() {
		var c = (flip) ? 'g_right' : 'g_left';
		$(big).attr('class',c);
	});

	$(big).find('.g_item img').each(function() {
		$(this).removeAttr("style");
		$(this).animate({
			height:164,
			width:200
		},600);
	});
	
	$(last).css('z-index',4);
	$(last).animate({
		left:left_l,
		top:top_l,
		height:height_l,
		width:width_l
	},600,'swing',function() {
		$(last).attr('class','g_big');
	});
	
	$(last).find('.g_item img').each(function() {
		$(this).removeAttr("style");
		$(this).animate({
			height:250,
			width:350
		},600);
	});
	
	$(after_last).css('display','none');
	var c = (flip) ? 'g_left' : 'g_right';
	$(after_last).attr('class',c);
	$(after_last).fadeIn(800);
}

