// -------------- EventList Object ---------------------
// The main loot list must be built before any of this is called

var AllEvents;

function EventList()
{
	this.list = new Array();
	this.IDSeed = "D"; // each event has its own PREID - can't use C,W,A,V,O,B (these are already used)
}

EventList.prototype.AddEvent = function(name)
{
	// bump the ID
	do
	{
		this.IDSeed = String.fromCharCode(this.IDSeed.charCodeAt(0) + 1);

	} while ( (this.IDSeed=='C') || (this.IDSeed=='W') || (this.IDSeed=='A') || (this.IDSeed=='V') || (this.IDSeed=='O')  || (this.IDSeed=='B')  )

	var ourevent = new Event(name,this.IDSeed);

	this.list.push(ourevent);
	return(ourevent);
}

EventList.prototype.UpdateEvents = function()
{
	var itemcount, uniquecount;
	var i,x;
	var obj;

	// scan all events and update the totals
	// EU[n] = span with Unique total
	// EI[n]  = span with Items total

	for(event=0;event<this.list.length;event++) // scan the list of events
	{
		lootlist = this.list[event].loot;
		itemcount = uniquecount = 0;
		numitems = lootlist.length;

		for(x=0 ; x < numitems; x++)
		{
			// get original loot item and its quantity
			lootitem = FindLootItemByID(lootlist[x].oID);
			itemcount += parseInt(lootitem.quantity);

			if(lootitem.quantity > 0 )
				uniquecount++;
		}

		obj = getObj("EI"+(event+1));
		obj.innerHTML = itemcount;

		obj = getObj("EU"+(event+1));
		obj.innerHTML = uniquecount + "/8";
	}
}

// -----------------------------------  UI Code for generating the tab -------------------------------
EventList.prototype.MakeUITable = function()
{
	var itemcount,uniquecount;
	var lootitem;
	var numitems;
	var lootlist;
	var x,i,event;
	var itemID;
	var output;

	output = "<table border='0' cellpadding='5' cellspacing='0' style='margin-bottom: 30px'>";

	for(oe=0;oe<this.list.length;oe++) // scan the list of events
	{
		lootlist = this.list[oe].loot;
		itemcount = uniquecount = 0;
		numitems = lootlist.length;

		output += "<tr><td colspan='5'><span style='font-size: 14pt; color: #bcd2ea'>" + this.list[oe].name + "</td></tr>";

		for(x=0 ; x < numitems; x++)
		{
			// get original loot item and its quantity
			lootitem = FindLootItemByID(lootlist[x].oID);
			itemcount += lootitem.quantity;
		}

		for(x=0; x < numitems; x++)
		{

			if( (x%4) == 0 )
				output += "<tr>";

			itemID = lootlist[x].id;
			lootitem = FindLootItemByID(lootlist[x].oID);

			output += "<td align='center'";


			if( ((x%8) >= 4) && (( x%8) <=7) )
				output += " style=\"border-bottom: 1px solid #bcd2ea\"";

			output += "><img src='" +getImageName(lootitem) + "' style=\"width: 50px; margin-bottom: 3px\" alt=\"" + lootitem.name + "\" title=\""+ lootitem.name+"\"><br/>";
			output += "<input type='text' style=\"width: 50px; padding-top: 3px\" size='4' maxlength='10' id='" +itemID + "'";

			if(lootitem.quantity > 0 )
			{
				output += " value = '" + lootitem.quantity + "'";
				uniquecount++;
			}
			else
				output += " value=''";

			output += " onchange=\"changeQty('" + lootlist[x].oID + "','" + itemID + "');\"/></td>";

			if(x==3) // total items found
				output += "<td align='center' valign='center'><span id=\"EI"+(oe+1)+"\" style=\"color: #7FFF00; font-size: 14pt\">" + itemcount + "</span></td>";

			if(x==7) // uniques found
				output += "<td nowrap align='center' valign='center' style=\"border-bottom: 1px solid #bcd2ea\"><span id=\"EU"+(oe+1)+"\" style=\"color: #7FFF00; font-size: 14pt\">" + uniquecount + "/8</span></td>";

			if( (x%4)==3)
				output += "</tr>";

		}

	}

	output += "</table>";

	obj = getObj("Eventstab");
	obj.innerHTML = output;
}

