/* © Zooven, Inc 2006-2007 - ALL RIGHTS RESERVED */

/**
 * modifiers - a collection of custom formatting modifiers.
 */
zooven.ui.modifiers = {

    /**
     * number - a formatting function that takes a number and returns that number formatted for human display with
     *  thousands seperators.
     * s            : the input
     * returns the number passed in, reformatted with thousands seperators.
     */
    number : function(s) {
        // make sure that s is a string
        s = String(s);
        // split apart the decimal part
        var parts = s.split('.');
        // grab the part before the decimal
        var beforeDecimal = parts[0];
        // assume that the number is non-negative
        var negative = false;
        // if it starts with a '-', then it IS negative - pull off the '-' and set the flag
        if (beforeDecimal.charAt(0) == '-') {
            beforeDecimal = beforeDecimal.substr(1);
            negative = true;
        }
        // figure out the length of the very first group by MODding it by 3
        var group1Len = beforeDecimal.length % 3;
        // initialize the retVal to the first group
        var retVal = beforeDecimal.substr(0, group1Len);
        // loop over the rest of the string, three digits at a time
        for (var i = group1Len; i < beforeDecimal.length; i+=3) {
            // tack on the separator
            if (retVal != '') {
                retVal += ',';
            }
            // tack on the group
            retVal += beforeDecimal.substr(i, 3);
        }
        // if there's a decimal part...
        if (parts.length > 1) {
            // append a '.' and the decimal part
            retVal += '.';
            retVal += parts[1];
        }
        // if it's negative, put the '-' back on
        if (negative) {
            retVal = '-' + retVal;
        }
        // return the modified string
        return retVal;
    }
}
