Application.Window = Class.create({
  initialize: function(container, options){
    this.detectSource(container);
    this.constructModalWindow(options);
    this.setupCloseButton();
    this.insertContent(container);
    this.setupWindowDelegates();
  },

  constructModalWindow: function(options){
    this.win = new Control.Modal(this.source, Object.extend({
      className: 'modal',  
      closeOnClick: 'overlay',
      fade: true,
      fadeDuration: 0.75,
      position: 'center',
      insertRemoteContentAt: (this.isIframe ? false : '.content'),
      afterClose: function(e){ Control.Modal.current.container.remove(); },
      onRemoteContentLoaded: function() { this.hideSpinner(); }.bind(this)
    }, options || {}));
  },
  
  detectSource: function(container){
    if(typeof container == "string" ) {
      this.source = container;
      this.isIframe = true;
    } else {
      this.source = $('#modal');
      this.isIframe = false;
    }
  },
  
  insertContent: function(container){
    if(this.isIframe) {
      var window_contents = new Element('div', { className: 'content'});
      window_contents.insert(container.innerHTML);
      this.win.container.insert(window_contents);      
    }
  },
  
  setupCloseButton: function(){
    var window_close = new Element('div', {className: 'close'});
    window_close.observe("click", function(){ this.win.close() }.bind(this));
    this.win.container.insert(window_close);
  },
  
  setupWindowDelegates: function(){
    // delegate these methods to the real window object
    $A(['open', 'close', 'position']).each(function(name){
      this[name] = function(){ this.win[name]() }
    }.bind(this));
  },
  
  content: function(){
    if(this.isIframe) {
      return this.win.container;
    } else {
      return this.win.container.down(".content");
    }
  },
  
  showSpinner: function(){
    this.content().insert('<div class="ajax_loading spinner large center"></div>');
  },
  
  hideSpinner: function(){
    var e = this.content().down('.ajax_loading');
    if(e){ e.remove(); }
  }
});

Object.extend(Application.Window, {
  makeModal: function(container, options){
    return new Application.Window(container, options);
  },
  
  makeModalFromRemote: function(href, options){
    var win = Application.Window.makeModal("", options);
    win.showSpinner();
    win.open();
    new Ajax.Request(href, {
      requestHeaders: { Accept: 'application/json' },
      method: 'GET',
      onSuccess: function(response){
        var content = response.responseJSON ? response.responseJSON.content : response.responseText;
        win.content().update(content);
        win.hideSpinner();
        win.position();
        if(options.onComplete){ options.onComplete(win); }
      }.bind(this)
    })
    return win;
  },

  loadPageInOverlay: function(href, styleOptions) {
    var win = new Application.Window(href, {iframe: true});
    win.showSpinner();  
    win.open();
    var iframe_container = win.content();
    if(typeof styleOptions == 'undefined') {
      iframe_container.setStyle({
        width: '640px',
        height:'480px'
      });
    } else {
      iframe_container.setStyle(styleOptions);
    }
    win.position();

    return win;
  }
});
