var Admin_Company_Edit_Widget = (function ($) {
return {
    dates: null,
    bindEvents: function (target, widgetId) {
        var w = this;
        $('#tariff').change(function () {
            ar(target, 'tariffChanged', 'code=' + encodeURIComponent(this.value))
        });
        $('#form_' + widgetId + ' .allow_html').ckeditor({
            customConfig : '/js/ckeditor_config.js',
            width:'400px'
        });

        this.dates = $('input[name=contractBegin], input[name=contractEnd]', $('#form_' + widgetId)).datepicker({
            dateFormat: 'dd.mm.yy',

            beforeShow: function (input) {
                var otherDate = w.dates.not(input).datepicker('getDate');
                var option = (input.name == "contractEnd"? "minDate" : "maxDate");
                $(input).datepicker("option", option, otherDate);
            }
        });
    },
    prepare_popup : function(id, mode) {
        var width = 1000;
        var height = 510;

        if (mode == 'edit') {
            width = 800;
            height = 650;
        }

        $('div.ui-dialog').remove();
        
        if(!$('#popup_'+id).length) {
            $('<div id="popup_' + id + '"></div>').appendTo('body');
        }
        
        $('#popup_'+id).dialog({
            bgiframe: true,
            autoOpen: false,
            width: width,
            height: height,
            modal: true
        })
    },
    show_popup : function (id) {
        $('#popup_' + id).dialog('open');
    },
    hide_popup : function(id) {
        $('#popup_' + id).dialog('close');
    },
    destroyEditor : function (id) {
        $('#form_' + id + ' .allow_html').each(
            function(){
                var inputId = $(this).attr('id');
                if (CKEDITOR.instances[inputId]) CKEDITOR.instances[inputId].destroy();
            }
        );
    }
};
}(jQuery));
