
var paragraphHeader = "\n<p class=\"liy-question\">" ;
var normalHeader = "Lost in Yosemite: The Game";

var numAnswered=0;
var curScore=0;
var curIdx=0;
var hintVisible=0;

// all the questions stored here
var easyQuestions = new Array();
var mediumQuestions = new Array();
var hardQuestions = new Array();
var evilQuestions = new Array();

var insults1 = new Array();
var insults2 = new Array();
var insults3 = new Array();
var insults4 = new Array();


var curQuestion;
var curQuestionSet;

var numEasyQuestionsToAsk = 10;
var numMediumQuestionsToAsk = 10;
var numHardQuestionsToAsk = 10;
var numEvilQuestionsToAsk = 5;
var totalQuestionsToAsk;

var pointsPerEasyQuestion = 1;
var pointsPerMediumQuestion = 1;
var pointsPerHardQuestion = 1;
var pointsPerEvilQuestion = 1;

var pointsPerHintedEasyQuestion = 0.5;
var pointsPerHintedMediumQuestion = 0.5;
var pointsPerHintedHardQuestion = 0.5;
var pointsPerHintedEvilQuestion = 0.5;

var highestPossibleScore;
var pointsPerAnswer;
var pointsPerHintedAnswer;

var allLevelsHighestPossibleScore;
var allLevelsTotalScore;

var timerCnt;
var timerInterval = 500;
var timeoutID;

var bonusURLs = new Array();
var bonusURLAnchorTexts = new Array();

var level = 1;

var originalHeader;
var originalBody;
var originalImage;

//******************************************
function StartGame()
{
	for ( var i=0  ;  i < document.difficultyLevel.level.length  ;  i++ )
	{
		if ( document.difficultyLevel.level[i].checked )
		{
			level = i+1;
			break;
		}
	}

	if ( level == 4 )
	{
		if ( ! confirm("Are you sure you want to try this? Level 4 has crushed the spirit of many a sturdy mountaineer.") )
			return;
	}


	contents = document.getElementById( "liy-header" );
	originalHeader = contents.innerHTML;
	contents = document.getElementById( "liy-body" );
	originalBody  = contents.innerHTML;
	contents = document.getElementById( "liy-image" );
	originalImage = contents.innerHTML;

	allLevelsHighestPossibleScore = 0;
	allLevelsTotalScore = 0;

	LoadInsults();

	var gastr = "/liy/start-reqlevel-" + level;
	urchinTracker(gastr);

	StartGameAtLevel( level );
}

//******************************************
function StartOver()
{
	contents = document.getElementById( "liy-header" );
	contents.innerHTML = originalHeader;
	contents = document.getElementById( "liy-body" );
	contents.innerHTML = originalBody;
	contents = document.getElementById( "liy-image" );
	contents.innerHTML = originalImage;
}

//******************************************
function ReplayGame()
{
	StartGameAtLevel( level + 1 );
}

//******************************************
function StartGameAtLevel( inLevel )
{
	var gastr = "/liy/start-level-" + inLevel;
	urchinTracker( gastr );

	level = inLevel;

	LoadQuestions();

	bonusURLs.length = 0;
	bonusURLAnchorTexts.length = 0;

	numAnswered=0;
	curScore=0;

	if ( level == 4 )
	{
		pointsPerAnswer = pointsPerEvilQuestion;
		pointsPerHintedAnswer = pointsPerHintedEvilQuestion;
		curQuestionSet = evilQuestions;
		totalQuestionsToAsk = numEvilQuestionsToAsk;
	}
	else if ( level == 3 )
	{
		pointsPerAnswer = pointsPerHardQuestion;
		pointsPerHintedAnswer = pointsPerHintedHardQuestion;
		curQuestionSet = hardQuestions;
		totalQuestionsToAsk = numHardQuestionsToAsk;
	}
	else if ( level == 2 )
	{
		pointsPerAnswer = pointsPerMediumQuestion;
		pointsPerHintedAnswer = pointsPerHintedMediumQuestion;
		curQuestionSet = mediumQuestions;
		totalQuestionsToAsk = numMediumQuestionsToAsk;
	}
	else
	{
		pointsPerAnswer = pointsPerEasyQuestion;
		pointsPerHintedAnswer = pointsPerHintedEasyQuestion;
		curQuestionSet = easyQuestions;
		totalQuestionsToAsk = numEasyQuestionsToAsk;
	}


//	totalQuestionsToAsk = numEasyQuestionsToAsk + numMediumQuestionsToAsk + numHardQuestionsToAsk + numEvilQuestionsToAsk ;
//	highestPossibleScore = (numEasyQuestionsToAsk * pointsPerEasyQuestion) + (numMediumQuestionsToAsk * pointsPerMediumQuestion) + (numHardQuestionsToAsk * pointsPerHardQuestion) + (numEvilQuestionsToAsk * pointsPerEvilQuestion);
	highestPossibleScore = pointsPerAnswer * totalQuestionsToAsk;
	allLevelsHighestPossibleScore += pointsPerAnswer * totalQuestionsToAsk;

	LoadNewQuestion();
}

