var lib;
if (!lib) lib = {};
else if (typeof lib != 'object') throw new Error('lib object exists');

lib.z_max = 2147483638;
lib.default_option = '---------';
lib.combo_dicts = {};

lib.opt = function (val, text) {
    var _opt = document.createElement('option');
    $(_opt).text(text).val(val);
    return _opt;
};

lib.sel = function () {
    var _sel = document.createElement('select');
    return _sel;
};

lib.hidden = function (name, value) {
    return $('<input type="hidden" name="'+name+'" value="'+value+'"/>');
};

lib.lvl = function (level) {
    return lib.z_max - (level+1);
};

lib.combo_from_dict = function(combo_selector, filter_id, dict, first_option) {
    var _combo = $(combo_selector);
    _combo.empty();
    _combo.append(lib.opt('1', first_option));
    if (filter_id in dict) {
        $(dict[filter_id]).each(function (inx, item) { _combo.append(lib.opt(item[0], item[1])); });
    }
};

lib.vtype = {'cars':1,'trucks':2,'motos':3};

lib.filter_int = function() {
    var _cleared = $(this).val().replace(/[^\d]+/g, '');
    var _int = parseInt(_cleared);
    if (isNaN(_int)) {
        _cleared = '';
    }
    $(this).val(_cleared);
};

lib.filter_dec = function() {
    var _cleared = $(this).val().replace(/[^\d\.]+/g, '');
    var _float = parseFloat(_cleared);
    if (isNaN(_float)) {
        _cleared = '';
    }
    $(this).val(_cleared);
};

lib.print_iframe = function(iframe_id) {
    var iframe = document.frames ? document.frames[iframe_id] : document.getElementById(iframe_id);
    var iframe_window = iframe.contentWindow || iframe;
    iframe.focus();
    iframe_window.print_document();
};

lib.message_top = 0;
lib.message_count = 0;
lib.message = function(message, delay, type) {
    if (typeof(type) == 'undefined') {
        type = 'info';
    }
    lib.message_count += 1;
    lib.message_top += 10;
    var _left = Math.round($(window).width() / 2) - 80;
    $('body').append('<div id="id_message_'+lib.message_count+'" class="js_message '+type+'" style="left:'+_left+'px;top:'+lib.message_top+'px"><div>'+message+'</div></div>');
    if (typeof(delay) == 'undefined') {
        delay = 3;
    }
    var _message_id = '#id_message_'+lib.message_count;
    var _height = $(_message_id).height();
    lib.message_top += _height;
    window.setTimeout(function(){$(_message_id).remove();lib.message_top-=10+_height;lib.message_count-=1;}, delay * 1000);
};

lib.ajax_post = function(post_url, post_data) {
    post_data['csrfmiddlewaretoken'] = probegauto.csrf;
    $.ajax({
        url: post_url,
        type: 'POST',
        data: post_data,
        error: function(){lib.message('Ошибка в Ajax запросе!', 3, 'error')}
    });
};

lib.show_dialog = function(e) {
    e.preventDefault();
    var _dialog = $('#id_dialog_container');
    if (_dialog.length == 0) {
        _dialog = $('<div id="id_dialog_container" style="display:hidden"></div>').appendTo('body');
    }
    var _href = $(this).attr('href');
    var _title = $(this).attr('title');
    var _is_load_dialog_js = $(this).hasClass('load_dialog_js');
    _dialog.load(_href,
            null,
            function() {
                _dialog.dialog({
                    open: function() {
                        if (_is_load_dialog_js) {
                            document.dialog_script.attach();
                        }
                    },
                    close: function() {
                        if (_is_load_dialog_js) {
                            document.dialog_script.detach();
                        }
                        _dialog.remove();

                    },
                    title: _title,
                    dialogClass: 'modal',
                    modal: true,
                    width: 450,
                    closeOnEscape: true,
                    draggable: false,
                    resizable: false,
                    position: 'center',
                    buttons: [
                        {
                            text: 'Отказаться',
                            click: function() { $(this).dialog('close');},
                            'class':'btn'
                        },
                        {
                            text: _title,
                            click: function() {$('#id_dialog_form').submit();},
                            'class':'btn btn-primary'
                        }
                    ]
                });
                var _left = Math.round($(window).width() / 2) - Math.round($('.modal.ui-dialog').width()/2);
                var _top = Math.round($(window).height() / 2) - Math.round($('.modal.ui-dialog').height()/2);
                $('.modal.ui-dialog').css({position:'fixed',left:_left+'px','top':_top+'px'});
            }
    );
};

