var intervalTime = 5000;
var Chart = function() {
};
Chart.prototype.createChart = function(container) {
	container.highcharts({
		chart : {
			type : 'spline'
		},
		tooltip : {
			formatter : function() {
				return Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x)
						+ '<br/>' + '<b>' + this.series.name + ':'
						+ '</b><br/>' + Highcharts.numberFormat(this.y, 2);
			}
		},
	});
	var chart = container.highcharts();
	chart.setTitle({
		text : ""
	});
	chart.xAxis[0].update({
		type : 'datetime',
		tickInterval : 1000 * 60,
		tickWidth : 0,
		gridLineWidth : 1,
		tickPixelInterval : 50
	});
	this.highChart = chart;
	this.modelArray = new Array();
};

Chart.prototype.addAxis = function(axis, seriesId,name, data) {
	this.highChart.addAxis({ // Secondary yAxis
		id : axis,
		title : {
			text : name,
		},
		lineWidth : 2,
		opposite : true
	});
	this.addSeries(axis, seriesId, data,name);
};

Chart.prototype.addSeries = function(axis, seriesId, data,name) {
	this.highChart.addSeries({
		id : seriesId,
		name : name,
		yAxis : axis,
		data : data,
		marker : {
			enabled : true,
			radius : 3
		},
		showInLegend : true
	}, true);
};
Chart.prototype.removeAxis = function(axisId) {
	this.highChart.get(axisId).remove();
};

Chart.prototype.addPoint = function(seriesId, point) {
	if (point.y == "NaN")
		point.y = 0;
	this.highChart.get(seriesId).addPoint([ point.x, point.y ]);
};
Chart.prototype.addPoints = function(seriesId, data) {
	var chart = this;
	for ( var i = 0; i < data.length; i++) {
		chart.addPoint(seriesId, data[i]);
	}

};

var Point = function(x, y) {
	this.x = x;
	this.y = y;
};
var PointData=function(data,time){
	this.data=data;
	this.time=time;
}

Chart.prototype.getGraphDataFirstly=function(model){
	var chart=this;
	chart.addAxis(model.axis, model.series, model.name,[]);
  $.post(model.url + "/" + model.time, {}, function(data) {
       loadItems.scriptBriefStatusAve = true;
     if (data==null||!data.success) {
    return;
    }
    data = data.result;
   if (data instanceof Array) {
   for ( var i = 0; i < data.length; i++) {
	  chart.addDataToGraph(chart, model, data[i]);
    }

  } else{
 chart.addDataToGraph(chart, model, data);
 }

 }, "json");
	
}

Chart.prototype.addGraph = function(model) {
	var chart = this;
	this.getGraphDataFirstly(model,chart);
	var intervalId = setInterval(function() {
		$.post(model.url + "/" + model.time, {}, function(data) {
			loadItems.scriptBriefStatusAve = true;// this need to change
			if (data==null||!data.success) {
				clearInterval(intervalId);
				return;
			}
			data = data.result;
			loadItems.scriptBriefStatusAve=true;
			if (data instanceof Array) {
				var points = new Array();
				for ( var i = 0; i < data.length; i++) {
					chart.addDataToGraph(chart, model, data[i]);
				}
			
			} else{
				chart.addDataToGraph(chart, model, data);
				}

		}, "json");
		if($("#status").attr("status")=="Complete"){
			clearInterval(intervalId);
		}

	}, intervalTime);
	model.setIntervalId(intervalId);
	this.modelArray.push(model);

};
Chart.prototype.addDataToGraph = function(chart, model, data) {


	var point = new Point(data.time, data.data);
	if (data.time > model.time){
		model.time = data.time;
	}
	model.time++;
	chart.addPoint(model.series, point);
};
Chart.prototype.deleteGraph = function(model) {
	for ( var i = 0; i < this.modelArray.length; i++) {
		if (model.series == this.modelArray[i].series) {
			clearInterval(this.modelArray[i].intervalId);
			break;
		}
	}
	this.removeAxis(model.axis);
};
Chart.prototype.addModel = function(model) {
	this.modelArray.push(model);
};