$(function(){
	function QuestionSingle(){
		var that = this, output = {question: null, term: null, badget: null}, out = {name:'', date:'', question: '', badget: 1};
		var setDate = function(){
			var d = new Date();
			out.date = ', am ' + d.getDate() + '.' + (d.getMonth() + 1) + '.' + d.getFullYear()
		};
		this.setOutput = function(obj){
			output = obj;
		};
		this.setInputs = function(inps){
			if(inps.age != undefined && inps.age.length && inps.country != undefined && inps.country.length){
				$(inps.age).keyup(function(){
						updateOutput('age', this.value)
					}).blur(function(){
						updateOutput('age', this.value)
					});
				out.age= '';
				$(inps.country).change(function(){
						updateOutput('country', this.options[this.value].text)
					}).blur(function(){
						updateOutput('country', this.options[this.value].text)
					});
				out.country= '';
			}

			$(inps.name).keyup(function(){
					updateOutput('name', this.value)
				}).blur(function(){
					updateOutput('name', this.value)
				});
			$(inps.question).keyup(function(){
					updateOutput('question', this.value)
				}).blur(function(){
					updateOutput('question', this.value)
				});


			for(var i in out){
				if(inps[i] != undefined && inps[i].length){
					if(i == 'country'){
						var inp = $(inps[i])[0];
						if(inp != undefined){
							if(inp.options[inp.selectedIndex].value != '')
								updateOutput(i, inp.options[inp.selectedIndex].text);
						}
					} else {
						updateOutput(i, $(inps[i]).val());
					}
				}
			}

			$(inps.thema).change(function(){
				output.badget.removeClass('question-badget-' + out.badget);
				updateOutput('badget', this.value)
			});
			updateOutput('badget', $(inps.thema).val());

		};
		var updateOutput = function(which, value){
			if(out[which] != value){
				out[which] = value;
				if(which == 'question'){
					$(output.question).html('“<span>' + out.question + '</span>”');
				} else if(which == 'badget') {
					output.badget.addClass('question-badget-' + out.badget);
				} else {
					if(typeof(out.age) != 'undefined'){
						$(output.term).html(out.name + ', ' + out.age + ' aus ' + out.country + out.date);
					} else {
						$(output.term).html('<span class="user">' + out.name + '</span>' + out.date);
					}
				}
			}
		};
		setDate();
	}

	var questionSingle = new QuestionSingle();

	function setEvents(html){
		var fieldset = $('.question-field', html);
		if(fieldset.length){
			var preview, output, quest, badget;
			preview = $('<h3>Vorschau</h3><div class="question-single"><dl><dt/><dd><blockquote></blockquote><span class="question-badget"></span></dd></dl></div>');
			var obj = {
				term: $('dt', preview),
				question: $('blockquote', preview),
				badget: $('.question-badget', preview)
			};
			$('#question-preview').append(preview);
			questionSingle.setOutput(obj);
			questionSingle.setInputs({
				name: $('#nickname'),
				age: $('#age'),
				country: $('#land'),
				question: $('#frage'),
				thema: $('#thema')
			})
		}
//		$('form', html).submit(function(){
//			var that, action, settings;
//			that = $(this);
//			action = that.attr('action');
//			$.post(action, that.serialize()+'&tx_fragenkatalog_pi1%5Bsubmit%5D=submit', function(data){
//				var html_new = $('.tx-fragenkatalog-pi1',data);
//				html.replaceWith(html_new);
//				setEvents(html_new);
//			});
//			return false;
//		});
	}
	setEvents($('.tx-fragenkatalog-pi1'));

	$('#question-link', '#question-comments-link').focus(function(){
		this.select();
	});
});

$(function(){
	$('.question-single form').submit(function(){
		var that = $(this),
		action = that.attr('data-xml');
		$.post(action, that.serialize()+'&tx_fragenkatalog_pi4%5Bsubmit%5D=submit', function(data){
			$('.votes .value').text($.trim(data));
		});
		return false;
	})
});

//$(function(){
//	function update(html){
////		console.debug(html);
//		if(html.length){
//			$('.paginator a', html).click(function(){
//				$.get($(this).attr('data-xml'), function(data){
//					console.debug($('*', html).remove());
//					console.debug($('list', data));
//					var content = $($('list', data).text());
//					console.debug(content);
//					html.append(content);
//					update(html);
//				});
//				return false;
//
//			});
//		}
//	}
//	update($('.tx-fragenkatalog-pi3 .xmlData'));
//});

