var CURRENT_CURRENCY;

function selectCurrency(object) {
	
	var item_a = jQuery(object).children('a');
	currency.convert(item_a.attr('exchange'),item_a.attr('symbol'));
    jQuery.cookie('currency', item_a.attr('title'), {path: '/'});
    jQuery('#currency').text(item_a.text()); 
    /*
    jQuery('#eb-country li').css('display', 'block');
    jQuery('#currency_'+item.attr('title')).css('display', 'none');
    */
    OLD_CURRENCY = CURRENT_CURRENCY;
    CURRENT_CURRENCY = jQuery(object).clone();
    jQuery(object).remove();
    newEl = jQuery('#eb-country li ul').append(OLD_CURRENCY);
    
	
}

function CurrencyConverter() {

	this.init = function(code) {

        var c = jQuery('#currency').text();
       
        var item = jQuery('[title='+c+']');
        currency.convert(item.attr('exchange'),item.attr('symbol'));
		
	}

	this.convert = function(exchange, symbol) {
		prices = jQuery('[localprice]').get();
		jQuery.each(prices, function(index, item) {
			price = jQuery(item).attr('localprice') * exchange;
			price = currency.format(price.toFixed(2)) + ' ' + symbol;
			jQuery(item).text(price);
		});
	}

	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();

$(document).ready(function(){
	
	$('#eb-country li').mouseover(function(){
		
		$('#eb-country li ul').show();
		
	}).mouseout(function(){
		
		$('#eb-country li ul').hide();
	});
	
	
	$('#eb-language li').mouseover(function(){
		
		$('#eb-language li ul').show();
		
	}).mouseout(function(){
		
		$('#eb-language li ul').hide();
	});
});


