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

	initialize: function(id,params){

		this.id = id;
		this.answer = params.answer;
		this.question_id = params.question_id;

	},

	question: function(){
		return eval('question_' + this.question_id);
	},

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

	repaint: function(){

		$( this.object_id() + '_answer_text').update( this.answer );

	},

	set_as_correct: function(){

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

	},

	mark_as_correct: function(){

		this.question().clear_correct();
		$( this.object_id() + '_correct_img').src = $( this.object_id() + '_correct_img').src.replace(/blank/,'active');

	},

	clear_correct: function(){
		$( this.object_id() + '_correct_img').src = $( this.object_id() + '_correct_img').src.replace(/active/,'blank');
	},

	clear: function(){
		$( this.object_id() + '_row').remove();
		this.question().remove_answer( this.id );
	},

	question: function(){
		return eval('question_' + this.question_id);
	},

	remove: function(){

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

	},

	update: function(){

		this.edit_box.show_activity();

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

		if (answer){
			new Ajax.Request('answer_update.php', {
				method: 'post',
				parameters: { answer_id: this.id,
							  answer: answer     }
			});
		}

	},

	update_thubmanil: function( image_id ){

		if ( $( this.object_id() + '_thumbnail').src.match(/blank.png/) ){
			$( this.object_id() + '_thumbnail').src = $( this.object_id() + '_thumbnail').src.replace(/img\/blank.png/,'assets/answer_img/' + this.id + '_' + image_id + '_thumbnail.png');
		} else {
			$( this.object_id() + '_thumbnail').src = $( this.object_id() + '_thumbnail').src.replace(/_[\d]+.png/,'_' + image_id + '.png');
		}

	},

	open_edit_box: function(){

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

	},


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


	edit_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_update_form:answer" name="answer">' + this.answer + '</textarea></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;

	},

	open_graphic_upload_box: function(){

		this.graphic_upload_box = new Box('graphic_upload_box_' + this.id,{header:'Upload Answer Graphic',body: this.graphic_upload_box_body() });
		this.graphic_upload_box.open();

	},

	close_graphic_upload_box: function(){
		this.graphic_upload_box.close();
		this.graphic_upload_box = false;
	},

	update_image: function(){

// 		this.graphic_upload_box.show_activity();
		eval('document.' + this.object_id() + '_image_upload.submit();');

	},

	graphic_upload_box_body: function(){

		var form_html = '<form target="uploader" name="' + this.object_id() + '_image_upload" action="answer_image_upload.php" method="post" enctype="multipart/form-data">';
		form_html += '<div class="form_element">';
			form_html += '<div class="label">Answer</div>';
			form_html += '<div class="input">' + this.answer + '</div>';
		form_html += '</div>';
		form_html += '<div class="form_element">';
			form_html += '<div class="label">Image File</div>';
			form_html += '<div class="input"><input type="file" name="answer_image"></div>';
		form_html += '</div>';
		form_html += '<div class="form_control">';
			form_html += '<input type="hidden" name="answer_id" value="' + this.id + '" />';
			form_html += '<span onclick="' + this.object_id() + '.update_image();">Upload</span>';
			form_html += '<span onclick="' + this.object_id() + '.close_graphic_upload_box();">Close</span>';
		form_html += '</div>';
		form_html += '</form>';

		form_html += '<iframe name="uploader" src="about:blank" style="display:none;" />';


		return form_html;

	}

});