//////////////////////////////////////////////////////////
//
//  File: Event Registration Tools
//  Author: Craig Nelson / Classic Labs
//
//  Features:
//      - accepts object of one or more items
//      - items can be named anything and have their own
//      unique properties

    var MP = MP || {};

    MP.Register = function (config) {
        // scope correction
        var that = this;

        // data
        this.items = config.items; // required
    
        // elements
        this.itemEls = config.itemEls || $$("input[id^='item-type']");
        this.itemName = config.itemName || $("item-name"); // for paypal
        this.itemAmount = config.itemAmount || $$(".item-amount"); // for paypal and email to customer

        // methods
        this.setItemSuffix = function (el, suffix) {
            suffix = suffix.slice(0, 1).toUpperCase() + suffix.replace(/^(\w)?/, "");
            el.value = (el.value.match(/\s-\s.+$/i)) ? el.value.replace(/\s-\s.+$/i, " - " + suffix) : el.value + " - " + suffix;
            return;
        };
    
        // set default item
        this.itemEls.each(function (e) {
            for (var item in that.items) {
                if (that.items.hasOwnProperty(item)) {
                    if (that.items[item].defaultItem && e.value.toLowerCase() === item.toLowerCase()) {
                        e.checked = true;
                        that.itemAmount.each(function (el) {
                            el.value = that.items[item].price;
                        });
                        that.setItemSuffix(that.itemName, that.items[item].title);
                    }
                }
            }
        });

        // events
        this.itemEls.each(function (e) {
            Event.observe(e, "change", function (event) {
                for (var item in that.items) {
                    if (that.items.hasOwnProperty(item)) {
                        if (e.value.toLowerCase() === item.toLowerCase()) {
                            that.itemAmount.each(function (el) {
                                el.value = that.items[item].price;
                            });
                            that.setItemSuffix(that.itemName, that.items[item].title);
                        }
                    }
                }
            });
        });
    }; // constructor