//******************************************
function LoadQuestions()
{
	if ( easyQuestions.length < numEasyQuestionsToAsk )
		LoadEasyQuestions();
	if ( mediumQuestions.length < numMediumQuestionsToAsk )
		LoadMediumQuestions();
	if ( hardQuestions.length < numHardQuestionsToAsk )
		LoadHardQuestions();
	if ( evilQuestions.length < numEvilQuestionsToAsk )
		LoadEvilQuestions();

//	alert( "num easy questions  = " + easyQuestions.length );
}

//******************************************
function LoadNewQuestion()
{
	curIdx = Math.ceil( curQuestionSet.length * Math.random() ) - 1 ;
	curQuestion = curQuestionSet[curIdx];

	hintVisible = 0;

//curIdx=7;
//curQuestion = hardQuestions[curIdx];

	if ( curQuestion.moreInfoURL != "" && curQuestion.moreInfoAnchorText != "" )
	{
		bonusURLs[ bonusURLs.length ] = curQuestion.moreInfoURL;
		bonusURLAnchorTexts[ bonusURLAnchorTexts.length ] = curQuestion.moreInfoAnchorText;
	}


	LoadHeaderSection();

	LoadImageSection();

	LoadAnswersSection();
}

//******************************************
function LoadHeaderSection()
{
	stage = GetCurrentStage();

	var newContents = "<h1>" ;
	if ( numAnswered >= totalQuestionsToAsk )
		newContents += "Game Over";
	else if ( level == 4 )
		newContents += "<h1 class=\"h2-quiz-fiendish\">Level 4: You Have No Chance" ;
//		newContents += "<h1 class=\"h2-quiz-fiendish\">Level 4: Fiendishly Difficult" ;
	else if ( level == 3 )
		newContents = "<h1 class=\"h2-quiz-hard\">Level 3: Hard";
	else if ( level == 2 )
		newContents = "<h1 class=\"h2-quiz-medium\">Level 2: Medium";
	else
		newContents = "<h1 class=\"h2-quiz-easy\">Level 1: Easy";

	newContents += "</h1>";

	contents = document.getElementById( "liy-header" );
	contents.innerHTML = newContents;
}

//******************************************
function LoadAnswersSection()
{
	var newContents = paragraphHeader + curQuestion.question + "</p>";

	newContents += "\n<form class=\"liy-radio-button\">\n";

	for ( var i=0  ;  i < curQuestion.NumAnswers()  ;  ++i )
	{
		newContents += "<input type=\"radio\" name=\"guess\" value=\"" + i + "\" " + "onClick=\"OnAnswerChosen(" + i + ")\"" + ">" + curQuestion.GetAnswerText( i ) + "<br />\n";
	}

	newContents += "</form>\n";

	newContents += paragraphHeader ;
	if ( hintVisible == 1 )
		newContents += curQuestion.hint ;
	else
		newContents += "<a href=\"javascript:OnHintRequested()\">hint please</a>"  ;
	newContents += "</p>";

//	alert( newContents );

	contents = document.getElementById( "liy-body" );
	contents.innerHTML = newContents;
}

