/***
 * Functions to favorite/unfavorite feature
 */

$(document).ready(function() {
    // Click Function
    $('.favtog').click(function() {
        // Get ID
        var id = $(this).attr('id').replace("favtog", "");
        // If element has class "scored" then we are unfavoriting, submit score of 0, otherwise submit score of 1
        if ($(this).hasClass('scored')) {
            mtMarkScore(id, 0, 'scored', 'scored');
        } else {
            mtMarkScore(id, 1, 'scored', 'scored');
        }
    });
    mtAttachEvent('load', mtUpdateScores);
    mtAttachEvent('usersignin', mtUpdateScores);
});


/***
 * mtMarkScore - handles action of scoring
 */
function mtMarkScore(obj_id, score, pending, complete) {
    // Define object to score
    var el = $('#favtog' + obj_id);
    // Die if object doesn't exist
    if( !el ) return false;
    // update class
    if (score) {
        $(el).addClass(pending);
    } else {
        $(el).removeClass(pending);
    };
    // Send ajax request, and call success function
    $.ajax({
      type: "POST",
      url: mt.blog.community.script,
      data: '__mode=vote&ow=1&f=sum&jsonp=mtMarkScore_cb&id=' + obj_id + '&score=' + score,
      success: function(r){
        var sum = eval( r );
        mtMarkScoreResponse(obj_id, score, sum, pending, complete);
      }
    });
}

/***
 * mtMarkScore_cb - handles response from ajax request after scoring action
 */
function mtMarkScore_cb(s_hash) {
    for (var id in s_hash) {
        // mark/unmark is always a single entry operation
        return s_hash[id].sum;
    }
}

/***
 * mtMarkScoreResponse - updates scores based on the response from ajax request
 */
function  mtMarkScoreResponse(obj_id, score, count, pending, complete) {
    // Define object to update
    var el = $('#favtog' + obj_id);
    // Die if object doesn't exist
    if (!el.size()) return false;
    // If scoring, add complete class and update text. Remove if setting score to zero.
    if (score) {
        $(el).removeClass(pending).addClass(complete).html(count);
    } else {
        $(el).removeClass(pending).removeClass(complete).html(count);
    };
}

/***
 * mtUpdateScores - on page load, updates scores based up the viewing user (or IP address)
 */
function mtUpdateScores() {
    // Get user
    var u = mtGetUser();
    // If Anonymous not allowed and no user, die.
    if (!mt.blog.community.ifAnonymousRecommendAllowed && !u) return false;
    // Create comma-separated string of Entry IDs. If non exist, die.
    var entry_ids = '';
    var scores = $('.favtog');
    if (scores.size()) {
        for (var i = 0; i < scores.length; i++) {
            var id = $(scores[i]).attr('id').replace("favtog", "");
            if (entry_ids != '') entry_ids += ",";
            entry_ids += id;
        }
    } else {
        return false;
    };

    // Submit entry_ids via Ajax
    $.ajax({
      type: "POST",
      url: mt.blog.community.script,
      data: '__mode=score&blog_id=' + mt.blog.id + '&f=scored,count&jsonp=mtScore_cb&id=' + entry_ids,
      success: function(r){
          eval(r);
      }
    });
    return false;
}

/***
 * mtScore_cb - handles response from ajax request after update scores action
 */
function mtScore_cb(s_hash) {
    // for each id in the returned hash
    for (var id in s_hash) {
        // if object has been scored
        if ( parseInt( s_hash[id].scored ) ) {
            // Get the object
            var el = $('#favtog' + id);
            // If object exists
            if ( el.size() ) {
                // add class scored and update content text
                $(el).addClass('scored').html(s_hash[id].count+'');
            }
        }
    }
}

/***
 * Functions to follow/leave feature
 */

function script_follow(id) {
    // Get user
    var u = mtGetUser();
    // Die if not logged in
    if (!u || !u.name) return;
    
    $.ajax({
      type: "POST",
      url: mt.blog.community.script,
      data: '__mode=follow&id=' + id + '&magic_token=' + u.sid + '&jsonp=follow',
      success: function(r){
          eval(r);
      }
    });
    $('#following_' + id + '_else').hide();
    $('#following-status').html('<img src="' + mt.blog.staticWebPath + 'images/indicator.white.gif" height="10" width="10" alt="Following..." />');
}

