var Wizard = Class.create(EventClass, {
	'initialize': function(container) {
		this.container = $(container);
		this.stages = [];
		this.stageIndex = 0;
		this.data = {};
		
		jQuery.history.init(this.callback(function(url) {
			url = url || 0;
			var stageIndex = parseInt(url);
			//alert(stageIndex);
			
			if (this.loaded) {
				this.loadStage(stageIndex);
			}
			else {
				if (stageIndex > 0) {
					this.goTo(0);
				}
			}
		}));
	},
	'configure': function() {
		
	},
	'addStage': function(stage) {
		stage.data = this.data;
		
		this.stages.push(stage);
		
		if (this.stages.length > 1) {
			stage.hide();
		}
		
		stage.bind('next', this.callback(function() {
			this.next();
		}));
		
		stage.bind('back', this.callback(function() {
			this.back();
		}));
	},
	'currentStage': function() {
		return this.stages[this.stageIndex];
	},
	'begin': function() {
		this.configure();
		jQuery.each(this.stages, function(i) { this.configure() });
		this.loaded = true;
	
		var stage = this.currentStage();
		stage.show();
	},
	'next': function() {
		if (this.stageIndex >= this.stages.length - 1) return;
		this.goTo(this.stageIndex + 1);
	},
	'back': function() {
		if (this.stageIndex <= 0) return;
		this.goTo(this.stageIndex - 1);
	},
	'goTo': function(index) {
		jQuery.history.load(index.toString());
	},
	'loadStage': function(index) {
		if (this.stageIndex == index) return;
	
		var prevStage = this.currentStage();
		this.stageIndex = index; //alert(this.stageIndex); alert(index);
		var nextStage = this.currentStage();
	
		var shownCallback = this.callback(function() {				
			prevStage.container.hide();
			nextStage.unbind('shown', shownCallback);
		});
	
		nextStage.bind('shown', shownCallback);
		nextStage.show();
	}
});

var WizardStage = Class.create(EventClass, {
	'initialize': function(container) {
		this.container = jQuery(container);
		
		this.container.find('.WizardBackButton').click(this.callback(function() {
			this.trigger('back');
		}));
	},
	'configure': function() {
		
	},
	'hide': function() {
		this.container.hide();
	},
	'show': function() {
		this._shown();
	},
	'_shown': function() {
		this.container.show();
		this.trigger('shown');
	}
});
