var ptv = {
	contents: {},
	addContents: function(name, body) {
		this.contents[name] = body;
	},
	datum: {},
	projects: null,
	addData: function(name, body) {
		if (this.projects == null) {
			this.projects = new Array();
		}
		this.datum[name] = body;
		this.projects.push(name);
	},
	versionChanged: function() {
		console.log("versionChanged");
		var version = document.getElementById("versions");
		ptv.load(version.options[version.selectedIndex].value);
		ptv.redraw();
	},
	info: {},
	testcaseChanged: function() {
		console.log("testcaseChagend");
		var testcases = document.getElementById("contents");
		var no = testcases.options[testcases.selectedIndex].text;
		var text = "tesecase: "+no+" , ";
		var version = ptv.info.version;
		for(var i=0; i<ptv.contents[version].body.length; i++) {
			if (no == ptv.contents[version].body[i].no) {
				text += ptv.contents[version].body[i].script + " |" +
						ptv.contents[version].body[i].type + " , " +
						ptv.contents[version].body[i].title + " |" +
						ptv.contents[version].body[i].api;
				ptv.graph.loadType(ptv.contents[version].body[i].graph);
				break;
			}
		}
		ptv.info.testcase = text;
		ptv.info.no = no;
		ptv.redraw();
	},
	projectChanged: function() {
		console.log("projectChagend");
		var projects = document.getElementById("projects");
		var text = "";
		var sep = "";
		for(var i=0; i<projects.options.length; i++) {
			if (projects.options[i].selected) {
				text += sep + projects.options[i].text;
				sep = ",";
			}
		}
		ptv.info.project = text;
		ptv.redraw();
	},
	redraw: function() {
		this.graph.clear();
		
		this.graph.push();
		
		this.graph.context().font = "14px 'Calibri'";
		this.graph.drawString("version: "+this.info.version, 20, 20);
		
		if (this.info.testcase != "") {
			var textArray = this.info.testcase.split("|");
			for(var i=0; i<textArray.length; i++) {
				this.graph.drawString(""+textArray[i], 20, 40+20*i);
			}
		}
		
		if (this.info.project != "") {
			this.graph.drawString("project: "+this.info.project, 20, 100);
		}
		
		if ((this.info.no != null) && (this.info.project != null)) {
			console.log("draw.dataload");
			var projects = this.info.project.split(",");
			console.log("this.info.project="+this.info.project+", n="+projects.length);
			var data = new Array();
			for(var i=0; i<projects.length; i++) {
				var project = projects[i];
				console.log("this.canvas.project="+project);
				for(var j=0; j<this.datum[project].results.length; j++) {
					if (this.info.no == this.datum[project].results[j].no) {
						data.push({project: projects[i], data: this.datum[project].results[j].data});
						break;
					}
				}
			}
			console.log("data="+data.length);
			this.graph.loadData(data);
		}
		
		this.graph.draw();
		
		this.graph.pop();

	},
	load: function(name) {
		var versions = document.getElementById("versions");
		var found = 0;
		for(var i=0; i<versions.options.length; i++) {
			var value = versions.options[i].value;
			if (name == value) {
				versions.selectedIndex = i;
				found = 1;
				break;
			}
		}
		
		if (found == 0) {
			return;
		}
		
		this.info.version = name;
	
		// test case
		var contents = document.getElementById("contents");
		// removes all list options.
		contents.options.length = 0;
		if (typeof(this.contents[name]) != "undefined") {
			for(var i=0; i<this.contents[name].body.length; i++) {
				var opt = document.createElement("option");
				opt.value = this.contents[name].body[i].no;
				opt.text  = this.contents[name].body[i].no;
				contents.appendChild(opt);
			}
		}
		this.info.testcase = "";
		
		// project
		var projects = document.getElementById("projects");
		// removes all list options.
		projects.options.length = 0;
		for(var i=0; i<this.projects.length; i++) {
			var project = this.projects[i];
			if (name == this.datum[project].contents) {
				var opt = document.createElement("option");
				opt.value = project;
				opt.text  = project;
				projects.appendChild(opt);
			}
		}
		this.info.project = "";
		
		this.redraw();
	},
	graph: null,
	init: function()
	{
		var canvas = document.getElementById("graph");
		var ctx = canvas.getContext("2d");
		this.graph = new ko2graph(ctx);
		this.graph.resize(640, 480);
		
		var versions = document.getElementById("versions");
		versions.addEventListener("change", this.versionChanged);
		
		var contents = document.getElementById("contents");
		contents.addEventListener("change", this.testcaseChanged);
		
		var projects = document.getElementById("projects");
		projects.addEventListener("change", this.projectChanged);
	}
};

function loadReports()
{
}


function loadSummary()
{
	$.ajax({
		type: "POST",
		url: "/cosvis",
		data: {cmd: "performance.get.folders" },
		success: function(data) {
			var d = JSON.parse(data);
			for(i=0; i<d.result.files.length; i++) {
			}
		}
	});
}

/*
function main()
{
	ptv.init();
	ptv.load("v3");
}

window.onload = main;


$.ajax({
	type: "POST",
	url: "/labo/page04/action?cmd=files",
	data: "",
	success: function(msg) {
		var d = JSON.parse(msg);
		for(var i=0; i<d.result.files.length; i++) {
			var script = document.createElement('script');
			script.src = d.result.files[i].name;
			document.body.appendChild(script);
		}
	}
});
*/

