// =================================================================
// ===== What Monster Are You? v0.1.0                          =====
// ===== Text by Bart Carroll 	                               =====
// ===== Concept and Javascript by Mark A Jindra               =====
// ===== (c) 2009 Wizards of the Coast LLC,                    =====  
// ===== a subsidiary of Hasbro, Inc. All Rights Reserved.	   =====	
// =================================================================

// Variables and Arrays 

dURL = window.location.href;
var time=new Date();
var year=time.getYear();
if (year < 2000) { year = year + 1900; }
 

var mwinner = 16;

var codehtml = "HTML Default";
var codebb = "BB Default";
var codetext = "Text Default";
var showwhat = "a";

var debugx = ""
var qtext = "-";
var Q = 0;
var tieq = 0;
var mtot = 0;
var qx = new Array(0,1,2,3,4,5,6,7,8,9);
var qlist = new Array();
var tx = new Array();
var montot = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

var questions = new Array(
"D&D is metal. Your thoughts?:2:I love metal!:5:I am also metal!:4",

"When is the best time to go on a monstrous rampage?:4:I'm a winter monster. Spilled blood looks best in snow.:2,7:I'm a summer monster. Sandy beaches are the best (place to eat people).:11,13:I'm a night monster. That's when monsters are at their scariest.:12:Everyone knows a thunderstorm is the best time to creep around.:6",

"When not devouring townfolk, how do I prefer to relax?:3:Throwing around the old pigskin ... or a rock.:3:With a picnic. Preferably attended by many juicy picknickers.:1,0:Call me a homebody. I love curling up with a good mystery.:13",

"Where is the best place for a lair?:3:Somewhere in the mountains. That way, I'll see the adventurers coming.:2,3:Under the mountains, where they'll have to work to find me!:9,0,1,5,8:Could I interest you in a nice piece of swamp?:14",

"What is best in this monstrous life?:5:To take possession of all I can! Mine! Mine!:15:I have but one goal&#58; To find my target. And to kill it. So I guess that's really two goals.:10:Whatever my master tells me.:4:To rule my clan. I love those guys.:12:I just like to eat shiny things. Is that so wrong?:5",

"Every monster can use an ally or two. Mine include:6:Harpies and hags (I have low standards).:9:Galeb duhr, because talking rocks are the best (or are possibly an anagram)!:3:Drow—c'mon, you know how cool they are.:11:Frost giants—someone big to watch my back.:7:Storm giants—someone really big to watch my back!:6:An imaginary mammoth that follows me around and that no one else can see.:2",

"Adventurers aside, my mortal enemies include:3:Mind flayers. I may be creepy, but those guys are seriously creepy.:15:Silver. Just saying.:12:Everyone.:14",

"When it comes to philosophy, I believe:3:The more eyes I have, the better it is to see you.:10,11:The more heads I have, the better it is to eat you.:8:The hotter my stomach, the better it is to cook you.:7",

"When it comes to evil intelligence,I believe:3:The more diabolical, the better. So study hard.:6,13,15:Please repeat the question. Slowly.:14:...?:10",

"What drips from my mouth?:4:Acid.:0:Poison.:4:Acidic poison.:1:What do you think? I'm a friggin' monster! The blood of many, many adventurers.:8,9"
);

var tieoneq = "How big of a monster am I?:4:Let's say normal sized. Still, you wouldn't want to mess with me.:5,14,15:Big. Larger than life.:0,1,2,3,4,12,13:Huge! I'm a monster, remember – rawr!:6,7,8,10,11:I will always be the tallest in the room.:9"

var tietwoq = "What kind of a monster am I?:5:I am a beast, a natural beast!:0,1,5,8:A magical creature, the likes of which you cannot imagine ... until you meet me.:6,9,13,15:Human in form, monstrous in power.:2,3,12,14:Mad wizards built me out of spare parts.:4,10:I emerged from the very elements. The Greek classical elements, I mean, not the periodic table.:7,11"

