
/***** product prices / variations *************************************/

var satchmo = satchmo || {};

satchmo.set_option_ids = function(arr) {
    //arr.sort(satchmo.numeric_compare);
    satchmo.option_ids = arr;
};
//satchmo.show_error = function () {}; //TODO ?

satchmo.get_current_price = function (detail, qty, use_sale) {
    var taxed = satchmo.default_view_tax,
        k, prices;
        
    var full_price, old_price;
    
    if (detail["SALE"]) {
        full_price = detail["SALE"]['1'];
        old_price = detail["PRICE"]['1'];
    } else {
        old_price = null;
        full_price = detail["PRICE"]['1'];
    }
    return {"old_price": old_price, "full_price":full_price};
    /*
    var prices = detail[k];

    if (prices) {
        var best = prices['1'];
        if (qty > 1) {
            for (var pricekey in prices) {
                var priceqty = parseInt(pricekey);
                if (priceqty > qty) {
                    break;
                }
                best = prices[pricekey];
            }
        }
        return best;        
    }
    else {
        return "";
    }*/		
};

function init_product() {
		function _update_price(ev) {
				var key = this.value || this.elm.value;
				var detail = satchmo.variations[key];
				if (detail) {
					var qty = parseInt($('quantity').get("value"));
					//TODO : sales, stocks
                    var prices = satchmo.get_current_price(detail, qty, true);
                    if (prices["old_price"])
                        $('old_price').set("html", prices["old_price"]);
					$('fullprice').set("html", prices["full_price"]);
				}
		}

		var candidates = $$('form#options select.priced');
		if (!candidates) return;
		var select = candidates[0];
		if (select.n2Select)
				select = select.n2Select;

		select.addEvent("change", _update_price);
}

window.addEvent("domready", init_product);
