var displayed_answer_style = false;
var displayed_forename_input = false;

var qpartsb = null;
var checkB = null;
var forenameB = null;

var clicked_ok = function()
{
	if (example_timer)
		clearTimeout(example_timer);
	if (!displayed_answer_style)
	{
		if (document.getElementById("input_field_left").value == "" && document.getElementById("input_field_right").value == "")
		{
			if (qpartsb != null)
				return;
			qpartsb = createBubble("Mindestens ein Teil der Frage sollte existieren.", false);
			var pos =  YAHOO.util.Dom.getXY("input_field_left");
			repositionBubble(qpartsb, pos[0]+50, pos[1]+20);
			showBubble(qpartsb);
			
			return;
		}
		if (qpartsb != null);
			close_bubble(qpartsb);

		displayed_answer_style = true;
		
		var move = new YAHOO.util.Motion("wrapper", {
			"margin-top": {
				by: -50,
				unit: "px"
			}
		}, 1, YAHOO.util.Easing.easeOutStrong);
		
		move.onComplete.subscribe(function(s, o)
		{
			select_answer_style();
		});
		
		move.animate();
	}
	else if (!displayed_forename_input)
	{
		if (answers_c < 2 && getCheckGroupValue(group) != 0)
		{
			if (checkB != null)
				return;
			checkB = createBubble("Gib bitte mindestens zwei verschiedene Antworten ein.", false);
			var pos =  YAHOO.util.Dom.getXY("add_answer_btn");
			repositionBubble(checkB, pos[0]+20, pos[1]+10);
			showBubble(checkB);
			
			return;
		}
		if (checkB != null)
			close_bubble(checkB);
		displayed_forename_input = true;
		show_forename_input();
		
		new YAHOO.util.Motion("wrapper", {
			"margin-top": {
				by: 20,
				unit: "px"
			}
		}, 1, YAHOO.util.Easing.easeOutStrong).animate();
	}
	else
	{
		if (document.getElementById("forename_input_field").value == "")
		{
			if (forenameB != null)
				return;
			forenameB = createBubble("Bitte gib deinen Vor- oder Spitznamen ein", false, "bottom");
			var pos =  YAHOO.util.Dom.getXY("forename_input_field");
			repositionBubble(forenameB, pos[0]+20, pos[1]-58);
			showBubble(forenameB);
			
			return;
		}
		if (forenameB != null)
			close_bubble(forenameB);
		submit_question();
	}
};

function select_answer_style()
{
	YAHOO.util.Dom.setStyle("select_answer_style", "visibility", "visible");
	new YAHOO.util.Motion("select_answer_style", {
		"height": {
			from: 0,
			to: 100,
			unit: "%"
		},
		"opacity": {
			from: 0,
			to: 1
		}
	}, 2, YAHOO.util.Easing.easeOutStrong).animate();
};

function show_forename_input()
{
	var fade_out_selection = new YAHOO.util.Motion("select_answer_style", {
		"height": {
			to: 0,
			unit: "px"
		},
		"opacity": {
			to: 0,
			unit: ""
		}
	}, 1, YAHOO.util.Easing.easeOut);

	fade_out_selection.onComplete.subscribe(function(s, o)
	{
		YAHOO.util.Dom.setStyle("select_answer_style", "display", "none");
	});
	
	fade_out_selection.animate();

	YAHOO.util.Dom.setStyle("enter_forename", "visibility", "visible");
	new YAHOO.util.Motion("enter_forename", {
		"height": {
			from: 0,
			to: 100,
			unit: "%"
		},
		"opacity": {
			from: 0,
			to: 1
		}
	}, 2, YAHOO.util.Easing.easeOutStrong).animate();
}

var submit_succeed = function(o)
{

	var result = {};
	
	try
	{
		result = YAHOO.lang.JSON.parse(o.responseText); 
	} catch (e)
	{
		result = { "error_code": -1 };
	}

	if (result.error_code != 0)
	{
		hide_loader();
		document.getElementById("submit_error").innerHTML = 
		"<h2>Leider ging etwas schief.</h2><p>Versuche es bitte nocheinmal.</p>";
		
		window.setTimeout(function() { hide_loader(); anim_show("submit_error", 1); }, 5000);
	}
	else
	{
		var prefix = "";
		if (document.getElementById("input_field_left").value != "")
		{
			prefix = url_friendly(document.getElementById("input_field_left").value) + "." + root_url.substr(7)  + "/";
		}
		else
		{
			prefix = root_url.substr(7)  + "/";
		}
		var suffix = url_friendly(document.getElementById("input_field_right").value);
		suffix = (suffix == "") ? suffix : (suffix + "/");
		document.getElementById("public_code").innerHTML = prefix + suffix + result.public_code;
		document.getElementById("personal_code").innerHTML = "so." + root_url.substr(7) + "/" + result.personal_code;
		
		window.setTimeout(function() { hide_loader(); anim_show("submitted", 1); }, 5000);
	}
};

var submit_failure = function(o)
{
	hide_loader();
	
	document.getElementById("submit_error").innerHTML = 
		"<h2>Leider ging etwas schief.</h2><p>Versuche es bitte nocheinmal.</p>";
	anim_show("submit_error");
};