var monsters = new Array("Ankheg","Hive Queen Ant","Frost Giant","Stone Giant","Iron Golem","Rust Monster","Behir","Remorraz","Heroslayer Hydra","Adamantine Dragon","Retriever","Bebilith Demon","Werewolf Lord","Sphinx Mystery","Bullywug","Neogi");

var monsterblurb = new Array("Ankheg","Hive Queen Ant","Frost Giant","Stone Giant","Iron Golem","Rust Monster","Behir","Remorraz","Heroslayer Hydra","Adamantine Dragon","Retriever","Bebilith Demon","Werewolf Lord","Sphinx Mystery","Bullywug","Neogi");

var monsterquote = new Array(
"Loathsome insects with a taste for bipedal prey, ankhegs burst from hiding to seize the unwary. An ankheg moves with great stealth, but might be detected by the faint whiff of acid dripping from its twitching mandibles.",
"In addition to birthing future generations, the hive queen directs the activities of her brood with a ruthless and single-minded purpose.",
"Frost giants live in bitter northern lands and among frigid mountains where they subsist by hunting and raiding. They are brutal, superstitious, and murderous creatures among whom only might makes right.",
"Atop forbidding peaks and in the deep reaches of mountain ranges dwell stone giants. These rockbound beings show little concern for smaller creatures, and they distrust strangers. A stone giant aroused to violence can be as startling and pitiless as an avalanche.",
"Infused with potent toxins, iron golems thunder toward foes and bash them into mush.",
"Rust monsters roam caverns and dungeons seeking metals to devour, making them a nightmare for any civilized creature dwelling underground.",
"The behir is a multilegged serpentine horror known for its fearsome lightning-spitting attack. Clever and voracious predators, behirs are deadly hunters from the time they are hatched. Shrewd and deadly, a behir is capable of running down prey and swallowing it whole.",
"Remorhazes use their intense internal heat to burrow through the ice and rock of their arctic homes, preying on any creatures they come across, even giants and dragons.",
"A creature out of legend, the heroslayer hydra earned its name from the heroes who fell to its fangs.",
"Adamantine dragons are tacticians that supplement their melee abilities with blasts of thunderous power. They can be found anywhere, but prefer to lair in huge underground caverns.",
"Retrievers are arachnoid monstrosities created by the primordials to seek out their foes with unerring accuracy. As constructs, they are built to follow the directions of powerful creatures.",
"Originating in the web-strewn realm of Lolth, the spiderlike bebiliths crawl through the Abyss and other planes, hunting and killing for the joy of it.",
"At the center of many lycanthropic clans, a werewolf lord calls the shots. The most feared of its kind, the werewolf lord is larger, stronger, and smarter than a werewolf and is a vicious adversary.",
"Some say the gods created sphinxes to test the mettle of heroes or to protect sacred locations; others say they are primordials birthed in a time before gods. These creatures amuse themselves by asking their prey for answers to riddles or for obscure bits of lore and interpretations of prophecy.",
"Nasty and noisome, bullywugs turn any swamp they inhabit into a dismal echo of the land it once was, so much so that even they view their existence as innately askew. Bullywugs sense this \"wrongness\" as a heightened paranoia, a feeling that everyone is out to kill them.",
"Neogi see the world in terms of ownership. The strong rule and possess the weak. Slavery and trade form the foundations of neogi culture, making neogi reliable merchants to devils, giants, drow, and other dark forces."
);

var complink = "http://ww2.wizards.com/dnd/insider/monster.aspx?id="; 
var complink2 = "http://www.wizards.com/dndinsider/compendium/database.aspx?searchterm=";
var monsterids = new Array("1","2","3","4","5","6","7","8","9","10","10","10","10","10","10","10"); 
var catlink = "http://www.wizards.com/default.asp?x=products/dndacc/" 
var monstersource = new Array("9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017","9780786951017"); 

// =================================================================

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
	rnd.seed = (rnd.seed*9301+49297) % 233280;
	return rnd.seed/(233280.0);
};

function rand(number) {
	return Math.round(rnd()*number) ;
};

// =================================================================

function theyear () {
	document.getElementById("thisyear").innerHTML = year; 
}

// =================================================================

shufflequestions();

