// lootDetail.js custom object for loot component detail view
// requires jsmx (engine.js), prototype.js, god knows what else...

function extendLootDetailTab()  {

	var xo={	
	
		render:function(){
			var s='';
			s+='<div id="lootDetailEmpty">No individual components are currently available for display.  Use the <a href="javascript:void(0);" onClick="pnlLoot.tabs[\'browser\'].open();">browser tab</a> to generate a component set for review.</div>';
			s+='<div id="lootDetailLoading">Loading data, please wait....</div>';
			s+='<div id="lootDetailMain">';
			s+='<div id="lootDetailControls">';
			s+='<table border="0" cellpadding="0" cellspacing="0" width="100%">';
			s+='<tr>';
			s+='<td width="50%" id="elem_LD_info">';
			s+='<span style="font-weight:bold;">Level <span id="field_LD_level"></span>&nbsp;<span id="field_LD_type"></span></span><br>';
			s+='<span id="field_LD_class"></span><br>';
			s+='Submitted by <span id="field_LD_user"></span>&nbsp;from&nbsp;<span id="field_LD_server"></span><br>';
			s+='<span id="field_LD_timestamp"></span>';
			s+='</td>';
			s+='<td width="50%" align="center" valign="middle" id="elem_LD_controls">';
			s+='<div id="elem_LD_recordsetDescription" style="margin-bottom:10px;"></div>';		
			s+='<img src="client/images/prev.gif" id="ctrl_LD_prev" alt="Previous Record" />&nbsp;';				
			s+='Component ';
			s+='<select id="ctrl_LD_goto"></select>';
			s+=' of <span id="field_LD_numrec">0</span> in current set';
			s+='&nbsp;<img src="client/images/next.gif" id="ctrl_LD_next" alt="Next Record" />';
			s+='</td>';
			s+='</tr></table></div>';
			s+='<div id="elem_LD_flagBox" class="lootDetailFlagBox"></div>';
			s+='<div id="elem_LD_verificationBox" class="lootDetailVerificationBox"></div>';
			s+='<div id="elem_LD_optionsBox" class="lootDetailOptionsbox"></div>';
			s+='<a href="javascript:void(0);" id="ctrl_LD_flag" class="lootDetailOption">Flag for Admin Review</a>';
			s+='<a href="javascript:void(0);" id="ctrl_LD_view" target="new" class="lootDetailOption">View Screenshot</a>';
			s+='<a href="javascript:void(0);" id="ctrl_LD_verify" class="lootDetailOption">Verify Component</a>';
			s+='<a href="javascript:void(0);" id="ctrl_LD_clearFlag" class="lootDetailOption">Clear Flag</a>';
			s+='<a href="javascript:void(0);" id="ctrl_LD_uploadScreenshot" class="lootDetailOption">Upload Screenshot</a>';
			s+='<a href="javascript:void(0);" id="ctrl_LD_requestVerification" class="lootDetailOption">Request Verification</a>';
			s+='<a href="javascript:void(0);" id="ctrl_LD_delete" class="lootDetailOption">Delete Component</a>';			
			s+='<div id="lootDetailData" class="lootDetailData"></div>';
			s+='</div>';
			
			Element.update(this.bodyElement,s);
			
			this.loadingElement=$('lootDetailLoading');
			this.mainElement=$('lootDetailMain');
			this.emptyElement=$('lootDetailEmpty');
			this.headerElement=$('lootDetailControls');
			this.controlsElement=$('elem_LD_controls');
			this.infoElement=$('elem_LD_info');
			this.dataElement=$('lootDetailData');
			this.descriptionElement=$('elem_LD_recordsetDescription');
			this.flagElement=$('elem_LD_flagBox');
			this.verificationElement=$('elem_LD_verificationBox');
			this.spacerElement=$('elem_LD_spacer');
			
			this.numRecElement=$('field_LD_numrec');
			
			this.levelElement=$('field_LD_level');
			this.userElement=$('field_LD_user');
			this.typeElement=$('field_LD_type');
			this.timestampElement=$('field_LD_timestamp');
			this.serverElement=('field_LD_server');
			this.classElement=('field_LD_class');
			
			this.flagControl=$('ctrl_LD_flag');
			this.viewControl=$('ctrl_LD_view');
			this.verifyControl=$('ctrl_LD_verify');
			this.clearFlagControl=$('ctrl_LD_clearFlag');
			this.deleteControl=$('ctrl_LD_delete');
			this.uploadControl=$('ctrl_LD_uploadScreenshot');
			this.requestControl=$('ctrl_LD_requestVerification');
			this.prevControl=$('ctrl_LD_prev');
			this.nextControl=$('ctrl_LD_next');
			this.gotoControl=$('ctrl_LD_goto');
			
			Event.observe(this.prevControl,'click',this.onPrevButtonClick.bindAsEventListener(this),false);
			Event.observe(this.nextControl,'click',this.onNextButtonClick.bindAsEventListener(this),false);
			Event.observe(this.gotoControl,'change',this.onGotoChange.bindAsEventListener(this),false);
			Event.observe(this.flagControl,'click',this.flag.bindAsEventListener(this),false);
			Event.observe(this.verifyControl,'click',this.verify.bindAsEventListener(this),false);
			//Event.observe(this.viewControl,'click',this.callImageView.bindAsEventListener(this),false);
			Event.observe(this.clearFlagControl,'click',this.clearFlag.bindAsEventListener(this),false);
			Event.observe(this.deleteControl,'click',this.doDelete.bindAsEventListener(this),false);
			Event.observe(this.uploadControl,'click',this.callImageSub.bindAsEventListener(this),false);
			Event.observe(this.requestControl,'click',this.requestVerification.bindAsEventListener(this),false);
			
			this.hideControls();
			
			Element.hide(this.flagElement);
			Element.hide(this.verificationElement);
			
			Element.hide(this.deleteControl);
			Element.hide(this.flagControl);
			Element.hide(this.viewControl);
			Element.hide(this.clearFlagControl);		
			Element.hide(this.uploadControl);	
			
			this.setMode('empty');
		},
		
		setGotoOptions:function(){
			var a=tabLootBrowser.recordset.length;
			var b=tabLootBrowser.totalrecords;
			var n=(a>b?b:a);
			for(var i=0;i<n;i++) {
				this.gotoControl.options[i]=new Option(i+1,i,false,false);
			}
			this.gotoControl.value=tabLootBrowser.activeRow;
		},		
		
		setTotalRecords:function(){
			this.numRecElement.innerHTML=tabLootBrowser.recordset.length;
		},
		
		setInfo:function(){
			var o=this.component;
			var cl=global.lookup.classes[o.classid];
			var c=global.lookup.components[cl.componenttypeid];
			Element.update(this.classElement,cl.name);
			Element.update(this.levelElement,cl.level + "/" + cl.relevel);
			Element.update(this.typeElement,c.name);
			Element.update(this.timestampElement,formatArchiveMessageDate(o.datesubmitted));
			Element.update(this.userElement,'<a href="javascript:void(0);" onClick="command.parse(\'/user ' + o.username + '\');">' + o.username + '</a>');
			Element.update(this.serverElement,global.lookup.server[o.serverid]);
		},
		
		setControls:function(){
			this.setGotoOptions();
			this.setTotalRecords();
			this.showControls();
		},
		
		setDeepLink:function(){
			if(this.component !== undefined) {
				global.deepLink = '/loot ' + this.component.componentid;
			}
		},
		
		hideControls:function(){this.controlsElement.style.visibility='hidden';},
		showControls:function(){this.controlsElement.style.visibility='visible';},
		
		setFlag:function(){
			if(this.component.verificationstatusid==901){
				var s='Flagged for administrative review by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.flaguser + '\');">' + this.component.flaguser + '</a> - ' + formatArchiveMessageDate(this.component.flagdate);
				Element.update(this.flagElement,s);
				Element.show(this.flagElement);
				Element.hide(this.verificationElement);
			} else if(this.component.verificationstatusid==902){
				var s='Flagged for administrative review by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.flaguser + '\');">' + this.component.flaguser + '</a> - ' + formatArchiveMessageDate(this.component.flagdate);	
				s+='<br><br>Screenshot verification requested by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.verificationrequestuser + '\');">' + this.component.verificationrequestuser + '</a> - ' + formatArchiveMessageDate(this.component.verificationrequestdate);
				Element.update(this.flagElement,s);
				Element.show(this.flagElement);
				Element.hide(this.verificationElement);		
			} else if(this.component.verificationstatusid==903){
				var s='Flagged for administrative review by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.flaguser + '\');">' + this.component.flaguser + '</a> - ' + formatArchiveMessageDate(this.component.flagdate);	
				s+='<br><br>Screenshot verification requested by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.verificationrequestuser + '\');">' + this.component.verificationrequestuser + '</a> - ' + formatArchiveMessageDate(this.component.verificationrequestdate);			
				s+='<br><br>Screenshot submitted for review by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.imagefileuser + '\');">' + this.component.imagefileuser + '</a> - ' + formatArchiveMessageDate(this.component.imagefiledate);
				Element.update(this.flagElement,s);
				Element.show(this.flagElement);
				Element.hide(this.verificationElement);
			} else if(this.component.verificationstatusid==904){
				var s='Flagged for administrative review by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.flaguser + '\');">' + this.component.flaguser + '</a> - ' + formatArchiveMessageDate(this.component.flagdate);	
				s+='<br><br>Screenshot verification requested by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.verificationrequestuser + '\');">' + this.component.verificationrequestuser + '</a> - ' + formatArchiveMessageDate(this.component.verificationrequestdate);			
				s+='<br><br>Screenshot submitted for review by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.imagefileuser + '\');">' + this.component.imagefileuser + '</a> - ' + formatArchiveMessageDate(this.component.imagefiledate);			
				s+='<br><br>Verified against screenshot submission by <a href="javascript:void(0);" onClick="command.parse(\'/user ' + this.component.verificationuser + '\');">' + this.component.verificationuser + '</a> - ' + formatArchiveMessageDate(this.component.verificationdate);
				Element.update(this.verificationElement,s);
				Element.show(this.verificationElement);
				Element.hide(this.flagElement);		
			} else {
				Element.show(this.flagControl);
				Element.hide(this.clearFlagControl);
				Element.hide(this.verificationElement);
				Element.hide(this.flagElement);
			}
		},
		
		setOptions:function(){
			var a=[this.flagControl,this.clearFlagControl,this.deleteControl];
			Element.hide(this.flagControl);
			Element.hide(this.clearFlagControl);
			Element.hide(this.deleteControl);
			Element.hide(this.requestControl);
			Element.hide(this.uploadControl);
			if(!global.user.username){
				return;
			}			
			var canClear=(global.user.isadmin || global.user.islootadmin || (global.user.username==this.component.flaguser?true:false));
			var canDelete=(global.user.isadmin || global.user.islootadmin || (global.user.username==this.component.username?true:false));
			var canRequest=(global.user.isadmin || global.user.islootadmin);
			var canUpload=canDelete;
			var canVerify=canDelete;
			if(this.component.verificationstatusid==901){
				Element[(canClear?'show':'hide')](this.clearFlagControl);
				Element.hide(this.flagControl);
				Element[(canRequest?'show':'hide')](this.requestControl);
				Element.hide(this.uploadControl);
				Element.hide(this.viewControl);
				Element.hide(this.verifyControl);
			} else if (this.component.verificationstatusid==902){
				Element[(canClear?'show':'hide')](this.clearFlagControl);
				Element.hide(this.flagControl);
				Element.hide(this.requestControl);
				Element[(canUpload?'show':'hide')](this.uploadControl);
				Element.hide(this.viewControl);
				Element.hide(this.verifyControl);
			} else if (this.component.verificationstatusid==903){
				Element[(canClear?'show':'hide')](this.clearFlagControl);
				Element.hide(this.flagControl);
				Element.hide(this.requestControl);
				Element.hide(this.uploadControl);
				Element.show(this.viewControl);
				if(this.component.imagefilesoffloaded==1){
					//set link to photobucket
				} else {
					this.viewControl.href="http://www.spaceloot.org/images/" + this.component.imagefilename + "_FULL." + this.component.imagefileext;
				}
				Element[(canVerify?'show':'hide')](this.verifyControl);
			} else if (this.component.verificationstatusid==904){
				Element.hide(this.clearFlagControl);
				Element.hide(this.flagControl);
				Element.hide(this.requestControl);
				Element.hide(this.verifyControl);
				if(this.component.imagefilesoffloaded==1){
					//set link to photobucket
				} else {
					this.viewControl.href="http://www.spaceloot.org/images/" + this.component.imagefilename + "_FULL." + this.component.imagefileext;
				}				
				Element.show(this.viewControl);
			} else {
				Element.hide(this.requestControl);
				Element.hide(this.uploadControl);
				Element.hide(this.clearFlagControl);
				Element.show(this.flagControl);
				Element.hide(this.verifyControl);
				Element.hide(this.viewControl);
			}
			Element[(canDelete?'show':'hide')](this.deleteControl);
		},
	
		
		onPrevButtonClick:function(e) {tabLootBrowser.curseBy(-1);},
		onNextButtonClick:function(e) {tabLootBrowser.curseBy(1);},
		onGotoChange:function(e) {tabLootBrowser.curseTo($F(this.gotoControl));},
		
		setDescription:function() {
			var c=global.lookup.components[tabLootBrowser.componentID].pluralname.toLowerCase();
			if(tabLootBrowser.order=='dateSubmitted'){
				var o='date submitted';
				var d=(tabLootBrowser.direction==1?'from newest to oldest':'from oldest to newest');
			} else {
				var o=global.lookup.attributes[tabLootBrowser.order].name.toLowerCase();
				var d=(tabLootBrowser.direction==1?'from best to worst':'from worst to best');
			}
			if(tabLootBrowser.classID!==0){var cl=global.lookup.classes[tabLootBrowser.classID].name};
			
			var s='';
			if(tabLootBrowser.level!==0) {s+='Level ' + tabLootBrowser.level + ' ';}
			s+=c;
			if(tabLootBrowser.username.length!==0){s+=' submitted by ' + tabLootBrowser.username;}
			if(tabLootBrowser.classID!==0){s+=' in class \'' + cl + '\'';}
			s+='<br>in order of ' + o + ' ' + d;
			Element.update(this.descriptionElement,s);
		},	
		
		setMode:function(m){
			var o={empty:this.emptyElement,loading:this.loadingElement,main:this.mainElement};
			for(k in o){
				Element[k==m?'show':'hide'](o[k]);
			}
		},
		
		reload:function(){
			if(_(this.standalone)) {
				this.populate(this.component.componentid);
			} else {
				this.populate();
			}
		},					
		
		populate:function(id){
			this.setMode('loading');
			if(id==undefined){
				id=tabLootBrowser.recordset[tabLootBrowser.activeRow].componentid;
				var c=global.lookup.components[tabLootBrowser.componentID];
				var a=c.attributes;
				this.setDescription();	
				delete this.standalone;
			} else {
				this.standalone=true;
				this.hideControls();
			}
			this.gotoControl.value=tabLootBrowser.activeRow;
			Element.update(this.dataElement,"Loading...");			

			var populate_Response = function(obj) {
				tabLootDetail.component=obj.data;
				this.setDeepLink();
				Element[global.user.userid==this.component.userid||global.user.isadmin?'show':'hide'](this.deleteControl);				
				var c=global.lookup.components[tabLootDetail.component.componenttypeid];
				var a=c.attributes;						
				tabLootDetail.setInfo();
				var s="";
				s+='<table class="lootTable" cellpadding="0" cellspacing="0" border="0">';
				s+='<thead>';
				s+='<tr>';
				s+='<th class="lootTableHeaderCell" style="width:200px;">Attribute</th>';
				s+='<th class="lootTableHeaderCell">Value</th>';
				s+='<th class="lootTableHeaderCell">Level Best</th>';
				s+='<th class="lootTableHeaderCell">Level Avg</th>';
				s+='<th class="lootTableHeaderCell">Level Std. Dev.</th>';
				s+='<th class="lootTableHeaderCell">Std. Dev.</th>';
				s+='<th class="lootTableHeaderCell">Rank</th>';
				s+='</tr>';
				s+='</thead>';
				s+='<tbody>';
				for(i=0;i<a.length;i++) {
					var o=obj.data.attributes[a[i].attributeid];
					var aM=a[i].mask.split(/\./);
					var aPl=aM[aM.length-1].length;					
					var trC=(i%2?'lootTableRowOdd':'lootTableRowEven');
					s+='<tr class="' + trC + '">';
					s+='<td class="lootTableDataCell" style="width:200px;text-align:left;">' + a[i].name + '</td>';
					s+='<td class="lootTableDataCell">' + obj.data['attribute_'+a[i].attributeid] + '</td>';
					s+='<td class="lootTableDataCell">' + parseFloat(o.levelbest).toFixed(aPl) + '</td>';
					s+='<td class="lootTableDataCell">' + parseFloat(o.levelavg).toFixed(aPl) + '</td>';
					s+='<td class="lootTableDataCell">' + o.levelstddev.toFixed(3) + '</td>';
					s+='<td class="lootTableDataCell">' + o.levelstddevthis.toFixed(3) + '</td>';
					s+='<td class="lootTableDataCell">' + o.rank + ' (' + Math.round(o.rankpct) + '%)</td>';
					s+='</tr>';
				}
	
				s+='</tbody>';
				s+='</table>';
				this.setFlag();		
				this.setOptions();		
				Element.update(tabLootDetail.dataElement,s);
				this.setMode('main');
			}
			
			var args=new Object;
			args.object='loot';
			args.method='getComponentDetail';
			args.componentID=id;
			http('POST','index.cfm',populate_Response.bind(this),args);	
			
		},		
	
		flag:function(){
			var flag_Response=function(obj){
				this.reload();
			}
			
			var args=new Object();
			args.object="loot";
			args.method="flag";
			args.componentID=this.component.componentid;
			http('POST','index.cfm',flag_Response.bind(this),args);
		},
		
		clearFlag:function(){
			var clearFlag_Response=function(obj){
				if(_(obj.error)){
					chat.echo({message:obj.errortext});
					return;
				}
				this.reload();
			}
			
			var args=new Object();
			args.object="loot";
			args.method="clearFlag";
			args.componentID=this.component.componentid;
			http('POST','index.cfm',clearFlag_Response.bind(this),args);
		},
		
		doDelete:function(){
			var doDelete_Response=function(obj){
				if(_(obj.error)){
					chat.echo({message:obj.errortext});
					return;
				}		
				var c=global.lookup.classes[this.component.classid];
				var l=c.relevel;
				var t=global.lookup.components[c.componenttypeid].name;
				var s='Level ' + l + ' ' + t + ' \'' + c.name + '\' deleted';
				delete this.component;				
				if(_(this.standalone)){
					this.setMode('empty');
				} else {		
					tabLootBrowser.deleteRow(tabLootBrowser.activeRow);
				}
				chat.echo({message:s});		
			}
			
			var args=new Object();
			args.object="loot";
			args.method="delete";
			args.componentID=this.component.componentid;
			http('POST','index.cfm',doDelete_Response.bind(this),args);
		},
		
		requestVerification:function(){
			var requestVerification_Response=function(obj){
				if(_(obj.error)){
					chat.echo({message:obj.errortext});
					return;
				}
				
				if(_(obj.notify)){
					Element.update('dspNotify',obj.notifytext);
					winNotify.open();
				}	
									
				this.reload();
			}
			
			var args=new Object();
			args.object="loot";
			args.method="requestVerification";
			args.componentID=this.component.componentid;
			http('POST','index.cfm',requestVerification_Response.bind(this),args);
		},
		
		verify:function(){
			var verify_Response=function(obj){
				if(_(obj.error)){
					chat.echo({message:obj.errortext});
					return;
				}
				this.reload();
			}
			
			var args=new Object();
			args.object="loot";
			args.method="verify";
			args.componentID=this.component.componentid;
			http('POST','index.cfm',verify_Response.bind(this),args);			
		},
		
		callImageSub:function(){
			imageSubIFrame.frmImageSub.imageSubObject.value="loot";
			imageSubIFrame.frmImageSub.imageSubMethod.value="uploadImage";
			imageSubIFrame.frmImageSub.imageSubID.value=this.component.componentid;
			winImageSub.open();
		},
		
		callImageView:function(){
			var file=this.component.imagefilename + "_full." + this.component.imagefileext;
			var server=this.component.imagesOffloaded?'http://www.photobucket.com...':'http://www.spaceloot.org/images/';
			$('winImageImage').src=server + file;
			Element.update('winImageCaption','this is my caption');
			winImage.open();
		}
	}
	
	tabLootDetail=(pnlLoot.tabs['detail']);
	Object.extend(tabLootDetail,xo);	
	tabLootDetail.render();
}