function hide_loader()
{
	anim_hide("submitting_question", 1);
}

function show_loader()
{
	anim_show("submitting_question", 1);
}

var group = "";
var answers_c = 0;

function submit_question()
{
	anim_hide("enter_forename", 1);
	
	show_loader();
	
	anim_hide("ok_btn", 1);
	
	YAHOO.util.Dom.setStyle("ok_btn", "display", "none");
	
	var type = (getCheckGroupValue(group) == 0) ? "rating" : "answers";
	
	var answers = "";
	
	if (type == "answers")
	{
		for (var i = 0; i<answers_c; i++)
		{
			answers += 
				"answers_list["+i+"]=" +
				escape(document.getElementById("answer_" + i).value) + 
				"&";
		}
	}
	
	YAHOO.util.Connect.asyncRequest(
		"POST",
		root_url + "/submit_question",
		{ 
			success: submit_succeed, 
			failure: submit_failure
		},
		"prefix=" + escape(document.getElementById("input_field_left").value) +
		"&suffix=" + escape(document.getElementById("input_field_right").value) +
		"&forename=" + escape(document.getElementById("forename_input_field").value) +
		"&type=" + escape(type) + "&" + answers
	);
}

var addAnswer = function(e) {
	if (answers_c > 6)
		return;
	
	var ul = document.getElementById("answers_list");
	
	var li = document.createElement("li");
	ul.appendChild(li);
	
	li.innerHTML = "<input type='text' value='' maxlength='17' id='answer_" + answers_c + "' />" +
			"<div class='deleteanswer' title='Diese Antwort entfernen'></div>";

	document.getElementById("answer_" + answers_c).focus();
	document.getElementById("answer_" + answers_c).select();

	answers_c++;
}

var introbubble = null; 

function drawIntroductionBubbleAt(x,y)
{
	repositionBubble(introbubble, x, y);
	
	showBubble(introbubble);
}

var examples = new Array(
	"Wie schön", 
	"Wie lustig", 
	"Wie attraktiv",
	"Wie sexy",
	"Wie freundlich");
var example_timer = 0;
var examples_shown = false;
var next_example = 0;
var stop_examples = false;

function showExample()
{
	var value = examples[next_example];
	if (next_example == examples.length-1)
		next_example = 0;
	else
		next_example++;
	
	var anim = new YAHOO.util.Motion("input_field_left", {
		"opacity": {
			from: 1,
			to: 0
		}
	}, 1, YAHOO.util.Easing.easeOutStrong);
	
	anim.onComplete.subscribe(function(s, o)
	{
		document.getElementById("input_field_left").value = value;
		var anim_show = new YAHOO.util.Motion("input_field_left", {
			"opacity": {
				from: 0,
				to: 1
			}
		}, 1, YAHOO.util.Easing.easeOutStrong);
		anim_show.onComplete.subscribe(function(s, o)
		{
			if (stop_examples)
				stopExamples();
		});
		anim_show.animate();
	});
	
	anim.animate();
	
	example_timer = setTimeout("showExample()", 4000);
}

var stopExamples = function()
{
	if (!examples_shown)
	{
		if (example_timer)
			clearTimeout(example_timer);
		document.getElementById("input_field_left").value = "";
		document.getElementById("input_field_left").style.opacity = 1;
		examples_shown = true;
		stop_examples = true;
	}
}

var initIndex = function(o)
{
	YAHOO.util.Event.on("plus_right", "click", show_right_input);

	YAHOO.util.Event.on("x_btn", "click", function(e){ document.getElementById("input_field_left").value = ""; });
	YAHOO.util.Event.on("q_btn", "click", function(e){ drawIntroductionBubbleAt(e.clientX,e.clientY); });
	
	YAHOO.util.Event.on("x_btn", "click", function() {
		document.getElementById("input_field_left").value = "";
	});

	show_left_input();
	
	YAHOO.util.Event.on("ok_btn", "click", clicked_ok);
	new YAHOO.util.KeyListener(document, { keys: [YAHOO.util.KeyListener.KEY.ENTER] }, clicked_ok).enable();
	
	YAHOO.util.Event.on("add_answer_btn", "click", addAnswer);
	
	group = createRadioButtons(["check_rating_stars", "check_rating_answers"]);
	YAHOO.util.Event.on("rating_stars", "click", checkOption, {
		"group": group,
		"checkbox": "check_rating_stars"
	});
	YAHOO.util.Event.on("rating_answers", "click", checkOption,{
		"group": group,
		"checkbox": "check_rating_answers"
	});
	
	introbubble = document.getElementById("intro");
	YAHOO.util.Event.on(document.getElementById("close_btn_intro"), "click", function(e){ close_bubble(introbubble); });

	showExample();
	YAHOO.util.Event.on("input_field_left", "click", stopExamples);
};


new YAHOO.util.YUILoader({ 
	require: ["dom", "animation", "event", "connection", "json", "history"], 
	loadOptional: true,
	onSuccess: initIndex
}).insert();