function shufflequestions () {
	var qs = qx.length
	for (z=0; z<qs; z++){
		getq = rand(qx.length - 1);
		qlist[z] = qx[getq]	
		qx.splice(getq, 1);
	}
	
		gett = rand(2 - 1);
		tx[0] = gett	
		if (gett == 0) {
			tx[1] = 1	
		} else {
			tx[1] = 0	
		}
 
	 
	
}

// =================================================================

function question () {
 
	document.getElementById("tally").innerHTML = "<a href='javascript:nextquestion()'><img id='nextimg' src='images/next.jpg' border='0'/></a>";
 
 
	
	var xanswer = new Array();
	var xpoints = new Array();	
	
	qnbr = qlist[Q];
	qsplit = questions[qnbr] 
	qsplit = qsplit.split(":")
	qslen = qsplit.length

	xanswer[0] = qsplit[0] // question
	xanswer[1] = qsplit[1] // number of questions

 	document.getElementById("question").innerHTML = "<span class='size18'>" + qsplit[0] + "</span>"; 	
	
	var xx = new Array();
	var xlist = new Array();

	var xlisttemp = "0";
	
	for (ax=1; ax<xanswer[1]; ax++){	
		xlisttemp += "," + ax
	}

	xx = xlisttemp.split(",")
	xlist = xlisttemp.split(",")
	var xs = xx.length

	for (z=0; z<xs; z++){
		getx = rand(xx.length - 1);
		xlist[z] = xx[getx]	
		xx.splice(getx, 1);
	}

	var xdisp = "";
	
	for (ax=0; ax<xanswer[1]; ax++){

		var aa = (xlist[ax] * 2) + 2

		xanswer[aa] = qsplit[aa]
		xanswer[aa + 1] = qsplit[aa + 1]

	    xdisp += "<img src='images/q_line.jpg' border='0' height='1px' width='560px'/><br/><img src='images/clear.gif' border='0' height='10px' width='560px'/><br/>"

		xdisp += "<table border='0' width='100%' cellpadding='2' cellspacing='2'><tr><td valign='top' width='20px'><input type='radio' name='qst' value='" + xanswer[aa + 1] + "'></td><td valign='top'><span style='font-weight: bold; color: #4a2500' class='size15'>" + xanswer[aa] + "</span></td></tr></table><img src='images/clear.gif' border='0' height='10px' width='600px'/><br/>" 

	}

	xdisp += "<img src='images/q_line.jpg' border='0' height='1px' width='560px'/><br/><img src='images/clear.gif' border='0' height='10px' width='560px'/>"
	
 	document.getElementById("answers").innerHTML = xdisp	

}

// =================================================================

function nextquestion () {
	chosen = ""
	len = document.form1.qst.length
	for (i = 0; i <len; i++) {
		if (document.form1.qst[i].checked) {
			chosen = document.form1.qst[i].value
		}
	}
	if (chosen == "") {
		alert("Please Choose an Answer")
	}
	else {

 

		var chx = new Array();
		chx = chosen.split(",")
		cxx = chx.length
		for (c = 0; c <cxx; c++) {
			if (Q < 9) {
				montot[chx[c]] += 1; 	
			} else {
				if (montot[chx[c]] == mtot) {				
					montot[chx[c]] += 1; 	
				}
			}
		}
 	
		
		if (Q < 9) {
			Q += 1
			question();
		} else {
			checkwinners();
		}
	}
} 


// =================================================================

 
function checkwinners () {
		mwinner = 16;
		var mtie = 0;
		mtot = 0;
		for (m = 0; m<=15; m++) {
			if (montot[m] == mtot) {
			 	mtie = 1; 
			}
			if (montot[m] > mtot) {
			 	mwinner = m;
				mtot = montot[m];
			 	mtie = 0; 
			}
		}		

		if (mtie == 0) {
			// we have a winner
 

 	
			showmonster (mwinner);
		} else {
			// we have a tie
			if (tieq == 2) {
				tieq = 3
 
				tierandom ();
			}
			if (tieq == 1) {
				tieq = 2
 
				ties (tx[1]);
			}
			if (tieq == 0) {
				tieq = 1
 
				ties (tx[0]);
			}
		}
		
 	

}










