//Loads all the xml files needed

function LoadRSS(rssFeed) 
{
	try
	{
		if (window.ActiveXObject)
		{
			var errorHappendHere = "Check Browser and security settings";
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.load(rssFeed);
			}
		else if(window.XMLHttpRequest)
			{
			var errorHappendHere = "Error handling XMLHttpRequest request";
			var d = new XMLHttpRequest();
			d.open("GET", rssFeed, false);
			d.send(null);
			xmlDoc=d.responseXML;
		} else {
			var errorHappendHere = "Error.";
			xmlDoc = document.implementation.createDocument("","",null);
			xmlDoc.async=false;
			xmlDoc.load(rssFeed);
		}
	}	
	catch(e)
	{
	alert(errorHappendHere);
	}
}

LoadRSS("fwchar.xml");
var x = xmlDoc.getElementsByTagName("character");
var charList = [];
var i=0;
var view = 0;
var ae_ID;
var bf_ID;
var track = 100;


//goes to the next fitwit when the next button is clicked


function list()
{
	line0 = "<form name = \"form1\" id = \"form1\" action = \"\">";
	line1 = "<select name = \"view\" onchange = changeview()>";
	if (view === 0){
		line2 =	"<option value = 0 selected = \"selected\"> View all characters</option>";
	}
	else{
		line2 = "<option value = 0 > View all characters</option>";
	}
	if (view == 1){
		line3 = "<option value = 1 selected = \"selected\">View only Fitwits</option>";
	}
	else{
		line3 = "<option value = 1 > View only Fitwits</option>";
	}
	if (view == 2){
		line4 = "<option value = 2 selected = \"selected\">View only Nitwits</option>";
	}
	else{
		line4 = "<option value = 2 >View only Nitwits</option>";
	}
	line5 = "</select>";
	line6 = "<input type=\"button\" name=\"B2\" id=\"B2\" value=\"Change View\" onclick=\"changeview()\" />";
	document.getElementById('choices').innerHTML = line0+line1+line2+line3+line4+line5;
	
	//alert (view);
}

//shows all the appropriate thimbnails given the value of the type of view (all fitwits, only fitwits, only nitwits)
function choice()
{	
	for (a = 0; a < x.length; a++)
	{
		charList[a] = x[a].getElementsByTagName("char_name")[0].childNodes[0].nodeValue;	
	}

	htmlString = "";
	for (n = 0; n < charList.length; n++)
	{
		if ((view == 1 && parseFloat(x[n].getElementsByTagName("char_type")[0].childNodes[0].nodeValue) == 1) || (view == 2 && parseFloat(x[n].getElementsByTagName("char_type")[0].childNodes[0].nodeValue) === 0) || view === 0)
		{
			imgpath = "pngChar/thumb/"+x[n].getElementsByTagName("char_id")[0].childNodes[0].nodeValue+".png";
			htmlString = htmlString + ("<a class='greenback' id = 'charlist"+n+"' href = 'javascript:change("+n+");'>  <img class='listreg' src = \""+imgpath+"\" alt = \""+charList[n]+"\" width = \"50px\" height = \"50px\"></img></a>");
		}
	}
	document.getElementById("list").innerHTML= htmlString;
}

//changes the view when the drop down menu value is changed
function changeview()
{
	num = document.form1.view.selectedIndex;
	view = document.form1.view.options[num].value;
	choice();
	list();
}

function mychange(mysel){
	view = mysel;
	choice();
}

//lists all the view options available






//inner function (ignore)
function setCount()
{
	i = charList.length - 1;
	track = 100;
}

//inner function (ignore)
function isFW(z, a)
{
	line = "line";
	if (x[z].getElementsByTagName("char_type")[0].childNodes[0].nodeValue == 1 && a< track)
	{
		i = z;
		line = line + "a";
	}
	document.getElementById("test").innerHTML= line;
}

//inner function (ignore)
function isNW(z, a)
{
	if (x[z].getElementsByTagName("char_type")[0].childNodes[0].nodeValue === 0 && a< track){
		i = z;
	}
}

//testing function (ignore)
function vote()
{
	alert ("Thanks for voting for me! Check out the results for the rest of the crew!");
	location.href = "vote.html";
	
}

