var winbox = {
    stack: {},
    _base: 0,
    zindex: 1000,
    init: function(){
        $(window).resize(function(){
            $('div.winbox_frame > div.body').css({
                width: '',
                height: ''
            })
            winbox._sizing();
            winbox._center();
        });
    },
    _create_window:function(whead, wbody, data, wid, noclose){
        var frame, bd, sh;
        this._base++;
        this.stack[wid || (wid = this._base)] = {
            modal: $('<div class="winbox_modal" id="winmodal_' + wid + '"></div>')
                        .css('z-index', winbox.zindex + this._base)
                        .appendTo('body'),
            wframe: frame = $('<div id="winframe_' + wid + '" class="winbox_frame" wid="' + wid + '"></div>')
                        .css('z-index', winbox.zindex + this._base + 1)
                        .appendTo('body'),
            whead: hd = $('<div class="head">' + whead + '</div>')
                        .appendTo(frame),
            wbody: bd = $('<div class="body"></div>')
                        .appendTo(frame),
            wclose: noclose != true
                    ? $('<div class="close">close</div>')
                        .appendTo(hd)
                        .click(function(){
                            winbox.close(this);
                        })
                    : null
        };
        $(data && wbody.match(/^\w+::\w+/) && dtpl.proc(wbody, data) || wbody).appendTo(bd);
        return wid;
    },
    _sizing: function(id){
        if(typeof(id) == 'undefined') {
            for(var i in this.stack) if(i !== null) this._sizing(i);
            return;
        } else if(this.stack[id] == null) return;
        var pp = this.stack[id],
            ca = this._get_box(pp.modal),
            pbox = this._get_box(pp.wframe),
            bbox = this._get_box(pp.wbody);

        if(ca.cwidth < pbox.width){
            $(pp.wbody).css('overflow-x', 'scroll');
            $(pp.wbody).css('width', ca.width - (ca.width * .2));
        } else $(pp.wbody).css('overflow-x', 'visible');
        if(ca.cheight < pbox.height){
            $(pp.wbody).css('overflow-y', 'scroll');
            $(pp.wbody).css({
                height: ca.height - (ca.height * .2),
                width: bbox.width + 15
            });
        } else $(pp.wbody).css('overflow-y', 'visible');
    },
    _center: function(id){
        if(typeof(id) == 'undefined') {
            for(var i in this.stack) if(i !== null) this._center(i);
            return;
        } else if(this.stack[id] == null) return;
        var pp = this.stack[id],
            pbox = this._get_box(pp.wframe),
            ca = this._get_box(pp.modal);

        $(pp.wframe).css({
            left: Math.floor((ca.width - pbox.width) / 2),
            top: Math.floor((ca.height - pbox.height) / 2)
        });
    },
    _get_box: function(ctrl){
        if(typeof(ctrl) == 'string') ctrl = $(ctrl);
        return {
            top: ctrl[0].offsetTop,
            left: ctrl[0].offsetLeft,
            right: ctrl[0].offsetLeft + ctrl[0].offsetWidth,
            bottom: ctrl[0].offsetTop + ctrl[0].offsetHeight,
            width: ctrl[0].offsetWidth,
            height: ctrl[0].offsetHeight,
            cwidth: ctrl[0].clientWidth,
            cheight: ctrl[0].clientHeight
        }
    },
    open: function(whead, wbody, data, wid, noclose){
        if(typeof(data) == 'string' && (typeof(wid) == 'undefined' || typeof(wid) == 'boolean')){
            noclose = wid;
            wid = data;
            data = null;
        }
        wid = this._create_window(whead, wbody, data, wid, noclose);
        this._sizing(wid);
        this._center(wid);
        return wid;
    },
    close: function(id){
        if(!this.stack[id]){
            switch(typeof(id)){
                case 'object':
                    id = $(id).parents('div.winbox_frame').attr('wid');
                    break;
            }
            if(!id) return;
        }
        $('#winframe_' + id)
            .fadeOut(250, function(){
                $(this).remove();
                $('#winmodal_' + id).remove();
            });
        this.stack[id] = null;
    },
    body_reload: function(id, src){
        var pp = this.stack[id];
        if(typeof(src) == 'string') src = $(src);
        $(pp.wbody).remove();
        pp.wbody = $('<div class="body"></div>').append(src);
        $(pp.wframe).append(pp.wbody);
        this._sizing(id);
        this._center(id);
    }
}
