// loot.js custom object for asynchronous messaging
// requires jsmx (engine.js), prototype.js, god knows what else...

function extendLootAnalysisTab()  {

	var xo={
	
		componentID:3,
		level:8,
		attributeID:0,
		
		//overwritten methods
		open:function() {
			if(this.parentObject.activeTab !== undefined) {this.parentObject.activeTab.close()};	
			this.isOpen=true;
			this.show();
			this.parentObject.activeTab=this;	
					
			this.setDeepLink();			
			Event.observe(document.body,'keypress',this.keyPressObserver,true);		
			Event.observe(window,'keypress',this.keyPressObserver,true);					
			setPanelHeight();	
		},		
		
		close:function() {
			this.isOpen=false;	
			this.hide();

			delete global.deepLink;
			Event.stopObserving(window,'keypress',this.keyPressObserver,false);			
		},		
		
		setDeepLink:function() {
			var s=false;
			if(this.componentID!==0) {s="/" + global.lookup.components[this.componentID].name;}
			if(this.level!==0) {s+=" " + this.level;}
			if(s){global.deepLink=s;}
		},
				
		render:function() {
			
			var oC=global.lookup.components;
			var aC=[1,4,7,9,3,2,8,5];
			
			var s='';
			s+='<br>Number hotkeys (level) and a,b,c,d,e,r,s,w hotkeys (components) are enabled.<br><br>';
			s+='<div id="lootAnalysisControlContainer">';
			s+='<select id="ctrlLevel">';
			s+='<option value="0">All Levels</option>';			
			for(var i=1;i<=10;i++) {
				s+='<option value="' + i + '">Level ' + i + '</option>';
			}
			s+='</select>&nbsp;&nbsp;';
			
			s+='<select id="ctrlComponent">';
			for(var i=0;i<aC.length;i++) {
				s+='<option class="ulFirst" value="' + aC[i] + '">' + oC[aC[i]].pluralname + '</option>';
			}
			s+='</select>&nbsp;&nbsp;';
			
			s+='<select id="ctrlAttribute">';
			s+='<option value="placeholder">placeholder</option>';
			s+='</select>';
			
			s+='</div>';
			s+='<div class="lootAnalysisCaptionContainer"><div id="lootAnalysisCaption"></div><div id="lootAnalysisOptions"></div></div>';
			s+='<div id="lootAnalysisData">data container</div>';			
					
			Element.update(this.bodyElement,s);
			this.dataElement=$('lootAnalysisData');
			this.captionElement=$('lootAnalysisCaption');
			this.optionsElement=$('lootAnalysisOptions');
			this.levelControl=$('ctrlLevel');
			this.componentControl=$('ctrlComponent');
			this.attributeControl=$('ctrlAttribute');
			
			this.levelControlChangeObserver=this.onLevelControlChange.bindAsEventListener(this);
			Event.observe(this.levelControl,'change',this.levelControlChangeObserver,false);
			
			this.componentControlChangeObserver=this.onComponentControlChange.bindAsEventListener(this);
			Event.observe(this.componentControl,'change',this.componentControlChangeObserver,false);
			
			this.attributeControlChangeObserver=this.onAttributeControlChange.bindAsEventListener(this);
			Event.observe(this.attributeControl,'change',this.attributeControlChangeObserver,false);	
			
			this.setAttributeControlOptions();		
		},		
		
		setAttributeControlOptions:function() {
			var o=this.attributeControl;
			var a=global.lookup.components[this.componentID].attributes;
			o.options.length=0;
			o.options[0]=new Option("All Attributes","0",false,false);
			for(var i=0;i<a.length;i++) {
				o.options[i+1]=new Option(a[i].name,a[i].attributeid,false,false);
			}
			this.attributeControl.value=this.attributeID;
		},
		
		distributePopulationCall:function(s) {
			if (this.attributeID!==0 && this.level==0)  {
				this[(s=='level'?'populateAttributesByLevel':'populateAttributeByClass')]();
			} else {
				if (this.attributeID!==0) {
					this.populateAttributeByClass();
				} else if (this.level==0) {
					this.populateAttributesByLevel();
				} else {
					this.populateAttributesByTypeLevel();
				}
			}
		},				
		
		setLevel:function(l) {
			this.levelControl.value=l;
			this.level=l;
			if(this.level==0) {
				this.attributeControl.value=0;
				this.attributeControl.disabled=true;
			} else {
				this.attributeControl.disabled=false;
			}
		},	
		
		onLevelControlChange:function(e) {
			this.setLevel(parseInt($F(this.levelControl)));
			this.distributePopulationCall('level');
		},		
		
		setComponent:function(c) {
			this.componentControl.value=c;
			this.componentID=c;
			this.setAttribute(0);
			this.setAttributeControlOptions();			
		},
		
		onComponentControlChange:function(e) {
			this.setComponent(parseInt($F(this.componentControl)));
			this.distributePopulationCall();
		},		
		
		setAttribute:function(l) {
			this.attributeControl.value=l;
			this.attributeID=l;
		},	
		
		onAttributeControlChange:function(e) {
			this.setAttribute(parseInt($F(this.attributeControl)));
			this.distributePopulationCall('attribute');
		},		
		
		onKeyPress:function(e) {
			if(global.fieldFocus || !this.isOpen || !this.parentObject.isOpen) { return }
			var k = (e.charCode) ? e.charCode : ((e.which) ? e.which : e.keyCode);
			var n=k-48;
			var l=false;
			var c=false;
			var oU={97:1,98:4,99:7,100:9,101:3,114:8,115:2,119:5};
			var oL={65:1,66:4,67:7,68:9,69:3,82:8,83:2,87:5};
			if(n==0) {
				l=10;
			} else if (n > 0 && n < 10) {
				l=n;
			}
			
			if(l) {
				this.setLevel(l);
				this.distributePopulationCall('level');
			}
			
			if(oU[k]){
				this.setComponent(oU[k]);
				this.distributePopulationCall('component');
			}
			
			if(oL[k]){
				this.setComponent(oL[k]);
				this.distributePopulationCall('component');
			}			
			
		},
		
		displayLoading:function() {
			Element.update(this.dataElement,"Loading...");
		},
		
		setCaption:function(s) {
			Element.update(this.captionElement,s);
		},
		
		setOptions:function(s) {
			Element.update(this.optionsElement,s);
		},
		
		setData:function(s) {
			Element.update(this.dataElement,s);
		},
		
		populateAttributesByLevel:function() {
			var singular = global.lookup['components'][this.componentID].name;
			var plural = global.lookup['components'][this.componentID].pluralname;
			this.displayLoading();
			
			var populateAttributesByLevel_Response = function(obj) {
				var s='';
				for(var i=0;i<global.lookup.components[this.componentID].attributes.length;i++) {
					var att=global.lookup.components[this.componentID].attributes[i];
					
					s+='<span style="font-weight:bold;">Attribute: ' + att.name + '</span>';
					s+='<table id="las_' + i + '" class="lootTable sorted" border="0" cellpadding="0" cellspacing="0">';
					s+='<thead><tr>';					
	
					var l=['Level','Best','Worst','Average','Std. Dev.','Num. Reported'];
					for(var k=0;k<l.length;k++) {
						s+='<th id="h' + i + k + '" class="lootTableHeaderCell' + (k==0?' sortedplus':'') + '">' + l[k] + '</th>';
					}
					s+='</tr></thead><tbody>';
					for(var j=0;j<=9;j++) {
						var n=(i*10)+j;
						var row = obj[n];
						var trc=(n%2?'Odd':'Even');
						var aM=att.mask.split(/\./);
						var aPl=aM[aM.length-1].length;
						s+='<tr class="lootTableRow' + trc + '">';
						s+='<td axis="number" headers="h' + i + '0" class="lootTableDataCell" style="font-weight:bold;" title="' + (j+1) + ' "><a href="javascript:void(0);" onClick="tabAnalysis.populateAttributesByClass(' + (j+1) + ');">' + (j+1) + '</a></td>';
						s+='<td axis="number" headers="h' + i + '1" class="lootTableDataCell">' + row[(att.sort=='ASC'?'b':'a')].toFixed(aPl) + '</td>';
						s+='<td axis="number" headers="h' + i + '2" class="lootTableDataCell">' + row[(att.sort=='ASC'?'a':'b')].toFixed(aPl) + '</td>';
						s+='<td axis="number" headers="h' + i + '3" class="lootTableDataCell">' + row['c'].toFixed(aPl) + '</td>';
						s+='<td axis="number" headers="h' + i + '4" class="lootTableDataCell">' + row['d'].toFixed(3) + '</td>';
						s+='<td axis="number" headers="h' + i + '5" class="lootTableDataCell">' + row['e'] + '</td>';
						s+='</tr>';
					}
					s+='</tbody></table><br><br>';
				}
				
				this.setData(s);	
				
				for(var i=0;i<global.lookup.components[this.componentID].attributes.length;i++) {
					var x=new SortedTable('las_' + i);
					x.onsort=x.colorizeLoot;
					x.sort($('h' + i + '0'));
				}		
			}			
			
			var args = {object:'loot',method:'getLAS',componentTypeID:this.componentID};
			http('POST','index.cfm',populateAttributesByLevel_Response.bind(this),args);
		},
		
		
		populateAttributeByClass:function(id)  {
			if(id!==undefined)  {
				this.setAttribute(id);
			}
			var plural = global.lookup['components'][this.componentID].pluralname;
			var singular = global.lookup['components'][this.componentID].name;
			var att=global.lookup.attributes[this.attributeID];		
			this.displayLoading();
			
			var populateAttributeByClass_Response = function(obj) {
	
				var s='';

				s+='<table class="lootTable sorted" id="cas" border="0" cellpadding="0" cellspacing="0">';
				s+='<thead><tr>';		
		
				var l=['Class','Best','Worst','Average','Std. Dev.','Num. Reported'];
				for(var k=0;k<l.length;k++) {
					var sp = (att.sort=='ASC' ? ' sortedminus' : ' sortedplus');				
					s+='<th class="lootTableHeaderCell' + (k==1?sp:'') + '" id="h' + k + '">' + l[k] + '</th>';
				}
				s+='</tr></thead><tbody>';
				for(var j=0;j<global.lookup.components[this.componentID].levels[this.level].length;j++) {
					var cl = global.lookup.components[this.componentID].levels[this.level][j];
					var row = obj[cl.classid];
					if(row) {
						var trc=(j%2?'Odd':'Even');
						var aM=att.mask.split(/\./);
						var aPl=aM[aM.length-1].length;
						s+='<tr class="lootTableRow' + trc + '">';
						s+='<td axis="string" headers="h0" class="lootTableDataCell" style="width:300px;text-align:left;">' + global.lookup.classes[row.x].name + '</td>';
						s+='<td axis="number" headers="h1" class="lootTableDataCell">' + row[(att.sort=='ASC'?'b':'a')] + '</td>';
						s+='<td axis="number" headers="h2" class="lootTableDataCell">' + row[(att.sort=='ASC'?'a':'b')] + '</td>';
						s+='<td axis="number" headers="h3" class="lootTableDataCell">' + row['c'].toFixed(aPl) + '</td>';
						s+='<td axis="number" headers="h4" class="lootTableDataCell">' + row['d'].toFixed(3) + '</td>';
						s+='<td axis="number" headers="h5" class="lootTableDataCell">' + row['e'] + '</td>';
						s+='</tr>';					
					}
				}
				s+='</tbody></table><br><br>';
				
				this.setData(s);
				var x=new SortedTable('cas');
				x.onsort=x.colorizeLoot;
				x.sort($('h1'));	 		
			}

			var args = {object:'loot',method:'getCAS',level:this.level,componenttypeid:this.componentID,attributeID:this.attributeID};
			http('POST','index.cfm',populateAttributeByClass_Response.bind(this),args);
		},
		
		populateAttributesByTypeLevel:function(l) {
	
			if(l!==undefined) {this.setLevel(l);}	
					
			var singular = global.lookup['components'][this.componentID].name;
			var plural = global.lookup['components'][this.componentID].pluralname;
			
			//this.setCaption('Level ' + this.level + ' ' + plural);
			this.displayLoading();			
			
			var populateAttributesByTypeLevel_Response = function(obj) {
				var s='';
	
					s+='<table class="lootTable" border="0" cellpadding="0" cellspacing="0">';
					s+='<thead><tr>';					
	
					var l=['Attribute','Best','Worst','Average','Std. Dev.','80%','85%','90%','95%','96%','97%','98%','99%','Num. Reported'];
					for(var k=0;k<l.length;k++) {
						if(l[k].match(/\%/)) {
							s+='<th id="h' + i + k + '" class="lootTableHeaderCell" style="width:50px;">' + l[k] + '</th>';
						} else {
							s+='<th id="h' + i + k + '" class="lootTableHeaderCell">' + l[k] + '</th>';
						}
					}
					s+='</tr></thead><tbody>';
					
					for(var i=0;i<global.lookup.components[tabAnalysis.componentID].attributes.length;i++) {
						var att=global.lookup.components[tabAnalysis.componentID].attributes[i];
						var row = obj[i];
						if(row!==undefined) {
							var trc=(i%2?'Odd':'Even');
							var aM=att.mask.split(/\./);
							var aPl=aM[aM.length-1].length;
							s+='<tr class="lootTableRow' + trc + '" id="row_LA_' + i + '">';
							s+='<td id="cell_LA_' + i + '_1" style="width:150px;text-align:left;font-weight:bold;" class="lootTableDataCell" title="' + att.name + ' "><a href="javascript:void(0);" onClick="tabAnalysis.populateAttributeByClass(' + att.attributeid + ');">' + att.name + '</a></td>';
							s+='<td id="cell_LA_' + i + '_2" class="lootTableDataCell">' + row[(att.sort=='ASC'?'b':'a')] + '</td>';
							s+='<td id="cell_LA_' + i + '_3" class="lootTableDataCell">' + row[(att.sort=='ASC'?'a':'b')] + '</td>';
							s+='<td id="cell_LA_' + i + '_4" class="lootTableDataCell">' + row['c'].toFixed(aPl) + '</td>';
							s+='<td id="cell_LA_' + i + '_5" class="lootTableDataCell">' + row['d'].toFixed(3) + '</td>';
							s+='<td id="cell_LA_' + i + '_7" class="lootTableDataCell" style="width:50px;">' + row['f'].toFixed(aPl) + '</td>';
							s+='<td id="cell_LA_' + i + '_8" class="lootTableDataCell" style="width:50px;">' + row['g'].toFixed(aPl) + '</td>';
							s+='<td id="cell_LA_' + i + '_9" class="lootTableDataCell" style="width:50px;">' + row['h'].toFixed(aPl) + '</td>';
							s+='<td id="cell_LA_' + i + '_10" class="lootTableDataCell" style="width:50px;">' + row['i'].toFixed(aPl) + '</td>';
							s+='<td id="cell_LA_' + i + '_11" class="lootTableDataCell" style="width:50px;">' + row['j'].toFixed(aPl) + '</td>';
							s+='<td id="cell_LA_' + i + '_12" class="lootTableDataCell" style="width:50px;">' + row['k'].toFixed(aPl) + '</td>';
							s+='<td id="cell_LA_' + i + '_13" class="lootTableDataCell" style="width:50px;">' + row['l'].toFixed(aPl) + '</td>';
							s+='<td id="cell_LA_' + i + '_14" class="lootTableDataCell" style="width:50px;">' + row['m'].toFixed(aPl) + '</td>';	
							s+='<td id="cell_LA_' + i + '_6" class="lootTableDataCell">' + row['e'] + '</td>';																																																														
							s+='</tr>';
						}
				}
				s+='</tbody></table>';			
				
				this.setData(s);	
				
				// cell event observers	
				for(var i=0;i<global.lookup.components[tabAnalysis.componentID].attributes.length;i++){
					for(j=2;j<=3;j++) {
						var cell=$('cell_LA_' + i + '_' + j);
						Event.observe(cell,'mouseover',tabAnalysis.onCellMouseOver.bindAsEventListener(tabAnalysis),false);
						Event.observe(cell,'mouseout',tabAnalysis.onCellMouseOut.bindAsEventListener(tabAnalysis),false);
						Event.observe(cell,'click',tabAnalysis.onATLCellClick.bindAsEventListener(tabAnalysis),false);
					}
				}
			}
			
			var args = {object:'loot',method:'getLAS',componentTypeID:this.componentID,level:this.level};
			http('POST','index.cfm',populateAttributesByTypeLevel_Response.bind(this),args);
		},
		
		onCellMouseOver:function(e){Event.element(e).style.backgroundColor=global.colophon.tabButtonMouseOverColor;},		
		onCellMouseOut:function(e){	Event.element(e).style.backgroundColor='';},
		
		onATLCellClick:function(e){
			var a=Event.element(e).id.split(/_/);
			var att=global.lookup.components[this.componentID].attributes[a[2]];
			var o={componentID:this.componentID,
				   level:this.level,
				   direction:(a[3]==2?1:0),
				   order:(att.attributeid) };
			tabLootBrowser.set(o);
			tabLootBrowser.execute(true);
			tabLootDetail.open();
		}
	}
	
	tabAnalysis=(pnlLoot.tabs['analysis']);
	Object.extend(tabAnalysis,xo);	
	tabAnalysis.keyPressObserver=tabAnalysis.onKeyPress.bindAsEventListener(tabAnalysis),		
	tabAnalysis.render();	
	tabAnalysis.setComponent(3);
	tabAnalysis.setLevel(8);
}