//On page load call convert
jQuery(document).ready(function(){
	if (jQuery.cookie("familiar_currency") == null ) { 
		jQuery.cookie("familiar_currency", "USD", { path: '/' });
	} else {
		var region = jQuery.cookie("familiar_currency");
		switch (region) {				
			case 'GBP' :
				var currencysymbol = '£'
			break;
			
			case 'EUR' :
				var currencysymbol = '€';
			break;
			
			case 'BRL' :
				var currencysymbol = 'R\$';
			break;
			
			case 'JPY' :
				var currencysymbol = '¥';
			break;
			
			default :
				var currencysymbol = '\$';
			break;
		}
		var prms = 'USD' + ':' + region + ':' + currencysymbol;
		jQuery('.currency').each(function(i,domEle){jQuery(domEle).convertCurrency(region, prms)});
	}
});

//Function to manually change currency
function manualCurrency(prms) {
	if (jQuery.cookie("familiar_currency") == null ) { jQuery.cookie("familiar_currency", "USD", { path: '/' }); }
	var region = jQuery.cookie("familiar_currency");
	var prms = prms.split(':');
	var currencycode = prms[0];
	jQuery.cookie("familiar_currency", prms[0], { path: '/' });
	var prms = region + ':' + prms[0] + ':' + prms[1];
	jQuery('.currency').each(function(i,domEle){jQuery(domEle).convertCurrency(currencycode, prms)});
}

//Change default currency
function changeCurrency(currency) {
	jQuery.cookie("familiar_currency", currency, { path: '/' });
}

;(function(){
	
var $$;

$$ = jQuery.fn.convertCurrency = function(currencycode, prms) {
	if (jQuery.cookie("familiar_currency") == null ) { jQuery.cookie("familiar_currency", "USD", { path: '/' }); }
	if (jQuery.cookie("familiar_currency") != 'USD') {
		var $this = jQuery(this);
		var prms = prms.split(':');
		if (jQuery.cookie("familiar_currency") != prms[0]) {
			var fAmnt = parseFloat($this.text());
			var cCode = currencycode;
			// check if the exchange rate has been retrieved today
			var cookieVal = jQuery.cookie('currencyrateUSD'+prms[1]);
			if (cookieVal != null)
			{
				frmtCurrency($this,prms[2],fAmnt*parseFloat(cookieVal),cCode,prms[1]);
			} else {
				try {
					reqAjax = jQuery.ajax({
						type: "POST",
						url: 'http://www.daveholland.com/content/plugins/familiar-jquery-currency/familiar-currency-ajax.php',
						dataType: "json",
						data: "action=rate" + "&currfrom=USD&currto=" + prms[1],
						success: function(json) {
							switch (json.errcode) {
							case 'ERR-100':
								jQuery.cookie('currencyrate'+prms[0]+prms[1],json.result,{expires: 6, path: '/' });
								frmtCurrency($this,prms[2],fAmnt*parseFloat(json.result),cCode,prms[1]);
								break;
							case 'ERR-200':
								break;
							default:
								break
							}
						},
						error: function(xhr, msg, ex) {
							reqAjax = null
						}
					})
				} catch(e) {
				}
			}
		}
		return this;
	} else {
		var $this = jQuery(this);
		var val = $this.text();
		frmtCurrency($this,'$',val,'USD');
		return this;
	}

	function frmtCurrency(ele,symb,val,code,cls) {
		// Round the currency to the nearest .05
		// val *= 2.0;
		// val = val.toFixed(1) / 2.0;
		// ... or no rounding
		val = parseFloat(val).toFixed(2);
		// build the text in the form: '$' '12.35' 'USD'
		ele.prev().text(symb);
		ele.next().text(val);
		ele.next().next().text(' ' + code);
	};
};

})();