$(document).ready(function() {
  var change_buttons_on_submit = function() 
    {
      /// Check if dialog window's html in current context contains any errorlist style classes.
      if ( !$('.errorlist', this).text() && !$('.form-message', this).text() ) 
      {
        /// No error(s) found. Change buttons
        $(this).dialog('option', 'buttons', { 'Close': function() { $(this).dialog('close'); window.location.reload();} });
      }
    };

  $(".go-through-login").click(function() { 
    $("#id_login_dialog").load( $(this).attr('rel'), function() { // load remote content from its 'rel' attribute into hidden div
          $(this)
            .dialog("destroy") // destroy dialog widget (if exists)
            .dialog({ // and recreate it with following options
                title: "Log in",
                modal: true,
                height: 500,
                width: 700,
                bgiframe: true,
                buttons: {
                    'Log in': function() {
                        f = $(this).find("form");
                        f.submit(); },
                    'Cancel': function() {
                        $(this).dialog('close'); }
                },
                close: function() { },
                open: function() { $(this).hijack(change_buttons_on_submit)} /// This is where hijack() magic happens.
            });
        });
      });
});