function script_leave(id) {
    // Get user
    var u = mtGetUser();
    // Die if not logged in
    if (!u || !u.name) return;

    $.ajax({
      type: "POST",
      url: mt.blog.community.script,
      data: '__mode=leave&id=' + id + '&magic_token=' + u.sid + '&jsonp=leave',
      success: function(r){
          eval(r);
      }
    });
    $('#following_' + id).hide();
    $('#following-status').html('<img src="' + mt.blog.staticWebPath + 'images/indicator.white.gif" height="10" width="10" alt="Leaving..." />');
}

function follow(user_info) {
    conditional_block(true, 'following_' + user_info['id']);
    $('#following-status').html('');
}

function leave(user_info) {
    conditional_block(false, 'following_' + user_info['id']);
    $('#following-status').html('');
}


function conditional_block(cond, id) {
    var true_block = $('#' + id);
    var false_block = $('#' + id + '_else');
    if (cond) {
        $(false_block).hide();
        if ($(true_block).size()) {
            var display = $(true_block).attr('mt:display_style');
            if (!display && $(false_block).size())
                display = $(false_block).attr('mt:display_style');
            if (!display) display = '';
            $(true_block).show();
        }
    } else {
        $(true_block).hide();
        if ($(false_block).size()) {
            var display = $(false_block).attr('mt:display_style');
            if (!display && $(true_block).size())
                display = $(false_block).attr('mt:display_style');
            if (!display) display = '';
            $(false_block).show();
        }
    }
}

/***
 * Simple routine for showing a DOM element (applying a CSS display
 * attribute of 'none').
 */
function mtHide(id) {
    var el = (typeof id == "string") ? document.getElementById(id) : id;
    if (el) el.style.display = 'none';
}

/***
 * Simple routine for showing a DOM element (applying a CSS display
 * attribute of 'block').
 */
function mtShow(id) {
    var el = (typeof id == "string") ? document.getElementById(id) : id;
    if (el) el.style.display = 'block';
}

/***
 * A utility function for assigning/adding handlers to window events.
 */
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 );
        }
    }
}

/***
 * Calls the event named, if there are handlers for it.
 */
function mtFireEvent(eventName,param) {
    var fn = window['on' + eventName];
    if (typeof fn == 'function') return fn(param);
    return;
}

/***
 * Displays a relative date.
 * 'ts' is a Date object, 'fds' is a string of the date which
 * will be displayed if the given date is older than 1 week.
 */
function mtRelativeDate(ts, fds) {
    var now = new Date();
    var ref = ts;
    var delta = Math.floor((now.getTime() - ref.getTime()) / 1000);

    var str;
    if (delta < 60) {
        str = mt.blog.phrase.relativeDate.momentsAgo;
    } else if (delta <= 86400) {
        // less than 1 day
        var hours = Math.floor(delta / 3600);
        var min = Math.floor((delta % 3600) / 60);
        if (hours == 1)
            str = mt.blog.phrase.relativeDate.hoursAgo;
        else if (hours > 1)
            str = mt.blog.phrase.relativeDate.hoursAgoPlural.replace(/2/, hours);
        else if (min == 1)
            str = mt.blog.phrase.relativeDate.minutesAgo;
        else
            str = mt.blog.phrase.relativeDate.minutesAgoPlural.replace(/2/, min);
    } else if (delta <= 604800) {
        // less than 1 week
        var days = Math.floor(delta / 86400);
        var hours = Math.floor((delta % 86400) / 3600);
        if (days == 1)
            str = mt.blog.phrase.relativeDate.daysAgo;
        else if (days > 1)
            str = mt.blog.phrase.relativeDate.daysAgoPlural.replace(/2/, days);
        else if (hours == 1)
            str = mt.blog.phrase.relativeDate.hoursAgo;
        else
            str = mt.blog.phrase.relativeDate.hoursAgoPlural.replace(/2/, hours);
    }
    return str ? str : fds;
}

/***
 * Used to display an edit link for the given entry.
 */
function mtEditLink(entry_id, author_id) {
    var u = mtGetUser();
    if (! u) return;
    if (! entry_id) return;
    if (! author_id) return;
    if (u.id != author_id) return;
    var link = '<a href="' + mt.blog.adminScript + '?__mode=view&amp;_type=entry&amp;id=' + entry_id + '">' + mt.blog.phrase.edit + '</a>';
    document.write(link);
}

/***
 * Called when an input field on the comment form receives focus.
 */
function mtCommentFormOnFocus() {
    // if CAPTCHA is enabled, this causes the captcha image to be
    // displayed if it hasn't been already.
    mtShowCaptcha();
}

