
function set_key_bind_events() {
    $(body).unbind('mousedown').mousedown(function(e) {
        mouse_click_active = true;
        if ($(e.target).is('span.text_seg') && !$(e.target).hasClass('other_block_rep') && all_func_disabled() == true && all_menu_func_disabled() == true && key_active[17] != true) {
            remove_select_block_parts("");
            $(e.target).addClass('selected_span');
            update_font_change_select($(e.target));
            active_font_toolbar(e);
            highlight_selected();
        }
        if ($(pageparts).children().find(input).length == 0 && $(e.target).is(span) && !$(e.target).hasClass('menu_item_label') && key_active[17] == true) {
            if ($(e.target).hasClass('selected_span')) {
                $(e.target).removeClass('selected_span');
            } else {
                $(e.target).addClass('selected_span');
            }
            highlight_selected();
        }
    });

    $(body).unbind('mouseup').mouseup(function(e) {
        mouse_click_active = false;
    });

    $(pageparts).unbind('mouseup').mouseup(function(e) {
        if (active_func.create_new_part && $(e.target).is('div.ui-sortable')) {
            $(pageparts).children().children().each(function(i, el) {
                if ($(el).is('span') || $(el).is('start') || $(el).is('end')) {
                    if ($(el).is('start') || $(el).is('end')) {
                        if (e.offsetY + 15 > $(el).position().top && e.offsetY - 15 < $(el).position().top) {
                            add_history_step('new block');
                            var clone = span_create_temp().clone();
                            clone.insertAfter($(el));
                            block_part_set_events(clone);
                            return false;
                        }
                    }
                    if (e.offsetY + 15 > $(el).position().top && e.offsetY - 15 < $(el).position().top &&
                        e.offsetX + 10 > $(el).position().left && e.offsetX - 10 < $(el).position().left) {
                        add_history_step('new point');
                        var clone = span_create_temp().clone();
                        clone.insertBefore($(el));
                        block_part_set_events(clone);
                    }
                }
            });
        }
        if (active_func.create_new_block) {
            $(pageparts).children().children().each(function(i, el) {
                if (($(el).is('span') || $(el).is('start') || $(el).is('end')) && active_func.create_new_block && !check_if_not_hidden($(el))) {
                    if ($(el).is('start') || $(el).is('end')) {
                        if (e.offsetY + 15 > $(el).position().top && e.offsetY - 15 < $(el).position().top) {
                            add_history_step('new block');
                            get_end_start_num($(el));
                            active_func.create_new_block = false;
                            disable_edit_tools();
                            return false;
                        }
                    }
                    if (e.offsetY + 15 > $(el).position().top && e.offsetY - 15 < $(el).position().top &&
                        e.offsetX + 15 > $(el).position().left && e.offsetX - 15 < $(el).position().left) {
                        add_history_step('new point');
                        get_end_start_num($(el));
                        active_func.create_new_block = false;
                        disable_edit_tools();
                    }
                }
            });
        }
    });

    $(pagepreview).unbind('mousewheel').bind('mousewheel', function(e) {
        if (key_active[17] == true) {
            if (e.originalEvent.wheelDelta > 0) {
                zoom_page_prev('', '+')
            } else {
                zoom_page_prev('', '-')
            }
            return false;
        }
    });

    $(pageparts).unbind('mousewheel').bind('mousewheel', function(e) {
        if (key_active[17] == true) {
            if (e.originalEvent.wheelDelta > 0) {
                zoom_page_edit('', '+')
            } else {
                zoom_page_edit('', '-')
            }
            return false;
        }
    });

    $(body).unbind('click').click(function(e) {

        if (all_func_disabled() && $(pageparts).children().find(input).length == 1) {
        }
        if ($(pageparts).children().find(input).length == 1 && $(e.target).is(input) == false && $(e.target).text() != "keyboard" && dialogs.open_keyboard == false) {
            block_input_edit($(pageparts).children().find('input'));
        }
        for (var i in key_active) {
            if (key_active[i] == true && key_bind_global[i] != undefined) {
                key_bind_global[i](e);
                disable_key_binds(e);
            }
        }
    });

    $(body).unbind('keydown').keydown(function(e) {
        console.log("key: " + e.keyCode);
        if (!key_active[e.keyCode] && key_bind_global[e.keyCode] != undefined) key_active[e.keyCode] = true;

        if (e.keyCode == 16) {
            unset_sortable_pageparts();
        }
        if (key_active[16] == true) {
            key_bind_global[e.keyCode];
        }
        if (e.keyCode <= 40 && e.keyCode >= 37) {
            key_bind_global[e.keyCode](e);
        }
        if (e.keyCode == 27) {
            key_bind_global[e.keyCode](e);
        }
        if (e.keyCode == 13 && $(selected_span).length == 1 && $(pageparts).children().find(input).length == 0) {
            block_input_edit($(selected_span));
        }
        if (e.keyCode == 13 && $(pageparts).children().find(input).length == 1) {
            remove_select_block_parts("");
        }
        if (e.keyCode == 46 && $(selected_span).length > 0 && $(pageparts).children().find(input).length == 0) {
            key_bind_global[e.keyCode](e);
            disable_key_active();
        }
    });

        if (key_active[e.keyCode] && key_bind_global[e.keyCode] != undefined) {
            key_active[e.keyCode] = false;
        }
        if (e.keyCode == 16) {
            set_sortable_pageparts();
            arrow_move_unset();
        }


            if ($(selected_span).length > 0) {
                load_block_style_change($(selected_span));
            }
        }

            insert_hidden_space(e);
        }

            var input = $(pageparts_input);
            var arr = input.val().split('');
            var echo = "";
            for (var i in arr) {
                echo += " " + arr[i] + " (" + arr[i].charCodeAt(0) + ")";
            }
            console.log(echo);
        }

            add_history_step('font');
            update_aip_size_keys("16");
        }
            add_history_step('font');
            update_aip_size_keys("24");
        }
            add_history_step('font');
            update_aip_size_keys("32");
        }
            add_history_step('font');
            update_aip_size_keys("16");
        }
            add_history_step('font');
            update_aip_size_keys("24");
        }
            add_history_step('font');
            update_aip_size_keys("32");
        }


            if ($(selected_span).length > 0) {
                add_history_step('font');
                show_hint_msg('Změna na Cyrillica Bulgarian 10U');
                font_update_keybind('ocs');
            }
        }
            if ($(selected_span).length > 0) {
                add_history_step('font');
                show_hint_msg('Změna na Free Serif');
                font_update_keybind('ru')
            }
        }
            if ($(selected_span).length > 0) {
                add_history_step('font');
                show_hint_msg('Změna na Arial Narrow');
                font_update_keybind('arial');
            }
        }


            if ($(selected_span).length > 0) {
                add_history_step('style');
                show_hint_msg('Změna na Tučné');
                update_aip_style_keys('bold');
            }

        }
            if ($(selected_span).length > 0) {
                add_history_step('style');
                show_hint_msg('Změna na Kurzíva');
                update_aip_style_keys('italics');
            }

        }
            if ($(selected_span).length > 0) {
                add_history_step('index');
                show_hint_msg('Změna na Horní index');
                update_aip_index_keys('horni');
            }
        }
            if ($(selected_span).length > 0) {
                add_history_step('index');
                show_hint_msg('Změna na Dolní index');
                update_aip_index_keys('dolni');
            }
        }


            if ($(selected_span).length > 0) {
                copy_clipboard();
            }
        }
            if ($(selected_span).length > 0) {
                paste_clipboard();
            }
        }
            if ($(selected_span).length > 0) {
                add_history_step('prev style');
                cut_clipboard();
            }
        }


        if (key_active[17] == false && e.keyCode == 17) {
        }
        if (key_active[16] == false && e.keyCode == 16 && $(selected_span).length > 1 && dialogs.open_delete_blocks == false) {

            set_sortable_pageparts();
        }
            if (key_active[16] == true) {
                key_bind_global[e.keyCode](e);
            }
        }
        if (e.keyCode == 16) {
            cache.select_first = undefined;
        }
            if (e.keyCode == 74) {
                key_bind_global[e.keyCode](e);
            }

        }
            history_prev('');
        }
            history_next('');
        }
        refresh_all_info();
    });
}
function update_aip_style_keys(index) {
    var same = true;
    $(selected_span).each(function() {
        if ($(this).attr('aip-style') != index) {
            same = false;
        }
    });
    if (same) {
        $(selected_span).css('font-weight', '');
        $(selected_span).attr('aip-style', 'normal');
        $(selected_span).css({
            'font-style': 'normal'
        });
    } else {
        $(selected_span).attr('aip-style', index);
        trasnlate_attr_(index, $(selected_span));
    }
    update_font_change_select($(selected_span).first());
}
function update_aip_index_keys(index) {
    var same = true;
    $(selected_span).each(function() {
        if ($(this).attr('aip-index') != index) {
            same = false;
        }
    });
    if (same) {
        $(selected_span).attr('aip-index', '');
        $(selected_span).css({
            'text-decoration': 'none'
        });
    } else {
        change_el_index($(selected_span), index);
    }
    update_font_change_select($(selected_span).first());
}
function update_aip_size_keys(size) {
    font_select_change_update_span(size, 'aip-size');
    update_font_change_select($(pageparts).children().find('.selected_span').first());
    refresh_all_info();
}
function arrow_move_unset() {
    cache.lr = undefined
    cache.ud = undefined
}
function arrow_move_get(se) {
    switch (se) {
        case "left":
        case "right":
            if (cache.lr == undefined) {
                cache.lr = se;
            }
            if (cache.lr == se) {
                return true;
            }
            return false;
            break;
        case "up":
        case "down":
            if (cache.ud == undefined) {
                cache.ud = se;
            }
            if (cache.ud == se) {
                return true;
            }
            return false;
            break;
    }
}
function remove_select_block_parts(e) {
    $('span').removeClass('selected_span');
    if (e != "") {
        disable_key_binds(e);
    }
    highlight_selected();
}

