/**
 * Converts euro prices automatically to "CHF".
 */
if (typeof Aida !== 'object') {
    var Aida = {};
}
if (typeof Aida.Price !== 'object') {
    Aida.Price = {};
}

Aida.Price.PriceConv =
{

    /**
     * Convert all prices to the new currency
     *
     * @param string currency Currency name (e.g. "CHF")
     * @param string rate     Currency rate (e.g. 1.5)
     * @param object footnote Footenote to show
     *
     * @return void
     */
    convertAll : function(currency, rate, footnote)
    {
        $(
            'div.fce-teaser-startseite-outer a:contains(" Euro")'
            + ',div.fce-teaser-startseite-outer a:contains(" €)")'
            + ',div.price a:contains(" Euro")'
            + ',div.price a:contains(" €")'
        ).each(function(index) {
            Aida.Price.PriceConv.replacePrice(this, currency, rate);
            if (typeof Aida.Footnotes == 'object') {
                Aida.Footnotes.Api.register(footnote);
            }
        });
    },

    /**
     * Replace the text of a single dom element to the new currency
     *
     * @param object domElem  DOM element to replace text in
     * @param string currency Currency name (e.g. "CHF")
     * @param string rate     Currency rate (e.g. 1.5)
     *
     * @return void
     */
    replacePrice : function(domElem, currency, rate)
    {
        var text = $(domElem).text();
        text.replace(
                /([0-9.]+) (Euro|€)/,
            function(alltext, oldprice, oldcurrency) {
                //var convprice = Math.round(parseFloat(oldprice) * rate);
                var point = oldprice.split('.');
                var n = 0;
                if (point[1]) {
                    var n = point[1].length;
                }
                var convprice =  Aida.Price.PriceConv.roundFloat(parseFloat(oldprice) * rate, n);
                text = text
                    .replace(oldcurrency, currency)
                    .replace(oldprice, convprice);
            }
        );
        $(domElem).text(text);
    },

    /**
     * Replace the text of a single dom element to the new currency
     *
     * @param price to round
     * @param n     how many values after point
     *
     * @return price
     */
    roundFloat : function(price, n)
    {
        return Math.round(price*Math.pow(10,n))/Math.pow(10,n);
    }
};

if ("CHF" != "{" + "CURRENCY}") {
    $(function(){
        Aida.Price.PriceConv.convertAll("CHF", "1.2073", {"hash":"bd7174872007726fb77a96ac8a340f04","number":1,"id":"fn-bd7174872007726fb77a96ac8a340f04","text":"footnote-CHF"});
    });
}

