

function Quiz_Transformers()
{
	this.leanings = {'current':{},'currentmax':{},'max':{}};
	
	this.question = -1;
	quiz.transformers = this;
	
	this.questions = [
		'nasty_food','band','covet','stay_in_touch','supervise','crowded_flight','engadget','false_promise','responsibility','inconvenient_truth','news_source','meetings','envy_friends',
		'soap','communicate','fav_jokes','halo','deadlines','make_cry','ladiesman217','goals','spider'
	];
	
	
	this.nextQuestion = function()
	{
		++this.question;
		if(this.question < this.questions.length)
			return this.showQuestion(this.question);
		return this.showResult();
	}
	
	this.showQuestion = function(iQuestionID)
	{
		var oQuestion = quiz.questions[this.questions[this.question]];
		
		var sContent = '';
		sContent += '<div style="text-align: left; font-family: Verdana, Arial, Sans-Serif; font-size: 12px; line-height: 18px; padding: 17px 20px;">';
		sContent += '<div style="color: rgb(210,19,15); font-weight: bold;">Question '+(this.question+1)+' of '+this.questions.length+'</div>';
		sContent += '<p>'+oQuestion.html+'</p>';
		sContent += '<table class="format" cellspacing="0">';
		for(var i = 0; i < oQuestion.responses.length; ++i)
		{
			sContent += '<tr>';
			sContent += '<td style="text-align: right; padding-right: 8px; padding-top: 5px;">'+(i+1)+'.</td>';
			sContent += '<td style="padding-top: 5px;"><span class="link" onclick="quiz.transformers.submitResponse('+i+');">'+oQuestion.responses[i].html+'</span></td>';
			sContent += '</tr>';
		}
		sContent += '</table>';
		sContent += '</div>';
		
		$('quiz_content').innerHTML = sContent;
	}
	this.showResult = function()
	{
		
		var sContent = '';
		
		
		var percent = {}
		percent.evil = Math.round(this.getLeaning('current','evil') / this.getLeaning('max','evil') * 100);
		percent.leader = Math.round(this.getLeaning('current','leader') / this.getLeaning('max','leader') * 100);
		percent.trendy = Math.round(this.getLeaning('current','trendy') / this.getLeaning('max','trendy') * 100);
		
		var evil = percent.evil > 50;
		var character;
		if(percent.leader > 65) character = evil?'megatron':'optimus';
		else if(percent.trendy > 50) character = evil?'starscream':'jazz';
		else character = evil?'frenzy':'bumblebee';
		
		var sURI = '/quiz/transformers/results/'+character+'/'+percent.evil+'/'+percent.leader+'/'+percent.trendy;
		
		window.document.location.href = sURI;
	}
	this.submitResponse = function(iSubmittedResponseID)
	{
		var oResponse = null;
		var maxleanings = {};
		
		for(var iResponseID in quiz.questions[this.questions[this.question]].responses)
		{
			var oResponse = quiz.questions[this.questions[this.question]].responses[iResponseID];
			
			for(var sLeaning in oResponse.leanings)
			{
				if(!defined(maxleanings[sLeaning]) || oResponse.leanings[sLeaning] > maxleanings[sLeaning])
					maxleanings[sLeaning] = oResponse.leanings[sLeaning];
				
				if(iResponseID == iSubmittedResponseID)
					this.addLeaning('current', sLeaning, oResponse.leanings[sLeaning]);
			}
		}
		
		for(var sLeaning in maxleanings)
			this.addLeaning('currentmax', sLeaning, maxleanings[sLeaning]);
		
		var iCurrentEvil = this.getLeaning('current','evil');
		var iCurrentMaxEvil = this.getLeaning('currentmax','evil');
		var iMaxEvil = this.getLeaning('max','evil');
		if(iMaxEvil > 0)
		{
			var fRunningEvilPercent = iCurrentEvil / iCurrentMaxEvil;
			fRunningEvilPercent -= 0.5;
			fRunningEvilPercent *= (iCurrentMaxEvil / iMaxEvil);
			fRunningEvilPercent += 0.5;
			this.updateBar(fRunningEvilPercent);
		}
		
		this.nextQuestion();
	}
	
	this.updateBar = function(fPercent)
	{
		iPixels = Math.floor(354 * fPercent) - 177;
		if(iPixels > 0)
		{
			$('quiz_inner_blue_bar').style.display = 'none';
			$('quiz_inner_red_bar').style.width = (iPixels + 14)+'px';
			$('quiz_inner_red_bar').style.display = '';
		}
		else if(iPixels < 0)
		{
			$('quiz_inner_blue_bar').style.width = (-1 * iPixels + 14)+'px';
			$('quiz_inner_red_bar').style.display = 'none';
			
			$('quiz_inner_blue_bar').style.display = '';
		}
		else
		{
			$('quiz_inner_blue_bar').style.display = 'none';
			$('quiz_inner_red_bar').style.display = 'none';
		}
	}
	
	this.addLeaning = function(type,leaning,value)
	{
		if(!defined(this.leanings[type][leaning])) this.leanings[type][leaning] = value;
		else this.leanings[type][leaning] += value;
	}

	this.getLeaning = function(type,leaning)
	{
		if(!defined(this.leanings[type])) return 0;
		if(!defined(this.leanings[type][leaning])) return 0;
		return this.leanings[type][leaning];
	}
	
	
// Total Up maximum possible leanings
//	for(var i in this.questions)
	for(var i=0; i < this.questions.length; ++i)
	{
		var maxleanings = {};
		
		for(var iResponseID in quiz.questions[this.questions[i]].responses)
		{
			var oResponse = quiz.questions[this.questions[i]].responses[iResponseID];
			
			for(var sLeaning in oResponse.leanings)
			{
				if(!defined(maxleanings[sLeaning]) || oResponse.leanings[sLeaning] > maxleanings[sLeaning])
					maxleanings[sLeaning] = oResponse.leanings[sLeaning];
			}
			
		}
		
		for(var sLeaning in maxleanings)
			this.addLeaning('max', sLeaning, maxleanings[sLeaning]);
	}
	
	this.updateBar(0.5);
	this.nextQuestion();
	return this;
}

