__zIndex = 1111;

var win = {

    id: null,
    window: null,

    template:
            '<tr>'+
                '<td class="top_left" rowspan="2" colspan="2"></td>'+
                '<td class="top_middle"></td>'+
                '<td class="top_right" rowspan="2" colspan="2"></td>'+
            '</tr>'+
            '<tr class="top">'+
                '<td><a href="#" onclick="win.close(this); return false;">Закрыть</a><h3>Окно</h3></td>'+
            '</tr>'+
            '<tr>'+
                '<td class="middle_left"></td>'+
                '<td class="content" colspan="3"></td>'+
                '<td class="middle_right"></td>'+
            '</tr>'+
            '<tr>'+
                '<td class="bottom_left" rowspan="2" colspan="2"></td>'+
                '<td class="bottom_middle"></td>'+
                '<td class="bottom_right" rowspan="2" colspan="2"></td>'+
            '</tr>'+
            '<tr>'+
                '<td class="bottom"></td>'+
            '</tr>',

    init: function(id, caption, w){
        if ($(id)) $(id).destroy(); // or return;
        caption = caption.replace(' *', '');
        this.id = id;
        this.create();
        this.window.inject(document.body);
        this.window.getElement('h3').set('text', caption);
        this.setWidth(id, w);
        this.gotoCenter(id);
        this.makeDraggableWindow(id);

        $(id).addEvent('click', function(){
            if ($(id)) $(id).setStyle('z-index', ++__zIndex);
        });
    },

    create: function(w){
        this.window = new Element('table', {
            id: this.id,
            'class': 'window',
            styles: {
                left: -9999,
                top: -9999,
                zIndex: ++__zIndex + 2
            }
        }).set('html', this.template);
    },

    setWidth: function(id, w){
        $(id).getElement('td.top_middle').set('width', w-46);
    },

    makeDraggableWindow: function(id){
        new Drag.Move(id, {
            handle: $(id).getElement('tr.top > td'),
            onBeforeStart: function(win){
                $(id).setStyle('z-index', ++__zIndex);
            }
        });
    },

    gotoCenter: function(id){
        if ($(id))
        $(id).set('styles', {
            left: (Window.getWidth() - this.window.getWidth()) / 2 + Window.getScrollLeft(),
            top: (Window.getHeight() - this.window.getHeight()) / 2 + Window.getScrollTop()
        });
    },

    gotoThis: function(id, gotoId, go){
        if (go == 'right') {
            var left = $(gotoId).getLeft() + $(gotoId).getWidth() - 24;
            var top = $(gotoId).getTop() - 14;
        }
        if (go == 'bottom') {
            var left = $(gotoId).getLeft() - 24 - 23;
            var top = $(gotoId).getTop() + $(gotoId).getHeight() - 14;
        }
        if (go == 'top') {
            var left = $(gotoId).getLeft();
            var top = $(gotoId).getTop();
        }

        $(id).set('styles', {
            left: left,
            top: top
        });
    },

    content: function(id)
    {
        return $(id).getElement('td.content');
    },

    close: function(this_)
    {
        if ($(this_).get('tag') == 'a') var elem = $(this_).getParent().getParent().getParent().getParent(); else var elem = $(this_);
        if (Browser.ie) elem.destroy();
        else
        {
            var del = function(){ elem.destroy(); };
            elem.fade(0);
            del.delay(500);
        }
    }


};
