Question = Class.create();
Object.extend(Question.prototype,{

	initialize: function(id,params){

		this.id = id;
		this.status            = params.status;
		this.question          = params.question;
		this.feedback          = params.feedback;
		this.order             = params.order;
		this.type_id           = params.type_id;
		this.type              = params.type;
		this.number_of_answers = 0;

		this.answers = Array();

	},

	object_id: function(){
		return 'question_' + this.id;
	},

	toggle: function(){

		$('question_' + this.id + '_slideout').toggle();
		$('question_' + this.id + '_manage_toggle').blur();

	},

	toggle_status: function(){

		var new_status = (this.status == 'active') ? 'inactive' : 'active';

		new Ajax.Request('question_status_toggle.php', {
			method: 'post',
			parameters: { question_id: this.id, status: new_status }
		});

	},

	remove_answer: function(answer_id){
		eval('delete answer_' + answer_id);
		this.number_of_answers -= 1;
	},

	add_answer: function(answer_id){
		this.answers.push( answer_id );
		this.number_of_answers += 1;
	},

	remove: function(){

		new Ajax.Request('question_delete.php', {
			method: 'post',
			parameters: { question_id: this.id }
		});

	},

	clear: function(){

		$( this.object_id() + '_row').remove();
		$( this.object_id() + '_slideout').remove();
// 		this.question().remove_answer( this.id );
	},

	repaint: function(){

		var img_src = $('question_' + this.id + '_status_image').src;

		if (this.status == 'active'){
			$('question_' + this.id + '_status_image').src = img_src.replace(/\/inactive/,'/active');
			$('question_' + this.id + '_status_toggle').update('Make<br/>Inactive');
			$('question_' + this.id + '_status_toggle').blur();
		} else {
			$('question_' + this.id + '_status_image').src = img_src.replace(/\/active/,'/inactive');
			$('question_' + this.id + '_status_toggle').update('Make<br/>Active');
			$('question_' + this.id + '_status_toggle').blur();
		}

		$( this.object_id() + '_question_text').update( this.question );
		$( this.object_id() + '_type_text').update( this.type );
		$( this.object_id() + '_order_text').update( this.order );
		$( this.object_id() + '_number_of_answers_text').update( this.number_of_answers );

	},

	populate_type_select: function(container_id){

		new Ajax.Updater(container_id, 'type_options.php',{
			method: 'get',
			parameters: {
				type_id: this.type_id
			}
		});

	},

	append: function( tr_el ){

		var last_tr = $('question_' + this.id + '_answer_add_control_row');
		last_tr.insert({before: tr_el});
	},

	clear_correct: function(){

		this.answers.each(function(answer_id) {
			eval('answer_' + answer_id).clear_correct();
		});


	},

	open_new_answer_box: function(){

		this.new_answer_box = new Box('new_answer_' + this.id,{header:'New Answer Choice',body: this.new_answer_box_body() });
		this.new_answer_box.open();

	},

	close_new_answer_box: function(){
		this.new_answer_box.close();
		this.new_answer_box = false;
	},

	add_new_answer: function(){

		this.new_answer_box.show_activity();

		var answer = $F('answer_new_form:answer');

		if (answer){
			new Ajax.Request('answer_new.php', {
				method: 'post',
				parameters: { question_id:  this.id,
							  answer: answer
				}
			});
		} else {
			alert('all values must be provided');
		}

	},


	new_answer_box_body: function(){

		var form_html = '<form>';
		form_html += '<div class="form_element">';
			form_html += '<div class="label">Answer</div>';
			form_html += '<div class="input"><textarea id="answer_new_form:answer" name="answer"></textarea></div>';
		form_html += '</div>';
		form_html += '<div class="form_control">';
			form_html += '<span onclick="' + this.object_id() + '.add_new_answer();">Update</span>';
			form_html += '<span onclick="' + this.object_id() + '.close_new_answer_box();">Close</span>';
		form_html += '</div>';
		form_html += '</form>';

		return form_html;

	},

	open_edit_box: function(){

		this.edit_box = new Box('edit_question_' + this.id,{header:'Edit Question',body: this.edit_box_body() });
		this.edit_box.open();

		this.populate_type_select('question_update_form:type_id');

	},

	close_edit_box: function(){
		this.edit_box.close();
		this.edit_box = false;
	},

	update: function(){

		this.edit_box.show_activity();

		var question = $F('question_update_form:question');
		var feedback = $F('question_update_form:feedback');
		var type_id  = $F('question_update_form:type_id');

		if (question && feedback && type_id){
			new Ajax.Request('question_update.php', {
				method: 'post',
				parameters: { question_id: this.id,
							  question:    question,
							  feedback:    feedback,
							  type_id:     type_id
				}
			});
		} else {
			alert('all values must be provided');
		}

	},

	edit_box_body: function(){

		var form_html = '<form>';
		form_html += '<div class="form_element">';
			form_html += '<div class="label">Question</div>';
			form_html += '<div class="input"><textarea id="question_update_form:question" name="question">' + this.question + '</textarea></div>';
		form_html += '</div>';
		form_html += '<div class="form_element">';
			form_html += '<div class="label">Feedback</div>';
			form_html += '<div class="input"><textarea id="question_update_form:feedback" name="feeback">' + this.feedback + '</textarea></div>';
		form_html += '</div>';
		form_html += '<div class="form_element">';
			form_html += '<div class="label">Graphic Type</div>';
			form_html += '<div class="input"><select id="question_update_form:type_id" name="type_id"></select></div>';
		form_html += '</div>';
		form_html += '<div class="form_control">';
			form_html += '<span onclick="' + this.object_id() + '.update();">Update</span>';
			form_html += '<span onclick="' + this.object_id() + '.close_edit_box();">Close</span>';
		form_html += '</div>';
		form_html += '</form>';

		return form_html;

	}

});