﻿/**
* Generic Price object.
* Formats it's value to have a price and a price description
* @param {Object} options
*/
var Price = base2.Base.extend({
    constructor: function(value, options) {
        this.SetValue(value);
        this.SetOptions(options);
    },

    Value: 0.00,

    IsPrice: function() {
        var parseValue = parseFloat(this.Value);

        return !(isNaN(parseValue));
    },

    SetOptions: function(options) {
        this.Defaults = {
            Value: 0,
            Precision: 2,
            Currency: "AUD",
            Caption: "inc. GST",
            Template: "%PRICE% <span>%CURRENCY% <em>%CAPTION%</em></span>"
        }

        this.Options = jQuery.extend({}, this.Defaults, options);
    },

    Render: function() {
        var template = this.Options.Template;

        template = template.replace("%PRICE%", this.Price());
        template = template.replace("%CURRENCY%", this.Options.Currency);
        template = template.replace("%CAPTION%", this.Options.Caption);

        var item = jQuery(template);

        return template;
    },

    Price: function() {
        var price = this.Value;
        try {
            price = parseFloat(price).toFixed(this.Options.Precision);

            if (price == "NaN") {
                price = this.Value;
            } else {
                price = price.toString();
                if (price.charAt(0) != '$') price = "$" + price;
            }

        } catch (Exception) {
            price = this.Value;
        }

        return price;
    },

    ToString: function() {
        return this.Price();
    },
	
	SetValue: function(value) {
        var price = String(value);
		price = price.replace("$", "");
		
        try {
            price = parseFloat(price).toFixed(this.Options.Precision);

            if (price == "NaN") {
				price = parseFloat(price).toFixed(this.Options.Precision);
			}
			this.Value = price;
			

        } catch (Exception) {
            this.Value = price;
        }

        return price;
	}

});

