$dr(document).ready(function(){
	
	$dr('.currency-hidden').each(function(){
		var currencyContainer = $dr(this);
		var cval = currencyContainer.find('input[name=currency_have_val]');
		var crur = currencyContainer.find('.line-value-rur').children('span');
		var cusd = currencyContainer.find('.line-value-usd').children('span');
		var ceur = currencyContainer.find('.line-value-eur').children('span');
		// currencyData.usd
		// currencyData.eur
		
		
		$dr(document).click(function(elem){
			if ( $dr(elem.target).closest('.currency-hidden-container').length == 0 && 
			     $dr(elem.target).closest('a.red-dashed').length == 0 && currencyContainer.hasClass('fullscreen')) {
				currencyContainer.removeClass('fullscreen');
			}
		});
		
		var currencyCalc = function() {
			var ccur = currencyContainer.find('input[name=currency_have]:checked');
			var cvalval = parseFloat(cval.val()) ? parseFloat(cval.val()) : 0;
			//cval.val( cvalval );
			
			if (ccur.val() == 'rur') {
				crur.html( addCommas (cvalval.toFixed(2)) );
				cusd.html( addCommas ((cvalval / currencyData.usd[1]).toFixed(2)) );
				ceur.html( addCommas ((cvalval / currencyData.eur[1]).toFixed(2)) );
			}
			if (ccur.val() == 'usd') {
				crur.html( addCommas ((cvalval * currencyData.usd[0]).toFixed(2)) );
				cusd.html( addCommas (cvalval.toFixed(2)) );
				ceur.html( addCommas ((cvalval * currencyData.usd[0] / currencyData.eur[1]).toFixed(2)) );
			}
			if (ccur.val() == 'eur') {
				crur.html( addCommas ((cvalval * currencyData.eur[0]).toFixed(2)) );
				cusd.html( addCommas ((cvalval * currencyData.eur[0] / currencyData.usd[1]).toFixed(2)) );
				ceur.html( addCommas (cvalval.toFixed(2)) );
			}
		}
		
		cval.change(function(){
			var cvalval = parseFloat(cval.val()) ? parseFloat(cval.val()) : 0;
			cval.val( cvalval );
			
			currencyCalc();
		});
		cval.keyup(function(){
			currencyCalc();
		});
		currencyContainer.find('input[name=currency_have]').change(function(){
			currencyCalc();
		});
		
		
		
	});
	
	
	$dr('.currency-informer a.red-dashed').click(function(elem){
		elem.preventDefault();
		var currencyContainer = $dr(this).closest('.currency-informer').next('.currency-hidden');
		currencyContainer.addClass('fullscreen');
		currencyContainer.find('input[name=currency_have_val]').change();
		currencyContainer.children('.currency-hidden-container').css('margin-top', ( (window.innerHeight - currencyContainer.children('.currency-hidden-container').height()) / 2) + 'px');
		
	});
	

	
});