(function($) {
	
	$.fn.formatCurrency = function(settings) {
		var symbols = {dollar: '&#36;', euro: '&euro;',emptyString: ' '}
		var default_symbol = 'euro';
		var default_format = 'hu';

		var formats = {
			hu:{decimal_symbol:',',digit_group_symbol:' ',number_is_first:true},
			en:{decimal_symbol:'.',digit_group_symbol:',',number_is_first:false}
		};

		var defs = {
			useHtml: true,
			symbol: 'euro',
			format: 'hu',
			dont_worry_about_commas: 1
		};
	    
		var opts = $.extend({}, defs, settings);
		var currency_symbol = (symbols[opts.symbol]) ? symbols[opts.symbol] : symbols[default_symbol];
		var currency_format = (formats[opts.format]) ? formats[opts.format] : formats[default_format];


		return this.each(function() {
			$this = $(this);
			var num = '0';
			num = $this[opts.useHtml ? 'html' : 'val']();
			num = num.replace(/\$|\,/g, '');
			if (isNaN(num)) num = '0';
			sign = (num == (num = Math.abs(num)));
			num = Math.floor(num * 100 + 0.50000000001);
			cents = num % 100;
			num = Math.floor(num / 100).toString();
			if (cents < 10) cents = '0' + cents;
			for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
			{
				num = num.substring(0, num.length - (4 * i + 3)) + currency_format.digit_group_symbol + num.substring(num.length - (4 * i + 3));
			}

			var money = ((sign) ? '' : '-') + num + currency_format.decimal_symbol + cents;
			money = (currency_format.number_is_first) ? (money += ' ' + currency_symbol) : (currency_symbol + ' ' + money);
			$this[opts.useHtml ? 'html' : 'val'](money);
		});
	};

})(jQuery);
