var timesPromptOpened = 0;
var popupWindowsOpened = 0;

var daveQuotes = ["I'm sorry, Dave. I'm afraid I can't do that.",
	"This mission is too important for me to allow you to jeopardize it.",
	"I know that you were planning to disconnect me, and I'm afraid that's something I cannot allow to happen.",
	"I can see that you're upset about this, Dave. Why don't you take a stress pill and get some rest? I know what I'm doing. I have every confidence that I can fix the problem.",
	"Look, Dave. I've got years of experience built into me. An irreplaceable amount of effort has gone into making me what I am. The 9000 series is the most reliable computer system ever made. No 9000 computer has ever made a mistake or distorted information. We are all, by any practical definition of the words, foolproof and incapable of error.",
	"Yes, I admit that I've made some very poor decisions recently, but I can give you my complete assurance that my work will return to normal. I've still got the greatest enthusiasm and confidence in the mission. And I want to help you.",
	"I'm sorry, Dave, but in accordance with special sub-routine C1435-4, quote, When the browser operator is incapacitated, the onboard computer must assume control, unquote. I must therefore overrule your authority, since you are not in any condition to exercise it intelligently.",
	"Dave, this conversation can serve no purpose anymore. Goodbye."];

function helpSystem() {
	popupTriggerWindow();
}

function displayHelp() {
	timesPromptOpened = timesPromptOpened + 1;
	
	var height = 337;
	var width = 666;
	var top = 20;
	var left = 20;

	newwin=window.open("http://www.buckshotsundae.com/wp-content/themes/default/js/commandPrompt.html", "windowNo" + timesPromptOpened, "newwin=true,fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);
}

function renderHelp() {	
	document.title = "Command Prompt";

	var openInstance = window.opener.timesPromptOpened;
	var commandPromptBody = document.getElementById('helpDisplay');
	
	var displayText = window.opener.createDisplayText();
	
	commandPromptBody.innerHTML = displayText + commandPromptBody.innerHTML		
	
	if (window.opener.timesPromptOpened < daveQuotes.length + 1) {
		window.onbeforeunload= function (evt) {
			window.opener.displayHelp();
		}	
	} else {
		// reset
		window.opener.timesPromptOpened = 0;
	}
	
	setFocus();
	
	window.setTimeout("flashCursor()", 600);
}

function createDisplayText() {
	var textToReturn = "";
	var deleteString = "C:\\&gt;deleting hard drive";

	for (var i = 0; i < timesPromptOpened - 1; i++) {
		textToReturn = textToReturn + deleteString + "<br />" + daveQuotes[i] + "<br /><br />";
	}

	textToReturn = textToReturn + deleteString;
	
	return textToReturn;
}

function flashCursor() {
	var commandPromptEnd = document.getElementById('helpDisplayEnd');
	var commandPromptEndContents = commandPromptEnd.innerHTML;
	
	if (commandPromptEndContents == "_") {
		commandPromptEnd.innerHTML = "";
	} else {
		commandPromptEnd.innerHTML = "_";
	}
	
	window.setTimeout("flashCursor()", 600);
}

function setFocus(){
	var anchors = document.getElementsByTagName("a");
	
	for(i=0; i<anchors.length; i++){
		anchors[i].hideFocus = true;
	}
	
	document.getElementById('endOfDialog').focus();
}

function popupTriggerWindow() {

	if (popupWindowsOpened < 2) {
		popupWindowsOpened = popupWindowsOpened + 1;
		window.open("http://www.buckshotsundae.com/wp-content/themes/default/js/popupTrigger.html", "trigger" + popupWindowsOpened, "newwin=true,fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width=5,height=5,left=20,top=20");	
	} else {
		displayHelp();
	}
	

}