var server = null;
var table = $("#scriptTab").DataTable();

$('.btn-setting').click(function(e) {
	e.preventDefault();
	$('#myModal').modal('show');
});
$("#mySecondModal #cancel").click(function() {
	$('#mySecondModal').modal('hide');
});
function startServer() {
	$.post("startRecordServer", {}, function(data) {
		if (data.success) {
			server = data.server;
			$('#myModal').modal('hide');
			$('#mySecondModal').modal('show');
			$('#scriptInfo2').text(
					$.i18n.prop("setProxy")+
						" IP:133.133.2.100"
								+ " ,port: " + server.port
								+ $.i18n.prop("recordScript"));
			//$('#startServer').attr('disabled', true);
			//$('#stopServer').attr('disabled', false);
		} else {
			$('#scriptInfo').html(
					$.i18n.prop("startServerFail") + data.failedMessage);
		}
	}, "json");
}
$('#uploadScript')
		.click(
				function() {
					var myModalContent = $('#myModal').html();
					$(".modal-footer").html("");
					var uploadForm = '<form method="post" enctype="multipart/form-data" action="uploadScriptFile">'
							+ '<p>script name:<input class="input-mini" type="text" name="scriptName" ></p>'
							+ '<p>script to upload: <input type="file" name="scriptFile"/></p>'
							+ '<p><input type="submit"/></p></form>';
					var status = "<div id='status'><h3></h3><div>";
					$('#scriptInfo').html("");
					$('#scriptInfo').html(uploadForm + status);

					$('form').ajaxForm({
						success : function(data) {
							if (data.success) {
								$("#status").text(data.message);
								loadScript(table, 2);
							}

							else
								$("#status").text(data.failedMessage);
						}
					});
					$('#myModal-close').click(function() {
						$('#myModal').html(myModalContent);
						$('#myModal').modal('hide');
					});
				});
function stopServer() {
	if (server != null) {
		$.post("stopRecordServer", {
			port : server.port,
			scriptRecordUUID : server.fileName
		}, function(data) {
			if (!data.success) {
				$('#scriptInfo3').text(
						"stop record server error:" + data.failedMessage);
				$('#stopServer').attr('disabled', true);
				$('#startServer').attr('disabled', false);
			}

			else {
				$('#stopServer').attr('disabled', false);
				$('#fileName').show();
				$('#scriptInfo3').text(
					$.i18n.prop("stopRecord")
						);
			}
		}, "json");
	} else {
		$('#scriptInfo3').text("record script server has not started");
	}

}

function saveScript() {
	var scriptName = document.getElementsByName("scriptname")[0].value;
	$.post("saveScriptRecorded", {
		scriptName : scriptName,
		port : server.port,
		scriptRecordUUID : server.fileName
	}, function(data) {
		if (data.success) {
			$("#scriptInfo3").text($.i18n.prop("saveScriptSuccess"));
			$('#fileName').hide();
			server = null;
		} else {
			$("#scriptInfo3").text($.i18n.prop("saveScriptFail") + data.failedMessage);
			$('#fileName').hide();
		}
		loadScript(table, 2);
	});
}
$(document).ready(function() {
	addEvent(table);
	loadScript(table);
	$('#stopServer').attr('disabled', true);

});

function refresh() {
	location.replace(location);
}

//add by hemeimei
function getScriptPlugins(scriptId) {

	$.post("getScriptPlugins", {
		scriptId : scriptId
	}, function(data) {
		if (!data.success) {
			information("failed to get script:" + data.failedMessage);
			return;
		} else {
			data.scriptPlugins;
		}

	}, "json")
}

function testRecordProxy() {
	$.post("testRecordProxy", {}, function(data) {
		if (data.success) {
			$('#mySecondModal').modal('hide');
			$('#myThirdModal').modal('show');
			//server = data.server;
			$('#scriptInfo3').text(
				$.i18n.prop("RecordingScript"));
			//$('#startServer').attr('disabled', true);
			//$('#stopServer').attr('disabled', true);
		} else {
			$('#scriptInfo2').html(
					$.i18n.prop("ProxySettingError") + data.failedMessage);
		}
	}, "json");
}

function startRecording() {
	if (server != null) {
		$.post("startRecording", {
			port : server.port,
			scriptRecordUUID : server.fileName
		}, function(data) {
			if (!data.success) {
				$('#scriptInfo3').text(
						"start recording error:" + data.failedMessage);
				$('#stopServer').attr('disabled', false);
				$('#startRecording').attr('disabled', true);
			}
			else {
				$('#scriptInfo3').text($.i18n.prop("stopRecord"));
				var url = $('#target-url').val();
				window.open(url);
				$('#stopServer').attr('disabled', false);
				$('#startRecording').attr('disabled', true);
			}
		}, "json");
	} else {
		$('#scriptInfo3').text("record script server has not started");
	}
}