var loadItems = new loadItems();

$(function() {
	var testPlanRunId = getvars()['testPlanId'];
	var testPlan = getTestInfo(testPlanRunId);
	$("#testplanName").html(testPlan.testPlanName);
	$("#createTime").html(new Date(testPlan.createDateTime));
	$("#testTime").html(testPlan.executeTime)
	if (!(testPlan.currentStatus == "InRunning" || testPlan.currentStatus == "Complete")) {
		return;
	}
	getTestBrief(testPlanRunId);

	// doProgress();
	var pageResult = new PageResult();
	pageResult.loadPageResult(testPlanRunId, testPlan.scripts);
	var behavior = new Behavior();
	behavior.loadBehaviorResults(testPlanRunId, testPlan.scripts);
	var tree = getResultTree(testPlanRunId);
	if (tree == null) {
		information($.i18n.prop('test-error'));
	}

	var chart = new Chart();
	chart.createChart($("#chart"));
	var chartSelector = createChartSelector(tree, chart,
			getShowTreePathValues(tree));
	chartSelector.initChart();
	graphButtonListener(chartSelector);

	$("#stop-testplan").click(function() {
		if ($("#status").attr("status") == "InRunning") {
			$.post("stop/" + testPlanRunId, {}, function(data) {
				if (data.success) {
					$('#status').attr("status", "Complete");
					var testStatus = testStatusMap("Complete");
					$('#status').html("<h4>" + testStatus + '</h4>');
				} else {
					information($.i18n("stopTestPlan_fail"));
				}

			})

		} else {
			return;
		}

	});
});
function getTestInfo(testPlanId) {
	var testPlan;
	$.ajax({
		type : "POST",
		url : testPlanId + "/runningInfo",
		dataType : "json",
		async : false,
		success : function(data) {
			if (!data.success) {
				information("no such testPlan!")
				return;
			} else {
				testPlan = data.testPlan;
			}
		}
	});
	return testPlan;
}

function graphButtonListener(chartSelector) {
	$("#del-graph").click(
			function(event) {

				$("#" + chartSelector.deleteSelectorId).html("");
				if (chartSelector.showTree != null) {
					if (chartSelector.showTree.root != null)
						chartSelector.createSelector($("#"
								+ chartSelector.deleteSelectorId),
								chartSelector.showTree.root);
				}

				chartSelector.addSelectlistEvent();
				$("#del-graph-modal").modal('show');
			});
	$("#add-graph").click(
			function(event) {
				$("#" + chartSelector.addSelectorId).html("");
				if (chartSelector.excessTree != null) {
					if (chartSelector.excessTree.root != null)
						chartSelector.createSelector($("#"
								+ chartSelector.addSelectorId),
								chartSelector.excessTree.root);
				}
				chartSelector.addSelectlistEvent();

				$("#add-graph-modal").modal('show');
			});
}

function createChartSelector(tree, chart, replaceArray, showTreePathValues) {

	var addSelectorId = "add-graph-list";
	var deleteSelectorId = "del-graph-list";
	var addButton = $("#add-graph-add");
	var deleteButton = $("#del-graph-del");
	var showTree = initShowTree(tree);
	var chartSelector = new ChartSelector(tree, showTree, chart, addSelectorId,
			deleteSelectorId, addButton, deleteButton, replaceArray,
			showTreePathValues);

	return chartSelector;
}
function getShowTreePathValues(tree) {
	var currentNode = tree.root.children[0];
	var pathValues = new Array();
	while (currentNode != null) {
		pathValues.push(currentNode.value);
		if (currentNode.children !== null)
			currentNode = currentNode.children[0];
		else
			currentNode = null;

	}
	return pathValues;
}
function initShowTree(tree) {

	var pathValues = getShowTreePathValues(tree);

	return tree.getCloneTreeFromVlaue(pathValues);
}
var ReplaceNode = function(oldWord, newWord) {
	this.oldWord = oldWord;
	this.newWord = newWord;
};

function getvars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(
			window.location.href.indexOf('?') + 1).split('&');
	for ( var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}
