/*jslint browser: true, eqeqeq: true */

/**
 * CW Status Checker for redemptions
 * @author gjohnson
 */

(function ($, global) {
	
global.StatusChecker = (function () {
			
	// ---------- private
	
	/**
	 * container element for the modal (fancybox)
	 */
	var appContainer = $("#checker");

	/**
	 * reference to fancybox 
	 */
	var fancy = $.fancybox;
		
	/**
	 * the current step of the process
	 */
	var currentStep = null;
	
	/**
	 * all the steps for the wizard
	 */
	var steps = {
					'begin': {
						template: $("#begin"), binding: function () {
							var opts = appContainer.find('input[type=radio]');
							
							opts.click(function (e) {
								if (this.value === 'no') {
									loadStep('begin-no');
								} else if(this.value === 'yes') {
									loadStep('begin-yes');
								}
							});
						}
					},
					
					'begin-no':{
						template: $('#begin-no'), binding: function () {
							// nuffin yet
						}
					},
					
					'begin-yes':{
						template: $('#begin-yes'), binding: function () {
							var opts = appContainer.find('input[type=radio]');
							
							opts.click(function(){
								loadStep(this.value + 'weeks');
							});
							
						}
					},
					
					'1weeks':{
						template: $('#1weeks'), binding: function () {
							
						}
					},
					
					'2weeks':{
						template: $('#2weeks'), binding: function () {
							
						}
					},
					
					'3weeks':{
						template: $('#3weeks'), binding: function () {
							
						}
					},
					
					'4weeks':{
						template: $('#4weeks'), binding: function () {
							
						}
					}

					
	}; // end steps

	
	/**
	 * loads a step for they given key
	 */
	var loadStep = function (key) {
		
		// desired step and its content
		var step = steps[key];
		var content = step.template.html();
		
		// show busy signal and wait a bit
		fancy.showActivity();

		setTimeout(function () {
			
			//  load our content, than load fancy
			appContainer.html(content);
			fancy(appContainer);
			
			// if the step has a binding, invoke it
			if (typeof step.binding === 'function') {
				step.binding();
			}
			
		}, (currentStep === null) ? 500 : 2500);

		currentStep = key;
	};


	// ---------- public 
	return {

		start: function () {
			loadStep('begin');
		},

		quit: function () {
			currentStep = null;
			appContainer.html('');
		}
	
	};

})();

// modal for status checker
$('#check-status').fancybox({
	titleShow: false,
	scrolling: false,
	overlayOpacity: 0.95,

	onStart: function () { StatusChecker.start(); },
	onClosed: function () { StatusChecker.quit(); }
});
	
	
})(jQuery, window);
