
var ShoppingCard = {


    image_dialog_images_url: "/iq/content/assets/modal",
    image_dialog_title: 'Medien',
    image_dialog_close_btn_text: 'schließen',
    image_dialog_ok_btn_text: 'Einfügen',
    data_id: null,
    data_obj: null,
    data_set: null,
    data_img: null,
    data_content: null,
    modal: null,

    init: function () {
        var _self = this;
        var $modal = _self.modal = $('#modal_iq_assets').hide();
        $modal.find("button#modal_iq_save").click(function(event)
        {
            var items = LFileManager.getSelectedItems();
            $modal.modal('hide');
            if(items[0]){
                var item = items[0];
                if(_self.data_img){
                    var img = $(_self.data_id).find(_self.data_img);
                    if(item['thumb_url'] && img){
                        img.attr('src', item['thumb_url']);
                    }
                }
                //console.log(_self.data_content);
                //console.log(item['content']);
                if(_self.data_content){
                    var input = $(_self.data_id).find('input[name="'+_self.data_obj+'['+_self.data_content+']"]');
                    var val = item['content'][_self.data_content];
                    //console.log(input);
                    //console.log(val);

                    if(input){
                        input.val(val).change();
                    }
                }
                _self.data_set.forEach(function (setter, index) {
                    var input = $(_self.data_id).find('input[name="'+_self.data_obj+'['+setter+']"]');
                    var val = item[setter]
                    if(input && val){
                        input.val(val).change();
                    }
                });
            }
            /*items.forEach(function (item, index) {
            });*/
        });

        $('.btn-lfm-open-modal').on('click', function () {

            _self.data_id = $(this).data('id');
            _self.data_obj = $(this).data('obj');
            _self.data_set = $(this).data('set');
            _self.data_img = $(this).data('img');
            _self.data_content = $(this).data('content');

            _self.fillModal();
            _self.modal.modal();
        });
    },

    fillModal: function () {
        var _self = this;
        var next = _self.setEvents;
        _self.getImagesFromUrl(next);
    },
    setEvents: function()
    {
        // images click event to select image
        /*self.$modal.find('img').click(function(event)
        {
           // $(this).toggleClass(self.select_class);
        });*/
    },
    getImagesFromUrl: function(callback)
    {
        var _self = this;
        // get images html from url
        $.get(_self.image_dialog_images_url, function(html)
        {
            _self.setModalHtml(html);
            callback();

        }).fail(function()
        {
            console.error("error loading from "+_self.image_dialog_images_url);
        })
    },
    setModalHtml: function(html)
    {   // set variabl parts to modal html
        var _self = this;

        _self.modal.find('.modal-title').html(_self.image_dialog_title);
        _self.modal.find('#modal_iq_close').html(_self.image_dialog_close_btn_text);
        _self.modal.find('#modal_iq_save').html(_self.image_dialog_ok_btn_tex);

        _self.modal.find('.modal-body').html(html);
        LFileManager.initFileManager(true);
    },
};