//returns a string of html to display the title and the image
function display1()
{
	var name;
	var picture;
	var id;
	
	name = x[i].getElementsByTagName("char_name")[0].childNodes[0].nodeValue;
	id = x[i].getElementsByTagName("char_type")[0].childNodes[0].nodeValue;
	
	picture = "<br><img src = \""+x[i].getElementsByTagName("character_pic_path")[0].childNodes[0].nodeValue+"\"></img>";
	
	if(id == 0) {// Nitwit
		document.getElementById("charname").innerHTML = "<h1 class=\"alt2\">"+name+"</h1>";
		document.getElementById("chartype").innerHTML = "Nitwit";
		document.getElementById("chartype").setAttribute("class", "nitwit")

	}else{
		document.getElementById("charname").innerHTML = "<h1 class=\"alt2\">"+name+"</h1>";
		document.getElementById("chartype").innerHTML = "Fitwit";
		document.getElementById("chartype").setAttribute("class", "fitwit")
	}
	document.getElementById("titleimage").innerHTML = picture;
	
}

//returns a string of html to display the description
function display2()
{
	var front_message;
	var id;
	
	front_message = x[i].getElementsByTagName("front_message")[0].childNodes[0].nodeValue;
	id = x[i].getElementsByTagName("char_type")[0].childNodes[0].nodeValue;
	
	//document.getElementById("description").innerHTML = front_message;
	
	if(id == 0) {// Nitwit
		document.getElementById("description").innerHTML = "<h2>"+front_message+"</p>";

	}else{
		document.getElementById("description").innerHTML = "<h2>"+front_message+"</p>";
	}
}

