var NO_DATA = "NO DATA";

var commandList = {
	
	A : function A(args) {
		alert("command: A");
		if( args != null ) {
			while( args.length  != 0 ) {
				alert("args:" + args.shift());
			}
		}
	},
	
	B : function A(args) {
		alert("command: B");
		if( args != null ) {
			while( args.length  != 0 ) {
				alert("args:" + args.shift());
			}
		}
	},
	/////////////////////////////////////////////////////////
	clear : function clear()
	{
		var CHILDREN_TO_PRESERVE = 0;
		while (_out.childNodes[CHILDREN_TO_PRESERVE])
		_out.removeChild(_out.childNodes[CHILDREN_TO_PRESERVE]);
	},
	
	print : function print(s) { println(s, "print"); },
	
	and : undefined,
	/////////////////////////////////////////////////////////
	help: function(args, input) {
		var helpStr = "help - display the command list\nreflexa - reflexa API\ntechs - technorati search API\ntechc - technorati cosmos API\nhotpepper - hotpepper API\ngmap - Google Maps API\noshirase - Oshira.se API\ngrep - grep xml using jQuery specific XPath\ntimeline - simile timeline\nxml2tree - display xml document with tree view\nma - morphological analysis by yahoo web service\nclear - clear display  ";
		printAnswer(helpStr);
	},
	/////////////////////////////////////////////////////////
	grep : function(args, input) {
		var ds;
		if( input instanceof DataSet) {
			var xpath = args[0];
			ds = input.grep( xpath )
		}
		return ds;
	},
	/////////////////////////////////////////////////////////
	oshirase : function(args, input) {
		var sourceName = "oshirase";
		var url = "cgi/oshirase/oshirase";
		var parameters = { "keyword": args[0] };
		
		return getDataSet(url,parameters,sourceName);
		
	},
	/////////////////////////////////////////////////////////
	reflexa : function(args, input) {
		var sourceName = "reflexa";
		var url = "cgi/refl/refl";
		var parameters = { "keyword": args[0] };
		
		return getDataSet(url,parameters,sourceName);
		
	},
	/////////////////////////////////////////////////////////
	techs : function(args, input) {
		var sourceName = "techs";
		var url = "cgi/tech/techs";
		var parameters = { "keyword": args[0] };
		
		return getDataSet(url,parameters,sourceName);
	},
	/////////////////////////////////////////////////////////
	techc : function(args, input) {
		var sourceName = "techc";
		var url = "cgi/tech/techc";
		var parameters = { "url": args[0] };
		
		return getDataSet(url,parameters,sourceName);
	},
	/////////////////////////////////////////////////////////
	hotpepper : function(args, input) {
		var sourceName = "hotpepper"
		var url = "cgi/hp/hp";
		var parameters = { "keyword": args[0] };
		
		return getDataSet(url,parameters,sourceName);
	},
	/////////////////////////////////////////////////////////
	gmap : function(args, dataSet) {
		var map = createMapDiv();
		
		var newSourceName = args[0];
		
		if(dataSet == "") {
			// dataSetがないときの処理
		} else {
			_out.appendChild(map);
			gmap(map.id, dataSet,newSourceName);
		}
		
		return NO_DATA;
	},
	/////////////////////////////////////////////////////////
	timeline: function(args, dataSet) {
		var tl = createTimelineDiv();
		
		var newSourceName = args[0];
		
		if(dataSet == "") {
			// dataSetがないときの処理
		} else {
			
			if(newSourceName == undefined) {
				sourceName = dataSet.getSourceName();
			} else {
				sourceName = newSourceName;
			}
			
			_out.appendChild(tl);
			timelineFactory.getTimeline(tl.id, dataSet, sourceName);
			
		}
		
		return NO_DATA;
	},
	/////////////////////////////////////////////////////////
	xml2tree : function(args, dataSet) {
		var tree = createTreeDiv();
		_out.appendChild(tree);
		
		var treeNode = treeNodeFromXml( dataSet.getData() );
		createTree(tree.id, treeNode);
		
		return NO_DATA;
	},
	/////////////////////////////////////////////////////////
	text: function(args, dataSet) {
		var text = "";
		if( dataSet != null ) {
			text = dataSet.getText();
		}
		printAnswer(text);
		
		return NO_DATA;
	},
	/////////////////////////////////////////////////////////
	ma: function(args, dataSet) {
		var sourceName = "ma";
		var url = "cgi/yahooMa/ma";
		var parameters = { "sentence": args[0] };
		
		return getDataSet(url,parameters,sourceName);
	}
	/////////////////////////////////////////////////////////
}

function getDataSet(url,parameters,sourceName) {
	var db = new DB(url,sourceName);
	var ds = db.load(parameters);
	return ds;
}

function createMapDiv() {
	
	// idはアルファベットスタート
	var id = "mapId_" + new Date().getTime();
	
	var newdiv = document.createElement("div");
	newdiv.id = id;
  newdiv.className = "map";
  newdiv.style.width = "700px";
  newdiv.style.height = "400px";
  
  return newdiv;
	
}

function createTreeDiv() {
	// idはアルファベットスタート
	var id = "treeId_" + new Date().getTime();
	
	var newdiv = document.createElement("div");
	newdiv.id = id;
  newdiv.className = "tree";
  
  return newdiv;
	
}

function createTimelineDiv() {
	
	var id = "timelineId_" + new Date().getTime();
	
	var newdiv = document.createElement("div");
	newdiv.id = id;
  newdiv.className = "timeline";
  newdiv.style.height = "250px";
  newdiv.style.width = "850px";
  
  newdiv.style.border ="1px solid #aaa";
  
  return newdiv;
	
	
	//<div id="my-timeline" style="height: 150px; border: 1px solid #aaa"></div>
	
}


