var legend_names = [
	"BLOODHOUND",
	"GIBRALTAR",
	"LIFELINE",
	"PATHFINDER",
	"WRAITH",
	"BANGALORE",
	"CAUSTIC",
	"MIRAGE",
	"OCTANE",
	"WATTSON",
	"CRYPTO",
	"REVENANT",
	"LOBA",
	"RAMPART",
	"HORIZON",
	"FUSE",
	"VALKYRIE",
	"SEER",
	"ASH"
	];

var legend_descriptions = [
	"TECHNOLOGICAL TRACKER",
	"SHIELDED FORTRESS",
	"COMBAT MEDIC",
	"FORWARD SCOUT",
	"INTERDIMENSIONAL SKIRMISHER",
	"PROFESSIONAL SOLDIER",
	"TOXIC TRAPPER",
	"HOLOGRAPHIC TRICKSTER",
	"HIGH-SPEED DAREDEVIL",
	"STATIC DEFENDER",
	"SURVEILLANCE EXPERT",
	"SYNTHETIC NIGHTMARE",
	"TRANSLOCATING THIEF",
	"BASE OF FIRE",
	"GRAVITATIONAL MANIPULATOR",
	"EXPLOSIVES ENTHUSIAST",
	"WINGED AVENGER",
	"AMBUSH ARTIST",
	"INCISIVE INSIGATOR"
	];

var legend_passives = [
	"TRACKER",
	"GUN SHIELD",
	"COMBAT REVIVE",
	"INSIDER KNOWLEDGE",
	"VOICES FROM THE VOID",
	"DOUBLE TIME",
	"NOX VISION",
	"NOW YOU SEE ME ...",
	"SWIFT MEND",
	"SPARK OF GENIUS",
	"NEUROLINK",
	"STALKER",
	"EYE FOR QUALITY",
	"MODDED LOADER",
	"SPACEWALK",
	"GRENADIER",
	"VTOL JETS",
	"FOCUS OF ATTENTION",
	"MARKED FOR DEATH"
	];

var legend_tacticals = [
	"EYE OF THE ALLFATHER",
	"DOME OF PROTECTION",
	"D.O.C HEAL DRONE",
	"GRAPPLING HOOK",
	"INTO THE VOID",
	"SMOKE LAUNCHER",
	"NOX GAS TRAP",
	"PSYCHE OUT",
	"STIM",
	"PERIMETER SECURITY",
	"SURVEILLANCE DRONE",
	"SILENCE",
	"BURGLAR'S BEST FRIEND",
	"AMPED COVER",
	"GRAVITY LIFT",
	"KNUCKLE CLUSTER",
	"MISSILE SWARM",
	"HEART SEEKER",
	"ARC SNARE"
	];

var legend_ultimates = [
	"BEAST OF THE HUNT",
	"DEFENSIVE BOMBARDMENT",
	"CARE PACKAGE",
	"ZIPLINE GUN",
	"DIMENSIONAL RIFT",
	"ROLLING THUNDER",
	"NOX GAS GRENADE",
	"LIFE OF THE PARTY",
	"LAUNCH PAD",
	"INTERCEPTION PYLON",
	"DRONE EMP",
	"DEATH TOTEM",
	"BLACK MARKET BOUTIQUE",
	"EMPLACED MINIGUN \"SHEILA\"",
	"BLACK HOLE",
	"THE MOTHERLODE",
	"SKYWARD DIVE",
	"EXHIBIT",
	"PHASE BREACH"
	];

var legend_classes = [0, 1, 2, 0, 3, 3, 1, 3, 3, 1, 0, 3, 2, 1, 3, 3, 0, 0, 3];
var legend_mods = [-1, 0, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];
var legend_classes_txt = ["RECON", "DEFENSE", "SUPPORT", "OFFENSE"];
var legend_mods_txt = ["FORTIFIED", "LOW PROFILE"];

