Class.create("JWindow", {
    __defaultOptions: {},
    initialize: function()
    {
      this.__element = null;
      this.__options = null;
      this.__title = null;
	    this.__pageBody = null;
	    this.__isVisible = false;
	    this.__mask = null;
	    this.__container = null;
	    this.__titlebar = null;
	    this.__dataContainer = null;
      this.__dataIframe = null;
	    this.__controlButtons = null;
	    this.__saveButton = null;
	    this.__closeButton = null;
      this.__dimensions = null;
	    this.__offset = null;
	    this.__onChange = this.__onWindowChange.bindAsEventListener(this);
	    this.hide = this.__onHide.bindAsEventListener(this);
      document.observe("dom:loaded", this.__init.bindAsEventListener(this));
    },
    __initWindow: function()
    {
      this.__dimensions = this.__getWindowDimensions();
      this.__setMaskSize();
      this.__mask.show();
      this.__centerWindow();
      this.__container.show();
    },
    __centerWindow: function()
    {
        var offset = document.viewport.getScrollOffsets();
        var top = (this.__dimensions.height/2) - (this.__container.getHeight()/2) + offset.top;
        top = (top > 0) ? top : 10;
        var left = (this.__dimensions.width/2) - (this.__container.getWidth()/2) + offset.left;
        left = (left > 0) ? left : 10;
        
        this.__container.setStyle({
            "top": top + "px",
            "left": left + "px",
	        "zIndex": 10001
        });
        
        return false;
    },
    __setMaskSize: function()
    {
	    this.__mask.setStyle({
	        "position": "fixed",
	        "top": "0",
	        "left": "0",
	        "height": this.__dimensions.height + "px",
	        "width": this.__dimensions.width + "px",
	        "zIndex": 10000
	    });
	    
	    return false;
    },
    __init: function(event)
    {
      this.__element = $(document.body);
	    this.__createElements();
	    this.__container.hide();
      Event.stopObserving(this.__closeButton);
      Event.observe(this.__closeButton, "click", this.cancel.bindAsEventListener(this));
	    Element.absolutize(this.__mask);
	    Event.observe(this.__dataContainer, "change", this.__onChange);
	    //Event.observe(window, "scroll", this.__onChange);
	    //Event.observe(window, "resize", this.__onChange);
    },
    __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.__initWindow();
      }
    },
    __onHide: function()
    {
      this.__dataIframe.src = "";
      this.__mask.hide();
      this.__container.hide();
      this.__isVisible = false;
	    
	    return false;
    },
    show: function(title, url, width, height)
    {
      var options = {
        innerHeight: height,
        innerWidth: width
      };
      this.__options = Object.extend(Object.extend({ }, this.__defaultOptions), options || { });
      //this.__dataContainer.morph('width:' + this.__options.innerWidth + 'px; height:' + this.__options.innerHeight + 'px;');
      var ref = this;
      this.__dataContainer.setStyle({
          'width': ref.__options.innerWidth + 'px',
          'height': ref.__options.innerHeight + 'px'
      });
      this.__dataIframe.src = url;
      this.__dataIframe.show();

      this.__isVisible = true;
      this.__onChange.defer(0);

      return false;
    },
    setSize: function(width, height)
    {
      var options = { innerHeight: height, innerWidth: width };
      this.__options = Object.extend(Object.extend({ }, this.__defaultOptions), options || { });

      var offset = document.viewport.getScrollOffsets();
      var top = (this.__dimensions.height/2) - (height/2);// + offset.top;
      top = (top > 0) ? top : 10;
      var left = (this.__dimensions.width/2) - (width/2);// + offset.left;
      left = (left > 0) ? left : 10;

      this.__container.setStyle({
          "top": top + "px",
          "left": left + "px"
      });
      
      this.__dataContainer.morph({width: this.__options.innerWidth + 'px', height: this.__options.innerHeight + 'px'}, {duration: 0.1});
    },
    cancel: function(event)
    {
      this.hide();
    },
    __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":"JWindowContainer"});
	    this.__closeButton = new Element("div", {"class":"closeButton","title":"Close Window"});
	    this.__container.appendChild(this.__closeButton);

      this.__dataContainer = new Element("div", {"class":"JWindowDataContainer"});
      this.__dataIframe = new Element("iframe", {"class": "JWindowIframe", "name": "JWindowIframe", "style": "width:100%;height:100%;display:none;border:none;"});
      this.__dataContainer.appendChild(this.__dataIframe);
      this.__container.appendChild(this.__dataContainer);

	    //this.__dataContainer = new Element("iframe", {"class":"data"});
	    //this.__container.appendChild(this.__dataContainer);
	    //this.__pageBody.appendChild(this.__mask);
	    //this.__pageBody.appendChild(this.__container);
      this.__element.appendChild(this.__mask);
      this.__element.appendChild(this.__container);
	    Element.absolutize(this.__closeButton);
	    Element.relativize(this.__closeButton);
    }
});
var JWindow = new JWindow();