function CurrencyConverter() {

	this.current = null;

	this.cswitch = function (item, code) {
		currency.convert(item.attr('exchange'),item.attr('symbol'));
		this.current = code.toLowerCase();
		jQuery.ajax({type: "POST", url: "/it/Ajax/call/Currency/NOPOST/1/CUR/"+code});
	};

	this.refresh = function() {
		var obj = jQuery('#sel_' + this.current);
		currency.convert(jQuery(obj).attr('exchange'), jQuery(obj).attr('symbol'));
	};

	this.init = function(code) {

		this.cswitch(jQuery('#sel_' + code), code);

		jQuery("a.list_href_currency").click(function() {

		    var sel_lang = jQuery(this).attr('title');
			var curr_selected = '#sel_' + sel_lang;

			jQuery('.image_currency').css('display','none');
			jQuery(curr_selected).css('display','block');

			var curr_list_selected = '#list_' + sel_lang;
			jQuery('.list_currency').css('display','block');
			jQuery(curr_list_selected).css('display','none');

			currency.cswitch(jQuery(this),sel_lang.toUpperCase());

		});

	}

	this.convert = function(exchange, symbol) {
		prices = jQuery('[localprice]').get();
		jQuery.each(prices, function(index, item) {
			price = jQuery(item).attr('localprice') * exchange;
			price2 = jQuery(item).attr('localprice2') * exchange;
			price = currency.format(price.toFixed(2)) + ' ' + symbol;
			//alert(price2);
			if (price2) price2 = currency.format(price2.toFixed(2)) + ' ' + symbol;
			value = price2 ? price + ' - ' + price2 : price;
			jQuery(item).text(value);
		});
		
	}

	this.format = function(price)
	{
		var delimiter = "#";
		var a = price.split('.',2)
		var d = a[1];
		var i = parseInt(a[0]);
		if(isNaN(i)) { return ''; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		var n = new String(i);
		var a = [];
		while(n.length > 3)
		{
			var nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		}
		if(n.length > 0) { a.unshift(n); }
		n = a.join(delimiter);
		if(d.length < 1) { price = n; }
		else { price = n + '.' + d; }
		price = minus + price;
		price = price.replace(/\./,',');
		price = price.replace(/#/,'.');
		return price;
	}

}

var currency = new CurrencyConverter();


