// lootBrowser.js custom object for loot browser
// requires jsmx (engine.js), prototype.js, god knows what else...

function extendLootBrowserTab()  {

	var xo={	
	
		componentID:0,
		level:0,
		classID:0,
		order:0,
		direction:0,
		recordset:new Array(),
	
		render:function() {
			
			var oC=global.lookup.components;
			var aC=[1,4,7,9,3,2,8,5];
			
			var s='';

 			s+='<div id="lootBrowserControlContainer">';
			
			// component control
			s+='<select id="ctrl_LB_Component">';
			s+='<option value="0" SELECTED>Choose Component...</option>';
			for(var i=0;i<aC.length;i++) {
				s+='<option value="' + aC[i] + '">' + oC[aC[i]].pluralname + '</option>';
			}
			s+='</select>&nbsp;&nbsp;';

			// level control
			s+='<select id="ctrl_LB_Level">';			
			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;';
			
			// class control
			s+='<select id="ctrl_LB_Class">';			
			s+='<option value="0">All Classes</option>';	
			s+='</select>&nbsp;&nbsp;';
			
			// order control
			s+='<select id="ctrl_LB_Order">';			
			s+='</select>&nbsp;&nbsp;';
			
			// direction control
			s+='<select id="ctrl_LB_Direction">';			
			s+='</select>&nbsp;&nbsp;';		
			
			// execute button
			s+='<button id="ctrl_LB_Execute">fetch</button>';		
			
			// advanced link
			s+='&nbsp;&nbsp;<button id="ctrl_LB_advSearch">options</button>';		
			s+='</div>';	
						
			// advanced search box
			s+='<div id="elem_LB_advSearch" style="display:none;margin-top:5px;">';
			s+='<span class="formLabelSubText"><input type="text" id="ctrl_LB_user" /><br>submitter name</span>';
			s+='</div>';

			s+='<br><div id="lootBrowserData"><br><br>Choose a looted component type to begin.</div>';			
					
			Element.update(this.bodyElement,s);
			
			
			// assign controls to object
			this.componentControl = $('ctrl_LB_Component');
			this.levelControl=$('ctrl_LB_Level');
			this.classControl=$('ctrl_LB_Class');
			this.usernameControl=$('ctrl_LB_user');
			this.orderControl=$('ctrl_LB_Order');
			this.directionControl=$('ctrl_LB_Direction');
			this.executeControl=$('ctrl_LB_Execute');
			this.advSearchControl=$('ctrl_LB_advSearch');
			
			// elements
			this.dataElement=$('lootBrowserData');
			this.advSearchElement=$('elem_LB_advSearch');			
			
			// assign event observers
			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.classControlChangeObserver=this.onClassControlChange.bindAsEventListener(this);
			Event.observe(this.classControl,'change',this.classControlChangeObserver,false);
			
			this.orderControlChangeObserver=this.onOrderControlChange.bindAsEventListener(this);
			Event.observe(this.orderControl,'change',this.orderControlChangeObserver,false);
			
			this.directionControlChangeObserver=this.onDirectionControlChange.bindAsEventListener(this);
			Event.observe(this.directionControl,'change',this.directionControlChangeObserver,false);
			
			this.executeControlClickObserver=this.onExecuteControlClick.bindAsEventListener(this);
			Event.observe(this.executeControl,'click',this.executeControlClickObserver,false);
			
			this.advSearchClickObserver=this.toggleAdvancedSearch.bindAsEventListener(this);
			Event.observe(this.advSearchControl,'click',this.toggleAdvancedSearch,false);
		},
		
		setOrderOptions:function() {
			var o=this.orderControl;
			var a=global.lookup.components[this.componentID].attributes;
			o.options[0]=new Option('sorted by date submitted','dateSubmitted',false,false);
			for(var i=0;i<a.length;i++) {
				o.options[i+1]=new Option('sorted by ' + a[i].name.toLowerCase(),a[i].attributeid,false,false);
			}
		},			
		
		setClassOptions:function() {
			var o=this.classControl;
			var a=global.lookup.components[this.componentID].levels[this.level];
			o.options[0]=new Option("All Classes",0,false,false);
			for(var i=0;i<a.length;i++) {
				var j=i+1;
				c=a[i];
				o.options[j]=new Option(c.name,c.classid,false,false);
			}
		},
		
		setDirectionOptions:function(type) {
			o=this.directionControl;
			if(type=='attribute') {
				o.options[0]=new Option("from best to worst",1,false,false);
				o.options[1]=new Option("from worst to best",0,false,false);
			} else {
				o.options[0]=new Option("from newest to oldest",1,false,false);
				o.options[1]=new Option("from oldest to newest",0,false,false);
			}
		},	
		
		// bind the values of the component,level and class attributes to their respective controls
		onComponentControlChange:function(){this.setComponent($F(this.componentControl));},		
		onLevelControlChange:function(){this.setLevel($F(this.levelControl));},		
		onClassControlChange:function(){this.setClass($F(this.classControl));},
		onOrderControlChange:function(){this.setOrder($F(this.orderControl));},
		onDirectionControlChange:function(){this.setDirection($F(this.directionControl));},
		onExecuteControlClick:function(){this.execute(true);},				
		toggleAdvancedSearch:function(e){Element.toggle('elem_LB_advSearch');},
		showAdvancedSearch:function(){Element.show('elem_LB_advSearch');},
		hideAdvancedSearch:function(){Element.hide('elem_LB_advSearch');},
		
		setComponent:function(c)  {
			c=_(c)?c:0;
			this.componentID=c;
			this.componentControl.value=c;
			this.setLevel(this.level);
			this.setOrder();
			this.setSearchable();
		},			
		
		setLevel:function(l) {
			l=_(l)?l:0;
			this.level=l;
			this.levelControl.value=l;
			Element[this.componentID==0?'hide':'show'](this.levelControl);
			this.setClass(0);
		},
		
		setClass:function(c) {
			c=_(c)?c:0;
			this.classID=c;
			if(this.classID==0)  {
				if(this.level==0 || this.componentID==0) {
					Element.hide(this.classControl);
				} else  {
					this.setClassOptions();
					Element.show(this.classControl);
				}
			}
			this.classControl.value=c;
		},
		
		setOrder:function(o) {
			if(o==undefined)  {
				if(this.componentID==0) {
					Element.hide(this.orderControl);
					Element.hide(this.directionControl);
					this.directionControl.value=1;
					this.direction=1;
				} else {
					Element.show(this.orderControl);
					Element.show(this.directionControl);
					this.setOrderOptions();
					if(this.order!=='dateSubmitted') {			
						o=global.lookup.components[this.componentID].defaultattribute;		
						this.order = o;										
						this.orderControl.value=o;
						this.setDirectionOptions(this.order=='dateSubmitted'?'date':'attribute');							
					}				
				}
			} else {		
				this.order = o;										
				this.orderControl.value=o;
				this.setDirectionOptions(this.order=='dateSubmitted'?'date':'attribute');							
			}
		},
		
		setUsername:function(u) {
			u=_(u)?u:'';
			this.username=u;
			this.usernameControl.value=u;
			this.showAdvancedSearch();
		},
		
		setDirection:function(d) {
			d=_(d)?d:0;
			this.direction=d;
			this.directionControl.value=d;
		},
		
		setSearchable:function() {
			this.executeControl.disabled=(this.componentID==0?true:false);
			this.advSearchControl.disabled=(this.componentID==0?true:false);
		},
		
		set:function(o){
			var l={ componentID:this.setComponent,
					order:this.setOrder,
					level:this.setLevel,
					direction:this.setDirection,
					username:this.setUsername,
					classID:this.setClass }
			
			for(var k in l) {
				l[k].bind(this)(o[k]);
			}
		},
		
		execute:function(isNew)  {
			if(isNew==undefined){isNew=false;}
			this.setUsername($F(this.usernameControl));
			execute_Response=function(obj) {
				var sA=function(a,b){
					var st = 'attribute_' + tabLootBrowser.order;
					var sort = global.lookup.attributes[tabLootBrowser.order].sort;
					if((tabLootBrowser.direction==0 && sort=='ASC') || tabLootBrowser.direction==1 && sort=='DESC') {
						return a[st] - b[st];
					} else {
						return b[st] - a[st];
					}
				}
				
				var sD=function(a,b){
					var timeA=new Date(a.datesubmitted).getTime();
					var timeB=new Date(b.datesubmitted).getTime();
					if(tabLootBrowser.direction==0){return timeA-timeB;}
					return timeB-timeA;
				}
				
				tabLootBrowser.recordset=tabLootBrowser.recordset.concat(obj.data);
				tabLootBrowser.recordset.sort(tabLootBrowser.order=='dateSubmitted'?sD:sA);				
				tabLootBrowser.totalrecords=obj.totalrecords;
				
				if(tabLootBrowser.recordset.length > 0) {
					tabLootBrowser.populate();
					tabLootDetail.setControls();
					tabLootBrowser.activateRow((isNew?0:tabLootBrowser.activeRow));		
				} else {
					Element.update(tabLootBrowser.dataElement,"No component records match the specified criteria");
				}	
			}
			
			if(isNew)  {this.recordset = new Array();}
			
			Element.update(this.dataElement,"Loading...");
			tabLootDetail.setMode('loading');
			
			args=new Object;
			args.object="loot";
			args.method="getComponents";
			args.componentID=this.componentID;
			args.level=this.level;
			args.classID=this.classID;
			args.username=this.username;
			args.order=this.order;
			args.direction=this.direction;
			args.start=this.recordset.length+1;
			args.num=25;
			http('POST','index.cfm',execute_Response,args);
		},
		
		populate:function() {
			var aL=['Class','Lv'];
			var aN=global.lookup.components[this.componentID].attributes;		
			for(var i=0;i<aN.length;i++){
				aL.push(aN[i].shortname);
				if(aN[i].attributeid==this.order) { var iSort=i+2; }
			}
			var s='';
			s+= this.recordset.length + ' of ' + this.totalrecords + ' matching components displayed.&nbsp;&nbsp;<a href="javascript:void(0);" onClick="tabLootBrowser.execute(false);">get more records</a><br><br>';
			s+='<table border="0" cellpadding="0" cellspacing="0" class="lootTable">';
			s+='<thead><tr>';
			for(var i=0;i<aL.length;i++){
				if(aL[i]=='Lv'){
					s+='<th class="lootTableHeaderCell" style="width:30px;">Lv</th>';
				} else if (aL[i]=='Class')  {
					s+='<th class="lootTableHeaderCell" style="width:250px;text-align:left;">Class</th>';
				} else if(iSort==i) {
					var sort = global.lookup.attributes[tabLootBrowser.order].sort;
					if((this.direction==0 && sort=='ASC') || this.direction==1 && sort=='DESC') {
						var cSort='sortedminus';
					} else {
						var cSort='sortedplus';
					}
					s+='<th class="lootTableHeaderCell ' + cSort + '" style="width:65px;" id="col_LB_' + aN[i-2].attributeid + '">' + aL[i] + '</th>';
				} else {
					s+='<th class="lootTableHeaderCell" style="width:65px;" id="col_LB_' + aN[i-2].attributeid + '">' + aL[i] + '</th>';
				}
			}
			s+='</thead></tr>';
			s+='<tbody>';
			for(var i=0;i<this.recordset.length;i++) {
				var tdB='<td style="width:75px;" class="' + 'lootTableDataCell' + '">';
				var row=this.recordset[i];
				var sCN=global.lookup.classes[row.classid].name;
				var sCL=global.lookup.classes[row.classid].relevel;
				var trC=(i%2?'lootTableRowOdd':'lootTableRowEven');
				s+='<tr id="row_LB_' + i + '" class="' + trC + '">';
				s+='<td id="cell_LB_' + i + '_1" class="lootTableDataCell" style="width:250px; text-align:left;">' + sCN + '</td>';
				s+='<td id="cell_LB_' + i + '_2" class="lootTableDataCell" style="width:30px;">' + sCL + '</td>';
				for(var j=0;j<aN.length;j++){
					s+='<td id="cell_LB_' + i + '_' + parseInt(j+3) + '" style="width:75px;" class="' + 'lootTableDataCell' + '">' + row['attribute_' + aN[j].attributeid] + '</td>';
				}
				s+='</tr>';
			}
			s+='</tbody></table>';
			Element.update(this.dataElement,s);
			
			// observe header events
			for(var i=0;i<aN.length;i++){
				Event.observe($('col_LB_' + aN[i].attributeid),'mouseover',this.onHeaderMouseOver.bindAsEventListener(this),false);
				Event.observe($('col_LB_' + aN[i].attributeid),'mouseout',this.onHeaderMouseOut.bindAsEventListener(this),false);		
				Event.observe($('col_LB_' + aN[i].attributeid),'click',this.onHeaderClick.bindAsEventListener(this),false);		
			}		
			
			// observe row events
			for(var i=0;i<this.recordset.length;i++) {
				Event.observe($('row_LB_' + i),'mouseover',this.onRowMouseOver.bindAsEventListener(this),false);
				Event.observe($('row_LB_' + i),'mouseout',this.onRowMouseOut.bindAsEventListener(this),false);						
				Event.observe($('row_LB_' + i),'click',this.onRowClick.bindAsEventListener(this),false);		
			}			
						
		},
		
		activateRow:function(i,wait){
			if(i<0){i=0;}
			if(i>this.recordset.length-1 && this.recordset.length > 0){
				if(wait==undefined){this.execute();}
				setTimeout('tabLootBrowser.activateRow(' + i + ',true)',500);		
				return;
			}
			var el=$('row_LB_' + i);
			if(el==undefined){
				setTimeout('tabLootBrowser.activateRow(' + i + ')',500);
				return;
			}
			if(this.activeRowElement!==undefined){this.activeRowElement.className=(this.activeRow%2?'lootTableRowOdd':'lootTableRowEven');}
			this.activeRow=i;
			this.activeRowElement=el;
			el.className='lootTableRowActive';
			tabLootDetail.populate();
		},
		
		deleteRow:function(i){
			this.recordset.splice(i,1);
			if(this.recordset.length==0){
				this.setComponent(0);
				tabLootDetail.setMode('empty');
				Element.update(this.dataElement,"<br><br>Choose a looted component type to begin.");
			} else {
				this.totalrecords--;
				this.populate();
				tabLootDetail.setTotalRecords();		
				if(_(this.recordset[this.activeRow])){
					this.activateRow(this.activeRow);
				} else {
					this.activateRow(this.activeRow-1);
				}
			}
		},
		
		curseBy:function(i){
			var j=parseInt(this.activeRow) + parseInt(i);
			if(j<0 || j>this.totalrecords){return;}
			this.curseTo(j);
		},
		
		curseTo:function(i){this.activateRow(parseInt(i));},
		
		onHeaderMouseOver:function(e){
			var el=Event.element(e);
			el.style.backgroundColor=global.colophon.tabButtonMouseOverColor;
		},
		
		onHeaderMouseOut:function(e){
			var el=Event.element(e);
			el.style.backgroundColor='#E3E3D1';
		},
		
		onHeaderClick:function(e){
			var el=Event.element(e);
			var id=el.id.split(/_/)[2];
			tabLootBrowser.setOrder(id);
			tabLootBrowser.execute(true);
		},
		
		onRowMouseOver:function(e){
			var i=Event.element(e).id.split(/_/)[2];
			var el=$('row_LB_' + i);
			if(parseInt(i)!==parseInt(this.activeRow)){el.className='lootTableRowMouseOver'};
		},
		
		onRowMouseOut:function(e){
			var i=Event.element(e).id.split(/_/)[2];
			var el=$('row_LB_' + i);
			if(parseInt(i)!==parseInt(this.activeRow)) {el.className=(i%2?'lootTableRowOdd':'lootTableRowEven')};
		},
		
		onRowClick:function(e){
			var i=Event.element(e).id.split(/_/)[2];
			this.activateRow(i);
			tabLootDetail.open();
		}		
	}
	
	tabLootBrowser=(pnlLoot.tabs['browser']);
	Object.extend(tabLootBrowser,xo);	
	tabLootBrowser.render();
	tabLootBrowser.setComponent(0);

	// events and initialization methods
}