$(document).ready(function() {

	//------------------------------------------		TOGGLE DESCRIPTION IN POLL HEADER
	$("div.pollHeader").find(".toggleContentMore").click(function() {
		var toggler = $(this);
		$(this).parents("div.pollHeader").find("p.toggleContent").toggle("normal", function() {
			if ($(this).is(":visible")) $(toggler).html('...ukryj'); else $(toggler).html('więcej...');
		});
		
	});

	//------------------------------------------		DELETE QUESTION ANSWERS
	$("#answers img.deleteAnswer").click(function() {
	
		var text = $(this).parents("li").find("textarea:first").val();
		if (!confirm('Czy na pewno chcesz usunąć:\n\n' + text)) return;
	
		removeAnswer($(this), 'answers');
		$("#minAnswer").children(":last").remove();
		$("#maxAnswer").children(":last").remove();
	});
	
	//------------------------------------------		ADD QUESTION ANSWERS
	$(".addAnswer").click(function() {
		$("#answers").append(emptyAnswer);
		$("#answers").children(":last").find("img.deleteAnswer").click(function() {removeAnswer($(this), 'answers')});
		updateNumerators('answers');
		addHover($("#answers").children(":last"));
		
		var index = $("#minAnswer").children().length;
		$("#minAnswer").append('<option value="' + index + '">' + index + '</option>');
		$("#maxAnswer").append('<option value="' + index + '">' + index + '</option>');
	});
	
	//------------------------------------------		SORTABLE FOR QUESTION ANSWERS
	$("#answers").sortable("enable");
	$("#answers").sortable({
		stop : function() {
			$(this).children("li").removeClass("pollPageElementsHover");
			updateNumerators('answers');
		}
	});

	//------------------------------------------		DELETE QUESTION OPTION, VALUE
	$("#options img.deleteOption").click(function() {
		
		var text = $(this).parents("li").find("textarea:first").val();
		if (!confirm('Czy na pewno chcesz usunąć:\n\n' + text)) return;
	
		removeAnswer($(this), 'options');
	});
	$("#values img.deleteValue").click(function() {
		
		var text = $(this).parents("li").find("textarea:first").val();
		if (!confirm('Czy na pewno chcesz usunąć:\n\n' + text)) return;
		
		removeAnswer($(this), 'values');
	});
	
	//------------------------------------------		ADD QUESTION OPTION, VALUE
	$(".addOption").click(function() {
		$("#options").append(emptyOption);
		$("#options").children(":last").find("img.deleteOption").click(function() { removeAnswer($(this), 'options'); });
		updateNumerators('options');
		addHover($("#options").children(":last"));
	});
	
	$(".addValue").click(function() {
		$("#values").append(emptyValue);
		$("#values").children(":last").find("img.deleteValue").click(function() { removeAnswer($(this), 'values'); });
		updateNumerators('values');
		addHover($("#values").children(":last"));
	});
	
	//------------------------------------------		SORTABLE FOR QUESTION OPTIONS, VALUES
	$("#options").sortable("enable");
	$("#options").sortable({
		stop : function() {
			$(this).children("li").removeClass("pollPageElementsHover");
			updateNumerators('options');
		}
	});
	
	$("#values").sortable("enable");
	$("#values").sortable({
		stop : function() {
			$(this).children("li").removeClass("pollPageElementsHover");
			updateNumerators('values');
		}
	});

	//------------------------------------------		QUESTION OPTIONS VALUES SWITCHER
	$("form[name='elementEditForm'] :radio[name='type']").change(function() {
		if ($(this).val()=="moptions") $(".valuesSection").show(); else $(".valuesSection").hide();
	});
	
	//------------------------------------------		ADD HOVERS FOR QUESTION ELEMENTS
	$("#answers li").each(function() { addHover($(this)); });
	$("#options li").each(function() { addHover($(this)); });
	$("#values li").each(function() { addHover($(this)); });
	
});

function addHover(item) {
	$(item).hover(function() {
		$(this).addClass("pollPageElementsHover");
	}, function() {
		$(this).removeClass("pollPageElementsHover");
	});
}

function removeAnswer(icon, area) {
	$(icon).parents("li").remove();
	updateNumerators(area);
}

function updateNumerators(area) {
	var idx = 0;
	$("#"+area).children().each(function() {
		$(this).find("input.numerator:checkbox").val(idx);
		$(this).find("textarea.numerator").attr("name", area+"Value["+idx+"]");
		idx++;
	});
}