function generate_type_index_array() {
    for (var i in word_type) {
        type_index_array.push(i);
    }
}
function select_prev_block_part_style(e) {
    if ($(pageparts).children().find(input).length == 0) {
        add_history_step('prev style');
        var index = 0;
        var in_ = 0;
        for (var i in type_index_array) {
            if (type_index_array[i] == $(pageparts).children().find('.selected_span').attr('aip-type')) {
                index = parseInt(i);
                break;
            }
        }
        if ((index - 1) >= 0) {
            in_ = index - 1;
        } else {
            in_ = type_index_array.length - 1;
        }
        show_hint_msg('Změna na: ' + word_type_trans[type_index_array[in_]]);
        $(pageparts).children().find('.selected_span').attr('aip-type', type_index_array[in_]);
        if (second_select) {
            switch (type_index_array[in_]) {
                case "zahlavi":
                    $(pageparts).children().find('.selected_span').attr('aip-grka', 'a-kmeny')
                    break;
                case "nasobne_zahlavi":
                    $(pageparts).children().find('.selected_span').attr('aip-grka', 'a-kmeny');
                    break;
                default:
                    $(pageparts).children().find('.selected_span').attr('aip-grka', '')
                    break;
            }
        }
        set_nepresne_to_ssl(type_index_array[in_],$(pageparts).children().find('.selected_span'));
        $(pageparts).children().find('.selected_span').css({
            'background-color': aip_type_colors[type_index_array[in_]]
        });
        if (type_index_array[in_] !== "text_hesla") {
            set_rule($(pageparts).children().find('.selected_span'));
        }
        refresh_all_info();
    }
}
function select_next_block_part_style(e) {
    if ($(pageparts).children().find(input).length == 0) {
        add_history_step('next style');
        var index = 0;
        var in_ = 0;
        for (var i in type_index_array) {
            if (type_index_array[i] == $(pageparts).children().find('.selected_span').attr('aip-type')) {
                index = parseInt(i);
                break;
            }
        }
        if ((index + 1) <= type_index_array.length - 1) {
            in_ = index + 1;
        } else {
            in_ = 0;
        }
        show_hint_msg('Změna na: ' + word_type_trans[type_index_array[in_]]);
        console.log(type_index_array[in_]);
        $(pageparts).children().find('.selected_span').attr('aip-type', type_index_array[in_]);
        if (second_select) {
            switch (type_index_array[in_]) {
                case "zahlavi":
                    $(pageparts).children().find('.selected_span').attr('aip-grka', 'a-kmeny')
                    break;
                case "nasobne_zahlavi":
                    $(pageparts).children().find('.selected_span').attr('aip-grka', 'a-kmeny');
                    break;
                default:
                    $(pageparts).children().find('.selected_span').attr('aip-grka', '')
                    break;
            }
        }
        set_nepresne_to_ssl(type_index_array[in_],$(pageparts).children().find('.selected_span'));
        $(pageparts).children().find('.selected_span').css({
            'background-color': aip_type_colors[type_index_array[in_]]
        });
        if (type_index_array[in_] !== "text_hesla") {
            set_rule($(pageparts).children().find('.selected_span'));
        }
        refresh_all_info();
    }
}
function select_prev_block_part(e) {
    var sp = $(pageparts).children().children('span');
    var sp_sl = $(pageparts).children().find('.selected_span');
    if ($(pageparts).children().find(input).length == 0 && dialogs.open_delete_blocks == false && dialogs.change_style == false && dialogs.open_delete_block == false) {
        if (key_active[16]) {
            if (arrow_move_get('left')) {
                $(pageparts).children().find('.selected_span').first().prev('span').addClass('selected_span');
            } else {
                if ($(selected_span).length > 1) {
                    $(pageparts).children().find('.selected_span').last().removeClass('selected_span');
                }
            }
        } else {
            if ($(pageparts).children().find('.selected_span').length == 0) {
                sp.last().addClass('selected_span');
            } else {
                if ($(selected_span).length > 1) {
                    var orl = sp_sl.first();
                    sp_sl.removeClass('selected_span');
                    orl.addClass('selected_span');
                } else {
                    sp_sl.prev().addClass('selected_span');
                    sp_sl.last().removeClass('selected_span');
                }
            }
        }
    }
    if (sp_sl.first().length > 0) {
        if ((sp_sl.first().offset().top) < 0 && sp_sl.first().offset().top !== undefined) {
            $(pageparts).scrollTop($(pageparts)[0].scrollTop - 100);
        }
        update_font_change_select(sp_sl.first().prev());
    }
    highlight_selected();
}
function select_next_block_part(e) {
    var sp = $(pageparts).children().children('span');
    var sp_sl = $(pageparts).children().find('.selected_span');
    if ($(pageparts).children().find(input).length == 0 && dialogs.open_delete_blocks == false && dialogs.change_style == false && dialogs.open_delete_block == false) {
        if (key_active[16]) {
            if (arrow_move_get('right')) {
                $(pageparts).children().find('.selected_span').last().next('span').addClass('selected_span');
            } else {
                if ($(selected_span).length > 1) {
                    $(pageparts).children().find('.selected_span').first().removeClass('selected_span');
                }
            }
        } else {
            if ($(pageparts).children().find('.selected_span').length == 0) {
                sp.first().addClass('selected_span');
            } else {
                if ($(selected_span).length > 1) {
                    var orl = sp_sl.last();
                    sp_sl.removeClass('selected_span');
                    orl.addClass('selected_span');
                } else {
                    sp_sl.next().addClass('selected_span');
                    sp_sl.first().removeClass('selected_span');
                }
            }
        }
    }
    var scroll = ($(pageparts)[0].clientHeight);
    if (sp_sl.first().offset()) {
        if ((sp_sl.first().offset().top + sp_sl.first().height()) > scroll && sp_sl.first().offset().top !== undefined) {
            if (cache.scroll_next == undefined) {
                $(pageparts).scrollTop(100);
                cache.scroll_next = 1;
            } else {
                $(pageparts).scrollTop($(pageparts)[0].scrollTop + 100);
            }
        }
    }
    update_font_change_select(sp_sl.last().next());
    highlight_selected();
}

