var ChartSelector = function(allTree, showTree, chart, addSelectorId,
		deleteSelectorId, addButton, deleteButton, showTreePathValues) {
	this.allTree = allTree;
	this.showTree = showTree;
	this.addSelectorId = addSelectorId;
	this.deleteSelectorId = deleteSelectorId;
	this.excessTree = this.getExcessTree(allTree, showTreePathValues);
	this.addButton = addButton;
	this.deleteButton = deleteButton;
	this.createSelector($("#" + addSelectorId), this.excessTree.root);
	this.createSelector($("#" + deleteSelectorId), this.showTree.root);
	this.chart = chart;
	this.addEventListener(this);
};
ChartSelector.prototype.getExcessTree = function(allTree, pathValues) {
	var excessTree = allTree.getClonedTree(allTree);

	excessTree.deleteFromTree(pathValues);
	return excessTree;
};
ChartSelector.prototype.addEventListener = function(chartSelector) {
	this.addButton.click(function(e) {
		e.preventDefault();
		chartSelector.addGraph();
	});
	this.deleteButton.click(function(e) {
		e.preventDefault();
		chartSelector.deleteGraph();
	});

};
ChartSelector.prototype.updateSelector = function(selectorId, updatedData, tree) {
	var parentDomNode = $("#" + selectorId);
	this.deleteChildSelector(parentDomNode, updatedData);
	var newShowTree = tree.getClondSubTreeByValue(updatedData);
	if (newShowTree == null)
		return;
	this.createSelector(parentDomNode, newShowTree.root);
	this.addSelectlistEvent();
};
ChartSelector.prototype.deleteChildSelector = function(parentDomNode,
		updatedData) {
	var selectArray = $("#" + parentDomNode.attr("id") + " select");
	var divArray = $("#" + parentDomNode.attr("id") + " div");
	parentDomNode.html("");
	for ( var i = 0; i < selectArray.length; i++) {
		parentDomNode.append(divArray[i]);
		if ($(selectArray[i]).find("option:selected").val() == updatedData) {
			break;
		}

	}

};
;
ChartSelector.prototype.createSelectorByValues = function(parentDomNode,
		treeNode, pathValues) {
	var currentNode = treeNode;
	if (currentNode == null)
		return;
	var selectedNode;
	if (currentNode.children != null && currentNode.children.length > 0) {
		selectedNode = this.getSelectNodeByValue(currentNode, pathValues);
		if (selectedNode == null)
			selectedNode = currentNode.children[0];
		var selectOptions = new Array();
		for ( var i = 0; i < currentNode.children.length; i++) {
			selectOptions.push(currentNode.children[i]);
		}
		var childName = "result";
		parentDomNode
				.append(this.createSingleListDom(childName, selectOptions));
		this.createSelectorByValues(parentDomNode, selectedNode, pathValues);
	}

};
ChartSelector.prototype.addSelectlistEvent = function() {
	var chartSelector = this;
	$("#" + this.deleteSelectorId + " select").change(
			function(event) {
				var updatedData = $(this).find("option:selected").val();
				chartSelector.updateSelector(chartSelector.deleteSelectorId,
						updatedData, chartSelector.showTree);
				event.stopImmediatePropagation();
			});
	$("#" + this.addSelectorId + " select").change(
			function(event) {
				var updatedData = $(this).find("option:selected").val();
				chartSelector.updateSelector(chartSelector.addSelectorId,
						updatedData, chartSelector.excessTree);
				event.stopImmediatePropagation();
			});
};
ChartSelector.prototype.createSelector = function(parentDomNode, treeNode) {
	var currentNode = treeNode;
	if (currentNode == null)
		return;

	var selectedNode;
	if (currentNode.children != null && currentNode.children.length > 0) {
		selectedNode = currentNode.children[0];
		var selectOptions = new Array();
		for ( var i = 0; i < currentNode.children.length; i++) {
			selectOptions.push(currentNode.children[i]);
		}
		parentDomNode.append(this.createSingleListDom("result", selectOptions));
		this.createSelector(parentDomNode, selectedNode);
	}

};
ChartSelector.prototype.getSelectNodeByValue = function(parentNode, values) {
	for ( var i = 0; i < values.length; i++) {
		for ( var j = 0; j < parentNode.children.length; j++) {
			if (parentNode.children[j].value == values[i])
				return parentNode.children[j];
		}
	}
	return null;

};
ChartSelector.prototype.createSingleListDom = function(lableText, options) {
	var div = $("<div></div>");
	div.addClass("control-group");
//	var lable = $("<lable></lable>");
//	lable.addClass("control-label");
//	lable.text(lableText);
//	div.append(lable);
	div.append("</br>");
	var select = $("<select ></select>");
	select.addClass("form-control");
	for ( var i = 0; i < options.length; i++) {

		var option = $("<option></option>");
		option.text(options[i].name);
		option.val(options[i].value);
		if (i == 0) {
			option.attr("selected", true);
		}

		select.append(option);
	}

	div.append(select);
	return div;
};

