var cWindow = {
    
    // abstract methods
    applyData: function(data)
    {
        alert('Window is reloaded');
    },
    
    ajaxRequest: function(rm, params, timeout)
    {
        if (!rm) {
            throw new Exception(ERROR_WRONG_PARAMETERS);
        }
        
        params = params ? params : {};
        
        params['rm'] = rm;
        params['window_class'] = this._class;
        params['window_name']  = this.name;
        
        var r = new Request();
        if (r !== null) {
            r.send(URL_AJAX_HANDLER, params, this, timeout);
        } else {
            throw new Exception(ERROR_WINDOW_AJAX);
        }
        
        delete(r);
    },

    reload: function()
    {
        this.ajaxRequest('window_reload', {});
    },

    handleJSException: function(e)
    {
        alert(e);
    },

    requestCallback: function(result, request_params, exception)
    {
        try {
            if (request_params.rm == 'window_reload') {
                this.applyData(result);
            }
        }
        catch (e) {
            this.handleJSException(e);
        }
    },

    // public methods
    setVisibility: function(visibile)
    {
        this.dom_window.style.display = visibile ? 'block' : 'none';
        this.is_visible = visibile;
    },
    
    getName: function()
    {
        return this.name;
    }
}

function Window(data) 
{
    if (!window.windows) {
        window.windows = [];
    }
            
    for (var f in cWindow) {
        this[f] = cWindow[f];        
    }    

    if (!data.name) {
        throw new Exception('ERROR_WINDOW_UNKNOWN_NAME');
    }
    
    this.name = data.name;
    this._class = data._class;
    this.dom_window = _(this.name);

    window.windows[this.name] = this;
    this.is_visible = data['visible'];
}