function select_up_block_part(e) {
    var sp = $(pageparts).children().children('span');
    var sp_sl = $(pageparts).children().find('.selected_span').first();
    if ($(pageparts).children().find(input).length == 0 && dialogs.open_delete_blocks == false && dialogs.change_style == false && dialogs.open_delete_block == false) {
        if (arrow_move_get('up')) {
            arrow_up_el($(pageparts).children().find('.selected_span').first(), select_up_block_part_callback);
        } else {
            arrow_up_el($(pageparts).children().find('.selected_span').last(), select_up_block_part_callback);
        }
    }
}
function select_up_block_part_callback(el) {
    var sp = $(pageparts).children().children('span');
    var sp_sl = $(pageparts).children().find('.selected_span');
    if (key_active[16]) {
        if (arrow_move_get('up')) {
            sp_sl.first().addClass('selected_span');
            sp_sl.first().prevUntil(el, "span").addClass('selected_span');
            el.addClass('selected_span');
        } else {
            var first = sp_sl.first();
            if (first.position().top < el.position().top) {
                sp_sl.removeClass('selected_span');
                el.prevUntil(first, "span").addClass('selected_span');
                el.addClass('selected_span');
                first.addClass('selected_span');
            } else {
                sp_sl.removeClass('selected_span');
                first.addClass('selected_span');
                arrow_move_unset();
            }
        }
    } else {
        sp_sl.removeClass('selected_span');
        el.addClass('selected_span');
    }
    setTimeout(function() {
        update_font_change_select(sp_sl.first());
    }, 100);
    highlight_selected();
    return false;
}
function select_down_block_part(e) {
    var sp = $(pageparts).children().children('span');
    var sp_sl = $(pageparts).children().find('.selected_span').last();
    if ($(pageparts).children().find(input).length == 0 && dialogs.open_delete_blocks == false && dialogs.change_style == false && dialogs.open_delete_block == false) {
        if (arrow_move_get('down')) {
            arrow_down_el($(pageparts).children().find('.selected_span').last(), select_down_block_part_callback);
        } else {
            arrow_down_el($(pageparts).children().find('.selected_span').first(), select_down_block_part_callback);
        }
    }
}
function select_down_block_part_callback(el) {
    var sp = $(pageparts).children().children('span');
    var sp_sl = $(pageparts).children().find('.selected_span');
    if (key_active[16]) {
        if (arrow_move_get('down')) {
            sp_sl.first().addClass('selected_span');
            sp_sl.first().nextUntil(el, "span").addClass('selected_span');
            el.addClass('selected_span');
        } else {
            var last = sp_sl.last();
            if (last.position().top > el.position().top) {
                sp_sl.removeClass('selected_span');
                el.nextUntil(last, "span").addClass('selected_span');
                el.addClass('selected_span');
                last.addClass('selected_span');
            } else {
                sp_sl.removeClass('selected_span');
                last.addClass('selected_span');
                arrow_move_unset();
            }
        }
    } else {
        sp_sl.removeClass('selected_span');
        el.addClass('selected_span');
    }
    setTimeout(function() {
        update_font_change_select(sp_sl.first());
    }, 100);
    highlight_selected();
    return false;
}


