﻿/* Copyright (c) 2009-2010 Michael Manning (actingthemaggot.com) 
* 
* Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.
*
* A simple, small (715 bytes) currency formatter; format a numeric value in an element, or pass a number to it and get a formatted value back.
*
* Options to set:
* s: the separator (default is comma),
* d: decimal point (default is period),
* c: digits after decimal point (default is two):
* p: prompt to display ex. $
* z: true c: will be set to 0 and replace with .00
* 	
* {s:",",d:".",c:2,p:"$",z:true}
*
* $('#some_element').currency() //will format the text of the element
* $.currency(123456.78,{options}) //formats number and returns string
* 
* =$123,456.78
*
*/
(function (A) {
    A.fn.extend({
        currency: function (B) {
            var C = {
                s: ",",
                d: ".",
                c: 2,
                p: "",
                z: false
            };
            C = A.extend({}, C, B);
            return this.each(function () {
                var dz = "";
                if (C.z == true) { C.c = 0; dz = ".00"; }
                var D = (C.n || A(this).text());
                D = (typeof D === "number") ? D : ((/\./.test(D)) ? parseFloat(D) : parseInt(D)), s = D < 0 ? "-" : "", i = parseInt(D = Math.abs(+D || 0).toFixed(C.c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
                A(this).text(C.p + s + (j ? i.substr(0, j) + C.s : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + C.s) + (C.c ? C.d + Math.abs(D - i).toFixed(C.c).slice(2) : "") + dz);
                return this
            })
        }
    })
})(jQuery);
jQuery.currency = function () {
    var A = jQuery("<span>").text(arguments[0]).currency(arguments[1]);
    return A.text()
};
