/* Initializer.
 * Copyright (C) 2010-2014 shinGETsu Project.
 *
 * addScriptPath came from
 * http://temping-amagramer.blogspot.jp/2012/02/jqueryjavascriptscript.html
 */

var shingetsu = (function () {
    var _initializer = [];
    var _recordsModifiers = [];

    var shingetsu = {
        debugMode: false,
        plugins: {},
        rootPath: '/',
        dummyQuery: '',
        uiLang: 'en'
    };

    shingetsu.log = function (arg) {
        if (typeof console == 'object') {
            console.log(arg);
        } else if (shingetsu.debugMode) {
            alert(arg);
        }
    };

    shingetsu.initialize = function (func) {
        _initializer[_initializer.length] = func;
    };
    shingetsu.addInitializer = shingetsu.initialize;

    shingetsu.addRecordsModifiers = function (func) {
        _recordsModifiers[_recordsModifiers.length] = func;
    };

    shingetsu.addScriptPath = function (path, onload) {
        if (typeof onload != 'function') {
            onload = function() {};
        }
        var sep = (path.indexOf('?') > 0) ? '&' : '?';
        var realPath = shingetsu.rootPath + path + sep + shingetsu.dummyQuery;
        var script = $('<script>');
        script.attr('type', 'text/javascript')
              .attr('src', realPath)
              .on('load', function() { onload(); });
        document.getElementsByTagName('head')[0].appendChild(script[0]);
    };

    shingetsu.modifyRecords = function ($container) {
        for (var i = 0; i < _recordsModifiers.length; i++) {
            if (shingetsu.debugMode) {
               _recordsModifiers[i]($container);
               continue;
            }
            try {
               _recordsModifiers[i]($container);
            } catch (e) {
                shingetsu.log(e);
            }
        }
    };

    var _initialize = function () {
        for (var i = 0; i < _initializer.length; i++) {
            if (shingetsu.debugMode) {
               _initializer[i]();
               continue;
            }
            try {
               _initializer[i]();
            } catch (e) {
                shingetsu.log(e);
            }
        }
        shingetsu.modifyRecords($(document));
    };

    $(_initialize);

    return shingetsu;
})();
/*
 * Jump New Posts.
 * Copyright (C) 2006-2019 shinGETsu Project.
 */

shingetsu.initialize(function () {
    var itemKey = null;
    if (location.pathname.search(/^\/?thread.cgi\/([^\/]+)$/) === 0) {
        try {
            itemKey = 'access_' + decodeURI(RegExp.$1);
        } catch (e) {
        }
    }

    function getAccess() {
        if (! itemKey) {
            return 0;
        }
        try {
            return parseInt(localStorage.getItem(itemKey));
        } catch (e) {
            return 0;
        }
    }

    function setAccess(access) {
        if (! itemKey) {
            return;
        }
        localStorage.setItem(itemKey, access);
    }

    function jumpto(id) {
        var s = new String(window.location);
        if (s.search("#") < 0) {
            $("html,body").animate({
                scrollTop: $("#r" + id).offset().top
            }, {
                duration: 200
            });
        }
    }

    function setNewPost(dt) {
        if (itemKey) {
            $(dt).addClass("newpost");
        }
    }

    function newPosts(access) {
        if (access) {
            var read = access
        } else {
            var read = 0;
        }
        var newid = "";
        var lastStamp = null;
        $("dt span.stamp[data-stamp]").each(function () {
            var stamp = $(this).data('stamp');
            if (stamp > read) {
                var dt = $(this).closest("dt").get(0);
                if (!newid) {
                    newid = dt.id.substring(1);
                }
                setNewPost(dt);
            }
            lastStamp = this;
        });
        if (lastStamp) {
            setAccess($(lastStamp).data('stamp') + 1);
        }
        if (! access) {
        } else if (newid) {
            jumpto(newid);
        } else {
            jumpto($(lastStamp).closest("dt").get(0).id.substring(1));
        }
    }

    newPosts(getAccess());
});
/*
 * Load jQuery Lazy
 * Copyright (C) 2014 shinGETsu Project.
 */