//******************************************
function LoadImageSection()
{
	var newContents = "<img src=\"" + curQuestion.image + "\"" +  "class=\"liy-image\" " + curQuestion.imageSize + ">" ;
	var contents = document.getElementById( "liy-image" );
	contents.innerHTML = "" + newContents + "";

//	contents.childNodes[0].nodeValue = newContents;
//	contents.childNodes[0].data = newContents;

//	document.getElementById( "liy-image" ).innerHTML = newContents;
}

//******************************************
function OnRequestNextQuestion()
{
	if ( numAnswered >= totalQuestionsToAsk )
		DisplayFinalScore();
	else
	{
//		UpdateLevelSettings();
		LoadNewQuestion();
	}
}

//******************************************
function OnAnswerChosen( answerIdx )
{
//	alert( "you chose " + answerIdx + "!" );

	++numAnswered;
	isCorrect = curQuestion.IsAnswerCorrect( answerIdx );
	if ( isCorrect )
	{
		if ( hintVisible )
		{
			curScore += pointsPerHintedAnswer;
			allLevelsTotalScore += pointsPerHintedAnswer;
		}
		else
		{
			curScore += pointsPerAnswer;
			allLevelsTotalScore += pointsPerAnswer;
		}
	}

	var newContents = "\n<p class=\"liy-question\">\n" ;

	newContents += curQuestion.GetResponseText( answerIdx );

	newContents += "\n</p>\n\n" ;

	if ( numAnswered >= totalQuestionsToAsk )
		newContents += "<button class=\"liy-push-button\" onClick=\"OnRequestNextQuestion()\">Continue</button>" ;
	else
		newContents += "<button class=\"liy-push-button\" onClick=\"OnRequestNextQuestion()\">Next Question</button>" ;

	var contents = document.getElementById( "liy-body" );
	contents.innerHTML = newContents;

	// remove this question from the list of available questions ('cause it's already been asked)
	curQuestionSet.splice( curIdx, 1 );
}

//******************************************
function OnHintRequested()
{
	hintVisible = 1;
	LoadAnswersSection();
}

//******************************************
function DisplayFinalScore()
{
	var gastr = "/liy/score-level-" + level + "-" + curScore;
	urchinTracker( gastr );


	LoadHeaderSection();

	var newContents = "&nbsp;" ;
	contents = document.getElementById( "liy-image" );
	contents.innerHTML = newContents;

	// score
	newContents = "\n<p class=\"liy-question\">\n" ;
	newContents += "<b>Your Score:</b> " + allLevelsTotalScore + " out of " + allLevelsHighestPossibleScore;
	newContents += "\n</p>\n\n" ;

	// ranking
	newContents += "\n<p class=\"liy-question\">\n" ;
	var ranking;
	var rankingDetails;
	var polite = 0;

	var bHasPlayedAllLevels = 0;
	if ( allLevelsHighestPossibleScore >= numEasyQuestionsToAsk + numMediumQuestionsToAsk + numHardQuestionsToAsk + numEvilQuestionsToAsk )
		bHasPlayedAllLevels = 1;

	if ( allLevelsTotalScore >= allLevelsHighestPossibleScore )
	{
		ranking = insults1[ 0 ];
		if ( insults1.length > 1 )
			insults1.splice( curIdx, 1 );
		polite = 1;
	}
	else if ( allLevelsTotalScore > allLevelsHighestPossibleScore * 0.5 )
	{
		if ( bHasPlayedAllLevels )
			ranking = insults2[ insults2.length - 1 ];
		else
		{
			ranking = insults2[ 0 ];
			if ( insults2.length > 1 )
				insults2.splice( curIdx, 1 );
		}
		polite = 1;
	}
	else if ( allLevelsTotalScore > 0 )
	{
		if ( bHasPlayedAllLevels )
			ranking = insults3[ insults3.length - 1 ];
		else
		{
			ranking = insults3[ 0 ];
			if ( insults3.length > 1 )
				insults3.splice( curIdx, 1 );
		}
	}
	else
	{
		ranking = insults4[ 0 ];
		if ( insults4.length > 1 )
			insults4.splice( curIdx, 1 );
//		ranking = "Never, ever leave the house without your guide dog." ;
//		ranking = "Bummer about the lobotomy. But at least you've still got your looks, right?" ;
//		ranking = "Do you happen to have any large scars on your skull that you can't remember how you got?";
	}
	newContents += ranking;
	newContents += "\n</p>\n\n" ;

	if ( level == 1 )
		newContents += "<br />\n<button class=\"liy-push-button\" onClick=\"ReplayGame()\">Play the Medium Level</button>\n\n" ;
	else if ( level == 2 )
		newContents += "<br />\n<button class=\"liy-push-button\" onClick=\"ReplayGame()\">Play the Hard Level</button>\n\n" ;
	else if ( level == 3 )
		newContents += "<br />\n<button class=\"liy-push-button\" onClick=\"ReplayGame()\">Play the Impossible Level</button>\n\n" ;
	else if ( level >= 4 )
		newContents += "<br />\n<button class=\"liy-push-button\" onClick=\"StartOver()\">Start Over</button>\n\n" ;

	// append the check-out-these-links stuff
	if ( bonusURLs.length > 0 )
	{
		newContents += "<br /><br />\n<p class=\"liy-question\">\n" ;
		if ( polite )
			newContents += "To enhance your already vast knowledge about the places you just saw, check out these links:<br /><br />\n";
		else
			newContents += "To help remedy your appalling ignorance about the places you just saw, check out these links:<br /><br />\n";
		for ( var i=0  ;  i < bonusURLs.length  ;  ++i )
		{
			var alreadyListed = 0;
			for ( var j=0  ;  j < i  ;  ++j ) // make sure we don't list any of them twice
			{
				if ( bonusURLs[i] == bonusURLs[j] )
				{
					alreadyListed = 1 ;
					continue;
				}
			}
			if ( alreadyListed )
				continue;
			newContents += "<a href=\"";
			newContents += bonusURLs[i];
			newContents += "\">" ;
			newContents += bonusURLAnchorTexts[i];
			newContents += "</a><br />\n";
		}
		newContents += "\n</p>\n\n" ;
	}

	newContents += "\n<p class=\"liy-question\">\n" ;
	newContents += "If you're gobsmacked by the photos in this game, you might be interested to learn that some of them are available in gigantic sizes as <a href=\"wallpapers.htm\">free desktop wallpaper</a>.";
	newContents += "</p>\n";

	contents = document.getElementById( "liy-body" );
	contents.innerHTML = newContents;

	bonusURLs.length = 0;
}

