var answers = new Hash;

var Answer = Class.create({
	initialize: function(text){
		this.text = text;
		this.options = Object.extend({
			file_name: '',
			width: 0,
			height: 0
		}, arguments[1] || { });
	}
});
var Question = Class.create({
	initialize: function(text){
		this.text = text;
		this.answers = new Array;
		this.control = new Element('span').update('Управляющий елемент не создан!');
		this.options = Object.extend({
			type:    'radio_group',
			direction:    'vertical',
			required_number_of_answer: 1,
			file_name: '',
			width: 0,
			height: 0,
			is_need_for_step: false
		}, arguments[1] || { });
	},
	addAnswer: function(answer){ this.answers.push(answer); },
	toElement: function(){
		var question_container = new Element('div', { className: 'question_container' });
		if (this.options.file_name!='') question_container.insert(new Element('h4', { className: 'question' }).update(this.text)).insert(new Element('p', { className: 'question_image' }).insert(new Element('img', { src: this.options.file_name, alt: '', title: '', width: this.options.width, height: this.options.height })));
		else question_container.insert(new Element('h4', { className: 'question' }).update(this.text));
		var answer_container = new Element('div', { className: 'answer_container' });
		if (this.options.direction=='horisontal') answer_container.addClassName('horizontal-direction');
		switch (this.options.type) {
		case 'radio_group': this.control = new RadioGroup(this.answers);
		break;
		case 'check_box': this.control = new Checkbox(this.answers, this.options.required_number_of_answer);
		break;
		case 'select': this.control = new SelectTag(this.answers);
		break;
		case 'textarea': this.control = new Textarea();
		break;
		case 'image_radio_group': this.control = new ImageRadioGroup(this.answers, ( this.options.direction=='table' ? 'table' : 'list' ));
		break;
		case 'image_check_box': this.control = new ImageCheckbox(this.answers, ( this.options.direction=='table' ? 'table' : 'list' ), this.options.required_number_of_answer);
		break;
		}
		answer_container.insert(this.control);
		question_container.insert(answer_container);
		return question_container;
	}
});
var Test = Class.create({
	initialize: function(container){
		this.questions = new Array;
		this.container = container;
		this.current_question = 1;
		this.questions_count = 0;
		this.options = Object.extend({
			web_dir_name: ''
		}, arguments[1] || {});
	},
	addQuestion: function(question){this.questions.push(question);},
	start: function(){
		this.container = $(this.container);
		this.questions_count = this.questions.length;
		var question_counter = new Element('span',{style: 'padding-right:0px;'}).update('1');
		$('current_question_container')
			.insert(new Element('span').update('&nbsp;из&nbsp;'+this.questions.size()))
			.insert(question_counter)
			.insert(new Element('span',{style: 'padding-right:0px;'}).update('Вопрос:&nbsp;'));
		
		var button = new Element('button', { type: 'submit', src: this.options.web_dir_name+'/controls/next_button.png', alt: 'Следующий вопрос', title: 'Следующий вопрос', id: 'next_question_button' });
		button.observe('click', this.nextQuestion.bindAsEventListener(this));
		this.container.insert(new Element('div', { id:'current_question', className:'survey1' }).insert(this.questions[this.current_question-1])).insert(new Element('div', { id: 'error_message', className: 'blank_message survey1'}).update('&nbsp;')).insert(button);
		this.question_counter = question_counter;
	},
	nextQuestion: function(event){
		var cur_value = this.questions[this.current_question-1].control.hasValue();
		if (cur_value){
			$('error_message').style.backgroundColor = '#e0efb7';
			if (this.questions[this.current_question-1].options.is_need_for_step){
				answers.set(this.questions[this.current_question-1].options.id, cur_value);
			}
			this.current_question++;
			this.question_counter.update(this.current_question);
			if (this.current_question<=this.questions_count){
				$('error_message').removeClassName('error_message').update('&nbsp;');
				$('current_question').update(this.questions[this.current_question-1]);
			}else {
				this.finish();
			}
		}else{
			var required_number_of_answer = this.questions[this.current_question-1].options.required_number_of_answer;
			var error_text = '';
			if (required_number_of_answer==1) error_text = 'Нужно выбрать 1 вариант ответа';
			else error_text = 'Ответов должно быть '+required_number_of_answer;
			$('error_message').update(error_text).addClassName('error_message');
			new Effect.Highlight('error_message', { startcolor: '#ff9999', endcolor: '#ffdddd', restorecolor: '#ffdddd'});
		}
	},
	finish: function(){
		if($('current_question')) $('current_question').remove();
		if($('questions_counter')) $('questions_counter').remove();
		if($('error_message')) $('error_message').remove();
		if($('next_question_button')) $('next_question_button').remove();
		$('current_question_container').hide();
		this.runUserSteps();
	},
	runUserSteps:function(){
		answers.each(function(pair){
			$('step_1_answer_c_'+pair.key).update(pair.value);
		});
		setPreLoader(1,1);
	} 
});

function setPreLoader(step_number, step_item_number) {
	var step_container = $('step_'+step_number);
	if (!step_container){
		$('download_button').appear();
	}else{
		var step_item_container = $('step_'+step_number+'_item_'+step_item_number);
		var loader = $('step_'+step_number+'_loader_'+step_item_number);
		var percent = $('step_'+step_number+'_percent_'+step_item_number);
		var preloader = $('step_'+step_number+'_preloader_'+step_item_number);
		
		step = random(1, 3);
		
		if (step_item_number == 1 ) { new Effect.Appear(step_container,{duration:0.5}); }
	
		if (!step_item_container){
			setPreLoader(step_number+1,1);
		}else if (!loader){
			new Effect.Appear(step_item_container,{duration:0.5,afterFinish:function(){ setPreLoader(step_number,step_item_number+1); }});
		}else if (loader.getWidth()==0){
			new Effect.Parallel([new Effect.Appear(step_item_container,{sync:true}), new Effect.Appear(preloader ,{sync:true}), new Effect.Appear(percent ,{sync:true})], {duration: 0.5, afterFinish: function(){
				loader.style.width = '1px';
				setPreLoader(step_number, step_item_number);
			}});
		}else if (loader.getWidth()+step >= 96 ){
			loader.style.width = '96px';
			percent.update('100%');
			new Effect.Parallel([new Effect.Fade(percent,{sync:true}),new Effect.Fade(preloader,{sync:true})],{duration:0.2,afterFinish:function(){
				new Effect.Appear('step_'+step_number+'_answer_'+step_item_number,{duration:0.2});
				setPreLoader(step_number, step_item_number+1);
			}});
		} else  {
			loader.style.width = loader.getWidth() + step + 'px';
			percent.update(Math.round(loader.getWidth()*100/96)+'%');
			setTimeout('setPreLoader('+step_number+','+step_item_number+')', random(50, 100) );
		}
	}
}