function find_up(source, target, top) {
    var source_ = source.position();
    var target_ = target.position();
    if (target_ != undefined && source_ != undefined) {
        if (((source_.left) >= target_.left && source_.left <= (target_.left + target.width() + 5)) &&
            ((source_.top - top) <= target_.top && (source_.top + top) >= (target_.top + target.height())) &&
            target.attr('list_num') != source.attr('list_num')) {
            return true;
        }
    }
}
function arrow_up_el(source, callback) {
    var i = 0;
    var g_ = false;
    var to_start = source.prevUntil($(pageparts_div).find('span').first(), 'span').length;
    var to_source = $(pageparts_div).find('span').first().nextUntil(source, "span").length + 1;
    var spans;
    if (to_start < 30) {
        spans = $(pageparts_div).find('span').first().nextUntil(source, "span");
    } else {
        spans = $(pageparts_div).find('span').slice(to_source - 30, to_source)
    }
    var oll;
    while (!g_) {
        spans.each(function() {
            if (find_up(source, $(this), i)) {
                console.log('mám: ' + $(this).text());
                oll = $(this);
                g_ = true;
            }
        });
        i += 5;
        if (i >= 300) {
            console.log('force stop');
            g_ = true;
            return false;
        }
    }
    callback(oll);
}
function find_down(source, target, top) {
    var source_ = source.position();
    var target_ = target.position();
    if (source_ && target_) {
        if (((source_.left) >= target_.left && source_.left <= (target_.left + target.width() + 5)) &&
            (((source_.top + source.height())) <= (target_.top + top) && (source_.top + top) >= (target_.top)) &&
            target.attr('list_num') != source.attr('list_num')) {
            return true;
        }
    }
}
function arrow_down_el(source, callback) {
    var i = 0;
    var g_ = false;
    var to_start = source.nextUntil($(pageparts_div).find('span').last(), 'span').length;
    var to_source = $(pageparts_div).find('span').first().nextUntil(source, "span").length + 1;
    var spans;
    if (to_start < 30) {
        spans = $(pageparts_div).find('span').last().prevUntil(source, "span");
    } else {
        spans = $(pageparts_div).find('span').slice(to_source, to_source + 30);
    }
    var oll;
    while (!g_) {
        spans.each(function() {
            if (find_down(source, $(this), i)) {
                console.log('mám: ' + $(this).text());
                oll = $(this);
                g_ = true;
            }
        });
        i += 5;
        if (i >= 300) {
            console.log('force stop');
            g_ = true;
            return false;
        }
    }
    callback(oll);
}



