function inputfocus(){
   if(cmdinput.style.display == "none") return;
   window.scrollTo(0,99999);
}

function move2top(){
	window.scrollTo(0,0);
}


function initLoad(){	

  cmdinput.focus();
  if( cookieValue("user") ){
    loadCmds();
  }
  setTimeout("showAdv(1);",1000*10);
  setInterval("showAdv(0);",1000*60);
  inputfocus();
}

function strtrim(str1){
  if(!str1) return str1;
  var str = str1;
  if(str.charAt(0) == " ")
       str = str.substring(1);
  if( str.charAt(str.length-1) == " ")
       str = str.substring(0,str.length-1);
		
  if(str == str1) return str;
  else{
	  return strtrim(str);
  }
}


function outprint(str){
	if(str){
	    var cont = document.createElement('span');
		cont.innerHTML = "<br>"+str;
		document.getElementById("output").appendChild(cont);
	}else{
		output.innerHTML += "<br>";
	}
	inputfocus();
}


function cmdkeydown(e,cmdinput){
   var key = e.which ? e.which : e.keyCode;
   if(key == 38){
     //up arrow
	 cmdindex--;
   }else if(key == 40){
     //down arrow
	 cmdindex++;
   }else if(key == 13){
	    //enter
	  runcmd(cmdinput.value,e);
	  cmdinput.value='';
      return false;
   }else{
      return true;
   }
   var result = "";
   if(currfunc ){
	  var holdname = currfunc.holdname||currfunc.name;
      var fcmds = cmdsMap[holdname];
	  if(fcmds){
		cmdindex = cmdindex%(fcmds.length+1);
	   if(!cmdindex){
		   cmdindex = 0;
		   result = "";
	   }else if(cmdindex < 0 ){
			var index1 = fcmds.length + cmdindex;
			result = fcmds[index1];
		}else if( cmdindex > 0 ){
			result = fcmds[cmdindex-1];
		}
		
	  }
	    
	  
   }else{
	   cmdindex = cmdindex%(cmds.length+1);
	   if(!cmdindex){
		   cmdindex = 0;
		   result = "";
	   }else if(cmdindex < 0 ){
			var index1 = cmds.length + cmdindex;
			result = cmds[index1];
		}else if( cmdindex > 0 ){
			result = cmds[cmdindex-1];
		}
          
   }
   
   cmdinput.value = result;
   inputfocus();
   return false;
}

function pushToHis(cmd,cmds1){
	if(!cmd) return;
    if(cmds1.length == 0) {
	   cmds1.push(cmd);
	   return;
	}
    if( cmds1[cmds1.length-1]!=cmd){
	    cmds1.push(cmd);
	}
	  
}

function pushToFuncHis(cmd,holdname){
	if(!cmdsMap[holdname]) cmdsMap[holdname] = [];
		   pushToHis(cmd,cmdsMap[holdname]);
}

function parseFuncObj(cmd){
	var command="";
	var param="";
	var func;
	var result = {};
   if(cmd){
      var ind = cmd.indexOf(" ");
      if(ind!=-1){
	     command = cmd.substring(0,ind).toLowerCase();
		 param = cmd.substring(ind+1);
		 func = functions[command]; 
		  if(func)
	    	result =  { func:func , param:strtrim(param) };
		  
	  }else{
	     command = cmd.toLowerCase();
		 func = functions[command]; 
		 if(func)
		 result = { func:func};
	  }
	  if(!result.func) {
	   func = qCmd("search");
	   result = { func:func, param:strtrim(cmd)};   
	  }
		
   }
   return result;
   
}


function runcmd(input1,e){
	input1 = strtrim(input1);
	bottom0.innerHTML = "";
	
	cmdindex = 0;
      
	   var showmsg = false;
	   if(!input1) {
	       input1 = "category";
		   showmsg = true;
	   }
       var funcObj = parseFuncObj(input1);
	   pushToHis(input1,cmds);
	   var currRun = funcObj.func;
	   var subcom = funcObj.param||"";
	   cmdout = "&gt; " + input1;

       outprint();
		if(cmdout){
		   outprint(cmdout);
		}
		 
		 out1 = currRun.run( subcom,e );
	
	if(out1){
	    outprint(out1);
	}
	
	if(currfunc )  
		cmdpath.innerHTML = "&gt;&nbsp;"+ (currfunc.holdname||currfunc.name) + "<br>&gt;&nbsp;";
	else
	    cmdpath.innerHTML = "&gt;&nbsp;";
     
	inputfocus();
}