var optic_txt = ["1X DIGITAL THREAT", "1X HOLO", "1X-2X VARIABLE HOLO", "1X HCOG 'CLASSIC'", "2X HCOG 'BRUISER'", "3X HCOG 'RANGER'", "2X-4X VARIABLE AOG", "6X SNIPER", "4X-8X VARIABLE SNIPER", "4X-10X DIGITAL SNIPER THREAT"];
var optic_level = [4, 1, 2, 1, 2, 3, 3, 2, 3, 4];

var hopup_txt = [
	"SELECTFIRE RECEIVER",
	"PRECISION CHOKE",
	"SKULL&shy;PIERCER RIFLING",
	"TURBO&shy;CHARGER",
	"DISRUPTOR ROUNDS",
	"DOUBLE TAP TRIGGER",
	"ANVIL RECEIVER",
	"HAMMER&shy;POINT ROUNDS",
	"GRAFFITI MOD",
	"QUICKDRAW HOLSTER",
	"SHATTER CAPS",
	"DEADEYE'S TEMPO",
	"BOOSTED LOADER",
	"DUAL SHELL"
	];
var hopup_level = [3, 3, 4, 4, 3, 3, 4, 3, 1, 3, 3, 3, 4, 3];

var mag_txt = ["EXTENDED LIGHT MAGAZINE", "EXTENDED HEAVY MAGAZINE", "EXTENDED SNIPER MAGAZINE", "EXTENDED ENERGY MAGAZINE"];
var ammo_txt = ["LIGHT ROUNDS", "HEAVY ROUNDS", "SNIPER AMMO", "ENERGY AMMO", "SHOTGUN SHELLS", "ARROWS"];
var weapons = [];

var weapon1 = -1;
var weapon2 = -1;

// Longest - KRABER
// Tallest - RE-45