/***
 * Displays a captcha field for anonymous commenters.
 */
var mtCaptchaVisible = false;
function mtShowCaptcha() {
    var u = mtGetUser();
    if ( u && u.is_authenticated ) return;
    if (mtCaptchaVisible) return;
    var div = document.getElementById('comments-open-captcha');
    if (div) {
        div.innerHTML = mt.blog.comments.captchaFields;
        mtCaptchaVisible = true;
    }
}

/* user object
    -- saved in user cookie --
    u.name (display name)
    u.url (link to home page)
    u.email (for anonymous only)
    u.userpic (url for commenter/author)
    u.profile (link to profile)
    u.is_trusted (boolean)
    u.is_author (user has posting rights)
    u.is_banned (banned status; neither post/comment perms)
    u.can_post (has permission to post)
    u.can_comment (has permission to comment)

    -- status fields --
    u.is_authenticated (boolean)
    u.is_anonymous (user is anonymous)
*/

var is_preview;
var user;
/***
 * Assigns a user object as the actively logged in user; also saves the
 * user information in a browser cookie.
 */
function mtSetUser(u) {
    if (u) {
        // persist this
        user = u;
        mtSaveUser();
        // sync up user greeting
        mtFireEvent('usersignin');
    }
}

/***
 * Simple function that escapes single quote characters for storing
 * in a cookie.
 */
