function showReplayTab(game_id) {
	$('#replay-tab-container').children('.replay-edit-tab').removeClass('replay-selected-tab');
	$('#replay-tab-container').children('.replay-edit-tab').addClass('replay-unselected-tab');
	$('#replay-tab-' + game_id).addClass('replay-selected-tab');
	$('#main-replay-container').children('.replay-edit-area').hide();
	$('#replay-' + game_id).show();
}

function closeReplay(game_id, edit_more) {
	if($('#changed-' + game_id).val() == '1') {
		var go_ahead = confirm('There are unsaved changes, to save them, press "Cance" and use the "Save Replay" button.');
		if (!go_ahead) {
			return;
		}
	}
	$.ajax({
		type: "POST",
		url: "replay.php",
		data: {game_id: game_id, m: 'DoneEditingGame'},
		success: function (data, textStatus) {
			try {
				var info = JSON.parse(data);
			} catch (err) {
				alert('json parse error! ' + err);
				return;
			}
			if (!info.success) {
				alert('did not remove replay from session!');
				return;
			}
			$('#replay-' + game_id).remove();
			$('#replay-tab-' + game_id).remove();
			var kids = $('#replay-tab-container').children('.replay-edit-tab');
			if  (!kids.length) {
				if (edit_more) {
					window.location = '/replay.php?m=EditReplay';
				} else {
					window.location = '/replay.php?m=PostEditRedirectPage';
				}
				return;
			} else {
				// highlight the next replay in order.
				var id =$('#replay-tab-container').children('.replay-edit-tab:first').attr('id');
				var bits = id.split('-');
				var gid = bits[bits.length - 1];
				showReplayTab(gid);
			}
		},
		error: function (request, textStatus, errorThrown) {
			alert('error: ' + textStatus + ' ' + errorThrown);
		}
	});
}

function saveReplay(game_id) {
	var data = { game_id: game_id };
	var container = $('#replay-' + game_id);
	try {
		var section_id = $('#section_id-' + game_id).val();
		var title = $('#title-' + game_id).val();
		var map_id = $("#map_id-" + game_id).val();
		var event_id = $("#event_id-" + game_id).val();

		if (event_id && event_id > 0) {
			data.event_id = event_id;
		} else {
			data.event_id = '';
		}
		if (map_id) {
			data.map_id = map_id;
		}
		if (section_id != undefined) {
			data.section_id = section_id;
		}
		if (title) {
			data.title = title;
		}
		var recommended = $("#recommended-" + game_id).val();
		if (recommended && recommended > 0) {
			data.recommended = 1;
		} else {
			data.recommended = 0;
		}

	} catch (err) {
		// these won't be available if the user isn't a 
		// replay submitter
	}
	data.gateway_id = $("#gateway_id-" + game_id).val();
	var tags = $("#tags-" + game_id).val();
	if (tags != undefined) {
		data.tags = tags;
	}
	data.description = $("#description-" + game_id).val();
	data.m = "SaveReplay";
	$.ajax({
		type: "POST",
		url: "replay.php",
		data: data,
		success: function (data, textStatus) {
			try {
				var info = JSON.parse(data);
			} catch (err) {
				alert('json parse error! ' + err);
				return;
			}
			$('#changed-' + info.game_id).val(0);
			if (info.success) {
				var box = $('#replay-success-area-' + info.game_id);
				box.text('Changes saved!');
				box.fadeIn('slow');
				setTimeout(function () {
					box.fadeOut('slow');
				}, 5000);
			} else {
				alert(info);
			}
		},
		error: function (request, textStatus, errorThrown) {
			alert('error: ' + textStatus + ' ' + errorThrown);
		}
	});
}

function updateReplayProperty(property, data, hash) {
	// data = JSON.stringify(data);
	$.ajax({
		type: "POST",
		url: "replay.php?m=EditReplayProperty",
		data: { name: property, value: data, hash: hash },
		success: function (data, textStatus) {
			var info = JSON.parse(data);
			if (info.success) {
				var message = $('<div id="message-box" class="flora" title="Success!">You edited it!</div>');
				message.dialog({
					modal: true,
					autoOpen: true
				});
			} else {
				alert(info);
			}
		},
		error: function (request, textStatus, errorThrown) {
			alert('error: ' + textStatus + errorThrown);
		}
	});
}

