// Set up currency modifiers
$(document).ready(function()
{
	// Make sure currency value matches select value on page load
	// Bind to change event on select; sbCurrencyConvert each time select is changed
	$('.sb-currency-select').each(function()
		{
			$(this).sbCurrencyConvert();
			$(this).change(function()
			{
				$(this).sbCurrencyConvert();
			});
		});
	// Bind click event for purchase divs
	$('.sb-release-purchase').click(function()
	{
		window.location = $(this).find('a').attr('href');
	});
});

$.fn.sbCurrencyConvert = function()
{
	var element = $(this);
	if(element.parent().hasClass('sb-currency-dropdown-global'))
	{
		// Let the script know we're setting multiple currency values.
		var convertVal = 'global';
		var convertFrom = $.trim(element.parent().prev('div.sb-currency-source-currency').text());
	}
	else
	{
		// Or set value to be converted
		var convertVal = parseFloat($.trim(element.parent().prev('div.sb-currency-source-price').text()));
		var convertFrom = $.trim(element.parent().prev('div.sb-currency-source-currency').text());
	}
	var countryCode = element.val();
	var symbol = sbCurrencyLookup(countryCode);
	// Check if the exchange rate has been retrieved today
	var convertRate = $.cookie('wearebusybodiesCurrencyRate' + countryCode);
	// If exchange rate is stored...
	if(convertRate == null)
	{
		// Execute AJAX request to retrieve currency conversion rate from Yahoo! Finance.
		$.ajax(
		{
			type: 'POST',
			url: 'http://www.wearebusybodies.com/assets/plugins/jq-currency/jq-currency-fetch.php',
			dataType: 'json',
			data: 'action=rate' + '&currfrom=' + convertFrom + '&currto=' + countryCode,
			success: function(json) 
			{
				if(json.errcode == 'ERR-100')
				{
					$.cookie('wearebusybodiesCurrencyRate' + countryCode, json.result, {expires: 6, path: '/'});
					sbCurrencyDisplay(element, symbol, convertVal, json.result, countryCode);
				}
				else 
				{
					alert(json.errcode);
					return false;
				}
			},
			error: function()
			{
				alert('Currency conversion failed.');
				return false;
			}
		});
	}
	else
	{
		sbCurrencyDisplay(element, symbol, convertVal, convertRate, countryCode);
	}
};

function sbCurrencyLookup(countryCode)
{
	switch(countryCode)
	{
		case 'USD' :
			return '$';
		break;
		
		case 'CAD' :
			return '$';
		break;
		
		case 'EUR' :
			return '&#8364;';
		break;
		
		case 'GBP' :
			return '&pound;';
		break;
		
		case 'JPY' :
			return '&yen;';
		break;
		
		case 'ARS' :
			return '$';
		break;
		
		case 'BRL' :
			return 'R$';
		break;
	
		default :
			return '$';
		break;
	}
}

function sbCurrencyDisplay(element, symbol, convertVal, convertRate, countryCode)
{
	if(element.parent().attr('class') == 'sb-currency-dropdown')
	{
		var convertedVal = sbCurrencyEquation(countryCode, convertVal, convertRate);
		element.parent().prev().prev().children().html
		(
			'<span class="sb-currency-value-symbol">' + symbol + '</span><span class="sb-currency-value-price">' + convertedVal + '</span>'
		);
	}
	else if(convertVal === 'global')
	{
		$('div.sb-currency-value').each(function()
			{
				convertVal = $(this).children('div.sb-currency-source-price').text();
				var convertedVal = sbCurrencyEquation(countryCode, convertVal, convertRate);
				$(this).children('p').html
				(
					'<span class="sb-currency-value-symbol">' + symbol + '</span><span class="sb-currency-value-price">' + convertedVal + '</span> <span class="sb-currency-value-code">' + countryCode + '</span>'
				);
			}
		);
	}
	else
	{
		alert('Currency display failed.');
	};
};

function sbCurrencyEquation(countryCode, convertVal, convertRate)
{
	if(countryCode !== 'JPY')
	{
		var convertedVal = (convertVal*parseFloat(convertRate)).toFixed(2);
	}
	else
	{
		var convertedVal = Math.round((convertVal*parseFloat(convertRate)));
	}
	return convertedVal;
}