function disable_key_binds(e) {
    for (var i in key_active) {
        key_active[i] = false;
    }
}
function select_block_part(e, target) {
    if ($(target).is('span') && $(pageparts).children().find(input).length == 0) {
        if (cache.select_first == undefined) {
            cache.select_first = target;
            cache.select_e = e;
        } else {
            remove_select_block_parts("");
            $(cache.select_first).addClass('selected_span');
            if (cache.select_e.pageY > e.pageY) {
                $(cache.select_first).prevUntil($(target), 'span').addClass('selected_span');
            } else {
                $(cache.select_first).nextUntil($(target), 'span').addClass('selected_span');
            }
            $(target).addClass('selected_span');
        }
    }
}
function delete_block_part(target) {
    if ($(target).is('span')) {
        add_history_step('remove part')
        select_deleted_prev($(target));
        $(target).remove();
        console.log(key_active);
    }
}
function select_deleted_prev(del) {
    var done = false;
    $(pageparts_div).find('span').removeClass('selected_span');
    del.first().prevAll().each(function() {
        if ($(this).is('span') && !done) {
            $(this).addClass('selected_span');
            done = true;
        }
    });
}
function all_func_disabled() {
    var wat = true
    for (var i in key_active) {
        if (key_active[i] == true) {
            wat = false;
        }
    }
    return wat;
}
function disable_all_func() {
    for (var i in key_active) {
        key_active[i] = false;
    }
    for (var i in active_func) {
        active_func[i] = false;
    }
    disable_edit_tools();
}
function all_menu_func_disabled() {
    var wat = true
    for (var i in active_func) {
        if (active_func[i] == true) {
            wat = false;
        }
    }
    return wat;
}