function mtEscapeJS(s) {
    s = s.replace(/'/g, "&apos;");
    return s;
}

/***
 * Simple function that unescapes single quote characters that were
 * stored in a cookie.
 */
function mtUnescapeJS(s) {
    s = s.replace(/&apos;/g, "'");
    return s;
}

/***
 * Serializes a user object into a string, suitable for storing as a cookie.
 */
function mtBakeUserCookie(u) {
    var str = "";
    if (u.name) str += "name:'" + mtEscapeJS(u.name) + "';";
    if (u.url) str += "url:'" + mtEscapeJS(u.url) + "';";
    if (u.email) str += "email:'" + mtEscapeJS(u.email) + "';";
    if (u.is_authenticated) str += "is_authenticated:'1';";
    if (u.profile) str += "profile:'" + mtEscapeJS(u.profile) + "';";
    if (u.userpic) str += "userpic:'" + mtEscapeJS(u.userpic) + "';";
    if (u.sid) str += "sid:'" + mtEscapeJS(u.sid) + "';";
    str += "is_trusted:'" + (u.is_trusted ? "1" : "0") + "';";
    str += "is_author:'" + (u.is_author ? "1" : "0") + "';";
    str += "is_banned:'" + (u.is_banned ? "1" : "0") + "';";
    str += "can_post:'" + (u.can_post ? "1" : "0") + "';";
    str += "can_comment:'" + (u.can_comment ? "1" : "0") + "';";
    str = str.replace(/;$/, '');
    return str;
}

/***
 * Unserializes a user cookie and returns a user object with the restored
 * state.
 */
function mtUnbakeUserCookie(s) {
    if (!s) return;

    var u = {}
    var m;
    while (m = s.match(/^((name|url|email|is_authenticated|profile|userpic|sid|is_trusted|is_author|is_banned|can_post|can_comment):'([^']+?)';?)/)) {
        s = s.substring(m[1].length);
        if (m[2].match(/^(is|can)_/)) // boolean fields
            u[m[2]] = m[3] == '1' ? true : false;
        else
            u[m[2]] = mtUnescapeJS(m[3]);
    }
    if (u.is_authenticated) {
        u.is_anonymous = false;
    } else {
        u.is_anonymous = true;
        u.can_post = false;
        u.is_author = false;
        u.is_banned = false;
        u.is_trusted = false;
    }
    return u;
}

/***
 * Retrieves an object of the currently logged in user's state.
 * If no user is logged in or cookied, this will return null.
 */
function mtGetUser() {
    if (!user) {
        var cookie = mtGetCookie(mtCookieName);
        if (!cookie) return;
        user = mtUnbakeUserCookie(cookie);
        if (! user) {
            user = {}
            user.is_anonymous = true;
            user.can_post = false;
            user.is_author = false;
            user.is_banned = false;
            user.is_trusted = false;
        }
    }
    return user;
}

/***
 * Issues a request to the MT comment script to retrieve the currently
 * logged-in user (if any).
 */
var mtFetchedUser = false;
if (mt.blog.id) {
    function mtFetchUser(cb) {
        if (!cb) cb = 'mtSetUser';
        if ( ( cb == 'mtSetUser' ) && mtGetUser() ) {
            var url = document.URL;
            url = url.replace(/#.+$/, '');
            url += '#comments-open';
            location.href = url;
        } else {
            // we aren't using AJAX for this, since we may have to request
            // from a different domain. JSONP to the rescue.
            mtFetchedUser = true;
            var script = document.createElement('script');
            var ts = new Date().getTime();
            script.src = mt.blog.comments.script + '?__mode=session_js&blog_id=' + mt.blog.id + '&jsonp=' + cb + '&ts=' + ts;
            (document.getElementsByTagName('head'))[0].appendChild(script);
        }
    }
}

/***
 * Called when the 'Remember me' checkbox is changed. If the checkbox
 * is cleared, the cached user cookie is immediately cleared.
 */
function mtRememberMeOnClick(b) {
    if (!b.checked)
        mtClearUser(b.form);
    return true;
}

/***
 * Called when comment form is sent.
 * Required parameter: Form DOM object of comment form.
 * If form has a 'bakecookie' member, it will be used to signal
 * storing the anonymous commenter information to a cookie.
 * If form has a 'armor' member, it will be used to store
 * a token that is checked by the comment script.
 */
if (mt.blog.id) {
    var mtRequestSubmitted = false;
    function mtCommentOnSubmit(f) {
        if (!mtRequestSubmitted) {
            mtRequestSubmitted = true;

            if (f.armor)
                f.armor.value = mt.blog.comments.armor;
            if (f.bakecookie && f.bakecookie.checked)
                mtSaveUser(f);

            // disable submit buttons
            if (f.preview_button) f.preview_button.disabled = true;
            if (f.post) f.post.disabled = true;

            var u = mtGetUser();
            if ( !is_preview && ( u && u.is_authenticated ) ) {
                // validate session; then submit
                mtFetchedUser = false;
                mtFetchUser('mtCommentSessionVerify');
                return false;
            }

            return true;
        }
        return false;
    }

    function mtCommentSessionVerify(app_user) {
        var u = mtGetUser();
        var f = document['comments_form'];
        if ( u && app_user && app_user.sid && ( u.sid == app_user.sid ) ) {
            f.submit();
        } else {
            alert(mt.blog.phrase.commenter.sessionExpired);
            mtClearUser();
            mtFireEvent('usersignin');
            if (mt.blog.registration.required) {
                mtShow('comments-form');
                mtHide('comments-open-footer');
            }
        }
    }

    function mtUserOnLoad() {
        var u = mtGetUser();

        // if the user is authenticated, hide the 'anonymous' fields
        // and any captcha input if already shown
        if ( document.getElementById('comments-form')) {
            if ( u && u.is_authenticated ) {
                mtShow('comments-form');
                mtHide('comments-open-data');
                if (mtCaptchaVisible)
                    mtHide('comments-open-captcha');
            } else {
                if (mt.blog.registration.required) {
                    mtHide('comments-form');
                }
            }
            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;
                } else {
                    if (u && u.sid && cf.sid)
                        cf.sid.value = u.sid;
                }
                if (cf.post.disabled)
                    cf.post.disabled = false;
                if (cf.preview_button.disabled)
                    cf.preview_button.disabled = false;
                mtRequestSubmitted = false;
            }
        }
    }
}

/***
 * Called when an entry archive page is loaded.
 * This routine controls which elements of the comment form are shown
 * or hidden, depending on commenter type and blog configuration.
 */
if (mt.blog.id) {
    function mtEntryOnLoad() {
        if (!mt.blog.pings.accepted)
            mtHide('trackbacks-info');
        if (!mt.blog.comments.accepted)
            mtHide('comments-open');
        mtFireEvent('usersignin');
    }

    function mtEntryOnUnload() {
        if (mtRequestSubmitted) {
            var cf = document['comments_form'];
            if (cf) {
                if (cf.post && cf.post.disabled)
                    cf.post.disabled = false;
                if (cf.preview_button && cf.preview_button.disabled)
                    cf.preview_button.disabled = false;
            }
            mtRequestSubmitted = false;
        }
        return true;
    }

    mtAttachEvent('usersignin', mtUserOnLoad);
}

/***
 * Handles the action of the "Sign in" link. First clears any existing
 * user cookie, then directs to the MT comment script to sign the user in.
 */
