function showControl(){
	$('#test_deviseS').html(window['DeviseSource']);
	$('#test_deviseC').html(window['DeviseCible']);
	$('#test_deviseR').html(window['DeviseRate']);
}

function refreshMontantSource(){
	var tmp = $('#MontantCible').val();
	tmp = tmp*(1/window['DeviseRate']);
	tmp = Math.round( tmp*100 )/100;
	$('#MontantSource').val( tmp );
}
function refreshMontantCible(){
	var tmp = $('#MontantSource').val();
	tmp = tmp*(window['DeviseRate']);
	tmp = Math.round( tmp*100 )/100;
	$('#MontantCible').val( tmp );
}

function arrondi2ch(nombre){
	return Math.round( nombre*100 )/100;
}

function rateRefresh(){
	if( window['changeRates'] == null ){
		delete window['DeviseRate'];
		$('#taux_droite').html('--');
		$('#taux_gauche').html('--');
	} else {
		window['DeviseRate'] = (1/window['changeRates'][window['DeviseSource']])*window['changeRates'][window['DeviseCible']];
		$('#taux_droite').html(arrondi2ch(window['DeviseRate']));
		$('#taux_gauche').html(arrondi2ch(1/window['DeviseRate']));
	}
}

jQuery(document).ready(function(){
	// Init
	$('#dv_dropdown_source').hide();
	$('#dv_dropdown_cible').hide();
	
	window['DeviseSource'] = 'EUR';
	window['DeviseCible']  = 'USD';
	window['DeviseRate']   = 1;
	rateRefresh();
	
	$('#dv_source_screen').html($('#'+window['DeviseSource']).html());
	$('#dv_cible_screen').html($('#'+window['DeviseCible']).html());
	
	showControl();
	
	// Animation menu déroulant
	$('#dv_source').click(function() {
		$('#dv_dropdown_source').slideToggle('fast');
		$('#dv_dropdown_cible').slideUp('fast');
	});
	$('#dv_cible').click(function() {
		$('#dv_dropdown_cible').slideToggle('fast');
		$('#dv_dropdown_source').slideUp('fast');
	});
	$('.champMontant').focus(function(){
		$('#dv_dropdown_source').slideUp('fast');
		$('#dv_dropdown_cible').slideUp('fast');
	});
	
	// Change montant
	$('#MontantSource').keyup(function(){
		refreshMontantCible();
	});
	$('#MontantCible').keyup(function(){
		refreshMontantSource();
	});
	
	// Sélection d'un élément dans la liste
	$('#dv_dropdown_source').children().click(function(){
		window['DeviseSource'] = $(this).children('.dv_rac').html();
		
		rateRefresh();
		
		$('#dv_source_screen').html($(this).html());
		$('#dv_dropdown_source').slideToggle('fast');
		showControl();
		
		refreshMontantSource();
	});
	
	$('#dv_dropdown_cible').children().click(function(){
		window['DeviseCible'] = $(this).children('.dv_rac').html();
		
		rateRefresh();
		
		$('#dv_cible_screen').html($(this).html());
		$('#dv_dropdown_cible').slideToggle('fast');
		showControl();
		
		refreshMontantCible();
	});
	
	// Rates history
	$.datepicker.regional['fr'] = {
		closeText: 'Fermer',
		prevText: '&#x3c;Préc',
		nextText: 'Suiv&#x3e;',
		currentText: 'Courant',
		monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'],
		monthNamesShort: ['Jan','Fév','Mar','Avr','Mai','Jun','Jul','Aoû','Sep','Oct','Nov','Déc'],
		dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
		dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
		dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
		weekHeader: 'Sm',
		dateFormat: 'dd/mm/yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''
	};
	$('#history_datepicker').datepicker($.datepicker.regional['fr']);
	$('#history_datepicker').datepicker('option', {
		onSelect: function(dateText, inst){
			// console.log('salut');
			$('#history_form form').submit();
			// inst.datepicker('hide');
		}
	});
	$('#history_toggle').click(function(e){
		e.preventDefault();
		$('#history_form').toggle();
		$(this).toggleClass('active');
	});
	$('#history_form form').submit(function(e){
		// $('#history_form').toggle();
		// $('#history_toggle').toggleClass('active');
		e.preventDefault();
		
		$.get(
			'/etc/ajax/getRateHistory.php',
			{date: $('#history_datepicker').val()},
			function(data){
				// console.log('hello');
				// console.log(data);
				
				var error_msg = '';
				$('#history_error').html('').hide();
				$('#history_datepicker').datepicker('hide');
				
				if( typeof(data.error) != 'undefined' ){
					// console.log('error');
					window['changeRates'] = null;
					// $('#history_error').html(data.msg);
					error_msg = data.msg;
				} else {
					// console.log('no error');
					window['changeRates'] = data;
					// $('#history_error').html('');
					// $('#history_form').toggle();
					// $('#history_toggle').toggleClass('active');
				}
				
				rateRefresh();
				refreshMontantCible();
				// showControl();
				$('#history_error').fadeOut(50).delay(150).html(error_msg).fadeIn(50);
			}
		);
	});
});