function set_input_binds(input) {
    input.unbind().keydown(function(e) {
        if (key_bind_input[e.keyCode] != undefined && all_func_disabled()) {
            key_bind_input[e.keyCode](e, input);
        }
    });
    input.keyup(function(e) {
        if (e.keyCode == 13 && key_active[18] == true) {
            create_new_block_input(e);
            return false;
        }
        if (e.keyCode == 8 && key_active[18] == true) {
            remove_prev_block_input(e);
            return false;
        }
        if (e.keyCode == 46 && key_active[18] == true) {
            disable_key_active();
            remove_next_block_input(e);
            return false;
        }
    });
    input.on('input', function(e) {
        check_safe_space(e, input);
    });
    input.click(function(e) {
        cache.input_key_pos = e.target.selectionStart;
    });
}
function check_safe_space(e, input) {
    var val = $.trim(input.val());
    var first;
    var parts = val.split(' ');
    if (parts.length > 1) {
        for (var i in parts) {
            if (parts[i] != "") {
                if (i == 0) {
                    input.val(parts[i]);
                } else {
                    var clone = cache.input_span_replace.clone();
                    clone.insertBefore(input);
                    clone.text(parts[i]);
                    clone.attr('list_num', ++cache.i);
                    clone.removeClass('new_create');
                    block_part_set_events(clone);
                    if (first == undefined) {
                        first = clone;
                    }
                    if (input.hasClass('new_create') == true) {
                        set_el_to_freeserif(clone);
                        input.addClass('change_to_freeserif');
                    }
                    set_inline_block();
                }
            }
        }
        input.insertBefore(first);
        input.focus();
    }
}
function merge_block_parts_delete(e, input) {
    var curs = e.target.selectionStart;
    if (input[0].selectionStart == input.val().length && input.next().is('span')) {
        var value = input.next().text();
        setTimeout(function() {
            input.val(input.val() + value);
            input.setCursorPosition(curs);
            input.next().remove();
            return false;
        }, 10);
    } else {
        if (input[0].selectionStart == input.val().length && !input.next().is('span')) {
            remove_next_block_input(e);
            return false;
        }
    }
}
function merge_block_parts_delete_prev(e, input) {
    if (input[0].selectionStart == 0 && input.prev().text() != "" && input.prev().is('span')) {
        add_history_step('merge part');
        var value = input.prev('span').clone().text();
        var input_val = input.clone().val();
        var result = value + input_val;
        setTimeout(function() {
            input.val(result);
            input.prev('span').remove();
            set_input_width_by_lenght(input, result);
            input.setCursorPosition(result.length - input_val.length);
        }, 100);
        return false;
    } else {
        if (input[0].selectionStart == 0 && !input.prev().is('span')) {
            remove_prev_block_input(e);
            return false;
        }
    }
}
function split_block_parts_space(e, input) {
    var clone = cache.input_span_replace.clone();
    var text = input.val();
    var cont = true;
    if (text.substr(0, input[0].selectionStart).length > 0 && text.substr(input[0].selectionStart, text.length).length > 0) {
        add_history_step('split part');
        clone.text(text.substr(input[0].selectionStart, text.length));
        input.val(text.substr(0, input[0].selectionStart));
        clone.insertAfter(input);
        block_part_set_events(clone);
        cont = false;
    }
    if (input[0].selectionStart == input[0].selectionEnd && $.trim(input.val()) != "" && cont == true) {
        add_history_step('new point');
        clone.unbind();
        input.unbind();
        block_part_set_events(clone);
        if (input[0].selectionStart != 0) {
            clone.insertAfter(input);
        } else {
            clone.insertBefore(input);
        }
        block_input_edit(input);
        clone.attr('list_num', ++cache.i);
        update_el_info(clone);
        block_input_edit(clone);
        setTimeout(function() {
            $(pageparts_div).find('input').val('');
        }, 100);
    }
}

function active_font_toolbar(e) {

}

function font_select_change_event(parent) {
    parent.find('select').each(function(i, el) {
        $(el).change(function(i_, el_) {
            font_select_change_update_span($(el).find('option:selected').val(), $(el).attr('wat'));
        });
    });
}
function activate_font_change_toolbar(a) {
    $(font_change_toolbar).stop().fadeIn(500)

}
