var content, preview, selfPrinter;
var Printer = function () {


    this.init = function () {

        this.reset = document.querySelector('.js-reset');
        this.delete = document.querySelector('.js-delete');

        content = this.content = document.querySelector('.js-content');


        selfPrinter = this;
        this.reset.addEventListener('click', this.clear);

        content.addEventListener("mouseup", this.contentResize);

        this.restoreContentHeight();

        if (this.delete != null) {
            this.delete.addEventListener('click', this.confirmDelete);
        }

        document.querySelectorAll('.icon').forEach(function (e) {
            e.addEventListener('click', function () {
                selfPrinter.selectIcon(this.title);
            });
        })

    };

    /**
     * resetuje wybór ikony
     */
    this.selectIcon = function(iconTitle){
        document.querySelectorAll('.icon').forEach(function (e, i) {
            e.classList.remove('selected');
        });
        document.querySelector('#icon').value = iconTitle;
        var selector = '.icon[title="' + iconTitle + '"]';
        document.querySelector(selector).classList.add('selected');
    };

    this.restoreContentHeight = function () {
        if(!!localStorage.getItem('contentHeight')){
            document.querySelector('.js-content').style.height = localStorage.getItem('contentHeight')
        }
    };

    this.contentResize = function () {
        localStorage.setItem('contentHeight', this.style.height);
    };

    this.divToTextbox = function () {
        content.value = preview.innerHTML;
    };

    this.clear = function () {
        selfPrinter.content.innerHTML = '';
        selfPrinter.selectIcon('empty');
    };

    this.confirmDelete = function (e) {
        if (!confirm('Usunąć wpis?')) {
            e.preventDefault();
            return false;
        }
    };


};