//******************************************
function OnLevelBoundary()
{
	if ( numAnswered == (numEasyQuestionsToAsk + numMediumQuestionsToAsk + numHardQuestionsToAsk) )
		return 3;
	else if ( numAnswered == (numEasyQuestionsToAsk + numMediumQuestionsToAsk) )
		return 2;
	else if ( numAnswered == numEasyQuestionsToAsk )
		return 1;
	return 0;
}

//******************************************
function UpdateLevelSettings()
{
	if ( numAnswered == (numEasyQuestionsToAsk + numMediumQuestionsToAsk + numHardQuestionsToAsk) )
	{
		curQuestionSet = evilQuestions;
		pointsPerAnswer = pointsPerEvilQuestion;
		pointsPerHintedAnswer = pointsPerHintedEvilQuestion;
	}
	else if ( numAnswered == (numEasyQuestionsToAsk + numMediumQuestionsToAsk) )
	{
		curQuestionSet = hardQuestions;
		pointsPerAnswer = pointsPerHardQuestion;
		pointsPerHintedAnswer = pointsPerHintedHardQuestion;
	}
	else if ( numAnswered == numEasyQuestionsToAsk )
	{
		curQuestionSet = mediumQuestions;
		pointsPerAnswer = pointsPerMediumQuestion;
		pointsPerHintedAnswer = pointsPerHintedMediumQuestion;
	}
}

//******************************************
function GetCurrentStage()
{
	if ( numAnswered >= (numEasyQuestionsToAsk + numMediumQuestionsToAsk + numHardQuestionsToAsk + numEvilQuestionsToAsk) )
		return 4;
	else if ( numAnswered >= (numEasyQuestionsToAsk + numMediumQuestionsToAsk + numHardQuestionsToAsk) )
		return 3;
	else if ( numAnswered >= (numEasyQuestionsToAsk + numMediumQuestionsToAsk) )
		return 2;
	else if ( numAnswered >= numEasyQuestionsToAsk )
		return 1;
	return 0;
}

//******************************************

