function getDocHeight(doc) {
    return Math.max(
        Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight),
        Math.max(doc.body.offsetHeight, doc.documentElement.offsetHeight),
        Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
    );
}

var LoginWindow = Class.create({
  initialize: function(options) {
    this.options = options || {};
    this.displayed = false;
    this.show = false;
  },

  check: function() {
    if (this.auth || LoginWindow.logged_in) return this.callOnLogin();
    if (this.displayed) return this.callOnFailure();
    this.show = false;
    this.createIframe();
  },

  login: function() {
    this.show = true;
    this.createOverlay();
    this.createIframe();
  },

  hide: function() {
    if (this.interval) clearInterval(this.interval);
    this.destroyIframe();
    if(this.show)
      this.destroyOverlay();
    this.displayed = false;
  },

  createOverlay: function() {
    var documentDimensions = document.viewport.getDimensions();
    var documentOffsets = document.viewport.getScrollOffsets();

    this.overlay = new Element('div');
    this.overlay.setOpacity(0.65);
    this.overlay.setStyle({
      position: 'absolute',
      top: documentOffsets.top + 'px',
      left: '0px',
      width: '100%',
      height: documentDimensions.height + 'px',
      backgroundColor: '#000000',
      display: 'none',
      zIndex: '999'
    });
    document.body.appendChild(this.overlay);
    Effect.Appear(this.overlay, {duration: 1.5, to: 0.7});

    this.loader = new Element('img', {src: 'http://www.digitalnz.org/images/login/ajax-loader.gif'});
    this.loader.setStyle({
      position: 'absolute',
      top: '0px',
      left: ((documentDimensions.width / 2) - (32 / 2)) + 'px'
    });
    this.overlay.appendChild(this.loader);
  },

  destroyOverlay: function() {
    Effect.Fade(this.overlay, {duration: 0.25});
    setTimeout(this.overlay.remove.bind(this.overlay), 500);
  },

  destroyIframe: function() {
    Effect.Fade(this.iframe, {duration: 0.25});
    setTimeout(this.iframe.remove.bind(this.iframe), 500);
  },

  createIframe: function() {
    var documentDimensions = document.viewport.getDimensions();
    var documentOffsets = document.viewport.getScrollOffsets();

    this.iframe = new Element('iframe', {
      src: this.url(),
      border: '0',
      frameborder: '0',
      vspace: '0',
      hspace: '0',
      marginwidth: '0',
      marginheight: '0',
      scrolling: 'no',
      noresize: 'noresize',
      ALLOWTRANSPARENCY: "true"
    });
    this.iframe.observe('load', this.iframeLoadHandler.bindAsEventListener(this));
    this.iframe.setStyle({
      position: 'absolute',
      width: '486px',
      height: '600px',
      border: '0px solid black',
      top: documentOffsets.top + 'px',
      left: ((documentDimensions.width / 2) - (486 / 2)) + 'px',
      display: 'none',
      zIndex: '1000'
    });
    document.body.appendChild(this.iframe);
  },

  iframeLoadHandler: function(event) {
    this.iframeWindow = this.iframe.contentWindow;

    setTimeout(function() {
      var height = $(this.iframeWindow.document.body).getHeight();
      if(height > this.iframe.getHeight())
        this.iframe.setStyle({'height': height + 'px'});
    }.bind(this), 50);

    if (!this.displayed) {
      this.displayed = true;

      if (this.show) {
        if(this.loader)
          this.loader.remove();

        Effect.Appear(this.iframe, {duration: 0.25});
      }

      this.interval = setInterval(this.poll.bind(this), 250);
      this.poll();
    }
  },

  url: function() {
    return LoginWindow.window_url + '?check=true&' + (Math.floor(Math.random() * 1000000));
  },

  getMessage: function() {
    if(this.iframeWindow.location.href)
      return this.iframeWindow.location.href.split('#').last();
    else
      return '';
  },

  poll: function() {
    var message = this.getMessage();

    switch (message) {
      case 'close':
        return this.hide();
      case 'failure':
        if (this.options['onFailure'])
          this.options['onFailure']();
        return;
      case 'not':
        if(!this.show) {
          this.callOnFailure();
          this.hide();
          return;
        }
    }

    var authRegex = /auth-(.+)/;
    var matches = authRegex.exec(message);

    if (matches) {
      this.auth = parseInt(matches[1]);
      this.callOnLogin();
      this.hide();
    }
  },

  callOnLogin: function() {
    if (this.options['onLogin'])
      this.options['onLogin']();
  },

  callOnFailure: function() {
    if (this.options['onFailure'])
      this.options['onFailure']();
  }
});

LoginWindow.show = function(options) {
  var window = new LoginWindow(options);
  window.login();
  return window;
};

LoginWindow.check = function(options) {
  var window = new LoginWindow(options)
  window.check();
  return window;
};

LoginWindow.window_url = 'http://www.digitalnz.org/login.html';