shingetsu.initialize(function () {
    function applyLazy($container) {
        $container.find("img[data-lazyimg]").lazy({
            effect: 'fadeIn',
            effectTime: 500
        });

        if ($container.hasClass('popup')) {
            $container.find("img[data-lazyimg]").each(function (i, e) {
                $(e).attr('src', $(e).attr('data-src'))
                    .removeAttr('data-lazyimg')
                    .removeAttr('data-src');
            });
        }
    }

    shingetsu.addRecordsModifiers(applyLazy);
});
/* Localtime of User Agent.
 * Copyright (C) 2006-2014 shinGETsu Project.
 */

shingetsu.initialize(function () {
    function format(n) {
        return ('0' + n).substr(-2);
    }

    function myLocaltime(date) {
        var year = date.getYear();
        if (year < 1900) year += 1900;
        var month = format(date.getMonth()+1);
        var day = format(date.getDate());
        var hours = format(date.getHours());
        var minutes = format(date.getMinutes());
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
    }

    function overrideDatetime($container) {
        $container.find('span.stamp[data-stamp]').each(function() {
            var container = $(this);
            var date = new Date();
            date.setTime(container.attr('data-stamp') * 1000);
            container.html(myLocaltime(date));
        });
    }

    shingetsu.addRecordsModifiers(overrideDatetime);
});
/* Popup.
 * Copyright (C) 2005-2012 shinGETsu Project.
 */
(function () {
    function Coordinate(e, opt_y, opt_z) {
        $.extend(this, {
            x: e,
            y: opt_y,
            z: opt_z
        });

        if (this.y === undefined) {
            if (e) {
                this.x = e.pageX;
                this.y = e.pageY;
            } else {
                this.x = event.clientX;
                this.y = event.clientY;
            }
        }
    }
    shingetsu.plugins.Coordinate = Coordinate;
})();

(function () {
    function Popup(opt_parameters) {
        $.extend(this, {
            parentPopup: null,
            childPopups: [],
            container: null,
            content: null,
            coordinate: null
        }, opt_parameters);

        if (!this.container) {
            this.container = this.createContainer(this.content);
        } else {
            this.container = $(this.container);
        }
        if ($(document).has(this.container).length === 0) {
            this.container.hide().appendTo(document.body);
        }
    }
    function createContainer(content) {
        return $('<div>')
            .addClass('popup')
            .css('position', 'absolute')
            .append(content);
    }
    function setContent(content) {
        this.content = content;
        this.container.empty().append(content);
    }
    function appendChild(childPopup) {
        this.childPopups.push(childPopup);
    }
    function reposition(coordinate) {
        var width = this.container.width();
        var height = this.container.height();
        var x = Math.max($(document).scrollLeft(), coordinate.x + 20);
        var y = Math.max($(document).scrollTop(), coordinate.y - height);
        this.container
            .css('left', x)
            .css('top', y);
        if (coordinate.z !== undefined) {
            this.container.css('zIndex', coordinate.z);
        }
    }
    function show(coordinate) {
        this.reposition(coordinate);
        this.container.show();
    }
    function hide(options) {
        options = $.extend({
            duration: Popup.hidingDuration
        }, options);
        this.container.fadeOut(options);
    }
    function destroy() {
        while (this.childPopups.length) {
            var child = this.childPopups[this.childPopups.length - 1];
            child.destroy();
            this.childPopups.splice(this.childPopups.length - 1, 1);
        }
        if (this.parentPopup) {
            var siblingPopups = this.parentPopup.childPopups;
            var i = siblingPopups.length;
            while (i--) {
                if (siblingPopups[i] == this) {
                    this.parentPopup.childPopups.splice(i, 1);
                    break;
                }
            }
            this.parentPopup = null;
        }
        this.container.remove();
        this.container = null;
    }
    $.extend(Popup, {
        hidingDuration: 200
    });
    Popup.prototype = {
        constructor: Popup,
        createContainer: createContainer,
        setContent: setContent,
        appendChild: appendChild,
        reposition: reposition,
        show: show,
        hide: hide,
        destroy: destroy
    };
    shingetsu.plugins.Popup = Popup;
})();

