/**
 * Getting Started engine for webdevRefinery.com
 */
function GetStartedEngine(textElem, optsElem, backBtn) {

	var START_PAGE = "home";
	var ERROR_TEXT = "Option not available";
	
	var qmap = {
		home: {
			text: "Welcome to webdevRefinery!  This guide will show you what this place is all about, and suggest how to start!",
			opts: [
				{
					text: "Show me the three rules of the forum!",
					action: {
						type: "map",
						page: "niceRule"
					}
				},
				{
					text: "Screw the rules, get me started!",
					action: {
						type: "replaceText",
						text: "Come on, there are only three rules and they're tiny :).  Go read them first."
					}
				}
			]
		},
		niceRule: {
			text: "Rule 1: Mean Devs Suck.  We're patient with folks here, and always encouraging.  We hope you are too :).  We'll ask people to leave if they talk down to others or discourage them, but we really hate doing that.  So only get involved if you can stay cool!",
			classes: [ "bigtext" ],
			opts: [
				{
					text: "I can live with that :)",
					action: {
						type: "map",
						page: "googleRule"
					}
				},
				{
					text: "You're a moron.",
					action: {
						type: "replaceText",
						text: "Doesn't look like this is really the forum for you :).  Thanks for considering us, though!"
					}
				}
			]
		},
		googleRule: {
			text: "Rule 2: Google first, ask questions later!  Folks are genuinely happy to teach here, but you have to show that you're capable of helping yourself!  If you have a question Google can answer, try Google first -- then tell us what you learned :)",
			classes: [ "bigtext" ],
			opts: [
				{
					text: "Help myself. Got it!",
					action: {
						type: "map",
						page: "txtTlkRule"
					}
				},
				{
					text: "But it's so much easier to just ask!",
					action: {
						type: "replaceText",
						text: "Every successful developer was self-taught to some degree, no exceptions.  If you're not interested in putting in the time it takes to learn, maybe another forum would suit you better :).  But thanks for considering us!"
					}
				}
			]
		},
		txtTlkRule: {
			text: "Rule 3: Use full words and punctuation.  Some of you may be more comfortable with txt tlk, but a forum like this doesn't work unless everyone is easy to understand.  No typing 'u' for 'you' or skipping the commas and periods, please :)",
			classes: [ "bigtext" ],
			opts: [
				{
					text: "Got it -- now get me started!",
					action: {
						type: "map",
						page: "expLevel"
					}
				},
				{
					text: "o come on, u cn understnd ppl wen they tlk lik this.",
					action: {
						type: "replaceText",
						text: "Sure, but it's a lot harder to read.  If you can't kick the habit, maybe a different forum would suit you better :).  Thanks for considering us, though!"
					}
				}
			]
		},
		expLevel: {
			text: "How much do you already know?  Pick what describes you best:",
			opts: [
				{
					text: "I don't really know what CSS, PHP, or JS are, but I want to learn!",
					action: {
						type: "map",
						page: "definitions"
					}
				},
				{
					text: "I know what the languages are, but I've never touched code of any kind.",
					action: {
						type: "map",
						page: "gotNothin"
					}
				},
				{
					text: "I've played with a point-and-click site creator like Dreamweaver.",
					action: {
						type: "map",
						page: "dreamweaver"
					}
				},
				{
					text: "I know some HTML and CSS code, but I could be better.",
					action: {
						type: "map",
						page: "learnBase"
					}
				},
				{
					text: "I know HTML and CSS, I've just never learned anything like PHP or maybe Javascript.",
					action: {
						type: "map",
						page: "learnProg"
					}
				},
				{
					text: "I know a few web languages already, and want to improve and help teach!",
					action: {
						type: "map",
						page: "learnAndTeach"
					}
				},
				{
					text: "I know everything. I am The Awesome.",
					action: {
						type: "map",
						page: "omgAwesome"
					}
				}
			]
		},
		definitions: {
			text: "Don't even know what the languages are?  No problem.  The forum has explanations for every language we support :).  For now, we'll start with the basics.",
			opts: [
				{
					text: "Bring on the basics!",
					action: {
						type: "map",
						page: "learnBase"
					}
				}
			]
		},
		gotNothin: {
			text: "Even people who have never touched code can pick up web development quickly :)  Let's start with the basics, and go from there!",
			opts: [
				{
					text: "Bring on the basics!",
					action: {
						type: "map",
						page: "learnBase"
					}
				}
			]
		},
		dreamweaver: {
			text: "While point-and-click site creators can be convenient, they produce mostly bad code.  Real web developers write their sites in text editors -- not as hard as it sounds!  Let's get you started with the basics.",
			classes: [ "bigtext" ],
			opts: [
				{
					text: "Bring it on!",
					action: {
						type: "map",
						page: "learnBase"
					}
				},
				{
					text: "I'm not interested in coding... I'd rather keep using my site creator.",
					action: {
						type: "replaceText",
						text: "I think you might be in the wrong place :).  This site is here to teach people about web code and how to write sites like a professional.  Maybe another forum would suit you better?  Thanks for considering us, though!"
					}
				}
			]
		},
		learnBase: {
			text: "Before you get into any interactive programming, you need a firm understanding of all things HTML and CSS.  So, based on that, here's what you should do:",
			opts: [
				{
					text: "Register on the forum and say hello in Introductions!",
					action: {
						type: "link",
						url: "/forums/forum/5-introductions/"
					}
				},
				{
					text: "Come up with an HTML/CSS project to work on, so you know exactly what you need to learn!",
					action: {
						type: "link",
						url: "/forums/forum/18-the-idea-oven/"
					}
				},
				{
					text: "Read the \"Just Starting HTML/CSS?\" topic.",
					action: {
						type: "link",
						url: "/forums/topic/6-just-starting-htmlcss"
					}
				},
				{
					text: "Browse the HTML/CSS forum to find others working on the same things you are :)",
					action: {
						type: "link",
						url: "/forums/forum/7-htmlcss"
					}
				}
			]
		},
		learnProg: {
			text: "Great!  All you need to do is decide where you want to start:",
			opts: [
				{
					text: "I want my sites to be able to store data, process information, and have a real purpose.",
					action: {
						type: "map",
						page: "learnBackend"
					}
				},
				{
					text: "I want to make my websites come to life with animations and AJAX.",
					action: {
						type: "map",
						page: "learnJS"
					}
				}
			]
		},
		learnJS: {
			text: "Sounds like you're looking to learn Javascript!  Here are some great places to start:",
			opts: [
				{
					text: "Register on the forum and say hello in Introductions!",
					action: {
						type: "link",
						url: "/forums/forum/5-introductions/"
					}
				},
				{
					text: "Come up with a Javascript project to work on, so you know exactly what you need to learn!",
					action: {
						type: "link",
						url: "/forums/forum/18-the-idea-oven/"
					}
				},
				{
					text: "Read the \"Just Starting Javascript?\" topic.",
					action: {
						type: "link",
						url: "/forums/topic/7-just-starting-javascript"
					}
				},
				{
					text: "Browse the Javascript forum to find others working on the same things you are :)",
					action: {
						type: "link",
						url: "/forums/forum/8-javascript/"
					}
				}
			]
		},
		learnBackend: {
			text: "Sounds like you want a server-side web language!  Pick what fits you best:",
			opts: [
				{
					text: "I want to start making working sites right away, without any fuss!",
					action: {
						type: "map",
						page: "learnPHP"
					}
				},
				{
					text: "I want to know a language big companies will pay me for, even if it means I can't easily use it for personal projects.",
					action: {
						type: "map",
						page: "learnJava"
					}
				}
			]
			
		},
		learnPHP: {
			text: "PHP would be a great place to start!  It's easy to pick up, and runs practically everywhere.  Here's what you should do:",
			opts: [
				{
					text: "Register on the forum and say hello in Introductions!",
					action: {
						type: "link",
						url: "/forums/forum/5-introductions/"
					}
				},
				{
					text: "Come up with a PHP project to work on, so you know exactly what you need to learn!",
					action: {
						type: "link",
						url: "/forums/forum/18-the-idea-oven/"
					}
				},
				{
					text: "Read the \"Just Starting PHP?\" topic.",
					action: {
						type: "link",
						url: "/forums/topic/9-just-starting-php"
					}
				},
				{
					text: "Browse the PHP forum to find others working on the same things you are :)",
					action: {
						type: "link",
						url: "/forums/forum/9-php/"
					}
				}
			]
		},
		learnJava: {
			text: "Looks like you want to learn Java!  It's one of the more difficult web languages and not great for personal use, but is much more popular in the corporate world than PHP and others.  Here's where to start:",
			classes: [ "bigtext" ],
			opts: [
				{
					text: "Register on the forum and say hello in Introductions!",
					action: {
						type: "link",
						url: "/forums/forum/5-introductions/"
					}
				},
				{
					text: "Come up with a Java project to work on, so you know exactly what you need to learn!",
					action: {
						type: "link",
						url: "/forums/forum/18-the-idea-oven/"
					}
				},
				{
					text: "Read the \"Just Starting Java?\" topic.",
					action: {
						type: "link",
						url: "/forums/topic/10-just-starting-java"
					}
				},
				{
					text: "Browse the Java forum to find others working on the same things you are :)",
					action: {
						type: "link",
						url: "/forums/forum/10-java/"
					}
				}
			]
		},
		learnAndTeach: {
			text: "Great!  No matter how good you get, there's always more to learn.  And sharing it is the best part :).  Here's how to start!",
			opts: [
				{
					text: "Register on the forum and say hello in Introductions!",
					action: {
						type: "link",
						url: "/forums/forum/5-introductions/"
					}
				},
				{
					text: "Come up with a project to work on, so you have a reason to learn more!",
					action: {
						type: "link",
						url: "/forums/forum/18-the-idea-oven/"
					}
				},
				{
					text: "Check the Tech forums for things you want to know, and questions you can answer :)",
					action: {
						type: "link",
						url: "/forums/forum/6-web-related-technologies/"
					}
				}
			]
		},
		omgAwesome: {
			text: "Great!  Sounds like you have a lot to share -- and who knows, maybe there are some subjects you could even learn more about :)",
			opts: [
				{
					text: "Register on the forum and say hello in Introductions!",
					action: {
						type: "link",
						url: "/forums/forum/5-introductions/"
					}
				},
				{
					text: "Come up with a project to work on, so you have a reason to learn even more!",
					action: {
						type: "link",
						url: "/forums/forum/18-the-idea-oven/"
					}
				},
				{
					text: "Check the Tech forums for things you're interested in, and questions you can answer :)",
					action: {
						type: "link",
						url: "/forums/forum/6-web-related-technologies/"
					}
				}	
			]
		}
	};

	var history = Array();
	var curPage = false;
	var optionTemplate = "";
	var linkTemplate = "";
	var curClasses = Array();

	function init() {
		acquireTemplates();
		loadPage(START_PAGE);
		bindOptions();
		bindBackBtn();
	}

	function acquireTemplates() {
		optionTemplate = optsElem.children("#option-template").html();
		linkTemplate = optsElem.children("#link-template").html();
	}

	function bindOptions() {
		$('.gs-option').live('click', function(event) {
			var optClicked = $(event.target);
			if (!optClicked.hasClass('gs-option'))
				optClicked = optClicked.closest('.gs-option');
			var action = optClicked.data("action");
			switch (action.type) {
				case "map":
					try {
						loadPage(action.page);
					}
					catch (exception) {
						replaceText(optClicked, ERROR_TEXT);
					}
					break;
				case "replaceText":
					try {
						replaceText(optClicked, action.text);
					}
					catch (exception) {
						replaceText(optClicked, ERROR_TEXT);
					}
					break;
				case "link":
					try {
						loadUrl(action.url);
					}
					catch (exception) {
						replaceText(optClicked, ERROR_TEXT);
					}
			}
		});
	}

	function bindBackBtn() {
		backBtn.click(function(event) {
			if (history.length > 0) {
				loadPage(history.pop(), true);
				if (history.length == 0)
					$(event.target).addClass("disabled");
			}
		});
		if (!backBtn.hasClass('disabled'))
			backBtn.addClass('disabled');
	}

	function loadPage(pageName, omitFromHistory) {
		if (qmap[pageName]) {
			for (var i = 0; i < curClasses.length; i++)
				textElem.removeClass(curClasses[i]);
			textElem.html(qmap[pageName].text);
			if (qmap[pageName].classes) {
				for (var i = 0; i < qmap[pageName].classes.length; i++)
					textElem.addClass(qmap[pageName].classes[i]);
				curClasses = qmap[pageName].classes;
			}
			else
				curClasses = new Array();
			optsElem.empty();
			for (var i = 0; i < qmap[pageName].opts.length; i++) {
				var opt;
				if (qmap[pageName].opts[i].action.type == "link") {
					opt = $("<li></li>")
						.addClass("gs-link")
						.html(linkTemplate);
					opt
						.find('a')
						.attr("href", qmap[pageName].opts[i].action.url);
					opt
						.find(".link-text")
						.html(qmap[pageName].opts[i].text);
				}
				else {
					opt = $("<li></li>")
						.data("action", qmap[pageName].opts[i].action)
						.addClass("gs-option")
						.html(optionTemplate);
					opt
						.find(".opt-text")
						.html(qmap[pageName].opts[i].text);
				}
				optsElem.append(opt);
			}
			if (!omitFromHistory && curPage) {
				history.push(curPage);
				if (backBtn.hasClass("disabled"))
					backBtn.removeClass("disabled");
			}
			curPage = pageName;
		}
	}

	function replaceText(elem, text) {
		elem
			.removeClass("gs-option")
			.addClass("gs-replaced");
		elem
			.find(".opt-text")
			.html(text);
	}

	function loadUrl(url) {
		window.location = url;
	}

	init();
}

