// command.js
// requires: prototype.js
// parses and distributes command line input


		
var Command = Class.create();

Command.prototype = {

	initialize:function() {

		this.aliases = {
			who: 			 {f:this.who},
			profile: 		 {f:this.profile},
			toons: 			 {f:this.toons},
			user: 			 {f:this.user},
			home: 			 {f:this.home},
			loot: 			 {f:this.loot},
			catsinsinks: 	 {f:this.catsinsinks},
			sink: 			 {f:this.sink},
			lines: 			 {f:this.lines},
			font: 			 {f:this.font},
			spacing: 		 {f:this.spacing},
			weapons:		 {f:this.lootAnalysis,a:{componentID:5}},
			armors:			 {f:this.lootAnalysis,a:{componentID:1}},
			boosters:		 {f:this.lootAnalysis,a:{componentID:4}},
			capacitors:		 {f:this.lootAnalysis,a:{componentID:7}},
			droid_interface: {f:this.lootAnalysis,a:{componentID:9}},
			reactors:		 {f:this.lootAnalysis,a:{componentID:8}},
			shields:		 {f:this.lootAnalysis,a:{componentID:2}},
			engines:		 {f:this.lootAnalysis,a:{componentID:3}},
			help:			 {f:this.help},
			link:			 {f:this.link},
			getLink:		 {f:this.getLink},
			posess:			 {f:this.posess},
			recalculateCAS:	 {f:this.recalculateCAS},
			recalculateLAS:	 {f:this.recalculateLAS},
			alerts:			 {f:this.alerts},
			recalcRewards:	 {f:this.recalcRewards}
		};
		
		this.inputElement = $('messageInput');
		return this;
	},
	
	lookup:function(s) {
		var ls=function(a,b) { return(a.toLowerCase()>b.toLowerCase?1:-1);}
		var a=$H(this.aliases).keys().sort(ls);
		var r=new RegExp("^" + s,"i");
		return a.grep(r)[0];
	},
	

	onKeyPress:function(e) {
		var k = (e.charCode) ? e.charCode : ((e.which) ? e.which : e.keyCode);
		if(k==13) { this.parse(); }
	},
	
	parse:function(c) {
		// if no command submitted, pull it from the command line
		if(c==undefined)  {	var c = $F(this.inputElement);}
			
		// if it's not a slash command, hand off to chat.send
		if(c.substr(0,1) !== "/") {	
			this.initCommandLine();			
			chat.send(c); 
			return;
		}	
		
		// if it is a slash command...	
		c=c.replace(/_/," ");
		c=c.split(/ /);	
		var fS=this.lookup(c[0].replace(/\//,''));
		if(fS==undefined) {
			if(global.user.username.length) {
				chat.echo({message:"You're confusing my poor, outdated droid brain with your unknown command requests.  What does " + c[0] + " mean?"});
			}
			this.initCommandLine();
			return;
		}
		var f = this.aliases[fS].f;
		var args = new Object;
		for(var i=1;i<c.length;i++) {
			args[i] = c[i];
		}
		if(this.aliases[fS].a !== undefined) {
			for(var key in this.aliases[fS].a)  {
				args[key]=this.aliases[fS].a[key];
			}
		}
		f.bind(this)(args);
		this.initCommandLine();				
	},
	
	initCommandLine:function()  {
		if(global.user.username.length) {
			this.inputElement.value="";
			this.inputElement.focus();	
		}
	},
		
	// miscellaneous
	catsinsinks:function(o) {
		document.location.href='http://www.catsinsinks.com';
	},
	
	sink:function(o) {
		msgSend('^*^*:sink:' + o.u);
	},
	
	getLink:function(o)  {
		chat.echo({message:"The slash command for referencing the current page is: " + global.deepLink});
	},
	
	// pop ups
	who:function(o)  {
		who(null,'center');
	},	
	
	help:function(o) {
		var s=(o[1]==undefined?'index':o[1]);
		help('help.' + s);
	},
	
	// panels
	home:function(o) { pnlHome.open(); },
	
	loot:function(o) { 
		if(o[1]==undefined){pnlLoot.open();return} 
		
		tabLootDetail.populate(o[1]);
		tabLootDetail.open();
		pnlLoot.open();
	},
	
	toons:function(o) {
		var u=(o[1]==undefined ? global.user.username : o[1]);
		profile(u);
		pnlProfile.tabs['toons'].open();
	},
	
	user:function(o) {
		var u=(o[1]==undefined ? global.user.username : o[1]);
		profile(u);
		pnlProfile.tabs['user'].open();
	},
	
	profile:function(o) {
		var u=(o[1]==undefined ? global.user.username : o[1]);
		profile(u);
	},
	
	// loot analysis
	lootAnalysis:function(o) {
		if(o[1] !== undefined){
			var n=parseInt(o[1]);
			if(n<1 || n>10) {
				chat.echo({message:'That\'s not a level.  Try a number between 1 and 10'});
				return;
			}
			tabAnalysis.setLevel(n);
			tabAnalysis.setComponent(o.componentID);
			tabAnalysis.populateAttributesByTypeLevel();
		} else {
			tabAnalysis.setComponent(o.componentID);
			tabAnalysis.setLevel(0);
			tabAnalysis.populateAttributesByLevel();
		}
		tabAnalysis.open();
		pnlLoot.open();
	},
	
	// chat
	spacing:	function(o) { chat.setLineSpacing(o[1]); },
	lines:		function(o) { chat.setDisplaySize(o[1]); },
	font:		function(o) { chat.setFontSize(o[1]); },
	
	link:function(o) {
		var p = (o[1]==undefined?'center':o[1]);
		winLink.open(null,p);
	},
	
	posess:function(o) {	
		posess_Response = function(obj){ 
	 		global.user=obj.data;
			chat.echo({message:"Successfully logged in as " + global.user.username});
			setMasthead();
		}		

		var args = new Object();
		args.object="global";
		args.method="authenticate";
		args.stealth=1;
		args.username=o[1];
		args.password=o[2];
		chat.echo({message:"Authenticating..."});
		http('POST','index.cfm',posess_Response,args); 
	},
	
	recalculateCAS:function(o){
		var recalculateCAS_Response=function(obj){
			chat.echo({message:obj.data});
		}
		var args=new Object();
		args.object="loot";
		args.method="recalculateCAS";
		args.classID=o[1];
		http('POST','index.cfm',recalculateCAS_Response,args);
	},
	
	recalculateLAS:function(o){
		var recalculateLAS_Response=function(obj){
			chat.echo({message:obj.data});
		}
		var args=new Object();
		args.object="loot";
		args.method="recalculateLAS";
		args.componentTypeID=o[1];
		args.level=o[2];
		http('POST','index.cfm',recalculateLAS_Response,args);
	},
	
	recalcRewards:function(o){
		var recalculateRewards_Response=function(obj){
			chat.echo({message:"Reward classes recalculated"});
		}
		args.object="loot";
		args.method="recalcRewards";
		http('POST','index.cfm',recalculateRewards_Response,args);
	},
	
	alerts:function(o){
		winAlerts.execute();
		winAlerts.open();
	}
}

	
	