//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com [v1.0]

String.prototype.repeat = function(l){
	return new Array(l+1).join(this);
};

var questions = [
	'Is your density 8 or more times greater than the sun?',
	'Have you ever broken your foot?',
	'Ever purchased a small animal unexpectedly?',
	'Does your file backup system take more than a minute to explain?',
	'Do you eat sushi?',
	'Does your life jerk along, or when people watch your life, do they experience it in short, incongruous, seemingly unrelated clips, and are you male?',
	'Have you ever sent some poor bastard out on a crazy mission for your love?',
	'Have you ever done verbal battle in a Tavern?',
	'Has a strange note ever been left in your kitchen?',
	'Did the tavern use the spelling with the "e" at the end ("taverne")?',
	'Does your diet consist solely of vegetation?',
	'Would you taste good in a stew (to people who enjoy eating stuff like you)?',
	'Do you really exist, or are you more of a concept that only a select group of scholars consider real?',
	'Really?',
	'Are your features simultaneously loathed and exploited?',
	'Is there a wasted devotion somewhere in your life?',
	'Have you ever "flicked a malfunctioning lighter and blown the right eye into the back of your skull?"',
	'Do you ever struggle with why some people code using single quotes and others double quotes in a programming language where the two are interchangeable?',
	'Has anyone ever cast a spell on you?',
	'Did everything that just happened turn out to be you recounting the story to a shrink? (Wait, that\'s not really what happened.)'
	
];
var qs_idx = 0;

var proclivity = {};
proclivity['Thin as String/Scrawn/Shrimp/Dad - White Dwarf'] = [0,14];
proclivity['Champion - The Champion'] = [1,4];
proclivity['Pelgram - The Rabbit'] = [2];
proclivity['Computer Leon'] = [3];
proclivity['Man - The Only Man'] = [5,17];
proclivity['Princess - The Champion'] = [6,16];
proclivity['[Evil] Stepmother - The Champion, Part 2'] = [7,9,15];
proclivity['Tinkerton - Tinkerton'] = [8,15];
proclivity['Rabbit - The Rabbit'] = [10,11];
proclivity['Point - The Point'] = [12,13];
proclivity['Point - The Point'] = [1];


var answers = [];
var ans_idx = 0;

$(function () {

	var $d = $(document);
	
	$('body').append('<div id="column"></div><div id="dialog"></div>"');
	
	$('#column').append('<p>' + ('blah '.repeat(r(1000))) + '</p>');
	
	$('#column').append('<p><a id="invite" href="#"><img src="facebook.png" /></a></p>');
	
	$('#column').append('<p>' + ('blah '.repeat(r(1000))) + '</p>');
	
	$('#invite').click(next_question);
	

});

function yes () {
	answers[ans_idx++] = true;
}

function no () {
	answers[ans_idx++] = false;
}

function next_question (e) {
	e.preventDefault();
	if (questions[qs_idx]) {
		$('#dialog').dialog('destroy');
		$('#dialog').html('<p>' + questions[qs_idx] + '</p>');
		$('#dialog').dialog({
			autoOpen: true,
			width: 600,
			buttons: {
				"Yes": function (e) {
					yes();
					next_question(e);
				} ,
				"No": function (e) {
					no();
					next_question(e);
				} ,
				"Cancel": function() { 
					alert('well then, we will never know...');
					$(this).dialog("close"); 
				} 
			}
		});
	
	} else {
		results();
	}
	qs_idx++;
	
}

function results () {
	var youarelike = {};
	var character;
	for (px in proclivity) {
		character = proclivity[px];
		for (py = 0; py < character.length; py++) {
			if (answers[character[py]]) {
				if (youarelike[px]) {
					youarelike[px]++;
				} else {
					youarelike[px] = 1;
				}
			}
		}
	}
	
	var mostlike;
	var highest_score = 0;
	for (i in youarelike) {
		if (youarelike[i] > highest_score) {
			highest_score = youarelike[i];
			mostlike = i;
		}
	}
	
	alert ('you are ' + mostlike);
}

function r (range) {
	//return Math.floor(Math.random() * range));
	return range;
}