(function () {
    var exclusivePopup;

    function showPopup(coordinate, objects) {
        if (exclusivePopup) {
            exclusivePopup.hide({
                complete: $.proxy(function () {
                    this.destroy();
                }, exclusivePopup)
            });
        }
        var popup = new shingetsu.plugins.Popup({
            coordinate: coordinate,
            content: objects
        });
        popup.container.prop('id', 'popup');
        exclusivePopup = popup;
        exclusivePopup.show(coordinate);

        $('select').hide();
        
        return exclusivePopup;
    }
    function hidePopup() {
        if (exclusivePopup) {
            exclusivePopup.hide({
                complete: $.proxy(function () {
                    this.destroy();
                }, exclusivePopup)
            });
            exclusivePopup = null;
        }

        $('select').show();
    }

    shingetsu.plugins.showPopup = showPopup;
    shingetsu.plugins.hidePopup = hidePopup;
})();
/* -*- coding: utf-8 -*-
 * Response Post.
 * Copyright (C) 2006-2012 shinGETsu Project.
 */

shingetsu.initialize(function () {
    var msg_res = '[Res.]';
    if (shingetsu.uiLang == 'ja') {
        msg_res = '[返信]';
    }

    function res(id) {
        var textArea = $('#body');
        if (textArea.val()) {
            textArea.val(textArea.val() + '\n>>' + id + '\n');
        } else {
            textArea.val('>>' + id + '\n');
        }

        var anchor = $('<a>').text('>>' + id);
        if ($('#records').find('#r' + id).length) {
            anchor.attr('href', '#r' + id)
                  .addClass('innerlink');
        } else {
            if (location.href.search(/(.+\/thread\.cgi\/[^\/]*)/) == 0) {
                anchor.attr('href', RegExp.$1 + '/' + id)
                      .addClass('reclink');
            }
        }
        $('#resreferrer').append(anchor);
        shingetsu.plugins.rootResAnchor.parseContent($('#resreferrer'));
        textArea.focus();
    }

    function addLink($container) {
        $container.find('dt').each(function (i, dt) {
            var $dt = $(dt);
            if ($dt.attr('data-record-id')) {
                $dt.find('a[data-responce-id]').remove();
                var id = $dt.attr('data-record-id');
                var $anchor = $('<a>');
                $anchor.attr('href', 'javascript:;')
                       .text(msg_res)
                       .attr('data-responce-id', id);
            }
            $dt.append($anchor);
        });
    }

    var ref = $('<div>');
    ref.attr('id', 'resreferrer');
    $('#body').before(ref);

    shingetsu.addRecordsModifiers(addLink);

    $(document).on('click', 'a[data-responce-id]', function (e) {
        res($(this).attr('data-responce-id'));
        return false;
    });
});
/* Tag Edit Tool.
 * Copyright (C) 2007,2010 shinGETsu Project.
 * $Id$
 */

