/**
 *  Copyright (c) 2007, AF83
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *  1° Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *
 *  2° Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 *  3° Neither the name of AF83 nor the names of its contributors may be used
 *  to endorse or promote products derived from this software without specific
 *  prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COMPANY AF83 AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 *  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  @package    Turbulences
 *  @author     bmichel
 *  @copyright  2007 AF83
 *  @license    BSD License (3 Clause) http://www.opensource.org/licenses/bsd-license.php
 */


/**
 * A lightweight javascript module for internationalization.
 */
if (typeof(I18N) == "undefined") {
    var I18N = {};
}

/**
 * The current locale.
 */
I18N.current_locale = 'en';

I18N.decimal_separators  =  {"en": ".", "fr": ","};
I18N.thousand_separators = {"en": ",", "fr": " "};

/**
 * Get the decimal separator for the given locale.
 * This separator is used between the integer and the fractionnal parts of a decimal number.
 *
 * @param locale The locale, or null for the default one
 * @return the decimal separator as a string
 */
I18N.get_decimal_separator = function (locale) {
    locale = locale || I18N.current_locale;
    return I18N.decimal_separators[locale] || '.';
}

/**
 * Get the thousand separator for the given locale.
 * 
 * @param locale The locale, or null for the default one
 * @return the thousand separator as a string
 */
I18N.get_thousand_separator = function (locale) {
    locale = locale || I18N.current_locale;
    return I18N.thousand_separators[locale] || '.';
};


I18N.number = {
    /**
     * Format a number according to a locale
     *
     * @example
     *    I18N.number.format(1234.56, {precision: 0, separator: ','})   => "1,234"
     *    I18N.number.format(1234.56, {precision: 3, separator: ''})    => "1234.560" 
     *
     * @param number The number to format
     * @param options An object where
     *        - precision is the number of decimal digits (default: 2)
     *        - separator is the thousand separator (default depends of the current locale)
     * @param locale The locale to use or null for the default one
     * @return the number as a string
     */
    format: function(number, options, locale) {
        var precision = (options && options.precision) || 2;
        var separator = (options && options.separator) || I18N.get_thousand_separator(locale);
        
        var integral = (precision > 0) ? Math.floor(number) : Math.round(number);
        var value = "";
        
        // TODO insert the thousand separator !
        value += String(integral);

        if (precision > 0) {
            var frac = (number - integral) * Math.pow(10, precision);
            value += I18N.get_decimal_separator(locale);
            value += String(Math.round(frac));
        }

        return value;
    },

    /**
     * Simple formatting for money (precision of 2, no fractionnal part for integers)
     * @param number The money amount
     * @return the number as a string
     */
    format_money: function(number, options, locale) {
        options = options || {};
        options.precision = options.precision || 2;
        var value = I18N.number.format(number, options);
        if (value.substr(-2, 2) == '00')
            return value.slice(0, -3);
        return value;
    },

    /**
     * Parse a string which represents an integer in the given locale
     * @param value A string which represents an integer
     * @param locale The locale to use or null for the default one
     * @return the integer
     */
    parseInt: function(value, locale) {
        var separator = I18N.get_thousand_separator(locale);
        return Number(value.replace(new RegExp("\\" + separator, "g"), ""));
    },

    /**
     * Parse a string which represents a float in the given locale
     * @param value A string which represents a float
     * @param locale The locale to use or null for the default one
     * @return the float
     */
    parseFloat: function(value, locale) {
        var parts = value.split(I18N.get_decimal_separator(locale));
        if (parts.length > 2) return Number.NaN;
        var n = 0.0;
        n += I18N.number.parseInt(parts[0], locale)
        
        if (parts.length > 1)
            n += Number(parts[1]) / Math.pow(10, String(parts[1]).length);
        return n;
    }
};

