var LoginWindow = Class.create({
  initialize: function(url) {
    this.url = url || '/session/new';
    this.show();
  },
  
  show: function() {
    if (this.dialog == null || !this.dialog.visible) {
      this.dialog = new UI.Window({
        theme:  "alphacube",
        shadowTheme: "mac_shadow",
        shadow: true,
        resizable: false,
        width:  730,
        height: 380
      }).
      setContent('<div class="inner-popup-wrapper f-options"><div class="f-options-inner"><div id="login-or-sign-up" class="popup-window"><img src="/images/ajax-loader.gif" border="0" /></div></div></div>');
    }
    keepDialogSizedRight(this.dialog, '.login-wrapper', function (element, window) {
	  $(element).style.height = window.getSize().height-23+"px"
	  $(element).style.width = window.getSize().width-18+"px"
      }) 
    this.load_content(this.url);
    this.dialog.show(true).center().activate();
  },
  
  hide: function() {
    this.dialog.close();
  },
  
  load_content: function(url) {
    new Ajax.Request(url, {
      method: 'get',
      onSuccess: function(transport) {
	  	//update_tracking('')
        $('login-or-sign-up').empty();
        $('login-or-sign-up').update(transport.responseText);
        
        if ($('skip-signup-button')) {
          $('skip-signup-button').observe('click', this.dialog.close);
        }
        
        if ($('restore-password-link')) {
          $('restore-password-link').observe('click', this.load_forgot_password.bindAsEventListener(this));
		  $('restore-password-link').href = "#" // this prevents following the link from changing pages if in ajax mode

        }
		
		if ($('request_login') && $('user_session_login')) {
			$('user_session_login').value = $('request_login').value
		}
		
		if ($('email') && typeof(previous_attempted_login)!="undefined") {
			$('email').value = previous_attempted_login
		}
        
      }.bind(this)
    });
  },
  
  load_forgot_password: function() {
  	previous_attempted_login = $('user_session_login').value
    this.load_content("/password_resets/new");
    return false;
  }
  
});

LoginWindow.show = function(url) {
  LoginWindow.instance = LoginWindow.instance || new LoginWindow(url);
  LoginWindow.instance.show();
};

LoginWindow.destroy = function() {
  if (LoginWindow.instance) {
    LoginWindow.instance.hide();
  }
};