shingetsu.initialize(function () {
    if (location.pathname.search('/admin.cgi/edittag') != 0) {
        return;
    }

    var checked = {};

    function insertTag(tag) {
        var form = document.getElementById('savetag');
        if (form.tag.value != '') {
            form.tag.value += ' ' + tag;
        } else {
            form.tag.value = tag;
        }
    }

    function removeTag(tag) {
        var form = document.getElementById('savetag');
        var tmp = ' ' + form.tag.value + ' ';
        tmp = tmp.replace(' '+tag+' ', ' ');
        tmp = tmp.replace(/\s+/g, ' ');
        tmp = tmp.replace(/^\s+/g, '');
        tmp = tmp.replace(/\s+$/g, '');
        form.tag.value = tmp;
    }

    function toggleTag(tag) {
        var strtag = tag.innerHTML;
        if (checked[strtag]) {
            tag.style.backgroundColor = '#efefef';
        } else {
            tag.style.backgroundColor = '#ccc';
        }
    }

    function toggleAllTags(strtag) {
        var span = document.getElementsByTagName('span');
        for (var i=0; i<span.length; i++) {
            if ((span[i].className == 'tag') &&
                (span[i].innerHTML == strtag)) {
                toggleTag(span[i]);
            }
        }
        checked[strtag] = ! checked[strtag];
    }

    function clickTag(tag) {
        var strtag = tag.innerHTML;
        if (! checked[strtag]) {
            insertTag(strtag);
        } else {
            removeTag(strtag);
        }
        toggleAllTags(strtag);
    }

    var span = document.getElementsByTagName('span');
    var strcheck = ' ' + document.getElementById('savetag').tag.value + ' ';
    for (var i=0; i<span.length; i++) {
        if (span[i].className == 'tag') {
            span[i].onclick =
                (function (tag) {
                    return function () {
                        clickTag(tag);
                    }})(span[i]);
            span[i].style.cursor = 'pointer';
            var strtag = span[i].innerHTML;
            if ((strcheck.search(' ' + strtag +' ') >= 0) &&
                (! checked[strtag])) {
                toggleAllTags(strtag);
            }
        }
    }
});
/* -*- coding: utf-8 -*-
 * Text Area Conttoller.
 * Copyright (C) 2006-2015 shinGETsu Project.
 */