weapons.push({name:"VK-47 FLATLINE", barrel:false, bolt:false, mag:[1], optic:[1, 2, 3, 4, 5, 6], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:1, filename:"flatline.png"});
weapons.push({name:"R-301 CARBINE", barrel:true, bolt:false, mag:[0], optic:[1, 2, 3, 4, 5, 6], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:0, filename:"r301.png"});
weapons.push({name:"C.A.R. SMG", barrel: true, bolt:false, mag:[1,0], optic:[0, 1, 2, 3, 4], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:1, filename:"car.png"});
weapons.push({name:"VOLT SMG", barrel:true, bolt:false, mag:[3], optic:[0, 1, 2, 3, 4], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:3, filename:"volt.png"});
weapons.push({name:"L-STAR EMG", barrel:true, bolt:false, mag:[3], optic:[1, 2, 3, 4, 5, 6], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:3, filename:"lstar.png"});
weapons.push({name:"RAMPAGE", barrel:true, bolt:false, mag:[1], optic:[1, 2, 3, 4, 5, 6], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:1, filename:"rampage.png"});
weapons.push({name:"HAVOC RIFLE", barrel:false, bolt:false, mag:[3], optic:[1, 2, 3, 4, 5, 6], stock_standard:true, stock_sniper:false, hopup:[3], multi_hopup:false, ammo:3, filename:"havoc.png"});
weapons.push({name:"30-30 REPEATER", barrel:false, bolt:false, mag:[1], optic:[1, 2, 3, 4, 5, 6], stock_standard:false, stock_sniper:true, hopup:[10, 13], multi_hopup:false, ammo:1, filename:"3030.png"});
weapons.push({name:"TRIPLE TAKE", barrel:false, bolt:false, mag:[3], optic:[1, 2, 3, 4, 5, 6], stock_standard:false, stock_sniper:true, hopup:[], multi_hopup:false, ammo:3, filename:"tripletake.png"});
weapons.push({name:"DEVOTION LMG", barrel:true, bolt:false, mag:[3], optic:[1, 2, 3, 4, 5, 6], stock_standard:true, stock_sniper:false, hopup:[3], multi_hopup:false, ammo:3, filename:"devotion.png"});
weapons.push({name:"WINGMAN", barrel:false, bolt:false, mag:[1], optic:[0, 1, 2, 3, 4], stock_standard:false, stock_sniper:false, hopup:[12], multi_hopup:false, ammo:1, filename:"wingman.png"});
weapons.push({name:"BOCEK COMPOUND BOW", barrel:false, bolt:false, mag:[], optic:[1, 2, 3, 4, 5], stock_standard:false, stock_sniper:false, hopup:[10, 11], multi_hopup:true, ammo:5, filename:"bocek.png"});
weapons.push({name:"HEMLOK BURST AR", barrel:true, bolt:false, mag:[1], optic:[1, 2, 3, 4, 5, 6], stock_standard:true, stock_sniper:false, hopup:[12], multi_hopup:false, ammo:1, filename:"hemlok.png"});
weapons.push({name:"PROWLER BURST PDW", barrel:false, bolt:false, mag:[1], optic:[0, 1, 2, 3, 4], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:1, filename:"prowler.png"});
weapons.push({name:"RE-45 AUTO", barrel:true, bolt:false, mag:[0], optic:[0, 1, 2, 3, 4], stock_standard:false, stock_sniper:false, hopup:[], multi_hopup:false, ammo:0, filename:"re45.png"});
weapons.push({name:"R-99 SMG", barrel:true, bolt:false, mag:[0], optic:[0, 1, 2, 3, 4], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:0, filename:"r99.png"});
weapons.push({name:"EVA-8 AUTO", barrel:false, bolt:true, mag:[], optic:[0, 1, 2, 3, 4], stock_standard:false, stock_sniper:false, hopup:[], multi_hopup:false, ammo:4, filename:"eva8.png"});
weapons.push({name:"MASTIFF SHOTGUN", barrel:false, bolt:true, mag:[], optic:[0, 1, 2, 3, 4], stock_standard:false, stock_sniper:false, hopup:[13], multi_hopup:false, ammo:4, filename:"mastiff.png"});
weapons.push({name:"PEACEKEEPER", barrel:false, bolt:true, mag:[], optic:[0, 1, 2, 3, 4], stock_standard:false, stock_sniper:false, hopup:[], multi_hopup:false, ammo:4, filename:"peacekeeper.png"});
weapons.push({name:"MOZAMBIQUE SHOTGUN", barrel:false, bolt:true, mag:[], optic:[0, 1, 2, 3, 4], stock_standard:false, stock_sniper:false, hopup:[], multi_hopup:false, ammo:4, filename:"mozambique.png"});
weapons.push({name:"P2020", barrel:false, bolt:false, mag:[0], optic:[0, 1, 2, 3, 4], stock_standard:false, stock_sniper:false, hopup:[], multi_hopup:false, ammo:0, filename:"p2020.png"});
weapons.push({name:"LONGBOW DMR", barrel:true, bolt:false, mag:[2], optic:[1, 2, 3, 4, 5, 6, 7, 8 ,9], stock_standard:false, stock_sniper:true, hopup:[], multi_hopup:false, ammo:2, filename:"longbow.png"});
weapons.push({name:"SENTINEL", barrel:false, bolt:false, mag:[2], optic:[1, 2, 3, 4, 5, 6, 7, 8 ,9], stock_standard:false, stock_sniper:true, hopup:[11], multi_hopup:false, ammo:2, filename:"sentinel.png"});
weapons.push({name:"CHARGE RIFLE", barrel:false, bolt:false, mag:[], optic:[1, 2, 3, 4, 5, 6, 7, 8 ,9], stock_standard:false, stock_sniper:true, hopup:[], multi_hopup:false, ammo:2, filename:"chargerifle.png"});
//weapons.push({name:"G7 SCOUT", barrel:true, bolt:false, mag:[0], optic:[1, 2, 3, 4, 5, 6], stock_standard:false, stock_sniper:true, hopup:[], multi_hopup:false, ammo:0, filename:"g7.png"});
//weapons.push({name:"M600 SPITFIRE", barrel:true, bolt:false, mag:[1], optic:[1, 2, 3, 4, 5, 6], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:1, filename:"spitfire.png"});
//weapons.push({name:"ALTERNATOR SMG", barrel:true, bolt:false, mag:[0], optic:[0, 1, 2, 3, 4], stock_standard:true, stock_sniper:false, hopup:[], multi_hopup:false, ammo:0, filename:"alternator.png"});
//weapons.push({name:"KRABER .50-CAL SNIPER", barrel:false, bolt:true, mag:[], optic:[1, 2, 3, 4, 5, 6, 7, 8 ,9], stock_standard:false, stock_sniper:true, hopup:[], multi_hopup:false, ammo:2, filename:"kraber.png"});

