Currencies = {
	ratesFilePath : "/hotel-cms/taux/taux.js?day=" + new Date().getDay(),
	datas : null,
	param : 0,
	countryCode : null,
	defaultCurrency : "EUR",
	rates : null,
	deviseDebug : null,
	prixDebug : null,
	autorun : true,
	langue : 0,

	_init : function(response, select){	
			
			if (utils.getCookieContent("userLocalization")!= null) {
				this.countryCode = utils.getCookieContent("userLocalization")[0];
			}else{ 
				this.countryCode = "fr";
			}

			Currencies.datas = response;
			Currencies.createRatesDatas();

			Currencies.getCurrencyFromCountry();
			Currencies._fillCurrencySelect(jQuery("#"+select));
			Currencies.updateBudgetFilters(this.defaultCurrency);
			
			Currencies._save(jQuery(".price"));
			Currencies._convert(Currencies.rates, this.defaultCurrency, jQuery(".price"));
			Currencies._roundDisplayedPrices();
	},
	
	getLangue : function(){
					var maLocation=window.location.pathname;
					if(maLocation.search("/nl/")>=0 || maLocation.search("/gb/")>=0){
						Currencies.langue=-1;
					} else {
						Currencies.langue=0;
					}
					return Currencies.langue;
					
	},
	
	
	_save : function(prices){
	
			(prices || jQuery(".price")).each(
				function(){
					var cur = jQuery(this).find(".currency");
					var amo = jQuery(this).find(".amount");
					if(Currencies.getLangue()==-1){
						jQuery(this).empty().append(cur).append(" ").append(amo)
						.append(amo.clone().removeClass('amount').addClass('origAmount').hide())
						.append(cur.clone().removeClass('currency').addClass('origCurrency').hide());
					}
				} 
			);
	},
	
	_convert : function(rates,currency,prices){
	
		prices = prices || jQuery(".price");
			prices.each(
			
				function(){
		
					var cur = jQuery(this).find(".currency");
					var amo = jQuery(this).find(".amount");
			
					var origCur = jQuery(this).find(".origCurrency").html();
					var origAmo = jQuery(this).find(".origAmount").html();
					var symbole = "EUR";
					for(i=0;i<Currencies.datas.length;i++){
						if(Currencies.datas[i].devise==currency){
							symbole = Currencies.datas[i].symbole;
						}
					}
					if (origCur!='---' && rates[currency] && rates[origCur] && rates[origCur]!=0 ) {
						cur.html(symbole);
						amo.html((origAmo/ (rates[origCur] / rates[currency])).toFixed(2));
					} else {
						cur.html(symbole);
						Currencies._revert(jQuery(this)); 
					} 
				}
			);
	},
	
	_revert : function(prices){
		prices = prices || jQuery(".price");
			prices.each(
				function(){
					jQuery(this).find(".amount").html(jQuery(this).find(".origAmount").html());
				}
			);
	},
	
	
	
	_fillCurrencySelect : function(select){
		if(select){
			var cur = "";
			for(i= 0; i < Currencies.datas.length; i++){
				if(cur != Currencies.datas[i].devise){
					cur = Currencies.datas[i].devise;
					var oOption = document.createElement('option');
					oOption.value = Currencies.datas[i].devise;
					oOption.innerHTML = Currencies.datas[i].devise;
					if(Currencies.datas[i].devise == Currencies.defaultCurrency) oOption.selected = true;
					select.append(oOption);
				}
			}
			select.hide().show();
		}
	},
	
	_roundDisplayedPrices : function(prices){
	
		prices = prices || jQuery(".price");
		prices.each(function(){
			jQuery(this).find(".amount").text(Math.ceil(Number(jQuery(this).find(".amount").text())));
		});
		
		var forkPrices = jQuery(".bloc_filter_budgets");
		var val;
		forkPrices.each(function(){
				var $tAmount = jQuery(this).find(".amount");
				
				val = Math.round(Number($tAmount.text()));
				val = Math.floor((val+5)/10)*10;
				$tAmount.text(val);
		});		
		
	},
	
	updateBudgetFilters : function(newCurrency){
	
  	var selBudget = jQuery("#bloc_filter_budgets");
  	var forks = selBudget.find("option");
	if(forks[0]){
	
					var libelle = forks[0].innerHTML;
					var budgetCurrency = jQuery("#budgetCurrency").val();
					var budgetInfo = "";
					
					if($.cookie('packageCookie')){
							var cookInfo = fillDate.getSearchArgs($.cookie('packageCookie'));
							budgetInfo = cookInfo['min-budget']+"-"+cookInfo['max-budget'];	
						}
					
					selBudget.empty();
					selBudget.append('<option value="">'+ libelle +'</option>');
					jQuery.each(forks, function(i){
						var fork = jQuery(this).val();

						if(fork != ""){
							var forkTab = fork.split("-");
							var min = forkTab[0];
							var max = forkTab[1];
								if(min != ""){
									min = (min/ (Currencies.rates[budgetCurrency] / Currencies.rates[newCurrency])).toFixed(2);
									min = Math.round(Number(min));
									min = Math.floor((min+5)/10)*10;
								}
								
								if(max != ""){
									max = (max/ (Currencies.rates[budgetCurrency] / Currencies.rates[newCurrency])).toFixed(2);
									max = Math.round(Number(max));
									max = Math.floor((max+5)/10)*10;
								}
								
								var sOption = "";
								
								if(min==0 &&  max != ""){
									sOption = '<option';
									if(fork == budgetInfo) sOption += ' selected="selected"';
									sOption += ' value="'+fork+'">&nbsp;<&nbsp;'+max+'&nbsp;'+newCurrency+'</option>';
								}
								else if(min>0 && max != ""){
									sOption = '<option';
									if(fork == budgetInfo) sOption += ' selected="selected"';
									sOption += ' value="'+fork+'">&nbsp;'+min+'&nbsp;-&nbsp;'+max+'&nbsp;'+newCurrency+'</option>'
								}
								else if(min>0 && max==""){
									sOption = '<option';
									if(fork == budgetInfo) sOption += ' selected="selected"';
									sOption += ' value="'+fork+'">&nbsp;>&nbsp;'+min+'&nbsp;'+newCurrency+'</option>';
								}
								
								if(sOption != "") selBudget.append(sOption);
							}
					});
			}
	},
	
	
	
	getCurrencyFromCountry : function(){
		for (var i = 0; i < Currencies.datas.length; i++) {
			if (Currencies.countryCode==Currencies.datas[i].codepays.toLowerCase()){
				Currencies.defaultCurrency=Currencies.datas[i].devise;
			}
		}
	},
	
	createRatesDatas : function(){
	
		this.rates = new Object();
		var cur = "";
		for(i= 0; i < Currencies.datas.length; i++){
			if(cur != Currencies.datas[i].devise){
				cur = Currencies.datas[i].devise;
				this.rates[cur] = Number(Currencies.datas[i].taux);
				
			}
		}
	},
	
	
	
	_initCurrenciesListObserver : function(select){
		jQuery("#"+select).change(function(){
			Currencies._convert(Currencies.rates, jQuery(this).val(), jQuery(".price"));
			Currencies.updateBudgetFilters(jQuery("#s_devise").val());
			Currencies._roundDisplayedPrices();
		});
	}
	
}




jQuery(document).ready(function(){
	if (Currencies.autorun) {
		jQuery.getJSON(
			Currencies.ratesFilePath,
			function(response){
				Currencies._init(response.listdev,"s_devise");	
				Currencies._initCurrenciesListObserver("s_devise");
			}
		);
	}
});