shingetsu.initialize(function () {
    var msg_spread = 'Spread';
    var msg_reduce = 'Reduce';
    var msg_preview = 'Preview';
    var msg_edit = 'Edit';
    if (shingetsu.uiLang == 'ja') {
        msg_spread = '拡大';
        msg_reduce = '縮小';
        msg_preview = 'プレビュー';
        msg_edit = '編集再開';
    }

    var textArea = $('#body');
    var textAreaContainer = textArea.parent();
    var buttonContainer = $('<div>');
    textArea.before(buttonContainer);
    buttonContainer.addClass('post-advanced');

    function TextAreaController(textArea, button) {
        this._textArea = textArea;
        this._button = button;
        this._isBigSize = false;
    }

    TextAreaController.prototype.toggle = function (event) {
        event.preventDefault();
        if (this._isBigSize) {
            this._reduce();
        } else {
            this._spread();
        }
    };

    TextAreaController.prototype._spread = function () {
        this._textArea.attr('rows', 30).css('width', '90%');
        this._button.text(msg_reduce);
        this._isBigSize = true;
    };

    TextAreaController.prototype._reduce = function () {
        this._textArea.attr('rows', 7).css('width', '');
        this._button.text(msg_spread);
        this._isBigSize = false;
    };

    var sizeButton = $('<button>');
    buttonContainer.append(sizeButton);
    sizeButton.text(msg_spread).addClass('btn');
    

    var textAreaController = new TextAreaController(textArea, sizeButton);
    sizeButton.click(function (e) { textAreaController.toggle(e) } );


    function html_format(message) {
        var e = document.all? 'null': 'event';
        if (document.location.href.search(/(.*?\/thread\.cgi\/[^\/]+)/) != 0) {
            return;
        }
        var thread_uri = RegExp.$1;
        thread_uri = thread_uri.replace(/&/g, '&amp;');
        thread_uri = thread_uri.replace(/</g, '&lt;');
        thread_uri = thread_uri.replace(/>/g, '&gt;');
        message = message.replace(/&/g, '&amp;');
        message = message.replace(/</g, '&lt;');
        message = message.replace(/>/g, '&gt;');
        message = message.replace(
            /(https?:..[^\x00-\x20"'()<>\[\]\x7F-\xFF]*)/g,
            '<a href="$1">$1</a>');
        message = message.replace(/&gt;&gt;([0-9a-f]{8})/g,
            '<a class="reclink" href="' + thread_uri + '/$1">&gt;&gt;$1</a>');
        message = message.replace(
            /\[\[([^/<>\[\]]+)\]\]/g,
            function ($0, $1) {
                return '<a href="/thread.cgi/' + encodeURIComponent($1) +
                       '">[[' + $1 + ']]</a>';
            });
        message = message.replace(
            /\[\[([^/<>\[\]]+)\/([0-9a-f]{8})\]\]/g,
            function ($0, $1, $2) {
                return '<a class="reclink" href="/thread.cgi/' + encodeURIComponent($1) +
                       '/' + $2 +
                       '">[[' + $1 + '/' + $2 + ']]</a>';
            });
        return message;
    }


    function PreviewController(textArea, previewArea, button, textAreaFriends) {
        this._textArea = textArea;
        this._previewArea = previewArea;
        this._textAreaFriends = textAreaFriends;
        this._button = button;
        this._isPreview = false;
    }

    PreviewController.prototype.toggle = function (event) {
        event.preventDefault();
        if (this._isPreview) {
            this._hide();
        } else {
            this._show();
        }
    };

    PreviewController.prototype._show = function () {
        $.each(this._textAreaFriends, function (i, v) { v.hide() });
        this._textArea.hide();
        var message = this._textArea.val();
        if (typeof marked === 'function' && message.startsWith("@markdown")) {
            message = marked(message.substring("@markdown".length));
            this._previewArea.css('white-space', 'normal');
        } else {
            message = html_format(this._textArea.val());
            this._previewArea.css('white-space', 'pre');
        }
        this._previewArea.html(message);
        shingetsu.plugins.rootResAnchor.parseContent($(this._previewArea));
        this._previewArea.show();
        this._button.text(msg_edit);
        this._isPreview = true;
    };

    PreviewController.prototype._hide = function () {
        $.each(this._textAreaFriends, function (i, v) { v.show() });
        this._textArea.show();
        this._previewArea.hide();
        this._button.text(msg_preview);
        this._isPreview = false;
    };

    var previewButton = $('<button>');
    buttonContainer.append(previewButton);
    previewButton.text(msg_preview).addClass('btn');

    var previewArea = $('<pre>').hide();
    previewArea.id = 'preview';
    buttonContainer.after(previewArea);
    var textAreaFriends = [$('#resreferrer'), sizeButton];

    var previewController = new PreviewController(textArea, previewArea, previewButton, textAreaFriends);
    previewButton.click(function (e) { previewController.toggle(e) } );
});
/* -*- coding: utf-8 -*-
 * Popup Res Anchor.
 * Copyright (C) 2005-2014 shinGETsu Project.
 */

shingetsu.initialize(function () {

    function ResAnchor(opt_parameters) {
        $.extend(this, {
            parentResAnchor: null,
            childResAnchors: [],
            container: null,
            aid: null,
            url: null,
            furtherPopup: null,
            destroyTimer: null
        }, opt_parameters);

        if (this.container) {
            this.container
                .on('mouseenter', $.proxy(function (event) {
                    this.cancelDescentDestroyTimer();
                    if (!this.furtherPopup) {
                        this.popup(
                            new shingetsu.plugins.Coordinate(event));
                    }
                }, this))
                .on('mouseleave', $.proxy(function () {
                    this.cancelDestroyTimer();
                    this.destroyTimer = setTimeout($.proxy(function () {
                        if (this.furtherPopup) {
                            this.furtherPopup.destroy();
                            this.furtherPopup = null;
                        }
                    }, this), shingetsu.plugins.Popup.hidingDuration);
                    if (this.furtherPopup) {
                        this.furtherPopup.hide();
                    }
                }, this));
        }
    }
    function popup(coordinate) {
        var that = this;
        var url = $(this.container).prop('href');
        var parentPopup;
        if (this.parentResAnchor) {
            parentPopup = this.parentResAnchor.furtherPopup;
        }
        var furtherPopup = new shingetsu.plugins.Popup({
            parentPopup: parentPopup,
            coordinate: coordinate
        });
        furtherPopup.container
            .on("mouseenter", function () {
                that.cancelDescentDestroyTimer();
            })
            .on("mouseleave", $.proxy(function () {
                that.cancelDestroyTimer();
                that.destroyTimer
                = setTimeout($.proxy(function () {
                    this.destroy();
                    that.furtherPopup = null;
                }, this), shingetsu.plugins.Popup.hidingDuration);
                that.furtherPopup.hide();
                that.refireDescentMouseleave();
            }, furtherPopup));
        if (parentPopup) {
            parentPopup.appendChild(furtherPopup);
        }
        this.furtherPopup = furtherPopup;
        furtherPopup.show(coordinate);
        this.loadContent();
    }
    function loadContent() {
        var html = this.cache[this.aid];
        if (!html) {
            var dt = $('#r' + this.aid);
            var dd = $('#b' + this.aid);
            if (dt.length && dd.length) {
                html = "<dl><dt>" + dt.html() + "</dt><dd>"
                    + dd.html() + "</dd></dl>";
                this.cache[this.aid] = html;
                this.setHtml(html);
                this.parseContent();
            } else {
                this.furtherPopup.setContent('<div>'
                    + ResAnchor.message.loading + '</div>');
                $.ajax({
                    url: this.url + '?ajax=true',
                    dataType: 'html',
                    success: $.proxy(function (html) {
                        this.cache[this.aid] = html;
                        this.setHtml(html);
                        this.parseContent();
                    }, this)
                });
            }
        } else {
            this.setHtml(html);
            this.parseContent();
        }
    }
    function setHtml(html) {
        html = html.replace(new RegExp('</?(input)[^<>]*>', 'ig'), '')
                       .replace(new RegExp('(<br[^<>]*>\\s*)*$', 'i'), '');
        if (html.search(/<dt/) < 0) {
            html = '<div>' + ResAnchor.message.notFound + '</div>';
        }
        this.furtherPopup.setContent(
            $(html).addClass('panel panel-default'));
    }
    function parseContent($container) {
        if (! $container) {
            $container = $(this.furtherPopup.container);
        }
        shingetsu.modifyRecords($container);
        var that = this;
        this.childResAnchors = [];
        $container.find('a').each(function () {
            if (!$(this).hasClass("innerlink")
                && !$(this).hasClass("reclink")) {
                return;
            }
            if (this.href.search(/([0-9a-f]{8})/) <= 0) {
                return;
            }
            var aid = RegExp.$1;
            var resAnchor = new ResAnchor({
                parentResAnchor: that,
                container: $(this),
                aid: aid,
                url: $(this).prop("href")
            });
            that.childResAnchors.push(resAnchor);

            $(this).click(function (e) {
                that.tryJump(e, aid);
            });
        
        });
    }
    function cancelDestroyTimer() {
        clearTimeout(this.destroyTimer);
        if (this.furtherPopup) {
            $(this.furtherPopup.container).stop(true, true).show();
        }
    }
    function cancelDescentDestroyTimer() {
        this.cancelDestroyTimer();
        if (this.parentResAnchor) {
            this.parentResAnchor.cancelDescentDestroyTimer();
        }
    }
    function refireMouseleave() {
        if (this.container) {
            this.container.trigger('mouseleave');
        }
    }
    function refireDescentMouseleave() {
        this.refireMouseleave();
        if (this.parentResAnchor) {
            this.parentResAnchor.refireMouseleave();
        }
    }
    function tryJump(event, id) {
        var root = shingetsu.plugins.rootResAnchor.furtherPopup;
        var i = root.childPopups.length;
        while (i--) {
            root.childPopups[i].destroy();
            root.childPopups.splice(i, 1);
        }
        if (!document.getElementById('r' + id)) {
            return;
        }
        if (event.originalEvent.button === 1) {
            return;
        }
        event.preventDefault();
        $('body').animate({scrollTop: $('#r' + id).offset().top}, 'fast'); 
        location.hash = '#r' + id;
    }

    var message = {
        'en': {
            loading: 'Loading...',
            notFound: 'Error or Not Found'
        },
        'ja': {
            loading: '読み込み中です・・・',
            notFound: 'エラーが発生したか、見つかりません'
        }
    };
    $.extend(ResAnchor, {
        message: message[shingetsu.uiLang] || message['en']
    });
    ResAnchor.prototype = {
        constructor: ResAnchor,
        cache: {},
        popup: popup,
        loadContent: loadContent,
        setHtml: setHtml,
        parseContent: parseContent,
        cancelDestroyTimer: cancelDestroyTimer,
        cancelDescentDestroyTimer: cancelDescentDestroyTimer,
        refireMouseleave: refireMouseleave,
        refireDescentMouseleave: refireDescentMouseleave,
        tryJump: tryJump
    };

    var resAnchor = new ResAnchor();
    resAnchor.furtherPopup = new shingetsu.plugins.Popup({
        container: document.body
    });
    resAnchor.parseContent();
    shingetsu.plugins.rootResAnchor = resAnchor;
});
// save "name", "mail", "signature", "desc_send", "error" in post form.
// original by Anonymous in shinGETsu.
// improved by shiroboushi.
// licenced by public domain.

shingetsu.initialize(function() {
    var saved = parse(localStorage.getItem('recform'));
    if (saved) {
        load(saved);
    }

    function load(saved) {
        $('#postarticle #dopost').prop('checked', saved.dopost);
        $('#error').prop('checked', saved.error);
        $('#name').val(saved.name);
        $('#mail').val(saved.mail);
        $('#passwd').val(saved.passwd);

        $('#postarticle').find('.post-advanced').each(function (i, element) {
            element = $(element);
            if (saved.name && element.find('#name').length > 0) {
                element.removeClass('post-advanced');
            }
            if (saved.mail && element.find('#mail').length > 0) {
                element.removeClass('post-advanced');
            }
            if (saved.passwd && element.find('#passwd').length > 0) {
                element.removeClass('post-advanced');
            }
            if (element.find(':checkbox:not(:checked)').length > 0) {
                element.removeClass('post-advanced');
            }
        });
    }

    function parse(saved) {
        try {
            return JSON.parse(saved);
        } catch (e) {
            return null;
        }
    }

    function save()
    {
        var item = {};
        var form = $('#postarticle');
        item.name = form.find('[name=name]').val();
        item.mail = form.find('[name=mail]').val();
        item.passwd = form.find('[name=passwd]').val();
        item.dopost = form.find('[name=dopost]').prop('checked');
        item.error = form.find('[name=error]').prop('checked');
        localStorage.setItem('recform', JSON.stringify(item));
    }

    $('#postarticle').submit(save);
});
/* -*- coding: utf-8 -*-
 * Advanced form inputs.
 * Copyright (C) 2012 shinGETsu Project.
 */

shingetsu.initialize(function () {
    var msg_show = 'Show detail';
    var msg_hide = 'Hide detail';
    if (shingetsu.uiLang == 'ja') {
        msg_show = '詳細を表示';
        msg_hide = '詳細を隠す';
    }

    function Controller(inputs, button) {
        this._inputs = inputs;
        this._button = button;
        this._isShowing = false;
    }

    Controller.prototype.toggle = function (event) {
        event.preventDefault();
        if (this._isShowing) {
            this.hide('fast');
        } else {
            this.show('fast');
        }
    };

    Controller.prototype.hide = function (speed) {
        this._inputs.hide(speed);
        this._isShowing = false;
        this._button.text(msg_show);
    };

    Controller.prototype.show = function (speed) {
        this._inputs.show(speed);
        this._isShowing = true;
        this._button.text(msg_hide);
    };

    var form = $('#postarticle');

    var button = $('<button>');
    button.addClass('btn');
    form.find('.form-actions').append(button);

    var inputs = form.find('.post-advanced');
    var controller = new Controller(inputs, button);
    button.click(function (e) { controller.toggle(e) } );

    controller.hide(0);
});