function zeroPad(nr,base)
{
  var  len = (String(base).length - String(nr).length)+1;
  return len > 0? new Array(len).join('0')+nr : nr;
}

function randomize_legend()
{
	var pick = Math.floor(Math.random() * legend_names.length);
	var img_str;

	document.getElementById("passive_bg").style.backgroundImage = "url('images/bg/abilities/passive.png')";
	document.getElementById("tactical_bg").style.backgroundImage = "url('images/bg/abilities/tactical.png')";
	document.getElementById("ultimate_bg").style.backgroundImage = "url('images/bg/abilities/ultimate.svg')";
	
	document.getElementById("legend_name").innerHTML = legend_names[pick];
	document.getElementById("legend_desc").innerHTML = legend_descriptions[pick];
	document.getElementById("passive_name").innerHTML = legend_passives[pick];
	document.getElementById("tactical_name").innerHTML = legend_tacticals[pick];
	document.getElementById("ultimate_name").innerHTML = legend_ultimates[pick];
	
	img_str = "images/legends/class/" + zeroPad(legend_classes[pick], 10) + ".png";
	document.getElementById("class_img").src = img_str;
	document.getElementById("legend_class").innerHTML = legend_classes_txt[legend_classes[pick]];
	
	if (legend_mods[pick] != -1)
	{
		img_str = "images/legends/mod/" + zeroPad(legend_mods[pick], 10) + ".png";
		document.getElementById("mod_img").src = img_str;
		document.getElementById("legend_mod").innerHTML = legend_mods_txt[legend_mods[pick]];
	}
	else
	{
		document.getElementById("mod_img").src = "";
		document.getElementById("legend_mod").innerHTML = "";
	}

	img_str = "images/legends/icon/" + zeroPad(pick, 10) + ".png";
	document.getElementById("icon_img").src = img_str;
	
	img_str = "images/legends/passive/" + zeroPad(pick, 10) + ".png";
	document.getElementById("passive_img").src = img_str;
	
	img_str = "images/legends/tactical/" + zeroPad(pick, 10) + ".png";
	document.getElementById("tactical_img").src = img_str;
	
	img_str = "images/legends/ultimate/" + zeroPad(pick, 10) + ".png";
	document.getElementById("ultimate_img").src = img_str;
}

function randomize_weapons()
{
	weapon1 = Math.floor(Math.random() * weapons.length);
	weapon2 = Math.floor(Math.random() * weapons.length);
	
	if (document.getElementById("weapon_check").checked)
	{
		while(weapon2 == weapon1)
		{
			weapon2 = Math.floor(Math.random() * weapons.length);
		}
	}

	if (weapon2 < weapon1)
	{
		var temp = weapon1;
		weapon1 = weapon2;
		weapon2 = temp;
	}
	document.getElementById("weapon1_name").innerHTML = weapons[weapon1].name;
	document.getElementById("weapon2_name").innerHTML = weapons[weapon2].name;
	
	document.getElementById("weapon1_img").src = "images/weapons/" + weapons[weapon1].filename;
	document.getElementById("weapon2_img").src = "images/weapons/" + weapons[weapon2].filename;
	
	document.getElementById("weapon1_bg").style.backgroundImage = "url('images/bg/weapons/" + zeroPad(weapons[weapon1].ammo, 10) + ".png')";
	document.getElementById("weapon2_bg").style.backgroundImage = "url('images/bg/weapons/" + zeroPad(weapons[weapon2].ammo, 10) + ".png')";
	
	document.getElementById("weapon1_ammo_img").src = "images/ammo/" + zeroPad(weapons[weapon1].ammo, 10) + ".png";
	document.getElementById("weapon2_ammo_img").src = "images/ammo/" + zeroPad(weapons[weapon2].ammo, 10) + ".png";
	
	clear_attachments();
}