// questions
quiz.addQuestion('crowded_flight','You are on a crowded flight. You have a horrible stomach ache and need to fart. The bathroom line is very long. What do you do?');
	quiz.addResponse('Wait in line.');
	quiz.addResponse('Blast one out - be loud and proud!');
		quiz.addLeaning('evil',2);
	quiz.addResponse('Silent but deadly is my preferred method.');
		quiz.addLeaning('evil',2);
	quiz.addResponse('Fart and then blame your neighbor.');
		quiz.addLeaning('evil',3);

quiz.addQuestion('inconvenient_truth','An Inconvenient Truth is');
	quiz.addResponse('An important documentary about global warming.');
	quiz.addResponse('Al Gore&#8217;s exaggerations and lies.');
		quiz.addLeaning('evil',2);
	quiz.addResponse('Realizing that what you did in Vegas did not stay in Vegas.');
		quiz.addLeaning('evil',2);

quiz.addQuestion('covet','What do you most want to own?');
	quiz.addResponse('iPhone');
		quiz.addLeaning('evil',2);
	quiz.addResponse('iRaq');
		quiz.addLeaning('evil',3);
	quiz.addResponse('iRan');
		quiz.addLeaning('evil',3);
	quiz.addResponse('iDon&#8217;t (want any of them)');

quiz.addQuestion('envy_friends','Do you envy friends who own expensive things?');
	quiz.addResponse('Very much so');
		quiz.addLeaning('evil',3);
	quiz.addResponse('Just a tad');
		quiz.addLeaning('evil',2);
	quiz.addResponse('No, that is superficial');
	quiz.addResponse('Hey, all my friends envy me!');
		quiz.addLeaning('evil',3);

quiz.addQuestion('fav_jokes','What type of jokes do you like the best?');
	quiz.addResponse('Play on words and puns');
	quiz.addResponse('Childrens jokes');
	quiz.addResponse('Derogatory jokes (Yo Mama, Racial, Blonde, etc)');
		quiz.addLeaning('evil',2);
	quiz.addResponse('Practical jokes');
		quiz.addLeaning('evil',2);

quiz.addQuestion('nasty_food','Have you ever spit or put something nasty in somebody else&#8217;s food?');
	quiz.addResponse('Yes');
		quiz.addLeaning('evil',3);
	quiz.addResponse('No');
	quiz.addResponse('No, but I thought about it');
		quiz.addLeaning('evil',2);
	quiz.addResponse('Not yet&#8230;');
		quiz.addLeaning('evil',2);

quiz.addQuestion('make_cry','Have you ever said something mean enough to make someone cry?');
	quiz.addResponse('Yes, but they deserved it');
		quiz.addLeaning('evil',3);
	quiz.addResponse('Yes, but I aplogized afterwards');
		quiz.addLeaning('evil',1);
	quiz.addResponse('No, but I wanted to');
		quiz.addLeaning('evil',2);
	quiz.addResponse('Never');

quiz.addQuestion('false_promise','Have you ever made a promise that you had no intention of keeping?');
	quiz.addResponse('Yes');
		quiz.addLeaning('evil',3);
	quiz.addResponse('No');
	quiz.addResponse('Yes, but never again. I promise!');
		quiz.addLeaning('evil',2);

quiz.addQuestion('spider','When you find a spider in your house, you:');
	quiz.addResponse('Kill it immediately');
		quiz.addLeaning('evil',2);
	quiz.addResponse('Move it outside');
	quiz.addResponse('Ignore it');
	quiz.addResponse('Put it in a jar to watch it suffocate');
		quiz.addLeaning('evil',3);