function mtSignIn() {
    var doc_url = document.URL;
    doc_url = doc_url.replace(/#.+/, '');
    var url = mt.blog.signInLink;
    if (is_preview) {
        if ( document['comments_form'] ) {
            var entry_id = document['comments_form'].entry_id.value;
            url += '&entry_id=' + entry_id;
        } else {
            url += '&return_url=' + mt.blog.url;
        }
    } else {
        url += '&return_url=' + encodeURIComponent(doc_url);
    }
    mtClearUser();
    location.href = url;
}

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 = mt.blog.phrase.signingIn + ' <span class="status-indicator">&nbsp;</span>';

    mtClearUser(); // clear any 'anonymous' user cookie to allow sign in
    mtFetchUser('mtSetUserOrLogin');
    
    return false;
}

function mtSetUserOrLogin(u) {
    if (u && u.is_authenticated) {
        mtSetUser(u);
    } else {
        // user really isn't logged in; so let's do this!
        mtSignIn();
    }
}

/***
 * 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.
 */
function mtSignOut(entry_id) {
    mtClearUser();
    var doc_url = document.URL;
    doc_url = doc_url.replace(/#.+/, '');
    var url = mt.blog.signOutLink;
    if (is_preview) {
        if ( document['comments_form'] ) {
            var entry_id = document['comments_form'].entry_id.value;
            url += '&entry_id=' + entry_id;
        } else {
            url += '&return_url=' + mt.blog.url;
        }
    } else {
        url += '&return_url=' + encodeURIComponent(doc_url);
    }
    location.href = url;
}

/***
 * Handles the action of the "Sign out" link.
 */
function mtSignOutOnClick() {
    mtSignOut();
    return false;
}

/***
 * Handles the display of the greeting message, depending on what kind of
 * user is logged in and blog comment policy.
 */
if (mt.blog.id) {
    function mtShowGreeting() {
        if (mt.blog.registration.allowed) {
            var cf = document['comments_form'];
            if (!cf) return;

            var el = document.getElementById('comment-greeting');
            if (!el)  // legacy MT 4.x element id
                el = document.getElementById('comment-form-external-auth');
            if (!el) return;

            var eid = cf.entry_id;
            var entry_id;
            if (eid) entry_id = eid.value;

            var phrase;
            var u = mtGetUser();

            if ( u && u.is_authenticated ) {
                if ( u.is_banned ) {
                    phrase = mt.blog.phrase.commenter.authBanned;
                } else {
                    var user_link;
                    user_link = '<a href="' + mt.blog.community.script + '?__mode=view&amp;blog_id=' + mt.blog.id + '">' + u.name + '</a>';
                    phrase = mt.blog.phrase.commenter.authSuccess;
                    phrase = phrase.replace(/__NAME__/, user_link);
                    // TBD: supplement phrase with userpic if one is available.
                }
            } else {
                if (mt.blog.registration.required) {
                    phrase = mt.blog.phrase.commenter.auth;
                } else {
                    phrase = mt.blog.phrase.commenter.authAnonyous;
                }
            }
            el.innerHTML = phrase;
        } else {
            mtShowCaptcha();
        }
    }
}

/***
 * Handles the action of the 'Reply' links.
 */
function mtReplyCommentOnClick(parent_id, author) {
    mtShow('comment-form-reply');

    var checkbox = document.getElementById('comment-reply');
    var label = document.getElementById('comment-reply-label');
    var text = document.getElementById('comment-text');

    // Populate label with new values
    var reply_text = mt.blog.phrase.commenter.replyingTo;
    reply_text = reply_text.replace(/__PARENT__/, parent_id);
    reply_text = reply_text.replace(/__AUTHOR__/, author);
    label.innerHTML = reply_text;

    checkbox.value = parent_id; 
    checkbox.checked = true;
    try {
        // text field may be hidden
        text.focus();
    } catch(e) {
    }

    mtSetCommentParentID();
}

/***
 * Sets the parent comment ID when replying to a comment.
 */
function mtSetCommentParentID() {
    var checkbox = document.getElementById('comment-reply');
    var parent_id_field = document.getElementById('comment-parent-id');
    if (!checkbox || !parent_id_field) return;

    var pid = 0;
    if (checkbox.checked == true)
        pid = checkbox.value;
    parent_id_field.value = pid;
}

/***
 * Persists a copy of the current user cookie into the browser cookie stash.
 */
function mtSaveUser(f) {
    // We can't reliably store the user cookie during a preview.
    if (is_preview) return;

    var u = mtGetUser();

    if (f && (!u || u.is_anonymous)) {
        if ( !u ) {
            u = {}
            u.is_authenticated = false;
            u.can_comment = true;
            u.is_author = false;
            u.is_banned = false;
            u.is_anonymous = true;
            u.is_trusted = false;
        }
        if (f.author != undefined) u.name = f.author.value;
        if (f.email != undefined) u.email = f.email.value;
        if (f.url != undefined) u.url = f.url.value;
    }

    if (!u) return;

    var cache_period = mtCookieTimeout * 1000;

    // cache anonymous user info for a long period if the
    // user has requested to be remembered
    if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked)
        cache_period = 365 * 24 * 60 * 60 * 1000;

    var now = new Date();
    mtFixDate(now);
    now.setTime(now.getTime() + cache_period);

    var cmtcookie = mtBakeUserCookie(u);
    mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
        location.protocol == 'https:');
}

