function Behavior() {
	this.table = $("#url").DataTable();
	this.table.column(5).visible(false);
	this.getBehaviorResultsFirstly = function(testPlanId, map,scripts) {

		for(var i=0;i<scripts.length;i++){
			var url =  testPlanId + "/" + scripts[i].scriptId
			+ "/behaviors";
		
		this.getBehaviorResult(url, map, scripts[i].scriptName, this.table);
	}
		
	}

	this.loadBehaviorResults = function(testPlanId, scripts) {
		
		var map = new HashMap();
		this.getBehaviorResultsFirstly(testPlanId,map,scripts);
		var behavior = this;
		if (scripts == null) {
			return;
		}

		this.intervalId = setInterval(function() {

			for ( var i = 0; i < scripts.length; i++) {

				var url =  testPlanId + "/" + scripts[i].scriptId
						+ "/behaviors";
				behavior.getBehaviorResult(url, map, scripts[i].scriptName,
				behavior.table);
			}

		}, 10000);

	}
	this.getBehaviorResult = function(url, map, scriptName, table) {
		var behaviorResult = this;
		$.post(url, {}, function(data) {
			if (!data.success) {
				return;
			}
			data = data.behaviors;
			map.put(scriptName, data);
			behaviorResult.insertDataToTable(table, map);
			if ($("#status").attr("status") == "Complete") {
				if(behaviorResult.intervalId!=null){
					clearInterval(behaviorResult.intervalId);
				}
				
			}
		}, "json");

	}
	this.insertDataToTable = function(table, map) {

		table.clear();
		for ( var k = 0; k < map.length(); k++) {
			var names = map.getKeys();
			var behaviors = map.get(names[k]).behaviorBriefModels;
			if (behaviors == null) {
				return;
			}

			for ( var j = 0; j < behaviors.length; j++) {
				var data = behaviors[j];
				var rowNode = table.row.add(
						[ "", data.behaviorUrl, names[k], data.totalCount,
								data.successfulCount,
								data.detailStatusCodeResultModels ]).draw()
						.node();
				var td = $(rowNode).find("td")[0];
				$(td).addClass("details-control");
			}
			table.draw();
		}
		this.addEvent();
	}

	this.createDetailTable = function(url, detailDatas) {
		var theader=[ $.i18n.prop('result-url'), $.i18n.prop('result-url-statusCode'), $.i18n.prop('result-url-count'), $.i18n.prop('result-url-contentLength'), $.i18n.prop('result-url-minRes'), $.i18n.prop('result-url-maxRes'), $.i18n.prop('result-url-totalRes')];
		var table = $("<table></table>");
		table.addClass("innerTable");
		$(table).attr("style", "width: 100%;");
		var thead = $("<thead></thead>");

		var tr = $("<tr></tr>");
		for ( var i = 0; i < theader.length; i++) {
			var th = $("<th>" + theader[i] + "</th>");
			th.attr("style", "width:10% ;");
			tr.append(th);
		}
		thead.append(tr);
		table.append(thead);
		var tbody = $("<tbody></tbody>");
		table.append(tbody);
		for ( var i = 0; i < detailDatas.length; i++) {
			var tr = this.createDetailRow(detailDatas[i], url, theader)
			tbody.append(tr);

		}
		return table;
	}
	this.createDetailRow = function(detailData, url, theader) {

		var tr = $("<tr></tr>");
		var dataArray = [url,detailData.statusCode,detailData.count,detailData.contentLength,detailData.minResponseTime,detailData.maxResponseTime,detailData.totalResponseTimeThisTime];
		dataArray[0] = url;
		for ( var i = 0; i < dataArray.length; i++) {
			var td = $("<td></td>");
			td.attr("style", "width: 10%;");
			td.html(dataArray[i]);
			tr.append(td);
		}
		return tr;
	}

	this.addEvent = function() {
		var behavior = this;
		var tbody = this.table.table().body();
		$("tbody td.details-control").click(
				function() {
					var tr = $(this).closest('tr');
					var row = behavior.table.row(tr);
					if (row.child.isShown()) {
						var td = $(this).closest('td');
						td.removeClass('shown');
						row.child.hide();
					} else {
						var td = $(this).closest('td');
						td.addClass('shown');
						row.child(
								behavior.createDetailTable(row.data()[1], row
										.data()[5])).show();
					}

				});
	};

}