// =================================================================

function switchanswer () {
	document.getElementById("questionback").style.visibility = "hidden";	
	document.getElementById("question").style.visibility = "hidden";
	document.getElementById("answers").style.visibility = "hidden";	
	document.getElementById("tally").style.visibility = "hidden";	
	document.getElementById("answerback").style.visibility = "visible";	
	document.getElementById("monsterimg").style.visibility = "visible";	
	document.getElementById("monsterblurb").style.visibility = "visible";	
	document.getElementById("bbhtml").style.visibility = "visible";	
	document.getElementById("answerback").style.visibility = "visible";	
	document.getElementById("again").style.visibility = "visible";	
	document.getElementById("image").style.visibility = "visible";		

	document.getElementById("source").style.visibility = "visible";		

    document.getElementById("exp").style.visibility = "visible";		
    document.getElementById("comp").style.visibility = "visible";		
  	document.getElementById("dnd").style.visibility = "visible";	
	
	if (showwhat = "a") {
	  	document.getElementById("a").style.visibility = "visible";	
	} else {
	  	document.getElementById("an").style.visibility = "visible";	
	}
			
	return;
}	


// =================================================================
	
function ties (txxx) {

if (txxx == 0) {

	document.getElementById("tally").innerHTML = "<a href='javascript:nextquestion ()'><img id='nextimg' src='images/next.jpg' border='0'/></a>";
 
 
	
	var xanswer = new Array();
	var xpoints = new Array();	

	qsplit = tieoneq; 
	qsplit = qsplit.split(":")
	qslen = qsplit.length

	xanswer[0] = qsplit[0] // question

	xanswer[1] = qsplit[1] // number of questions

 	document.getElementById("question").innerHTML = "<span class='size18'>" + qsplit[0] + "</span>"; 	
	
	var xx = new Array();
	var xlist = new Array();

	var xlisttemp = "0"
		for (az=1; az<xanswer[1]; az++){	
		xlisttemp += "," + az
	}

	xx = xlisttemp.split(",")
	xlist = xlisttemp.split(",")
	var xs = xx.length

	for (z=0; z<xs; z++){
		getx = rand(xx.length - 1);
		xlist[z] = xx[getx]	
		xx.splice(getx, 1);
	}

	var xdisp = "";
	
	for (az=0; az<xanswer[1]; az++){

		var aa = (xlist[az] * 2) + 2

		xanswer[aa] = qsplit[aa]
		xanswer[aa + 1] = qsplit[aa + 1]

	    xdisp += "<img src='images/q_line.jpg' border='0' height='1px' width='560px'/><br/><img src='images/clear.gif' border='0' height='10px' width='560px'/><br/>"

		xdisp += "<table border='0' width='100%' cellpadding='2' cellspacing='2'><tr><td valign='top' width='20px'><input type='radio' name='qst' value='" + xanswer[aa + 1] + "'></td><td valign='top'><span style='font-weight: bold; color: #4a2500' class='size15'>" + xanswer[aa] + "</span></td></tr></table><img src='images/clear.gif' border='0' height='10px' width='600px'/><br/>" 

	}

	xdisp += "<img src='images/q_line.jpg' border='0' height='1px' width='560px'/><br/><img src='images/clear.gif' border='0' height='10px' width='560px'/>"
	
 	document.getElementById("answers").innerHTML = xdisp	
	
} else {

	document.getElementById("tally").innerHTML = "<a href='javascript:nextquestion ()'><img id='nextimg' src='images/next.jpg' border='0'/></a>";
 
	var xanswer = new Array();
	var xpoints = new Array();	

	qsplit = tietwoq; 
	qsplit = qsplit.split(":")
	qslen = qsplit.length

	xanswer[0] = qsplit[0] // question

	xanswer[1] = qsplit[1] // number of questions

 	document.getElementById("question").innerHTML = "<span class='size18'>" + qsplit[0] + "</span>"; 	
	
	var xx = new Array();
	var xlist = new Array();

	var xlisttemp = "0"
	
	for (aq=1; aq<xanswer[1]; aq++){	
		xlisttemp += "," + aq
	}

	xx = xlisttemp.split(",")
	xlist = xlisttemp.split(",")
	var xs = xx.length

	for (z=0; z<xs; z++){
		getx = rand(xx.length - 1);
		xlist[z] = xx[getx]	
		xx.splice(getx, 1);
	}

	var xdisp = "";
	
	for (aq=0; aq<xanswer[1]; aq++){

		var aa = (xlist[aq] * 2) + 2

		xanswer[aa] = qsplit[aa]
		xanswer[aa + 1] = qsplit[aa + 1]

	    xdisp += "<img src='images/q_line.jpg' border='0' height='1px' width='560px'/><br/><img src='images/clear.gif' border='0' height='10px' width='560px'/><br/>"

		xdisp += "<table border='0' width='100%' cellpadding='2' cellspacing='2'><tr><td valign='top' width='20px'><input type='radio' name='qst' value='" + xanswer[aa + 1] + "'></td><td valign='top'><span style='font-weight: bold; color: #4a2500' class='size15'>" + xanswer[aa] + "</span></td></tr></table><img src='images/clear.gif' border='0' height='10px' width='600px'/><br/>" 

	}

	xdisp += "<img src='images/q_line.jpg' border='0' height='1px' width='560px'/><br/><img src='images/clear.gif' border='0' height='10px' width='560px'/>"
	
 	document.getElementById("answers").innerHTML = xdisp	

}

}
 
