var Window = Class.create({
    _params: {  
        name: 'window',
        className: 'window',
        post: null
    },
    _load_html: '<div class="window_load"></div>',
    _onshow: null,
    _onload: null,
    _onclose: null,
    
    initialize: function(params) {
        name = 'w' + Math.round(Math.random()*1000000);
        
        this._setparams(params);
        this._params.name = name;
        this.show();
    },
    
    _setparams: function(params) {
        if(params != undefined) {
            if(params.url != undefined) {
                this._params.url = params.url;
            }
            else {
                this._params.html = params.html;
            }
            
            if(params.post != undefined) {
                this._params.post = params.post;
            }
            
            if(params.onshow != undefined) {
                this._onshow = params.onshow;
            }
            if(params.onclose != undefined) {
                this._onclose = params.onclose;
            }
            if(params.onload != undefined) {
                this._onload = params.onload;
            }
        }
    },
    
    show: function() {
        this._bg = new Element('img');
        this._bg.addClassName('window_bg');
        if(isSuxx()) {
            $(this._bg).setStyle({display: 'none'});
        }
        $$('body')[0].appendChild(this._bg);
        
        
        this._window = new Element('div');
        this._window.addClassName('window');
        
        this._close = new Element('a', {href: '#'});
        this._close.addClassName('window_close');
        this._close.observe('click', (function(e) { e.stop(); this.close()}).bind(this));
        this._window.appendChild(this._close);
        
        this._body = new Element('div');
        this._body.addClassName('window_body');
        this._window.appendChild(this._body);
        
        
        if(this._params.url != undefined) {
            this.update({url: this._params.url, post: this._params.post});
        }
        else {
            this.update({html: this._params.html});
        }
        $$('body')[0].appendChild(this._window);
        
        Event.observe(window, 'keyup', (function(e) {
            if(e.keyCode == Event.KEY_ESC) { 
                this.close();
                e.stop(); 
            } 
        }).bind(this));
        //callback
        if(this._onshow != null) {
            (this._onshow)();
        }
    },
    
    update: function(params) {
        this._setparams(params);
        if(params.html != undefined) {
            this._body.update(params.html);
            this.center();
        }
        else if (params.url != undefined) {
            if(params.post != undefined) {
                post = params.post;
            }
            else {
                post = {};
            }
            this.update({html: this._load_html});
            new Ajax.Request(this._params.url, {
                parameters: post,
                onComplete: (function(t) {
                    this.update({html: t.responseText});
                    //callback
                    if(this._onload != null) {
                        (this._onload)();
                    }
                    this.center();
                }).bind(this)
            });
        }
        
    },
    
    center: function(animate) {
        if(animate == undefined) {
            if(!isSuxx()) {
                this._window.setStyle({marginLeft: '-' + Math.round(this._window.getWidth()/2) + 'px', marginTop: '-' + Math.round(this._window.getHeight()/2) + 'px'});
            }
            else {
                var width1 = 'auto';
                if(this._window.getWidth() > 0) {
                    width1 = this._window.getWidth() + 'px';
                    
                }
                this._window.setStyle({position: 'absolute', width: width1, marginLeft: '-' + Math.round(this._window.getWidth()/2) + 'px', marginTop: '-' + Math.round(this._window.getHeight()/2) + 'px'});
            }
        }
    },
    
    close: function() {
        this._window.remove();
        this._bg.remove();
        //callback
        if(this._onclose != null) {
            (this._onclose)();
        }
        
        Event.stopObserving(window, 'keyup');
    }
    
});