//returns a string o thml to display all the character information (back of the card)
function display3()
{
	
	var name;
	var picture;
	var front_message;
	var bf;
	var ae;
	var act1;
	var act2;
	var fat;
	var sugar;
	var rating;
	var back_message;
	var recipe01;
	var recipe02;
	var health_comment;
	var count;
	var temp;

	count = 0;
	recipe01 = recipe02 = "";
	
	bf_ID = x[i].getElementsByTagName("char_bfID")[0].childNodes[0].nodeValue;
	ae_ID = x[i].getElementsByTagName("char_aeID")[0].childNodes[0].nodeValue;
	
	bf_num = parseFloat(bf_ID); bf_num--;
	ae_num = parseFloat(ae_ID); ae_num--;
	
	name = x[i].getElementsByTagName("char_name")[0].childNodes[0].nodeValue + "<br>";
	picture = "<br><img src = \""+x[i].getElementsByTagName("character_pic_path")[0].childNodes[0].nodeValue+"\"></img>";
	front_message = "<br>"+x[i].getElementsByTagName("front_message")[0].childNodes[0].nodeValue;
	if (bf_num >= 0){
		bf = "<a href = 'javascript:change("+bf_num+");'>" + x[i].getElementsByTagName("char_bfName")[0].childNodes[0].nodeValue + "</a>";
	}else {
		bf = x[i].getElementsByTagName("char_bfName")[0].childNodes[0].nodeValue;
	}
	if ( ae_num >= 0){
		ae = "<a href = 'javascript:change("+ae_num+");'>" + x[i].getElementsByTagName("char_aeName")[0].childNodes[0].nodeValue + "</a>";
	}else{
		ae = x[i].getElementsByTagName("char_aeName")[0].childNodes[0].nodeValue;
	}
	temp = x[i].childNodes;
	childLength = temp.length;
	
	for (j=0; (j<childLength); j++)
	{
		if (x[i].childNodes[j].nodeName == "back_message")
		{
			back_message = x[i].childNodes[j].childNodes[0].nodeValue + "<br>";
		}
		
		if (x[i].childNodes[j].nodeName == "recipe_title")
		{
			back_message = x[i].childNodes[j].childNodes[0].nodeValue + "<br>";
		}
		
		if (x[i].childNodes[j].nodeName == "recipe_serving")
		{
			back_message = back_message + x[i].childNodes[j].childNodes[0].nodeValue + "<br>";
		}
		
		if (x[i].childNodes[j].nodeName == "recipe_p1")
		{
			var tempstr = x[i].childNodes[j].childNodes[0].nodeValue;
			var tempstr2 = tempstr.replace(/,/g,"<br>");
			//back_message = back_message + tempstr2 + "<br>";
			recipe01 = tempstr2 + "<br>";
		}
		
		if (x[i].childNodes[j].nodeName == "recipe_p2")
		{
			//back_message = back_message + x[i].childNodes[j].childNodes[0].nodeValue +"<br>";
			recipe02 = x[i].childNodes[j].childNodes[0].nodeValue +"<br>";
		}
	}
	count = 0;
	var type1;
	var type2;
	
	for (j=0; (j<childLength && count < 2); j++)
	{
		
		if ( x[i].childNodes[j].nodeName == "hobbies" || x[i].childNodes[j].nodeName == "sport" || x[i].childNodes[j].nodeName == "games" || x[i].childNodes[j].nodeName == "dance_move" || x[i].childNodes[j].nodeName == "hangouts" || x[i].childNodes[j].nodeName == "heroes" || x[i].childNodes[j].nodeName == "joke" || x[i].childNodes[j].nodeName == "joke_answer")
		{
			if (count === 0){
				type1 = x[i].childNodes[j].nodeName;
				act1 = x[i].childNodes[j].childNodes[0].nodeValue;
			}
			else{
				type2 = x[i].childNodes[j].nodeName;
				act2 = x[i].childNodes[j].childNodes[0].nodeValue;
			}
			count++;
		}
	
	
	}
	
	if(type1 == "dance_move"){type1 = "dance move";}
	if(type2 == "dance_move"){type2 = "dance move";}
	if(type1 == "joke_answer"){type1 = "answer";}
	if(type2 == "joke_answer"){type2 = "answer";}
	
	
	var fperc = (parseFloat(x[i].getElementsByTagName("fat")[0].childNodes[0].nodeValue)) * 1.75;
	var sperc = (parseFloat(x[i].getElementsByTagName("sugar")[0].childNodes[0].nodeValue)) * 1.75;
	
	fat = "<div style=\"height:15px; width:" + fperc + "px; background:#FBB316;\"></div>";
	sugar = "<div style=\"height:15px; width:" + sperc + "px; background:#AE519D;\"></div>";
	
	rating = parseFloat(x[i].getElementsByTagName("fitwit_rating")[0].childNodes[0].nodeValue);
	
	if (rating === 0)
	{
		rating = "<div><img src=\"../images/smiley0.jpg\" alt=\":>\" /></div>";
	}
	
	else if (rating == 1)
	{
		rating = "<div><img src=\"../images/smiley1.jpg\" alt=\":|\" /></div>";

	}
	else if (rating == 2)
	{
		rating = "<div<img src=\"../images/smiley2.jpg\" alt=\":(\" /></div>";
	}
	
	health_comment = x[i].getElementsByTagName("health_comment")[0].childNodes[0].nodeValue + "<br>";
	
	// TESTING
	
	document.getElementById("cd_friend").innerHTML = bf;
	document.getElementById("cd_enemy").innerHTML = ae;
	
	document.getElementById("cd_type1").innerHTML = type1;
	document.getElementById("cd_type2").innerHTML = type2;
	
	document.getElementById("cd_act1").innerHTML = act1;
	document.getElementById("cd_act2").innerHTML = act2;
	
	document.getElementById("cd_rating").innerHTML = rating;
	
	document.getElementById("cd_fat").innerHTML =	fat;
	document.getElementById("cd_sugar").innerHTML =	sugar;
	
	//document.getElementById("cd_health").innerHTML = health_comment;
}

//
function display()
{
	display1();
	display2();
	display3();
}

function next()
{
	if (i<x.length-1)
	{
	  var elmo = document.getElementById("charlist"+i);
	  elmo.setAttribute("class", "greenback");
	  i++;
	  elmo = document.getElementById("charlist"+i);
	  elmo.setAttribute("class", "on");
	  display();
	}
}

//goes to the previous fitwit when the previous button is clicked
function previous()
{
	if (i>0)
	{
	  var elmo = document.getElementById("charlist"+i);
	  elmo.setAttribute("class", "greenback");
	  i--;
	  elmo = document.getElementById("charlist"+i);
	  elmo.setAttribute("class", "on");
	  display();
	}
}

//start of the page, default view of the first character, showing all the fitwit choices
function start()
{
	display();
	choice(view);
	var elmo = document.getElementById("charlist"+0);
	elmo.setAttribute("class", "on");
	
	//list();
	
}

//changes the character information displayed

function change (y)
{
	var elmo = document.getElementById("charlist"+i);
	elmo.setAttribute("class", "greenback");
	
	i = y;
	display();
	
	elmo = document.getElementById("charlist"+y);
	elmo.setAttribute("class", "on");
	
}
