/**
 * Behaviors for Brad Eldeen logistics
 */

/*global document, $ */

document.observe("dom:loaded", function () {
    /**
     * Show and hide fields on the contact form
     *
     * @see http://projects.cramerdev.net/ticket/554
     */
    (function handleFields() {
        var fields = {
                make: null, 
                model: null,
                tarps: null
            },
            tt = $("trailer-type");

        // Make field containers the values and field names the keys in the 
        // fields object
        for (var f in fields) {
            if (fields.hasOwnProperty(f)) {
                fields[f] = $(f) ? $(f).up() : null;
            }
        }

        /**
         * Show and hide the fields based on the selection on the trailer
         * type field.
         */
        function change(evt) { 
            if (!tt) { return; } 

            switch (tt.value) {
            case 'double-drop' : // Show all
                Object.values(fields).invoke("show");
                break;
            case ('flatbed' || 'stepdeck') : // Show tarps
                fields.make.hide();
                fields.model.hide();
                fields.tarps.show();
                break;
            case 'removable-gooseneck' : // Show make & model
                fields.make.show();
                fields.model.show();
                fields.tarps.hide();
                break;
            default : // Hide all
                Object.values(fields).invoke("hide");
                break;
            }
        }

        if (tt) {
            // Hide initial fields
            Object.values(fields).invoke("hide");
            change();
            tt.observe("change", change);
        }
    })();
});
