
var MultiStepWizard = Class.create();

MultiStepWizard.prototype = {
	initialize: function(options) {  
		if (typeof(options) == "undefined") { options = {} }
		if (typeof(options['hide_on_setup']) == "undefined") { options['hide_on_setup'] = true }
		if (typeof(options['wrapperClass']) == "undefined") { options['wrapperClass'] = 'multi-step-wizard' }
		if (typeof(options['stepPrefix']) == "undefined") { options['stepPrefix'] = 'step-' }
		if (typeof(options['numOfSteps']) == "undefined") { options['numOfSteps'] = 1 }
		  
		this.current_step_index = 1;
		this.wrapperClass = options['wrapperClass'];
		this.stepPrefix = options['stepPrefix'];
		this.steps = {};
		
		for(var i=0;i<options['numOfSteps'];i++) { 
			var index = (i + 1);
			this.steps[index.toString()] = $$(this.selectorFor(index))[0];
			
			if (options['hide_on_setup'] == true && index != 1) {
				this.steps[index.toString()].hide();
			}
		}
		
		this.setAsCurrentStep(this.steps['1']);
	},
	
	//
	// selectorFor('1')
	//
	selectorFor: function(index) {
		return ('.' + this.wrapperClass + ' .' + this.stepPrefix + index);
	},
	
	//
	// currentStep()
	//
	currentStep: function() {
		this.current_step = $$(this.selectorFor(this.current_step_index))[0];
		return this.current_step;
	},
	
	//
	// nextStep()
	//
	nextStep: function() {
		var selector = this.selectorFor((this.current_step_index + 1));
		this.next_step = $$(selector)[0];
		if (!this.next_step) { 
			this.current_step_index = 1; 
			this.next_step = $$(this.selectorFor(this.current_step_index))[0]; 
		}
		
		return this.next_step;
	},

	//
	// prevStep()
	//
	prevStep: function() {
		var selector = this.selectorFor((this.current_step_index - 1));
		this.prev_step = $$(selector)[0];
		if (!this.prev_step) { 
			initial_i = 0;
			for(var step_index in this.steps) { initial_i += 1; }
			
			this.current_step_index = initial_i;
			this.prev_step = $$(this.selectorFor(this.current_step_index))[0];
		}

		return this.prev_step;
	},
	
	//
  //  nextWizardStep()
  //
  nextWizardStep: function() {
		this.nextStep().show(); 
		this.currentStep().hide();
		this.setAsCurrentStep(this.next_step);
		return true;
	},

	//
  //  prevWizardStep()
  //
  prevWizardStep: function() {
	  this.prevStep().show();
	  this.currentStep().hide();
		this.setAsCurrentStep(this.prev_step);
	},
	
	//
	// setAsCurrentStep(this.next_step)
	//
	setAsCurrentStep: function(step) {
		for(var step_index in this.steps) { 
			if (this.steps[step_index] == step) { this.current_step_index = parseInt(step_index); }
		}
	}
}


// To initialize: document.observe('dom:loaded', function () { multiStepWizard = new MultiStepWizard(); });
