Index: /branches/release-38/default_templates/signin.mtml
===================================================================
--- /branches/release-38/default_templates/signin.mtml (revision 2052)
+++ /branches/release-38/default_templates/signin.mtml (revision 2363)
@@ -1,65 +1,49 @@
-<script type="text/javascript" src="<$mt:Link template="<__trans phrase="JavaScript">"$>"></script>
+<mt:IfRegistrationAllowed>
 <div class="widget-sign-in widget">
     <h3 class="widget-header"><__trans phrase="Sign In"></h3>
-    <div class="widget-content">
+    <div class="widget-content" id="signin-widget-content"></div>
+</div>
 <script type="text/javascript">
 /* <![CDATA[ */
-var name, id, url, blog_ids;
-if (typeof(commenter_name) != 'undefined')
-    name = commenter_name
-if (typeof(commenter_id) != 'undefined')
-    id = commenter_id;
-if (typeof(commenter_url) != 'undefined')
-    url = commenter_url;
-if (typeof(commenter_blog_ids) != 'undefined')
-    blog_ids = commenter_blog_ids;
-
-if (!name && !id) {
-    if ('<$mt:CGIHost exclude_port="1"$>' != '<$mt:BlogHost exclude_port="1"$>') {
-        document.write('<scr' + 'ipt src="<$mt:CGIPath$><$mt:CommentScript$>?__mode=cmtr_name_js">');
-        document.write("</scr" + "ipt>");
+function mtUpdateSignInWidget(u) {
+    var el = document.getElementById('signin-widget-content');
+    if (!el) return;
+    if (u) {
+        if (u && u.is_authenticated) {
+            user = u;
+            mtSaveUser();
+        } else {
+            // user really isn't logged in; so let's do this!
+            return mtSignIn();
+        }
     } else {
-        name = getCookie('commenter_name');
-        ids = getCookie('commenter_id').split(':');
-        id = ids[0];
-        blog_ids = ids[1];
-        url = getCookie('commenter_url');
+        u = mtGetUser();
     }
-}
-showMessage(name, id, url);
-
-function showMessage(commenter_name, commenter_id, commenter_url) {
-    static = location.href;
-    if ( commenter_name &&
-         ( !commenter_id 
-        || commenter_blog_ids.indexOf("'<$mt:BlogID$>'") > -1))
-    {
+    if (u) {
         var url;
-        if (commenter_id) {
-            url = '<$mt:CGIPath$><$mt:CommentScript$>?__mode=edit_profile&commenter=' + commenter_id + '&blog_id=<$mt:BlogID$>';
-            url += '&static=' + static;
-        } else if (commenter_url) {
-            url = commenter_url;
+        if (u.is_authenticated) {
+            url = '<$mt:CGIPath$><$mt:CommentScript$>?__mode=edit_profile&blog_id=<$mt:BlogID$>';
+            url += '&static=' + escape( location.href );
+        } else if (u.url) {
+            url = u.url;
         } else {
             url = null;
         }
         var content = '<__trans phrase="You are signed in as " escape="js">';
-        if (url) {
-            content += '<a href="' + url + '">' + commenter_name + '</a>';
-        } else {
-            content += commenter_name;
-        }
-        content += '.  (<a href="<$mt:RemoteSignOutLink no_static="1"$>&static=' + static + '"><__trans phrase="sign out" escape="js"></a>)';
-        document.write(content);
-    } else if (commenter_name) {
-        document.write('<__trans phrase="You do not have permission to sign in to this blog." escape="js"> (<a href="<$mt:RemoteSignOutLink no_static="1"$>&static=' + static + '"><__trans phrase="sign out" escape="js"></a>)');
+        if (url)
+            content += '<a href="' + url + '">' + u.name + '</a>';
+        else
+            content += u.name;
+        content += '.  (<a href="javascript:void(0)" onclick="return mtSignOutOnClick()"><__trans phrase="sign out" escape="js"></a>)';
+    } else if (u && u.is_banned) {
+        content = '<__trans phrase="You do not have permission to sign in to this blog." escape="js">';
     } else {
-    <mt:IfRegistrationAllowed>
-        document.write('<a href="<$mt:CGIPath$><$mt:CommentScript$>?__mode=login&blog_id=<$mt:BlogID$>&static=' + static + '"><__trans phrase="Sign In" escape="js"></a>');
-    </mt:IfRegistrationAllowed>
+        content = '<a href="javascript:void(0)" onclick="return mtSignInOnClick(\'signin-widget-content\')"><__trans phrase="Sign In" escape="js"></a>';
     }
+    el.innerHTML = content;
 }
+mtAttachEvent('usersignin', mtUpdateSignInWidget);
+mtUpdateSignInWidget();
 /* ]]> */
 </script>
-    </div>
-</div>
+</mt:IfRegistrationAllowed>
Index: /branches/release-38/default_templates/javascript.mtml
===================================================================
--- /branches/release-38/default_templates/javascript.mtml (revision 2341)
+++ /branches/release-38/default_templates/javascript.mtml (revision 2363)
@@ -39,4 +39,33 @@
     var el = (typeof id == "string") ? document.getElementById(id) : id;
     if (el) el.style.display = 'block';
+}
+
+<mt:Ignore>
+/***
+ * A utility function for assigning/adding handlers to window events.
+ */
+</mt:Ignore>
+function mtAttachEvent(eventName,func) {
+    var onEventName = 'on' + eventName;
+    var old = window[onEventName];
+    if( typeof old != 'function' )
+        window[onEventName] = func;
+    else {
+        window[onEventName] = function( evt ) {
+            old( evt );
+            return func( evt );
+        };
+    }
+}
+
+<mt:Ignore>
+/***
+ * Calls the event named, if there are handlers for it.
+ */
+</mt:Ignore>
+function mtFireEvent(eventName,param) {
+    var fn = window['on' + eventName];
+    if (typeof fn == 'function') return fn(param);
+    return;
 }
 
@@ -150,5 +179,5 @@
         mtSaveUser();
         // sync up user greeting
-        mtUserOnLoad();
+        mtFireEvent('usersignin');
     }
 }
@@ -318,44 +347,43 @@
     // if the user is authenticated, hide the 'anonymous' fields
     // and any captcha input if already shown
-    if ( u && u.is_authenticated ) {
-        mtShow('comments-form');
-        mtHide('comments-open-data');
-        if (mtCaptchaVisible)
-            mtHide('comments-open-captcha');
-    } else {
+    if ( document.getElementById('comments-form')) {
+        if ( u && u.is_authenticated ) {
+            mtShow('comments-form');
+            mtHide('comments-open-data');
+            if (mtCaptchaVisible)
+                mtHide('comments-open-captcha');
+        } else {
 <mt:IfRegistrationRequired>
-        mtHide('comments-form');
+            mtHide('comments-form');
 </mt:IfRegistrationRequired>
-    }
-    if ( u && u.is_banned ) {
-        mtHide('comments-form');
-    }
-
-    // if we're previewing a comment, make sure the captcha
-    // field is visible
-    if (is_preview) {
-        mtShowCaptcha();
-    } else {
-        // display greeting appropriate to the current user
-        mtShowGreeting();
-    }
-
-    // populate anonymous comment fields if user is cookied as anonymous
-    var cf = document['comments_form'];
-    if (cf) {
-        if (u && u.is_anonymous) {
-            if (u.email) cf.email.value = u.email;
-            if (u.name) cf.author.value = u.name;
-            if (u.url) cf.url.value = u.url;
-            if (cf.bakecookie)
-                cf.bakecookie.checked = u.name || u.email;
         }
-        if (cf.post.disabled) {
-            cf.post.disabled = false;
-            cf.post.value = '<__trans phrase="Submit">';
-        }
-        if (cf.preview_button.disabled) {
-            cf.preview_button.disabled = false;
-            cf.preview_button.value = '<__trans phrase="Preview">';
+        if ( u && u.is_banned )
+            mtHide('comments-form');
+
+        // if we're previewing a comment, make sure the captcha
+        // field is visible
+        if (is_preview)
+            mtShowCaptcha();
+        else
+            mtShowGreeting();
+
+        // populate anonymous comment fields if user is cookied as anonymous
+        var cf = document['comments_form'];
+        if (cf) {
+            if (u && u.is_anonymous) {
+                if (u.email) cf.email.value = u.email;
+                if (u.name) cf.author.value = u.name;
+                if (u.url) cf.url.value = u.url;
+                if (cf.bakecookie)
+                    cf.bakecookie.checked = u.name || u.email;
+            }
+            if (cf.post.disabled) {
+                cf.post.disabled = false;
+                cf.post.value = '<__trans phrase="Submit">';
+            }
+            if (cf.preview_button.disabled) {
+                cf.preview_button.disabled = false;
+                cf.preview_button.value = '<__trans phrase="Preview">';
+            }
         }
     }
@@ -370,14 +398,10 @@
 </mt:Ignore>
 function mtEntryOnLoad() {
-<mt:Unless tag="IfPingsAccepted">
-    mtHide('trackbacks-info');
-</mt:Unless>
-
-<mt:Unless tag="IfCommentsAccepted">
-    mtHide('comments-open');
-</mt:Unless>
-
-    mtUserOnLoad();
-}
+    <mt:Unless tag="IfPingsAccepted">mtHide('trackbacks-info');</mt:Unless>
+    <mt:Unless tag="IfCommentsAccepted">mtHide('comments-open');</mt:Unless>
+    mtFireEvent('usersignin');
+}
+
+mtAttachEvent('usersignin', mtUserOnLoad);
 
 <mt:Ignore>
@@ -396,9 +420,12 @@
 }
 
-function mtSignInOnClick() {
-    // display throbber
-    var el = document.getElementById('comment-greeting');
-    if (!el)  // legacy MT 4.x element id
-        el = document.getElementById('comment-form-external-auth');
+function mtSignInOnClick(sign_in_element) {
+    var el;
+    if (sign_in_element) {
+        // display throbber
+        el = document.getElementById(sign_in_element);
+        if (!el)  // legacy MT 4.x element id
+            el = document.getElementById('comment-form-external-auth');
+    }
     if (el)
         el.innerHTML = '<__trans phrase="Signing in..."> <img src="<$mt:StaticWebPath$>images/indicator.white.gif" height="16" width="16" alt="" />';
@@ -420,12 +447,23 @@
 <mt:Ignore>
 /***
- * Handles the action of the "Sign out" link. First clears any existing
- * user cookie, then direts to the MT comment script to sign the user out.
+ * Handles sign out from the web site.
+ * First clears any existing user cookie, then direts to the MT comment
+ * script to sign the user out.
  */
 </mt:Ignore>
 function mtSignOut(entry_id) {
-    var url = '<$mt:SignOutLink$>&entry_id=' + entry_id;
     mtClearUser();
+    var url = '<$mt:SignOutLink$>&return_to=' + encodeURIComponent(location.href);
     location.href = url;
+}
+
+<mt:Ignore>
+/***
+ * Handles the action of the "Sign out" link.
+ */
+</mt:Ignore>
+function mtSignOutOnClick() {
+    mtSignOut();
+    return false;
 }
 
@@ -457,5 +495,5 @@
     if ( u && u.is_authenticated ) {
         if ( u.is_banned ) {
-            phrase = '<__trans phrase="You do not have permission to comment on this blog. ([_1]sign out[_2])" params="<a href="javascript:void(0);" onclick="mtSignOut(' + entry_id + ');return false;">%%</a>">';
+            phrase = '<__trans phrase="You do not have permission to comment on this blog. ([_1]sign out[_2])" params="<a href="javascript:void(0);" onclick="return mtSignOutOnClick();">%%</a>">';
         } else {
             var user_link;
@@ -473,11 +511,11 @@
             }
             // TBD: supplement phrase with userpic if one is available.
-            phrase = '<__trans phrase="Thanks for signing in, [_1]. ([_2]sign out[_3])" params="' + user_link + '%%<a href="javascript:void(0)" onclick="mtSignOut(' + entry_id + ');return false;">%%</a>">';
+            phrase = '<__trans phrase="Thanks for signing in, [_1]. ([_2]sign out[_3])" params="' + user_link + '%%<a href="javascript:void(0)" onclick="return mtSignOutOnClick();">%%</a>">';
         }
     } else {
         if (reg_reqd) {
-            phrase = '<__trans phrase="[_1]Sign in[_2] to comment." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick()">%%</a>">';
+            phrase = '<__trans phrase="[_1]Sign in[_2] to comment." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick(\'comment-greeting\')">%%</a>">';
         } else {
-            phrase = '<__trans phrase="[_1]Sign in[_2] to comment, or comment anonymously." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick()">%%</a>">';
+            phrase = '<__trans phrase="[_1]Sign in[_2] to comment, or comment anonymously." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick(\'comment-greeting\')">%%</a>">';
         }
     }
@@ -587,4 +625,6 @@
 </mt:Ignore>
 function mtSetCookie(name, value, expires, path, domain, secure) {
+    if (domain && domain.match(/^\.?localhost$/))
+        domain = null;
     var curCookie = name + "=" + escape(value) +
         (expires ? "; expires=" + expires.toGMTString() : "") +
@@ -618,5 +658,7 @@
 </mt:Ignore>
 function mtDeleteCookie(name, path, domain, secure) {
-    if (mtGetCookie(name))
+    if (mtGetCookie(name)) {
+        if (domain && domain.match(/^\.?localhost$/))
+            domain = null;
         document.cookie = name + "=" +
             (path ? "; path=" + path : "") +
@@ -624,4 +666,5 @@
             (secure ? "; secure" : "") +
             "; expires=Thu, 01-Jan-70 00:00:01 GMT";
+    }
 }
 
