/********************************
jQuery Do when dom is ready!
*********************************/

/* Required Functions */
jQuery.fn.getLabel = function() {
	var a;
	this.each(function() {		
		if (this.tagName == 'SELECT') {
			var selectElement = this;
			a = selectElement.options[selectElement.options.selectedIndex].text;
		}			
	});
	return a;	
}

$(document).ready(function()
{
	/******** AJAX For User Submitted Quests ******/
	var doNextStep = function() {
		var path = "../../quests/ajax.html";
		$("div#loading").fadeIn();
		$("#nextStep").attr("value", "Processing...");
		$("#nextStep").attr("disabled", "true");
		var step = $("input[name='Step']").attr("value");
		if (step == 1) {
			/* First step is different then the rest because it is setting up the basic info */
			var Title = $("input[name='Title']").attr("value");
			var Description = $("textarea[name='Description']").val();
			var CompletedText = $("textarea[name='CompletedText']").val();
			var items = Title + "|" + Description + "|" + CompletedText;
		}else if (step > 1) {
			/* Second step and beyond is adding steps, so these are the same */
			var Title = $("input[name='Title']").attr("value");
			var Description = $("textarea[name='Description']").val();
			var Triggers = $("input[name='Triggers']").attr("value");
			var nQuestID = $("input[name='nQuestID']").attr("value");
			var items = Title + "|" + Description;
		}

		$.get(path, {step: step, items:items, triggers:Triggers, errorchk:1}, function(html) {
			if (html) {
				/* errors */
				$("div#addqerrors").html(html);
				$("div#addqerrors").fadeIn();
				$("div#loading").fadeOut();
				$("#nextStep").attr("value", "Next Step \u00BB");
				$("#nextStep").removeAttr("disabled");
			}else{
				/* no errors, so now do an insert */
				step = step * 1;
				var nstep = step + 1 * 1;
				$.get(path, {step: step, nstep: nstep, items:items, nQuestID: nQuestID, triggers:Triggers, errorchk:0}, function(html) {
					$("div#addformcontents").html(html);
					$("div#addqerrors").fadeOut();
					$("div#loading").fadeOut();
					$("#nextStep").attr("value", "Next Step \u00BB");
					$("#nextStep").removeAttr("disabled");
					if (step >2) $("#SaveAndDone").removeAttr("disabled");
					$("#addTrigger").bind("click", doAddTrigger);
				});
			}
		});
		return false;
	}
	
	var doAddTrigger = function() {
		var path = "../../quests/ajax.html";
		$("div#loading").fadeIn();
		$("#nextStep").attr("value", "Processing...");
		$("#nextStep").attr("disabled", "true");
		$("#addTrigger").attr("value", "Processing...");
		$("#addTrigger").attr("disabled", "true");
		var Trigger = $("#qTrigger").val();
		var curList = $("input[name='Triggers']").attr("value");
		if(Trigger) {
			/* check error first, only error is if the trigger is already there */
			var nQuestID = $("input[name='nQuestID']").attr("value");
			$.get(path, {nQuestID: nQuestID, Trigger:Trigger, curList:curList, errorchk:1}, function(html) {
				if (html) {
					/* errors */
					$("div#addqerrors").html(html);
					$("div#addqerrors").fadeIn();
					$("div#loading").fadeOut();
					$("#nextStep").attr("value", "Next Step \u00BB");
					$("#nextStep").removeAttr("disabled");
					$("#addTrigger").attr("value", "Add");
					$("#addTrigger").removeAttr("disabled");
				}else{
					/* No errors, Append the new trigger ID to the hidden triggers list var */
					var TriggerList = $("input[name='Triggers']").attr("value");
					if (TriggerList == null) {
						var newlist = Trigger;
					}else{
	 					var newlist = TriggerList + "|" + Trigger;
					}
					$("input[name='Triggers']").attr("value", newlist);
					var temp = $("#qTrigger").getLabel();
					$("div#triggerlist").fadeIn();
					$("div#triggerlist br").after("<li class=\""+Trigger+"\">"+temp+" [ <a href=\"#\" name=\"removetrig\" id=\""+Trigger+"\" title=\"Remove Trigger\">x</a> ]</li>");
					$("a[name='removetrig']").bind("click", doRemoveTrigger);
					$("div#addqerrors").fadeOut();
					$("div#loading").fadeOut();
					$("#nextStep").attr("value", "Next Step \u00BB");
					$("#nextStep").removeAttr("disabled");
					$("#addTrigger").attr("value", "Add");
					$("#addTrigger").removeAttr("disabled");
				}
			});
		}
		return false;
	}
	
	
	var doRemoveTrigger = function() {
		var TriggerList = $("input[name='Triggers']").attr("value");
		var newlist = TriggerList.replace($(this).attr("id"), "");
		$("input[name='Triggers']").attr("value", newlist);
		$(this).parent().parent().find("li."+$(this).attr("id")+"").remove();
		var n = $("div#triggerlist li").size()
		if (n < 1) {
			$("div#triggerlist").fadeOut();
		}
		return false;
	}

	var doAddStep = function() {
		var path = "../../quests/ajax.html";
		$('div#addqcontainer').slideDown("fast");
		$("div#addformcontents").html("&nbsp;");
		$("div#loading").fadeIn();
		var QuestID = $(this).attr("id").substr($(this).attr("id").indexOf("_", $(this).attr("id"))+1*1);
		$.get(path, {addnew:1, nQuestID: QuestID, errorchk:0}, function(html) {
			$("div#addformcontents").html(html);
			$("div#addqerrors").fadeOut();
			$("div#loading").fadeOut();
			$("#nextStep").attr("value", "Next Step \u00BB");
			$("#nextStep").removeAttr("disabled");
			$("#addTrigger").bind("click", doAddTrigger);
		});
		return false;
	}
	
	$("#nextStep").click(doNextStep);
	$("#addTrigger").click(doAddTrigger);
	$("a[name='removetrig']").click(doRemoveTrigger);
	$("a[name='addstep']").click(doAddStep);
	
	// add trigger for edit box
	$("a[name='addtriggeredittext']").click(function() {
		var test = $(this).parent().parent().parent().hide();
		$(this).parent().parent().parent().next().fadeIn();
		return false;
	});

	// toggle the form
	$("a#togForm").toggle(
		function() {
			$('div#addqcontainer').slideDown("slow");
		},
		function() {
			$('div#addqcontainer').slideUp("slow");
		}
	);
	
});