Class.create("JWindow", {
    initialize: function()
    {
        this.element = null;
        this.title = null;
	    this.pageBody = null;
	    this.isVisible = false;
	    this.mask = null;
	    this.container = null;
	    this.titlebar = null;
	    this.dataContainer = null;
	    this.controlButtons = null;
	    this.saveButton = null;
	    this.closeButton = null;
	    this.offset = null;
	    this.hide = this.onHide.bindAsEventListener(this);
	    //this.show = this.onShow.bindAsEventListener(this);
	    this.onChange = this.onWindowChange.bindAsEventListener(this);
	    Event.observe(window, "load", this.initWindow.bindAsEventListener(this));
    },
    initWindow: function(event)
    {
        this.element = $(document);
        this.pageBody = $(document.body);
	    this.createElements();
	    this.attachEvents();

	    Element.absolutize(this.mask);
        this.offset = Element.cumulativeOffset(this.element);
        this.dimensions = this.getWindowDimensions();
	    Event.observe(window, "scroll", this.onChange);
	    Event.observe(window, "resize", this.onChange);
        this.hide();
    },
    getWindowDimensions: function()
    {
		var _width = 0, _height = 0;
		if(typeof(window.innerWidth) == "number")
		{
			//Non-IE
			_width = window.innerWidth;
			_height = window.innerHeight;
		}
		else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
			//IE 6+ in 'standards compliant mode'
			_width = document.documentElement.clientWidth;
			_height = document.documentElement.clientHeight;
		}
		else if(document.body && (document.body.clientWidth || document.body.clientHeight))
		{
			//IE 4 compatible
			_width = document.body.clientWidth;
			_height = document.body.clientHeight;
		}
		// screen.availWidth and screen.availHeight work cross browser
		// but are not the correct viewport width/height
		var obj = new Object();
		obj.width = _width;//screen.availWidth;
		obj.height = _height;//screen.availHeight;
		
		return obj;
    },
    onWindowChange: function(event)
    {
        if (this.isVisible)
        {
            this.centerWindow();
            this.setMaskSize();
        }
    },
    show: function(title, url, width, height)
    {
		var ref = this;
		/*
        if (width != null && width != undefined)
        {
		    this.dataContainer.setStyle({
		        "width": width + "px"
		    });
        }
        else
        {
		    Event.observe(this.dataContainer, "load", function()
		    {
		        ref.dataContainer.setStyle({
		            "width": $(ref.dataContainer).document.body.getWidth() + "px"
		        });
		        ref.centerWindow();
		    });
        }
        if (height != null && height != undefined)
        {
		    this.dataContainer.setStyle({
		        "height": height + "px"
		    });
        }
        else
        {
		    Event.observe(this.dataContainer, "load", function()
		    {
		        ref.dataContainer.setStyle({
		            "height": $(ref.dataContainer).document.body.getHeight() + "px"
		        });
		        ref.centerWindow();
		    });
        }
        */
		this.dataContainer.src = url;
		this.dataContainer.setStyle({
		    "width": width + "px",
		    "height": height + "px"
		});
		this.centerWindow();
		this.setMaskSize();
		this.mask.show();
		this.container.show();
		this.isVisible = true;
		
		return false;
    },
    onHide: function()
    {
        this.mask.hide();
        this.container.hide();
        this.dataContainer.src = "";
        this.isVisible = false;
	    
	    return false;
    },
    setTitle: function(title)
    {
        this.title = title;
        this.titlebar.update(title);
    },
    centerWindow: function()
    {
        var offset = document.viewport.getScrollOffsets();
		var dimensions = Position.getWindowSize();//this.getWindowDimensions();
        //var top = (document.viewport.getHeight()/2) - (this.container.getHeight()/2) + offset.top;
        //var left = (document.viewport.getWidth()/2) - (this.container.getWidth()/2) + offset.left;
        var top = (dimensions.height/2) - (this.container.getHeight()/2) + offset.top;
        var left = (dimensions.width/2) - (this.container.getWidth()/2) + offset.left;
        this.container.setStyle({
            "top": top + "px",
            "left": left + "px"
        });
        
        return false;
    },
    setMaskSize: function()
    {
        //this.dimensions = new Object();
        //this.dimensions.width = document.viewport.getWidth();//window.innerWidth;//this.element.clientWidth;
        //this.dimensions.height = document.viewport.getHeight();//window.innerHeight;//this.element.clientHeight;
        var offset = document.viewport.getScrollOffsets();
		var dimensions = Position.getWindowSize();//this.getWindowDimensions();
		var scrollbarSize = 16;
		if (Prototype.Browser.IE)
		{
		    scrollbarSize = 0;
		}
	    this.mask.setStyle({
	        "top": offset.top + "px",
	        "left": offset.left + "px",
	        "height": (dimensions.height) + "px",
	        "width": (dimensions.width - scrollbarSize) + "px"
	    });
	    
	    return false;
    },
    attachEvents: function()
    {
        var ref = this;
        Event.observe(this.closeButton, "click", this.hide);
    },
    createElements: function()
    {
	    this.mask = new Element("div", {"class":"JWindowMask"});
	    this.container = new Element("div", {"class":"JWindow"});
	    this.closeButton = new Element("div", {"class":"closeButton","title":"Close Window"});
	    this.container.appendChild(this.closeButton);
        //this.titlebar = new Element("div", {"class":"JWindowTitlebar"});
        //this.container.appendChild(this.titlebar);
	    this.dataContainer = new Element("iframe", {"class":"data"});
	    this.container.appendChild(this.dataContainer);
	    this.pageBody.appendChild(this.mask);
	    this.pageBody.appendChild(this.container);
	    Element.absolutize(this.closeButton);
	    Element.relativize(this.closeButton);
    }
});
var JWindow = new JWindow();