function queryUrls($links,newwin){
   $result = "";
   $first = "";
   for(var prop in $links){
	     if(!$first)
		    $first = prop;
		 $result += "<a href=\""+$links[prop] +"\" "+(newwin?"target='_blank'":"target='cmdresult'") + ">"+prop+"</a> ";
	}
	outprint($result);
	return $first;
}

function queryFrame(obj,newwin){
	$first = queryUrls( obj,newwin) ;
	var link1 = obj[$first];
	openurl(link1,newwin);
	
}


function openurle($url,e){
    openurl($url,isNewWin(e));
	return false;
}

function openurl($url,newwin){
	if(newwin){
		window.open($url);
		return;
	}
	if(sbar && (isFF || isIE&&!isMT) ){
		window.open($url,"_main");
		return;
	}
	window.open($url,"cmdresult");
}

function isNewWin(e){
	if( (isIE||isOP) && e && ( e.shiftKey || e.ctrlKey) ){
		return true;
	}
	if( sbar && isFF && e && ( e.shiftKey || e.ctrlKey) ){
		return true;
	}
	return false;
}

function regCmds(){
	
	functions["define"] = qCmd("define");
	functions["def"] = qCmd("define");
	
	functions["category"] = qCmd("category");
	functions["cat"] = qCmd("category");
	
	functions["lucky"] = qCmd("lucky");
	
	functions["help"] = new HelpCmd();
	functions["?"] = new HelpCmd();
	
	//functions["save"] = new Query("save");
	//functions["delete"] = new Query("delete");
	//functions["del"] = new Query("delete");
	
	//functions["debug"] = new DebugCmd();
	
	functions["english"] = qCmd("english");
	functions["en"] = qCmd("english");
	
	functions["fr"] = qCmd("french");
	functions["french"] = qCmd("french");
	
	functions["jp"] = qCmd("japanese");
	functions["japanese"] = qCmd("japanese");
	
	functions["calc"] = new JsqCmd();
	functions["calculator"] = new JsqCmd();
	
	functions["clear"] = new ClearCmd();
	functions["cls"] = new ClearCmd();
	
	functions["favor"] = qCmd("favor");
	functions["bookmark"] = qCmd("favor");
	functions["bkmk"] = qCmd("favor");
	
	functions["find"] = qCmd("search");
	functions["web"] = qCmd("search");
	functions["search"] = qCmd("search");
	
	functions["pic"] = qCmd("pic");
	functions["picture"] = qCmd("pic");
	functions["img"] = qCmd("pic");
	functions["image"] = qCmd("pic");
	
	functions["wea"] = qCmd("wea");
	functions["weather"] = qCmd("wea");
	
	functions["news"] = qCmd("news");
	
	functions["music"] = qCmd("music");
	functions["audio"] = qCmd("music");
	functions["mp3"] = qCmd("music");
	
	functions["video"] = qCmd("video");
	
	functions["gp"] = qCmd("stock");
	functions["stock"] = qCmd("stock");
	
	functions["home"] = sCmd("home");
	
	functions["path"] = sCmd("path");
	
	functions["go"] = sCmd("tiny");

	functions["login"] = sCmd("login");
	functions["logout"] = sCmd("logout");

	
}

function qCmd(cmd){
   return new QueryCmd("cmds/"+cmd,cmd);
}

function sCmd(cmd){
   return new QueryCmd("cmdsys/"+cmd,cmd);
}


function QueryCmd(cmd,helpcmd){
	this.cmd = cmd;
	this.helpcmd = helpcmd;
	
	this.run = function(cmd) {
	  serviceCall(this.cmd,{q:cmd},function(result){
					queryFrame( json(result) );					   
										   });
    };
}
