$(document).ready(function() {
	/* feedback form validation logic */
	/*
	$("#fbk_form").validate({
		submitHandler: function(form) {
			$("#fbk_form").hide("slow");
		}							
	});
	*/
	$('li.fbk_survey').pngFix();
	
	/* feedback dialog box setup */
	$('#feedback-dialog').dialog({
		height: 'auto',
		width: '525px',
		modal: true,
		autoOpen: false,
		resizable: false,
		buttons: {
			"Send Feedback": function() {
				$("#fbk_form").hide("slow");  //attempting to send feedback, let's hide the form
				$(".ui-dialog-buttonpane button").hide("slow"); //now hide our buttons
				$("#feedPostDiv").show("slow"); //show our thank you message
				$("#feedbackSpinner").show(); //show the spinner (only seen if this ajax request takes too long)
				//let's ajax submit the form!
				$("#fbk_form").ajaxSubmit({
					target: "#feedbackFormStatus",
					dataType: "html",
					success: function(data, textStatus) {
						//upon completion let's first hide the spinner
						$("#feedbackSpinner").hide("slow");
						$("#feedbackFormStatus").text(data);  //update the status element with our ajax response (text)
						$("#feedbackFormStatus").fadeIn(500); //fadeIn in case we're hidden (second form post)
						//fade out our status text after 3 seconds
						var delayedDelete = setTimeout(function () {
							$("#feedbackFormStatus").fadeOut(500, function() { $("#feedbackFormStatus").text(""); });
						}, 3000);
						//after 5 seconds, let's go ahead and close the dialog for the user
						var delayedClose = setTimeout(function() {							
							$("#feedback-dialog").dialog("close");
						}, 5000);
					}
				});
			}, 
			"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	});

	/* feedback button click logic */
	$('#fdbk_tab').click(function(){
		$('#feedback-dialog').dialog('open');
		$("#feedPostDiv").hide();
		$("#fbk_form").show();
		$(".ui-dialog-buttonpane button").show();
		return false;
	});

	/* our tab functionality, change our hidden input "fbk_type" based on which tab the user picks */
	$("#tabs").tabs({
		select: function(event, ui) {
			var newTypeText = ui.tab.id;
			//console.log(newTypeText);
			$("#fbk_type").attr("value", newTypeText);
			//$("#fbk_type").val(newTypeText);
		}
	});

	/* change our hidden input "fbk_smiley_type" based on which selectable item the user picks */
	$("#fbk_selectable").selectable({
		selected: function(event, ui) {
			var newTypeID = ui.selected.id;
			//console.log(newTypeID);
			$("#fbk_smiley_type").attr("value", newTypeID);
			//$("#fbk_smiley_type").val(newTypeID);
		}		
	});
	
	/* show conditional questions based on input attributes target and action */
	$(".question").click(function(){
		var targetName = "#" + $(this).attr("name");
		//console.log("target is: %s", targetName);
		if ($(this).hasClass("hide")) {
			//console.log("hide %s", targetName);
			//$(this).parent().parent().parent().siblings().find(".conditional").hide("slow");
			$(targetName).hide("slow");
			//hide the next conditional
			//console.log("hide the conditional!");
		}
		if ($(this).hasClass("show")) {
			//console.log("show %s", targetName);
			$(targetName).show("slow");
			//$(this).parent().parent().parent().siblings().find(".conditional").show("slow");
			//show the next 
			//console.log("show the conditional!");
		}		
	});
	
});