/*
	Ratings v0.1 for Te Pou training online case studies
	Requires MooTools 1.2
	
	Author: Andrew Ferri <andrew@blacksheepcreative.co.nz>
	Date: 31 October 2008
*/

var Ratings = new Class({
	Implements: [Options, Events],
	
	options: {
		transition: Fx.Transitions.Sine.easeOut,
		speed: 700,
		container: 'rating-form',
		qContainer: 'li.question-container',
		gContainer: 'div.glossary',
		togglers: '',
		closers: 'div.glossary h2 a',
		openers: 'label.question a, div.top',
		nextTogglers: 'ul.options input, ul.options-specify input',
		current_question: 1
	},
	
	initialize: function(options)
	{
		this.setOptions(options);
		this.current_question = this.options.current_question;
		this.createSliders();
		this.createEvents();
	},
	
	createSliders: function()
	{
		//this.windowSlider = new Fx.Scroll(window, {transition: this.options.transition, speed: this.options.speed});
		
		$$(this.options.qContainer).each(function(el){
			glossary = el.getElement(this.options.gContainer);
			el.slider = new Fx.Slide(glossary, {transition: this.options.transition, speed: this.options.speed});
			if (glossary.hasClass('display') == true)
			{
				el.slider.show();
			} else
			{
				el.slider.hide();
			}
			glossary.addClass('display');
			glossary.getParent().addClass('slider');
		}.bind(this));
	},
	
	createEvents: function()
	{
		$$(this.options.qContainer).each(function(el){
			el.getElements(this.options.openers).each(function(opener){
				opener.addEvent('click', function(e){
					if (opener.get('tag') == 'a')
					{
						e = new Event(e).stop();
					}
					this.hideAll(el);
				}.bind(this));
			}.bind(this));
			
			el.getElements(this.options.nextTogglers).each(function(opener){
				opener.addEvent('click', function(e){
					if (opener.get('tag') == 'a')
					{
						e = new Event(e).stop();
					}
					var name = opener.getProperty('name');
					var current_question = name.replace('q','');
					//alert(this.current_question+' : '+current_question);
					//if (current_question <= this.current_question)
					//{
					if ((el.getElement('ul.options-specify')) && (opener.getParent().getParent().getParent().hasClass('options-specify') == false) && (opener.value == 7))
					{
						var pause = true;
						//console.log(opener.getParent().getParent().getParent());
					} else
					{
						var pause = false;
					}
					
					if (pause == false)
					{
						el.addClass('complete');
						this.hideAll(el.getNext(),1);
						this.checkError(el);
						if (this.current_question == current_question)
						{
							this.current_question++;
						}
					}
					//} else
					//{
						//opener.checked = false;
						//alert('Please fill in the answers in order');
					//}
				}.bind(this));
			}.bind(this));
			
			el.getElements(this.options.closers).each(function(opener){
				opener.addEvent('click', function(e){
					if (opener.get('tag') == 'a')
					{
						e = new Event(e).stop();
					}
					this.hide(el);
				}.bind(this));
			}.bind(this));
		}.bind(this));
	},
	
	hideAll: function(show,toggle)
	{
		$$(this.options.qContainer).each(function(el){
			if (el != show)
			{
				el.slider.slideOut();
				el.removeClass('selected');
			} else
			{
				if (toggle == 1)
				{
					el.slider.slideIn();
					el.addClass('selected');
				} else
				{
					el.slider.toggle();
					el.toggleClass('selected');
				}
			}
		}.bind(this));
		if (show)
		{
			//this.windowSlider.toElement(show.getPrevious());
		}
	},
	
	hide: function(el)
	{
		el.slider.slideOut();
	},
	
	checkError: function(li)
	{
		if (li.hasClass('error') == true)
		{
			li.morph = new Fx.Morph(li.getElement('label.question'), {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
			li.amorph = new Fx.Morph(li.getElement('label.question a'), {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
			li.morph.start({
				'background-color': '#ffffff',
				'color': '#000000'
			});
			li.amorph.start({
				'color': '#000000'
			});
			li.removeClass('error');
		}
	}
});