//----------------- Event Object -----------------------

function Event(name,ID)
{
	this.name = name;
	this.loot = new Array();
	this.ID = ID;
}

Event.prototype.AddLoot = function(name)
{
	var theloot, ourID, ourloot, i;

	for(i=0;i<arguments.length;i++)
	{	
		theloot = FindLootItemByName(arguments[i]);

		if(theloot)
		{
			ourID = this.ID + this.loot.length;
			ourloot = new EventLoot(arguments[i],ourID,theloot.id);

			this.loot.push(ourloot);
			theloot.AddAltID(ourID);
		}
	}

}

//----------------- EventLoot Object ---------------------

function EventLoot(name,id,orgID)
{
	this.name = name;
	this.id = id;
	this.oID = new String(orgID);
}

//-------------------- Call this to get everything off the ground - the loot list itself MUST exist first ------------
//-- this is called from main() at the end of building all loot items ---

function BuildEvents()
{
	var oe;

	AllEvents = new EventList();

	oe = AllEvents.AddEvent("4th of July Loot");
		oe.AddLoot("Bayonet","Cannon","Pair of Flintlock Pistols","Musket","Saber","Davy Crockett Hat","Red Coat","Tri-Point Hat");

	oe = AllEvents.AddEvent("Halloween Loot");
		oe.AddLoot("Fake Vampire Teeth","Frankenstein Poker Chips","Laser Squirrel","Monkey Brain Stew","YoZombie","Dead End","Kraken","Zombie Cow");

	oe = AllEvents.AddEvent("Hard Labor Day Loot");
		oe.AddLoot("Baby Face Nelson's .351","Dillinger's Wooden Gun","Ma Barker's Machine Gun","Machine Gun Kelly's Gun","Pretty Boy Floyd's .45","Ness' Fedora","Bonnie & Clyde's B-400","Capone's Armored 341A");

	oe = AllEvents.AddEvent("Moscow Expansion Loot");
		oe.AddLoot(".45 ACP Pistol","Dragunov","Grach","IZH-35m","PPSH 41 Submachine gun","VSK-94 Sniper Rifle","Ushanka","Impression");

	oe = AllEvents.AddEvent("Public Enemy #1 Loot");
		oe.AddLoot("Agent Purvis' Rifle","Dillinger's Pistols and Holster","Dillinger's Wooden Gun","Fur Coat","Locket of Billie","Set of Prison Stripes","Public Enemy #1 Newspaper","V8");

	oe = AllEvents.AddEvent("Thanksgiving Loot");
		oe.AddLoot("Cooked Goose","Electric Carving Knife","Potato Mash","Pea Shooter","Stuffed Turkey","Food Coma","Lucky Wishbone","Gravy Boat");

	oe = AllEvents.AddEvent("Tigers Unleashed Loot");
		oe.AddLoot("Liger","Pro's 2 Iron","Siberian Tiger","Tora Assault Rifle","Tigerskin Armored Vest","Le Tigre","Tiger Tank","Tigershark Submersible");

//	oe = AllEvents.AddEvent("Valentine's Day Massacre");
//		oe.AddLoot("Lookout","Senza Pari","Purple Gang's Gun","Highball","Cupid's Arrow","Cupid");

	// lets look at the lists and verify they are correct
/*
	var i,x;
	var output = "";

	for(i=0;i<AllEvents.list.length;i++)
	{
		output += AllEvents.list[i].name + "\r\n\t";
		for(x=0;x<AllEvents.list[i].loot.length;x++)
			output += "["+AllEvents.list[i].loot[x].name + ","+AllEvents.list[i].loot[x].oID+","+AllEvents.list[i].loot[x].id+"] ";
		output += "\r\n";
	}
	alert(output);
*/
	AllEvents.MakeUITable();
}