// =================================================================

function tierandom () {
 
	// select the monsters that are tied
	// roll randomly for a winner
		var mrx = ""
		var monroll = "";
		for (r = 0; r < 16; r++) {
			if (montot[r] == mtot) {				
				monroll += mrx + r 
				var mrx = ","
			}
		}	

 
		
		mroll = monroll.split(",")
		getroll = rand(mroll.length - 1);
		mwin = mroll[getroll]	

 

		montot[mwin] += 1;

	 	nextquestion ();		


}

// =================================================================
 
function showmonster (mmm) {

 	document.getElementById("monsterimg").innerHTML = "<img src='images/titles/" + mmm + ".jpg' border='0'/>";

 	document.getElementById("image").innerHTML = "<img src='images/splash/" + mmm + ".jpg' border='0'/>";

 	document.getElementById("monsterblurb").innerHTML = "<span class='size12'><b>" + monsterquote[mmm] + "</b></span>"; 	

	switchanswer();	
	setcode(mmm);
	dispcode('html');
} 


// =================================================================

function setcode (smmm) {

	codehtml = '<p align="center"><img src="http://www.wizards.com/dnd/monster/images/banners/' + smmm + '.jpg"/><br/><a href="http://www.wizards.com/dnd/" target="_blank">D&D Home Page</a> - <a href="http://www.wizards.com/dnd/monster/index.asp" target="_blank">What Monster Are You?</a> - <a href="' + complink2 + monsters[mwinner] + '" target="_blank">D&amp;D Compendium</a></p>';

	codedetailedhtml = '<p align="center"><img src="http://www.wizards.com/dnd/monster/images/banners/' + smmm + '.jpg"/>' + monsterblurb[mwinner] + '<a href="http://www.wizards.com/dnd/" target="_blank">D&D Home Page</a> - <a href="http://www.wizards.com/dnd/monster/index.asp" target="_blank">What Monster Are You?</a> - <a href="' + complink2 + monsters[mwinner] + '" target="_blank">D&amp;D Compendium</a></p>';

	codebb = '[CENTER][IMG]http://www.wizards.com/dnd/monster/images/banners/' + smmm + '.jpg[/IMG]\n[URL="http://www.wizards.com/dnd/"]D&D Home Page[/URL] - [URL="http://www.wizards.com/dnd/monster/index.asp"]What Monster Are You?[/URL] - [URL="' + complink2 + monsters[mwinner] + '"]D&D Compendium[/URL][/CENTER]';

	if (smmm == "0" || smmm == "4" || smmm == "9") {
		codetext = "I am an " + monsters[smmm] + " ... \"" + monsterquote[smmm] + "\"" ;
		showwhat = "an";
	} else {
		codetext = "I am a " + monsters[smmm] + " ... \"" + monsterquote[smmm] + "\"" ;
	}
	
	codeimage = 'http://www.wizards.com/dnd/monster/images/banners/' + smmm + '.jpg' ;

}

