ui = {};
ui.window = window;
ui.html_elements = {};

ui.__ui_element_wrapper = function(element) {
	this.element = element;
};

ui.element_wrapper = function(element) {
	ui.__ui_element_wrapper(element);
    this.event_handlers = {};
};

ui.element_wrapper_html = function(parent_tb, button_id, the_default) {
    this.parent_tb = parent_tb;
	this.button_id = button_id;
    this.value = the_default;
};
ui.element_wrapper_html.prototype.get_value = function() {
    console.log(this.parent_tb.element.items)
}

ui.create_toolbar = function(name, tb_parent) {
    tb_parent = typeof tb_parent !== 'undefined' ? a : $(document.body);
    return new function(name, tb_parent) {
        var div_parent = $('<div id="' + name + '" style="display: inline-block; margin: 5px; padding: 4px; border: 1px solid silver; border-radius: 3px"></div>').appendTo(tb_parent);
        this.element = div_parent.w2toolbar({
            event_handlers: {},
            name: name,
            items: [],
            onClick: function(event) {
                if (event.target in this.event_handlers) {
                    this.event_handlers[event.target][0].addInput(new Event("mouse_press", this.event_handlers[event.target][1], []), 0);
                }
            }
        });
        // what a terribly ugly hack
        $('#tb_' + name + '_right').hide();
    }(name, tb_parent);
}

ui.create_button = function(button_id, caption, img, parent_tb, controller, port) {
    var btn = parent_tb.element.add({ type: 'button', id: button_id, img: img, caption: caption });
    parent_tb.element.event_handlers[button_id] = [controller, port];
    return new ui.element_wrapper(btn);
}

ui.create_tb_element = function(button_id, html, the_default, parent_tb, controller, port) {
    var btn = parent_tb.element.add({ type: 'html', id: button_id, html: html });
    parent_tb.element.event_handlers[button_id] = [controller, port];
    var wrapper = new ui.element_wrapper_html(parent_tb, button_id, the_default);
    ui.html_elements[button_id] = wrapper;
    return wrapper;
}

ui.parameter_changed = function(button_id) {
    ui.html_elements[button_id].value = $('#' + button_id + '_input')[0].value;
}