// default

var commenter_name;
var commenter_blog_ids;
var is_preview;
var mtcmtmail;
var mtcmtauth;
var mtcmthome;



commenter_name = getCookie('commenter_name');
ids = getCookie('commenter_id').split(':');
commenter_id = ids[0];
commenter_blog_ids = ids[1];
commenter_url = getCookie('commenter_url');



// onload

function individualArchivesOnLoad(commenter_name, entry_id, blog_id, commenter_id, commenter_url) {
    if (document.getElementsByTagName && document.createElement) {
        makeImage('em');
        makeBookmarks();
        writeGreeting(commenter_name, entry_id, blog_id, commenter_id, commenter_url);


        var comments_form = document.forms['comments-form'];
        if (comments_form) {
    
        // comments are allowed but registration not required

            if ( commenter_name &&
                ( !commenter_id
                || commenter_blog_ids.indexOf("'1'") > -1)) {
                comments_form.author.disabled = true;
                comments_form.author.value = commenter_name;
                comments_form.email.disabled = true;
            } else if (is_preview) {
                delayShowCaptcha();
            } 
    

            if (!commenter_name && (comments_form.email != undefined) &&
                (mtcmtmail = getCookie("mtcmtmail")))
                comments_form.email.value = mtcmtmail;
            if (!commenter_name && (comments_form.author != undefined) &&
                (mtcmtauth = getCookie("mtcmtauth")))
                comments_form.author.value = mtcmtauth;
            if (comments_form.url != undefined &&
                (mtcmthome = getCookie("mtcmthome")))
                comments_form.url.value = mtcmthome;
            if (comments_form.bakecookie) {
                if (mtcmtauth || mtcmthome) {
                    comments_form.bakecookie.checked = true;
                } else {
                    comments_form.bakecookie.checked = false;
                }
            }
        }
    }
}


// image

function makeImage(tagName) {
    var tcoll = document.getElementsByTagName(tagName);
    if (!tcoll) return;

    for (var i = 0,j = tcoll.length; i < j; i++) {
        if (tcoll[i].className == 'linktoanimage') {
            var srcName = tcoll[i].firstChild.getAttribute('href');
            var c = tcoll[i].firstChild;
            if (c.firstChild.nodeValue) {
                var altName = c.firstChild.nodeValue;
                c.removeChild(c.firstChild);
                tcoll[i].removeChild(tcoll[i].firstChild);
                var x = document.createElement('img');
                x.setAttribute('src', srcName);
                x.setAttribute('alt', altName);
                x.setAttribute('title', altName);
                tcoll[i].appendChild(x);
            }
        }
    }
}


// bookmark

function makeBookmarks() {
    var meta = document.getElementsByTagName('p')[1];
    if (!meta) return;

    var url = 'http://' + location.hostname + location.pathname;
    var text = new Array(1);
    var a = new Array(1);
    var img = new Array(1);

    text[0] = document.createTextNode(' | ');

    a[0] = document.createElement('a');
    a[0].setAttribute('href','http://b.hatena.ne.jp/entry/' + url);
    img[0] = document.createElement('img');
    img[0].setAttribute('src','http://d.hatena.ne.jp/images/b_entry.gif');
    img[0].setAttribute('alt','はてなブックマーク');
    img[0].setAttribute('title','はてなブックマーク');

    text[1] = document.createTextNode(' ');

    a[1] = document.createElement('a');
    a[1].setAttribute('href','http://b.hatena.ne.jp/entry/' + url);
    img[1] = document.createElement('img');
    img[1].setAttribute('src','http://b.hatena.ne.jp/entry/image/' + url);
    img[1].setAttribute('alt','');

    meta.appendChild(text[0]);
    meta.appendChild(a[0]).appendChild(img[0]);
    meta.appendChild(text[1]);
    meta.appendChild(a[1]).appendChild(img[1]);
}


// greeting

function writeGreeting(commenter_name, entry_id, blog_id, commenter_id, commenter_url) {

    var a = new Array(3);
    var text = new Array(5);
    if (document.getElementById && document.createElement) {
        var gts = document.getElementById('greeting');
        a[0] = document.createElement('a');
        a[0].setAttribute('href','http://adp.daa.jp/cgi/mt2/mt-comments.cgi?__mode=login&entry_id=' + entry_id + '&blog_id=' + blog_id + '&static=1');
        text[0] = document.createTextNode('サインイン');
        a[1] = document.createElement('a');
        a[1].setAttribute('href','http://adp.daa.jp/cgi/mt2/mt-comments.cgi?__mode=handle_sign_in&amp;static=1&amp;logout=1&entry_id=' + entry_id);
        text[1] = document.createTextNode('サインアウト');
        text[2] = document.createTextNode(commenter_name);
        if ( commenter_name &&
            ( !commenter_id
            || commenter_blog_ids.indexOf("'" + blog_id + "'") > -1)) {
            var url;
            if (commenter_id) {
                url = 'http://adp.daa.jp/cgi/mt2/mt-comments.cgi?__mode=edit_profile&commenter=' + commenter_id + '&blog_id=' + blog_id;
                if (entry_id) {
                    url += '&entry_id=' + entry_id;
                } else {
                    url += '&static=1';
                }
            } else if (commenter_url) {
                url = commenter_url;
            } else {
                url = null;
            }
            if (url) {
                gts.removeChild(gts.lastChild);
                a[2] = document.createElement('a');
                a[2].setAttribute('href',url);
                gts.appendChild(a[2]).appendChild(text[2]);
            } else {
                gts.removeChild(gts.lastChild);
                gts.appendChild(text[2]);
            }
            text[3] = document.createTextNode('さん、コメントをどうぞ。');
            gts.appendChild(text[3]);
            gts.appendChild(a[1]).appendChild(text[1]);
        } else if (commenter_name) {
            text[3] = document.createTextNode('このブログにはコメントする権限を持っていません。');
            gts.appendChild(text[3]);
            gts.appendChild(a[1]).appendChild(text[1]);
        } else {

            text[3] = document.createTextNode('OpenID、またはLiveJournal, Vox, TypeKeyのアカウントがある方は、');
            text[4] = document.createTextNode('してからコメントくださればこちらでお客様登録をしますので、2回目以降はコメントがすぐ反映されます。');
//            gts.appendChild(text[3]);
//            gts.appendChild(a[0]).appendChild(text[0]);
//            gts.appendChild(text[4]);

        }
    }

}

// cookie

// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
        (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

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

function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    now = now.toGMTString();
    if (!commenter_name && (f.author != undefined))
       setCookie('mtcmtauth', f.author.value, now, '/', '', '');
    if (f.email != undefined)
       setCookie('mtcmtmail', f.email.value, now, '/', '', '');
    if (f.url != undefined)
       setCookie('mtcmthome', f.url.value, now, '/', '', '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '/', '');
    deleteCookie('mtcmthome', '/', '');
    deleteCookie('mtcmtauth', '/', '');
}