function clear_attachments()
{
	var arr = [];
	arr = document.getElementsByClassName("attachment_bg");
	for (var i = 0; i < arr.length; ++i)
	{
		arr[i].style.backgroundImage = "";
	}
	
	arr = document.getElementsByClassName("attachment_img");
	for (var i = 0; i < arr.length; ++i)
	{
		arr[i].src = "";
	}
	
	arr = document.getElementsByClassName("attachment_txt");
	for (var i = 0; i < arr.length; ++i)
	{
		arr[i].innerHTML = "";
	}
}

function randomize_attachments_single(weapon)
{
	var result = [];
	var pick;

	// Barrel
	if (weapon.barrel == true)
	{
		pick = Math.floor(Math.random() * 4);
		result.push(pick);
		if (pick == 4)
		{
			result.push("images/attachments/barrel4.png");
		}
		else
		{
			result.push("images/attachments/barrel.png");
		}
		if (pick != 0)
		{
			result.push("BARREL STABILIZER");
		}
		else
		{
			result.push("");
		}
	}

	// Shotgun Bolt
	if (weapon.bolt == true)
	{
		pick = Math.floor(Math.random() * 4);
		result.push(pick);
		result.push("images/attachments/bolt.png");
		if (pick != 0)
		{
			result.push("SHOTGUN BOLT");
		}
		else
		{
			result.push("");
		}
	}

	// Extended Mag
	if (weapon.mag.length != 0)
	{
		if (weapon.mag.length != 1)
		{
			// Multi Mag
			pick = Math.floor(Math.random() * 5);
			result.push(pick);
			if (pick != 0)
			{
				var pick2 = Math.floor(Math.random() * weapon.mag.length);
				result.push("images/attachments/mags/" + zeroPad(weapon.mag[pick2], 10) + ".png");
				result.push(mag_txt[weapon.mag[pick2]]);
			}
			else
			{
				var mag_str = "images/attachments/mags/";
				for (let i = 0; i < weapon.mag.length; ++i)
				{
					mag_str += zeroPad(weapon.mag[i], 10);
					if (i != weapon.mag.length - 1)
					{
						mag_str += "_";
					}
				}
				mag_str += ".png"
				result.push(mag_str);
				result.push("");
			}
		}
		else
		{
			// Single Mag
			pick = Math.floor(Math.random() * 5);
			result.push(pick);
			result.push("images/attachments/mags/" + zeroPad(weapon.mag[0], 10) + ".png");
			if (pick != 0)
			{
				result.push(mag_txt[weapon.mag[0]]);
			}
			else
			{
				result.push("");
			}
		}
	}

	// Optics
	if (!document.getElementById("attachment_check").checked)
	{
		pick = Math.floor(Math.random() * (weapon.optic.length + 1));
		if (pick == weapon.optic.length)
		{
			result.push(0);
			result.push("images/attachments/optics/none.svg");
			result.push("");
		}
		else
		{
			result.push(optic_level[weapon.optic[pick]]);
			result.push("images/attachments/optics/" + zeroPad(weapon.optic[pick], 10) + ".png");
			result.push(optic_txt[weapon.optic[pick]]);
		}
	}
	else
	{
		result.push(0);
		result.push("images/attachments/optics/none.png");
		result.push("");
	}

	// Standard Stock
	if (weapon.stock_standard == true)
	{
		pick = Math.floor(Math.random() * 4);
		result.push(pick);
		result.push("images/attachments/stock-standard.png");
		if (pick != 0)
		{
			result.push("STANDARD STOCK");
		}
		else
		{
			result.push("");
		}
	}

	// Sniper Stock
	if (weapon.stock_sniper == true)
	{
		pick = Math.floor(Math.random() * 4);
		result.push(pick);
		result.push("images/attachments/stock-sniper.png");
		if (pick != 0)
		{
			result.push("SNIPER STOCK");
		}
		else
		{
			result.push("");
		}
	}

	// Hopups
	if (weapon.multi_hopup)
	{
		for (var i = 0; i < weapon.hopup.length; ++i)
		{
			pick = Math.floor(Math.random() * 2);
			if (pick == 0)
			{
				result.push(hopup_level[weapon.hopup[i]]);
				result.push("images/attachments/hopups/" + zeroPad(weapon.hopup[i], 10) + ".png");
				result.push(hopup_txt[weapon.hopup[i]]);
			}
			else
			{
				result.push(0);
				result.push("images/attachments/hopups/" + zeroPad(weapon.hopup[i], 10) + ".png");
				result.push("");
			}
		}
	}
	else
	{
		if (weapon.hopup.length != 0)
		{
			pick = Math.floor(Math.random() * (weapon.hopup.length + 1));
			if (pick == weapon.hopup.length)
			{
				result.push(0);
				if (weapon.hopup.length == 1)
				{
					result.push("images/attachments/hopups/" + zeroPad(weapon.hopup[0], 10) + ".png");
				}
				else
				{
					result.push("images/attachments/hopups/multi.png");
				}
				result.push("");
			}
			else
			{
				result.push(hopup_level[weapon.hopup[pick]]);
				result.push("images/attachments/hopups/" + zeroPad(weapon.hopup[pick], 10) + ".png");
				result.push(hopup_txt[weapon.hopup[pick]]);
			}
		}
	}
	return result;
}

