'use strict';

oml.ui.toolbar = function() {

    var ui = oml.user.ui,

        buttons = {
            'list': ['openButton', 'previewButton', 'listViewButtons'],
            'item': ['backButton', 'fullscreenButton', 'itemViewButtons']
        },

        view = getView(),

        that = Ox.Bar({size: 24})
            .css({zIndex: 2})
            .bindEvent({
                doubleclick: function(event) {
                    if (view == 'list') {
                        (
                            ui.listView == 'list'
                            ? oml.$ui.list.$body
                            : oml.$ui.list
                        ).animate({scrollTop: 0}, 250);
                    }
                },
                oml_item: function(data) {
                    if (!!data.value != !!data.previousValue) {
                        view = getView();
                        var previousView = view == 'list' ? 'item' : 'list';
                        buttons[previousView].forEach(
                            function(previousButton, index) {
                                var button = buttons[view][index];
                                oml.$ui[previousButton].replaceWith(
                                    oml.$ui[button] = oml.ui[button]()
                                )
                            }
                        );
                    }
                }
            });

    buttons[view].forEach(function(button) {
        that.append(oml.$ui[button] = oml.ui[button]());
    });

    that.append(
        oml.$ui.sortElement = oml.ui.sortElement()
    ).append(
        oml.$ui.findElement = oml.ui.findElement()
    );

    function getView() {
        return !ui.item ? 'list' : 'item';
    }

    return that;

};