/**
 * @brief	banque
 * @author  Benoit
 * @date	05/05/2010
 */
function LoadPaiement()
{
	$('a[href="#paiement-hipay"]').click( function() {
		$('#infoAllopass').hide();
		$('#MPcontentAP').hide('fast');		
	});
	
	$('a[href="#paiement-tel"]').click( function() {
		$('#infoAllopass').show();
		$('#MPcontentAP').show('fast');
		
		
	});
	
	$('a[href="#paiement-sms"]').click( function() {
		$('#infoAllopass').show();
		$('#MPcontentAP').show('fast');
		
	});
}

function hideAllop()
{
		$('#MPcontentAP').hide();
		$('#infoAllopass').hide();

}

function validerCode(){
		
		//alert('trou du cul');
		
		var data = $('#APform :not(:disabled)').serializeArray();
		div = 'MPcontentAP';
		//envoie de la requete ajax
		$.post('/paiement/valider.ajax', data, function(json)
		{
			if (json.message == '0')
				$('#' + div).html('<p class="error">Votre code est invalide</p>');
			else if(json.message == '1'){
				$('#' + div).html('<p class="success">Vous avez été crédité de 5 crédits pass.</p>');
				document.getElementById("nb-credits").innerHTML =json.credits;
				sound.Play('passage-a-premium');
			}
			else if(json.message== '2'){
				$('#' + div).html('<p class="error">Le code a déjà été utilisé.</p>');
			}				
		}, 'json');
	
}

/* special hipay */
function hipayCreditsChange(e){
	e = $(e);
	var o = eval('p'+e.val());
	hipayBuy(o);
	var html = o.label;
	if(o.free>0)
		html += '<br /><span class="pt">'+o.free+'% de bonus !</span>';

	$('#hipayCredits').html(html);
	if($('#refProduct').length>0)	$('#refProduct').val(e.val());
}

function hipayBuy(o){
	if ($('#amountHipay').length > 0) {
		$('#amountHipay').val(o.usd);
	}
	if($('#nameHipay').length>0)	$('#nameHipay').val('Achat de '+o.label + ' sur www.secteur18.com');
	if($('#orderHipay').length>0)	$('#orderHipay').val(''+o.label + ' ');

}


function Palier(usd, credits, free, label){
	this.usd	=	usd;
	this.credits		=	credits;
	this.free	=	free;
	this.label	=	label;
}

/**
 * Intégration de la banque 2.0.
 * @author Romain Bretécher <romain.b@beemoov.com>
 */

$(document).ready(function() {
	
	/**********************************************************
	 * 
	 * 			Fonctionnement général de la banque.
	 * 
	 *********************************************************/
	
	/**
	 * Change de moyen de paiement lors du clic sur un onglet.
	 */
	$('ul#bank-selection li[class!=active]').live('click', function() {
		$('ul#bank-selection li.active').removeClass('active');
		$(this).addClass('active');
		
		$('.payment-method').hide();
		$('.payment-method[id='+$(this).attr('id')+']').show();
	});
	/**
	 * Affiche les différents pays disponibles.
	 */
	$('a#bank-show-other-countries').live('click', function() {
		$('ul#bank-selection li').hide();
		
		$('.payment-method').hide();
		$('#other-countries').show();
		
		return false;
	});
	
	/**********************************************************
	 * 
	 * 			Méthodes de paiement : Allopass
	 * 
	 *********************************************************/
	
	/**
	 * Modification du doc_id au clic sur un radio.
	 */
	$('input.allopass-select-in-game-currency').live('click', function() {
		$(this).parents('.payment-method').find('input#DOC_ID').val($(this).val());
		
		var currentValue = $(this).parents('.payment-method').find('input[name="DATAS"]').val();
		var elements = currentValue.split('|');
		var newValue = elements[0]+'|'+elements[1]+'|'+$(this).parents('.payment-method').find('#DOC_ID').val()+'|'+elements[3];
		
		$(this).parents('.payment-method').find('input[name="DATAS"]').val(newValue);
	});
	/**
	 * Modification du doc_id au changement dans le select
	 */
	$('select.allopass-select-in-game-currency').live('change', function() {
		$(this).parents('.payment-method').find('input#DOC_ID').val($(this).find(':selected').val());
		
		var currentValue = $(this).parents('.payment-method').find('input[name="DATAS"]').val();
		var elements = currentValue.split('|');
		var newValue = elements[0]+'|'+elements[1]+'|'+$(this).parents('.payment-method').find('#DOC_ID').val()+'|'+elements[3];
		
		$(this).parents('.payment-method').find('input[name="DATAS"]').val(newValue);
	});
	
	/**********************************************************
	 * 
	 * 			Méthode de paiement : Hipay
	 * 
	 *********************************************************/
	
	/**
	 * Modifie l'affiche ainsi que le formulaire lors du changement du montant.
	 */
	$('select#bank-hipay').change(function() {
		updateHipay($(this).parents('.payment-method'));
	});
	/**
	 * Met à jour l'affichage ainsi que le formulaire lors du changement de monnaie In Game (via un clic sur un radio).
	 */
	$('input.select-in-game-currency').live('click', function() {
		updateHipay($(this).parents('.payment-method'));
	});
	/**
	 * Met à jour l'affichage ainsi que le formulaire au changement de monnaie In Game (via la sélection dans une liste).
	 */
	$('select.select-in-game-currency').live('change', function() {
		updateHipay($(this).parents('.payment-method'));
	});
});

/**
 * Met à jour l'affichage de la banque ainsi que le formulaire Hipay.
 * @param {Jquery Object} container Conteneur dans lequel actualiser les données Hipay.
 */
function updateHipay(container) {
	// Il y a deux monnaies In Game, on récupère donc l'ID du palier via des input radio.
	if($('input.select-in-game-currency', container).length > 0) {
		var inGameCurrencyId = $('input.select-in-game-currency:checked', container).val();
	}
	else {
		var inGameCurrencyId = $('select.select-in-game-currency option:selected', container).val();
	}
	// Aucune monnaie InGame
	inGameCurrencyId = (inGameCurrencyId == undefined) ? 0 : inGameCurrencyId;
	
	levelId = $('select.select-level-id option:selected', container).attr('id');
	
	// Récupération du palier In Game.
	var inGameLevel = inGameLevels[levelId][inGameCurrencyId];
	
	// Mise à jour de l'affichage.
	$('span.in-game-value').html(inGameLevel.value+' '+inGameLevel.in_game_currency);
	
	// Mise à jour du formulaire
	var form = $('form#hipay_form', container);
	$('input#hipay_payment_level_in_game_id', form).val(inGameLevel.id);
}


