﻿if (!Headwater) { var Headwater = {} }

Headwater.CartBuilder = Class.create({

    url: "",
    config: {},
    form: null,

    initialize: function(url, config) {
        this.url = url;
        this.config = config;
        this.createForm();
    },

    addItem: function(product) {
        this.addNameValues(product);
        this.addInput("add", "add");
        this.form.submit();
    },

    viewCart: function() {
        this.addInput("display", "display");
        this.form.submit();
    },

    createForm: function() {
        this.form = new Element("form", {
            action: this.url,
            method: "post"
        });

        this.addNameValues(this.config);
        $$('body')[0].appendChild(this.form);
    },

    addNameValues: function(collection) {
        for (var attribute in collection) {
            this.addInput(attribute, collection[attribute]);
        }
    },

    addInput: function(name, value) {
        var input = new Element("input", {
            type: "hidden",
            name: name,
            value: value
        });

        this.form.appendChild(input);
    }
});