/***
 * Clears the blog-side user cookie.
 */
function mtClearUser() {
    user = null;
    mtDeleteCookie(mtCookieName, mtCookiePath, mtCookieDomain,
        location.protocol == 'https:');
}

/***
 * Sets a browser cookie.
 */
function mtSetCookie(name, value, expires, path, domain, secure) {
    if (domain && domain.match(/^\.?localhost$/))
        domain = null;
    var curCookie = name + "=" + escape(value) +
        (expires ? "; expires=" + expires.toGMTString() : "") +
        (path ? "; path=" + path : "") +
        (domain ? "; domain=" + domain : "") +
        (secure ? "; secure" : "");
    document.cookie = curCookie;
}

/***
 * Retrieves a browser cookie.
 */
function mtGetCookie(name) {
    var prefix = name + '=';
    var c = document.cookie;
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return '';
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

/***
 * Deletes a browser cookie.
 */
function mtDeleteCookie(name, path, domain, secure) {
    if (mtGetCookie(name)) {
        if (domain && domain.match(/^\.?localhost$/))
            domain = null;
        document.cookie = name + "=" +
            (path ? "; path=" + path : "") +
            (domain ? "; domain=" + domain : "") +
            (secure ? "; secure" : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function mtFixDate(date) {
    var skew = (new Date(0)).getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

/***
 * Returns a XMLHttpRequest object (for Ajax operations).
 */
function mtGetXmlHttp() {
    if ( !window.XMLHttpRequest ) {
        window.XMLHttpRequest = function() {
            var types = [
                "Microsoft.XMLHTTP",
                "MSXML2.XMLHTTP.5.0",
                "MSXML2.XMLHTTP.4.0",
                "MSXML2.XMLHTTP.3.0",
                "MSXML2.XMLHTTP"
            ];

            for ( var i = 0; i < types.length; i++ ) {
                try {
                    return new ActiveXObject( types[ i ] );
                } catch( e ) {}
            }

            return undefined;
        }
    }
    if ( window.XMLHttpRequest )
        return new XMLHttpRequest();
}

// BEGIN: fast browser onload init
// Modifications by David Davis, DWD
// Dean Edwards/Matthias Miller/John Resig
// http://dean.edwards.name/weblog/2006/06/again/?full#comment5338

function mtInit() {
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer
    // DWD - check against window
    if ( window._timer ) clearInterval(window._timer);

    // DWD - fire the window onload now, and replace it
    if ( window.onload && ( window.onload !== window.mtInit ) ) {
        window.onload();
        window.onload = function() {}
    }
}

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", mtInit, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
    if (this.readyState == "complete") {
        mtInit(); // call the onload handler
    }
}
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            mtInit(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = mtInit;

// END: fast browser onload init

if (mt.blog.id && mt.blog.registration.allowed) {
    /***
     * If request contains a '#_login' or '#_logout' hash, use this to
     * also delete the blog-side user cookie, since we're coming back from
     * a login, logout or edit profile operation.
     */
    var clearCookie = ( window.location.hash && window.location.hash.match( /^#_log(in|out)/ ) ) ? true : false;
    if (clearCookie) {
        // clear any logged in state
        mtClearUser();
        if (RegExp.$1 == 'in')
            mtFetchUser();
    } else {
        /***
         * Uncondition this call to fetch the current user state (if available)
         * from MT upon page load if no user cookie is already present.
         * This is okay if you have a private install, such as an Intranet;
         * not recommended for public web sites!
         */
        if ( is_preview && !user )
            mtFetchUser();
    }
}