quiz.addQuestion('band','If you were with a rock band, who would you most want to be?');
	quiz.addResponse('Lead singer');
		quiz.addLeaning('leader',3);
	quiz.addResponse('Guitarist');
		quiz.addLeaning('leader',2);
	quiz.addResponse('Drummer');
		quiz.addLeaning('leader',1);
	quiz.addResponse('Keyboards');
		quiz.addLeaning('leader',1);
	quiz.addResponse('Groupie');

quiz.addQuestion('supervise','Do you like to manage or supervise others?');
	quiz.addResponse('Yes');
		quiz.addLeaning('leader',3);
	quiz.addResponse('Sometimes');
		quiz.addLeaning('leader',2);
	quiz.addResponse('No');

quiz.addQuestion('responsibility','How do you feel when given an important responsibility?');
	quiz.addResponse('Overwhelmed');
	quiz.addResponse('Undeserving');
		quiz.addLeaning('leader',1);
	quiz.addResponse('Excited');
		quiz.addLeaning('leader',3);
	quiz.addResponse('Deserving');
		quiz.addLeaning('leader',3);

quiz.addQuestion('meetings','In important meetings you:');
	quiz.addResponse('Listen and take notes');
		quiz.addLeaning('leader',1);
	quiz.addResponse('Zone out');
	quiz.addResponse('Surf the web on your phone');
	quiz.addResponse('Lead the discussion');
		quiz.addLeaning('leader',3);

quiz.addQuestion('communicate','How do you communicate with others?');
	quiz.addResponse('I don&#8217;t communicate much');
	quiz.addResponse('I listen with one ear and speak distractedly');
	quiz.addResponse('I&#8217;m pretty focused and communicate well');
		quiz.addLeaning('leader',3);
	quiz.addResponse('I&#8217;m very attentive and clear');
		quiz.addLeaning('leader',3);

quiz.addQuestion('deadlines','Your work / chores are piling up. There&#8217;s no way you can finish everything. What do you do?');
	quiz.addResponse('Pick the ones I can finish quickly');
		quiz.addLeaning('leader',2);
	quiz.addResponse('Pick the most pressing tasks');
		quiz.addLeaning('leader',3);
	quiz.addResponse('Pick the most difficult tasks');
	quiz.addResponse('Pick the ones I&#8217;m most interested in doing');

quiz.addQuestion('goals','Do you set goals for yourself?');
	quiz.addResponse('Not really');
	quiz.addResponse('Sometimes');
		quiz.addLeaning('leader',1);
	quiz.addResponse('I have some general goals');
		quiz.addLeaning('leader',2);
	quiz.addResponse('Yes - I have clear goals');
		quiz.addLeaning('leader',3);

quiz.addQuestion('engadget','What is Engadget?');
	quiz.addResponse('A popular weblog');
		quiz.addLeaning('trendy',2);
	quiz.addResponse('A cartoon');
		quiz.addLeaning('trendy',0);
	quiz.addResponse('<span style="font-weight: normal; color: rgb(204,0,0);">Did you mean: </span><span style="font-weight: bold; font-style: italic; color: rgb(0,0,204);">A Gadget?</span>');
		quiz.addLeaning('trendy',0);

quiz.addQuestion('news_source','Which source of news best describes where you go to stay up to date?');
	quiz.addResponse('Online news sites');
		quiz.addLeaning('trendy',2);
	quiz.addResponse('Blogs / RSS Feeds');
		quiz.addLeaning('trendy',3);
	quiz.addResponse('Digg or user rated news');
		quiz.addLeaning('trendy',3);
	quiz.addResponse('Newspaper / Magazines');
	quiz.addResponse('Radio / Television');

quiz.addQuestion('stay_in_touch','What is the main way you stay in touch with friends?');
	quiz.addResponse('Letters');
	quiz.addResponse('Phone (voice)');
	quiz.addResponse('Email, IM, Text (sms)');
		quiz.addLeaning('trendy',2);
	quiz.addResponse('Social Network, Blogs, Twitter');
		quiz.addLeaning('trendy',3);

quiz.addQuestion('soap','What does SOAP mean to you?');
	quiz.addResponse('An object used for cleaning');
	quiz.addResponse('Simple Object Access Protocol');
		quiz.addLeaning('trendy',2);
	quiz.addResponse('A 1970s sitcom');
	quiz.addResponse('Something you don&#8217;t want to drop in a public shower');

quiz.addQuestion('halo','What does HALO mean to you?');
	quiz.addResponse('A ring atop the heads of angels and saints');
	quiz.addResponse('A video game');
		quiz.addLeaning('trendy',2);
	quiz.addResponse('A common greeting');
	quiz.addResponse('High Altitude Low Opening');

quiz.addQuestion('ladiesman217','In the Transformers 2007 movie, Sam&#8217;s eBay name is LadiesMan217. The name&#8217;s significance is:');
	quiz.addResponse('Sam has had 217 girlfriends');
	quiz.addResponse('Michael Bay&#8217;s birthday is February 17.');
		quiz.addLeaning('trendy',1);
	quiz.addResponse('Sam created the eBay account on February 1, 2007.');
	quiz.addResponse('I have no idea');