function randomize_attachments()
{
	var arr = [];
	var weapon_num;
	var attachment_num;

	if ((weapon1 == -1) || (weapon2 == -1))
	{
		return;
	}
	clear_attachments();
	
	arr = randomize_attachments_single(weapons[weapon1]);
		weapon_num = 1;
	attachment_num = 1;
	for (var i = 0; i < arr.length; i += 3)
	{
		document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_bg").style.backgroundImage = "url('images/bg/attachments/" + zeroPad(arr[i], 10) + ".png')";
		document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_img").src = arr[i + 1];
		document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_txt").innerHTML = arr[i + 2];
		if (arr[i] == 0)
		{
			document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_img").style.opacity = 0.2;
		}
		else
		{
			document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_img").style.opacity = 1;
		}
		attachment_num++;
	}
	
	arr = randomize_attachments_single(weapons[weapon2]);
	weapon_num = 2;
	attachment_num = 1;
	for (var i = 0; i < arr.length; i += 3)
	{
		document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_bg").style.backgroundImage = "url('images/bg/attachments/" + zeroPad(arr[i], 10) + ".png')";
		document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_img").src = arr[i + 1];
		document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_txt").innerHTML = arr[i + 2];
		if (arr[i] == 0)
		{
			document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_img").style.opacity = 0.2;
		}
		else
		{
			document.getElementById("weapon" + weapon_num + "_attachment" + attachment_num + "_img").style.opacity = 1;
		}
		attachment_num++;
	}
}

function all_func()
{
	if (document.getElementById("all_check1").checked)
	{
		randomize_legend();
	}

	if (document.getElementById("all_check2").checked)
	{
		randomize_weapons();
	}

	if (document.getElementById("all_check3").checked)
	{
		randomize_attachments();
	}
}
