// requires jquery >= 1.4 !!!  ( wegens jQuery.param )
function survey_data(id,$frm) {
	this.id = id;
	this.$frm = $frm;
	this.data = null;
	survey_data.surveys[id] = this;
}
survey_data.surveys = {};
survey_data.do_processSurveyData = true;

survey_data.ready = function() {
	$.metadata.setType("attr", "validate");
	$('.survey_form').each(function() {
		var $f = $(this);
		var id = $f.find( 'input[name=survey_id]' ).val();
		var self = new survey_data(id, $f);
		$f.find("input:submit").click(function() {
			this.form.elements.action.value = $(this).attr('name');
			if ( this.form.elements.action.value=='prev' ) {
				$(this.form).find('input[validate],textarea[validate]').attr('validate','');
			}
			
		});
		$f.validate({ submitHandler: function() { self.validate_submit.call(this); } });
		if ( survey_data.do_processSurveyData )	self.processSurveyData();
	});
};
survey_data.prototype.before_process_data = function() {
	this.$frm.find('.survey_error').html('');
}
survey_data.prototype.do_start = function(display) {
	// We are at the first page of the survey, so only show start-button.
	this.$frm.find('input[name=prev],input[name=next],input[name=answer],input[name=save],input[name=restart]').hide();
	if ( display!=null ) this.$frm.find('.survey_page').html( display ? display : '' );
	if ( this.$frm.find('input[name=start]').length==0 ) {
		// no startbutton -> to first page.
		this.$frm.find('input[name=action]').val('start');
		this.$frm.submit();
	} else {
		this.$frm.find('input[name=start]').show();
	}
}
survey_data.prototype.do_end = function() {
	this.$frm.find('input[name=prev],input[name=next],input[name=answer],input[name=save],input[name=start]').hide();
	this.$frm.find('input[name=restart]').show();
	this.$frm.find('.survey_page').html(this.data.display ? this.data.display : '');
}
survey_data.prototype.do_question = function() {
	this.$frm.find('input[name=start],input[name=restart]').hide();
	this.$frm.find('input[name=prev]')[Number(this.data.hide_prev_button)?'hide':'show']();
}
survey_data.prototype.after_question = function( ) {
	if ( Number(this.data.has_answers) ) {
		this.$frm.find('input[name=answer]')[Number(this.data.modify_answer_allowed)?'show':'hide']();
		this.$frm.find('input[name=next]').show();
	} else {
		if ( this.$frm.find('.survey_page').find('input').length ) {
			this.$frm.find('input[name=answer]').show();
			this.$frm.find('input[name=next]').hide();
		} else {
			this.$frm.find('input[name=answer]').hide();
			this.$frm.find('input[name=next]').show();
		}
	}
}
survey_data.prototype.display_data = function() {
	this.$frm.find('.survey_page').html( this.data.display );
}
survey_data.prototype.display_error = function() {
	this.$frm.find( '.survey_error' ).html( this.data.error );
}
survey_data.prototype.display_feedback = function() {
	var self = this;
	$.each( this.data.question_ids, function(i,id) {
		var rem = '';
		if ( self.data.is_correct!=null && self.data.is_correct[id]!=null ) {
			rem = self.$frm.find('.survey_remark_'+(Number(self.data.is_correct[id]) ? 'correct' : 'wrong' )).html();
			if ( !Number(self.data.is_correct[id]) && self.data.feedback!=null && self.data.feedback[id] ) rem += self.data.feedback[id];
			self.display_question_remark( id, rem );
		} else if ( self.data.feedback!=null && self.data.feedback[id] ) {
			self.display_question_error( id, self.data.feedback[id] );
		} else {
			self.hide_question_feedback( id );
		}
		if ( self.data.is_answered!=null && Number(self.data.is_answered[id]) && !Number(self.data.modify_answer_allowed) ) {
			self.disable_question_input( id );
		}
	})
}
survey_data.prototype.display_question_remark = function( id, str ) {
	this.$frm.find('#question_'+id+' .remark').html( str ).show();
	this.$frm.find('#question_'+id+' .error').hide();
}
survey_data.prototype.display_question_error = function( id, str ) {
	this.$frm.find('#question_'+id+' .remark').hide();
	this.$frm.find('#question_'+id+' .error').html( str ).show();
}
survey_data.prototype.hide_question_feedback = function( id, str ) {
	this.$frm.find('#question_'+id).find('.remark,.error').hide();
}

survey_data.prototype.disable_question_input = function( id ) {
	this.$frm.find('.survey_page').find('input[name=answer'+id+'],textarea[name=value'+id+'],input[name=answer'+id+'[]],textarea[name=value'+id+'[]],input[name=value'+id+'],input[name=value'+id+'[]]').attr('disabled','disabled');
}

survey_data.prototype.validate_submit = function() {
	var $frm = $(this.currentForm);
	var id = $frm.find("[name=survey_id]").val();
	var self = survey_data.surveys[id];
	var validator = this;
	var settings = this.settings;
	$frm.find("input[name=checksum]").val(location.hostname);
	var data = $frm.serialize();
	var sdata = self.data;
	if (sdata) {
		if (sdata.display) delete(sdata.display)
		data += '&' + jQuery.param({'survey_data':sdata});
	}
	$.ajax({
		url : "/jqajax?jqajax=surveyplus:handleForm"
		,dataType: "json"
		,type: "post"
		,data: data
		,success: function (data,statuscode,xhr) {
			if (data && data.status) {
				if ( data.status ) {
					if ( data.restart ) {
						self.processSurveyData( null, data.survey_data.display );
					} else if ( data.survey_data && data.survey_data.surveyplus_id) {
						self.processSurveyData( data.survey_data );
					}
				}
			} else {
				if (data && data.message) {
					if (typeof(Sexy) == "object" && Sexy.alert) {
						Sexy.alert('<h1>Error</h1><p>' + data.message + "</p>");
					} else {
						alert(data.message);
					}
				}
			}
		}
		,error: function (xhr,errorType,errorException) {alert(errorType);}
	});
}
survey_data.prototype.processSurveyData=function(data, display) {
	this.data = data;
	this.before_process_data();
	if ( data == null ) {
		this.do_start(display);
	} else if ( data.ended ) {
		this.do_end();
	} else {
		this.do_question();
		if ( data.error ) {
			this.display_error();
		}
		if ( data.display!=null ) {
			this.display_data();
		}
		if ( data.question_ids!=null ) {
			this.display_feedback();
		}		
		this.after_question();
	}
}
$(document).ready(survey_data.ready);

