/**
 * Classe representant le Wizard de commande
 */
function CommandWizard(){
	var self = this;
	
	/**
	 * Soumet un formulaire
	 */
	this.submitForm = function(form, submitName){
		var success = function(json){
			self.refresh(json);
		};
		
		var conf = {
			success : 	success,
			data : 		form.serializeArray(),
			url : 		form.attr('action')
			//error:		erreur
		};
		
		//ajout de la valeur bouton submit pour reconnaitre quel formulaire a été envoyé
		conf.data.push({name:submitName, value:true});		
		core.ajax(conf);
		return false;
	}
	
	/*================== Etape 1 et 2 ===========*/
	
	/**
	 * "coche" le bouton radio qui est cachÃ© et qui suit le lien "selectionner"
	 */
	this.selectProduct = function(){
		$('.wg_command_step_produit_selectionner').parents('.wg_command_step_produit').removeClass('wg_command_selected_product');
		$('.wg_command_step_produit_selectionner').next().removeAttr('checked');
		
		$(this).next().attr('checked', 'checked');
		$(this).parents('.wg_command_step_produit').addClass('wg_command_selected_product');
		$('#wg_wizard_next_button').click();
		return false;
	}
	
	this.productHover = function(){
		$(this).parents('.wg_command_step_produit').addClass('wg_command_hover_product');
	}
	this.productOut = function(element){
		$(this).parents('.wg_command_step_produit').removeClass('wg_command_hover_product');	
	}
	
	/**
	 * initialise le javascript sur les elements de l'etape 1 et 2
	 */
	this.initProductsStep = function(){
		$('.wg_command_step_produit_selectionner').click(self.selectProduct);
		$('.wg_command_step_produit_selectionner').hover( self.productHover, self.productOut );
	}
	
	
	
	
	
	/*================== Etape 3 ================*/


	this.loadOption = function(){
		//var params = new Object();
		//params.dossierId = dossier_id;
		//urlLoadOptions et urlLoadOptionsAbonnement sont définis dans le template Smarty
		$('#wg_recapitulatif').load( urlLoadOptions, $('#wg_form_options').serializeArray(), function(){ scrollRecap() } );	
		$('#wg_recapitulatif_abonnement').load( urlLoadOptionsAbonnement, $('#wg_form_options').serializeArray(), function(){ scrollRecap() } );
	}

	this.disableAlpha = function(event){
		switch (event.keyCode) {
			//touches acceptées
			case 8: //backspace
			case 46: //suppr
			case 37: //fleche gauche
			case 39: //fleche droite
			case 118: //touche F5
				break;
			default :
				//sinon aon autorise que les chiffres
				//IE
				if (navigator.appName == "Microsoft Internet Explorer") {
					if(event.keyCode < 48 || event.keyCode > 57) {
						event.cancelBubble = true;
						event.returnValue = false;
					}				
				} 
				//DOM
				else {
					if(event.which < 48 || event.which > 57) {
						event.preventDefault();
						event.stopPropagation();
						return false;
					}
				}
		}
	}
	
	this.initOptionsStep = function(){
		//$('#wg_form_options input').change(self.loadOption);
		$('#wg_form_options input').click(self.loadOption);
		$('#wg_form_options input').keypress(self.disableAlpha);
	}
	
	/*================== Etape 4 ================*/
	
	
	/**
	 * envoie le formulaire login en ajax et
	 * passe Ã  lÃ©tape suivante si les identifiants sont correctes
	 */
	this.logClient = function(){
		return self.submitForm($(this), 'submitLogin');
	};
	
	/**
	 * 	envoie le formulaire d'inscription en ajax
	 *	et active le bouton continuer si l'utilisateur a bien Ã©tait crÃ©Ã©
	 */
	this.createClient = function(){
		return self.submitForm($(this), 'submitInscription');
	};
	
	/**
	 * 	place les bonnes actions sur le boutons dans formulaires
	 *	formCLientLOgin et formClientInscription
	 */
	this.initConnexionStep = function(){
		$('#wg_command_form_login').submit(self.logClient);
		$('#wg_command_form_inscription').submit(self.createClient);
	};
	
	/* =================== Etape 5 =================== */
	
	/**
	 * 	envoie le formulaire de paiement
	 */
	this.submitPayment = function(){
		return self.submitForm($(this), 'submitPayment');
	};
	
	/**
	 * 	Initialise l'Ã©tape 5
	 * 	place les bonnes actions sur le bouton etape suivante
	 *  a savoir : soumission du formulaire de paiement
	 */
	this.initPaymentStep = function(){
		//$('#wg_command_form_payment').submit(self.submitPayment);
	};
}

var wizard = new Wizard( {classPreffix:'wg_'} );
var commandWizard = new CommandWizard();

//commandWizard 'hÃ©rite' de wizard
$.extend(commandWizard, wizard);

/**
 * Initialise le wizard
 * @return
 */
var initWizard = function(){
	commandWizard.init();
}

/**
 * Initialisation du wizard au chargement de page
 */
$(initWizard);
