function formatCurrency(rate, precision) {

	var rateStr = '';
	var rateFormattedStr = '';
	if(rate>=1 || rate<=-1) {
		rateStr = Math.abs(rate).toFixed(precision).toString();
		if(rate<0) {
			rateFormattedStr = '($' + rateStr + ')';
		} else {
			rateFormattedStr = '$' + rateStr;
		}
	} else {
		rateStr = (Math.abs(rate)*100).toFixed(precision).toString();
		if(rate<0) {
			rateFormattedStr = '(' + rateStr + '&cent;)';
		} else {
			rateFormattedStr = '' + rateStr + '&cent;';
		}
	}
	return rateFormattedStr;
}

$(document).ready(function() {
	$('.sign_up_section_link').tooltip({
	    track: true,
	    top: -25,
	    left: 40,
	    showBody: " - "
	});
	Shadowbox.init();
	
	$.ajax({
		type: "GET",
		url: "http://www.tel3.com/include/AJAX_top_rates_json.jsp",
    	data: {
    		"action" : "top_rates",
    		"param1" : "US,Canada,Germany,Cuba,Mexico,Jamaica"
    	},
		dataType: "jsonp",
		jsonp: "jsoncallback",
		timeout: 1000,
		success: function(data, status) {
			$("#ajax_wait").hide();
			htmlString = "";
    		$.each(data, function(i,item){
				htmlString += '<li>';
				htmlString += '<a href="' + item.pageURL + '" target="_blank">';
				htmlString += '<img title="Call ' + item.country + '" src="' + item.imageURL + '" alt="Call ' + item.country + '" width="35" height="21"/>';
				htmlString += '</a>';
				htmlString += '<span>' + formatCurrency(item.rate, 2) + '</span>/min to ';
				htmlString += '<a href="' + item.pageURL + '" target="_blank"> call ' + item.country + '</a>';
				htmlString += '</li>';
    		});
    		$(htmlString).appendTo("ul#top_rates_list");
		},
		error: function(jqXHR, status, error) {
			if (status == "timeout") {
				$("#top_rates_container").html("Still loading...");
			} else {
				$("#top_rates_container").html("404 Not found");
			}
		}
	});
});