ChartSelector.prototype.getSlectedVluesArray = function(selectorId) {
	var dataArray = new Array();
	var selectArray = $("#" + selectorId + " select");
	for ( var i = 0; i < selectArray.length; i++) {
		dataArray.push($(selectArray[i]).find("option:selected").val());
	}
	return dataArray;
};
ChartSelector.prototype.getSlectedNames = function(selectorId) {
	var dataArray = new Array();
	var selectArray = $("#" + selectorId + " select");
	for ( var i = 0; i < selectArray.length; i++) {
		dataArray.push($(selectArray[i]).find("option:selected").html());
	}
	return dataArray;
}

ChartSelector.prototype.getSelectedNames = function(selectorId) {
	var dataArray = new Array();
	var selectArray = $("#" + selectorId + " select");
	for ( var i = 0; i < selectArray.length; i++) {
		dataArray.push($(selectArray[i]).find("option:selected").text());
	}
	return dataArray;
};

ChartSelector.prototype.addGraphToTree = function(tree, selectorId) {
	var pathValues = this.getSlectedVluesArray(selectorId);
	var singlePathTree = this.allTree.getCloneTreeFromVlaue(pathValues);
	if (tree == null)
		tree = singlePathTree;
	else
		tree.insertToTree(tree.root, singlePathTree.root);
	return tree;
};
ChartSelector.prototype.deleteGraphFromTree = function(tree, selectorId) {
	if (tree == null)
		return null;
	var pathValues = this.getSlectedVluesArray(selectorId);
	tree.deleteFromTree(pathValues);
	return tree;
};

ChartSelector.prototype.addGraph = function() {
	this.showTree = this.addGraphToTree(this.showTree, this.addSelectorId);
	this.excessTree = this.deleteGraphFromTree(this.excessTree,
			this.addSelectorId);
	var model = new Model();
	model.setSeries(this.getSlectedVluesArray(this.addSelectorId));
	model.setUrl(this.getSlectedVluesArray(this.addSelectorId), this);
	model.setName(this.getSelectedNames(this.addSelectorId));
	model.axis = model.series + "_Axis";
	this.chart.addGraph(model);
};
ChartSelector.prototype.initChart = function() {
	var model = new Model();
	model.setSeries(this.getSlectedVluesArray(this.deleteSelectorId));
	model.setUrl(this.getSlectedVluesArray(this.deleteSelectorId), this);
	model.axis = (model.series + "_Axis");
	model.setName(this.getSelectedNames(this.deleteSelectorId));
	this.chart.addGraph(model);
};
ChartSelector.prototype.deleteGraph = function() {
	this.showTree = this.deleteGraphFromTree(this.showTree,
			this.deleteSelectorId);
	this.excessTree = this.addGraphToTree(this.excessTree,
			this.deleteSelectorId);
	var model = new Model();
	model.setSeries(this.getSlectedVluesArray(this.deleteSelectorId));
	model.axis = (model.series + "_Axis");
	model.setName(this.getSelectedNames(this.deleteSelectorId));
	this.chart.deleteGraph(model);
};
var Model = function() {
	this.url = "";
	this.series = "";
	this.data = null;
	this.intervalId = null;
	this.axis = "";
	this.time = 0;
};
Model.prototype.setName = function(pathValue) {
	this.name = pathValue[0];
	for ( var i = 1; i < pathValue.length; i++) {
		this.name += "_" + pathValue[i];
	}
}

Model.prototype.setUrl = function(pathValue, chartSelector) {
	/* this.url = "/"+chartSelector.allTree.root.value; */
	this.url = chartSelector.allTree.root.value;
	if (pathValue == null)
		return null;
	if (pathValue.length <= 0)
		return null;

	for ( var i = 0; i < pathValue.length; i++) {
		this.url += "/" + pathValue[i];
	}

};

Model.prototype.setSeries = function(pathValue) {
	this.series = "";
	if (pathValue == null)
		return null;
	if (pathValue.length <= 0)
		return null;
	this.series += pathValue[0];
	for ( var i = 1; i < pathValue.length; i++) {
		this.series += "_" + pathValue[i];
	}
};
Model.prototype.setIntervalId = function(intervalId) {
	this.intervalId = intervalId;
};
function getSubDataArray(indexData, dataArray) {
	var subDataArray = new Array();
	for ( var i = 0; i < dataArray.length; i++) {
		subDataArray.push(dataArray[i]);
		if (dataArray[i] == indexData) {
			break;
		}
	}
	return subDataArray;
}