/*  assumes 'CURRENCYTPE' is already set in the cookie - otherwise nothing gets changed.
    and the conversion rates for that currency are set in cookies too (unless conversion is hard-coded)
    and that the GBP value is set in an attribute called data-gbp
    if a hard-coded conversion is wanted, then it needs to have data-usd set too */
jQuery.currency_convert = function() {
    if ($.cookie('CURRENCYTYPE') != null) {
        var currency = $.cookie('CURRENCYTYPE');
        var symbol = '&pound;';
        if (currency == 'USD') {
            symbol = '$';
        }
        if (currency == 'EUR') {
            symbol = '&euro;';
        }
        
        $("[data-gbp]").each(function(){
            var gbp_price = $(this).attr('data-gbp');
            
            if (currency == 'GBP') {
                $(this).html(symbol + gbp_price);
            } else if (currency == 'USD' && typeof $(this).attr('data-usd') != 'undefined') {
                $(this).html(symbol + $(this).attr('data-usd'));
            } else if (currency == 'EUR' && typeof $(this).attr('data-eur') != 'undefined') {
                $(this).html(symbol + $(this).attr('data-eur'));
            } else if ($.cookie(currency) != null) {
                // hard-coding the USD conversion rate to 1.61
                var newprice = gbp_price * 1.61; //$.cookie(currency);
                $(this).html(symbol + newprice.toFixed(2));
            }
        });
        
        $("#currency li").removeClass("high");
        var finder = "[data-currency='" + currency + "']";
        $(finder).closest("li").addClass("high");
    }
};

jQuery.set_currency = function(currency) {
    if (currency == 'GBP' || currency == 'USD' || currency == 'EUR') {
        $.cookie('CURRENCYTYPE', currency, {expires: 1, path: '/', domain: '.easynic.com'});
    }
};

$(document).ready(function(){
    $('.switch_currency').click(function(){
        $.set_currency($(this).attr('data-currency'));
        location.reload();
        //$.currency_convert();
    });
    
    //$.currency_convert();
});
