Window = Class.create();

Window.TOP_LEFT = 0;
Window.TOP_CENTER = 1;
Window.TOP_RIGHT = 2;
Window.MIDDLE_LEFT = 3;
Window.CENTER = 4;
Window.MIDDLE_RIGHT = 5;
Window.BOTTOM_LEFT = 6;
Window.BOTTOM_CENTER = 7;
Window.BOTTOM_RIGHT = 8;

Window.prototype = {
    wins: {},
    options: "",

    initialize: function(options) {
        if(options["width"] != null || options["height"] != null) {
            var width = options["width"];
            var height = options["height"];

            var position = (options["position"] != null) ? parseInt(options["position"]) : Window.CENTER;
            switch(position) {
                case Window.TOP_LEFT: options["top"] = 0;
                                      options["left"] = 0;
                    break;
                case Window.TOP_CENTER: options["top"] = 0;
                                        options["left"] = (screen.width - width) / 2;
                    break;
                case Window.TOP_RIGHT: options["top"] = 0;
                                       options["left"] = (screen.width - width);
                    break;
                case Window.MIDDLE_LEFT: options["top"] = (screen.height - height) / 2;
                                         options["left"] = 0;
                    break;
                case Window.CENTER: options["top"] = (screen.height - height) / 2;
                                    options["left"] = (screen.width - width) / 2;
                    break;
                case Window.MIDDLE_RIGHT: options["top"] = (screen.height - height) / 2;
                                          options["left"] = (screen.width - width);
                    break;
                case Window.BOTTOM_LEFT: options["top"] = (screen.height - height);
                                         options["left"] = 0;
                    break;
                case Window.BOTTOM_CENTER: options["top"] = (screen.height - height);
                                           options["left"] = (screen.width - width) / 2;
                    break;
                case Window.BOTTOM_RIGHT: options["top"] = (screen.height - height);
                                          options["left"] = (screen.width - width);
                    break;
            }

            // adjust for object bar
            if(options["top"] >= 34) { options["top"] -= 34; }
        }

        for(var option in options) {
            var optionValue = options[option];
            switch(optionValue) {
                case true:  optionValue = 1; break;
                case false: optionValue = 0; break;
                default: /* optionValue = optionValue */ ;
            }
            if(this.options == "") { this.options += option + "=" + optionValue; }
            else { this.options += "," + option + "=" + optionValue; }
        }
    },

    open: function(url, params) {
        var queryString = "";
        for(var param in params) {
            if(queryString == "") { queryString += param + "=" + params[param]; }
            else { queryString += "&" + param + "=" + params[param]; }
        }

        var request = url + "?" + queryString;

        try {
            if(this.wins[request] == null || this.wins[request].location == null) {
                this.wins[request] = window.open(request, "", this.options);
            }
            this.wins[request].focus();
        } catch(e) {
            this.wins[request] = window.open(request, "", this.options);
            this.wins[request].focus();
        }
    }
}