// =================================================================

function rawr () {
 
	
 	document.getElementById("monsterimg").innerHTML = "<a href='" + complink + "' target='_blank'><img src='images/titles/16x.jpg' border='0'/></a>";

 	document.getElementById("image").innerHTML = "<a href='" + catlink + "' target='_blank'><img src='images/splash/16x.jpg' border='0'/></a>";

 	document.getElementById("monsterblurb").innerHTML = "<img src='images/rawr.jpg'/>"; 	

	switchanswer();	

	codehtml = '<p align="center"><img src="http://www.wizards.com/dnd/monster/images/banners/16x.jpg"/><br/><a href="http://www.wizards.com/dnd/" target="_blank">D&D Home Page</a> - <a href="http://www.wizards.com/dnd/monster/index.asp" target="_blank">What Monster Are You?</a> - <a href="' + complink2 + '" target="_blank">D&amp;D Compendium</a></p>';

	codedetailedhtml = '<p align="center"><img src="http://www.wizards.com/dnd/monster/images/banners/16x.jpg"/>' + "I'm a Monster RAWR!" + '<a href="http://www.wizards.com/dnd/" target="_blank">D&D Home Page</a> - <a href="http://www.wizards.com/dnd/monster/index.asp" target="_blank">What Monster Are You?</a> - <a href="' + complink2 + '" target="_blank">D&amp;D Compendium</a></p>';

	codebb = '[CENTER][IMG]http://www.wizards.com/dnd/monster/images/banners/16x.jpg[/IMG]\n[URL="http://www.wizards.com/dnd/"]D&D Home Page[/URL] - [URL="http://www.wizards.com/dnd/monster/index.asp"]What Monster Are You?[/URL] - [URL="' + complink2 + '"]D&D Compendium[/URL][/CENTER]';

	codetext = "I'm a Monster RAWR!" ;

	codeimage = 'http://www.wizards.com/dnd/monster/images/banners/16x.jpg' ;	
	
	showwhat = "a";
	
	
	dispcode('html');	
	
	var d = document.getElementById("image");

	d.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="410" height="273" id="loadersceen_1" align="left"><param name="movie" value="gnome_tieflingLoader.swf"/><param name="quality" value="high"/><param name="bgcolor" value="#000000"/><param name="wmode" value="transparent"/><embed src="gnome_tieflingLoader.swf" quality="high" bgcolor="#000000" width="410" height="273" name="gnome_tieflingLoader" align="left" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent"></embed></object>';	
 
	
}

// =================================================================

function dispcode (ctype) {
	if (ctype == "html")
		{
			document.getElementById("codeword").innerHTML = "HTML";
			document.form1.classcode.value = codehtml;
		} 
	else if (ctype == "detailedhtml")
		{
			document.getElementById("codeword").innerHTML = "HTML";
			document.form1.classcode.value = codedetailedhtml;
		} 		
	else if (ctype == "bb")
		{
			document.getElementById("codeword").innerHTML = "BB code";
			document.form1.classcode.value = codebb;
		} 
	else if (ctype == "image")
		{
			document.getElementById("codeword").innerHTML = "image";
			document.form1.classcode.value = codeimage;
		} 
	else 
		{
			document.getElementById("codeword").innerHTML = "text";
			document.form1.classcode.value = codetext;
		} 
		
	if (showwhat == "an") {
	  	document.getElementById("an").style.visibility = "visible";	
	  	document.getElementById("a").style.visibility = "hidden";	
	} else {
	  	document.getElementById("an").style.visibility = "hidden";	
	  	document.getElementById("a").style.visibility = "visible";	
	}			
		
		
}

// =================================================================

function playagain () {
	window.location.reload( true );
}



// =================================================================

// Debug

function debugdisp (dd) {
	 
}

function debugstart () {

 

}
 