Event.observe(window, 'load', function() {
    noFlicker();
    browserSize();
    checkboxStyleReset();
    listLastElement.init('latestbox', 'ouchibox', 'arrange-list-new', 'arrange-list-ranking', 'lounge-list');
    swapImg.init('-o', ['submitbox', 'btnbox', 'btn-detail', '#formbox h1', '#formbox p', '#loginbox div', '#agree-alert .btnbox', '.sort-btn']);
    popup.init('help', 'nav-footer');
}, false);


(function() {
    return bbsCommentAgree = {
        init: function() {
        },
        call: function(id, btn) {
            this.btn = btn;
            this.btnParentNode = this.btn.parentNode;
            this.url = '/forum/api/agree/' + id;
            var t = (new Date()).getTime();
            new Ajax.Request(this.url, {
                method: "get",
                parameters: "cache=" + t,
                onLoading: this.loading.bindAsEventListener(this),
                onSuccess: this.success.bindAsEventListener(this),
                onFailure: this.failure.bindAsEventListener(this)
            });
        },
        loading: function() {
            this.btnParentNode.removeClassName('btnbox');
            this.btnParentNode.stopObserving();
            this.btnParentNode.innerHTML = '<img src="/img/main/btn-lounge-sending.gif" width="63" height="23" alt="送信中" />';
        },
        success: function(httpObj) {
            var req = parseInt(httpObj.responseText);
            if (req === 0) { this.failure() };
            if (req === 1) {
                this.btnParentNode.innerHTML = '<img src="/img/main/btn-lounge-agreed.gif" width="63" height="23" alt="同感済" />';
                this.valueIncrement();
            }
        },
        failure: function(httpObj) {
            $$('body')[0].insert('<div id="agree-alert"><p>システムエラーが発生しました。<br />「OK」ボタンをクリックしてください。</p><div class="btnbox"><img src="/img/main/btn-loungeok.gif" width="102" height="23" alt="OK" onclick="bbsCommentAgree.pageReload()" /></div></div>');
            var scrollH = document.viewport.getScrollOffsets()['top'];
            var alertObj = $('agree-alert');
            var alertW = alertObj.offsetWidth;
            var alertH = alertObj.offsetHeight;
            alertObj.style.left = ((browserW - alertW) / 2) + 'px';
            alertObj.style.top = ((browserH - alertH) / 2 + scrollH - 10) + 'px';
        },
        pageReload: function() {
            location.reload(true);
        },
        valueIncrement: function() {
            var parentNode = this.btnParentNode.tagName;
            var node = this.btnParentNode.parentNode.getElementsByClassName('agree-value')[0];
            if (node) {
                var currentValue = parseInt(node.firstChild.nodeValue);
                node.innerHTML = ++currentValue;
            }
        }
    }
})();





function noFlicker() {
    try { document.execCommand('BackgroundImageCache', false, true) } catch(e) {};
}

function checkboxStyleReset() {
    var checkboxNodes = $$('input[type="checkbox"]');
    checkboxNodes.each(function(elm) {
        elm.setStyle('width: auto;');
    });
}

function listLastElement() {
    this.init = function() {
        this.options(arguments);
    };
    this.evt = function(obj, num) {
        var lists = obj.getElementsByTagName('li');
        this.add(lists[lists.length - 1]);
    };
    this.add = function(elm) {
        Element.addClassName(elm, 'last-elm');
    };
}
listLastElement.prototype.options = options;
listLastElement.prototype.execute = execute;
var listLastElement = new listLastElement();


function popupWin() {
    this.init = function(id) {
        this.w = 617;
        this.options(arguments);
    };
    this.evt = function(obj) {
        Event.observe(obj, 'click', click, false);
    };
    this.open = function(u, w, h, options, uri) {
        var wStr = (typeof w == 'number') ? 'width=' + w : '';
        var hStr = (typeof h == 'number') ? ',height=' + h + ',' : '';
        var winStr = u.replace(/[^a-zA-Z0-9]/g, '');
        var optStr = (options) ? options : '';
        var options = wStr + hStr + optStr;
        var win = window.open(uri ? uri : u, winStr, options);
        try { win.focus() } catch(e) {};
    };
    var click = function(e) {
        var t = getTarget(e);
        if ((typeof t.onclick != 'function') && (typeof t.href == 'string') && (t.hostname == location.hostname)) {
            this.open(t.href);
            e.returnValue = false;
            e.preventDefault();
        }
    }.bindAsEventListener(this);
};
popupWin.prototype.options = options;
popupWin.prototype.execute = execute;
var popup = new popupWin();


function memberWin(flag) {
    var w = 990,h = 618;
    var top = Math.floor((screenH - h) / 2) - 50;
    var left = Math.floor((screenW - w) / 2);
    var opt = 'top=' + top + ',left=' + left + ',toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,directories=no';
    popup.open('/member/', w, h, opt);
    if (flag) { location.href = '/top.html' };
}

// !!! login
function loginWin(uri) {
    var w = 990,h = 618;
    var top = Math.floor((screenH - h) / 2) - 50;
    var left = Math.floor((screenW - w) / 2);
    var opt = 'top=' + top + ',left=' + left + ',toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,directories=no';
    popup.open('member', w, h, opt, uri);
}

function helpWin(num) {
    if (!num) {
        popup.open('/help/');
    } else {
        var n = new String(num + 100);
        n = n.substring(1, n.length);
        popup.open('/help/' + n + '.html');
    }
}

function contactWin() {
    popup.open('/enquete/form/contact');
}

function policybbsWin() {
    popup.open('/memberpolicy/bbs.html');
}

function ouchiWin() {
    var ow = window.open('/my', 'ouchiwin');
    try { ow.focus() } catch(e) {};
}

function smilesWin() {
    try {
        parent.opener.focus();
    } catch(e) {
        window.open('/');
    }
}


function swapImg() {
    this.init = function(suffix, ids) {
        this.suffix = suffix;
        this.ids = ids;
        this.options(this.ids);
    };
    this.evt = function(obj) {
        Event.observe(obj, 'mouseover', function(evt) {
            click.init(evt, 'over');
        }, false);
        Event.observe(obj, 'mouseout', function(evt) {
            click.init(evt, 'out');
        }, false);
    };
    var click = {
        init: function(e, type) {
            this.t = getTarget(e);
            this.srcAttr = this.t.src;
            if (type == 'over') this.over(this.srcAttr);
            if (type == 'out') this.out(this.srcAttr);
        },
        over: function(attr) {
            if (typeof attr == 'string') {
                this.t.src = this.getFileName(attr) + '-o' + '.' + this.getExt(attr);
                if (/-o-o/.test(this.t.src)) {
                    this.out(this.t.src);
                }
            }
        },
        out: function(attr) {
            if (typeof attr == 'string') {
                this.t.src = this.getFileName(attr).replace('-o', '') + '.' + this.getExt(attr);
            }
        },
        detectExt: function(str) {
            var dot = str.lastIndexOf('.');
            return dot;
        },
        getFileName: function(str) {
            return str.slice(0, this.detectExt(str));
        },
        getExt: function(str) {
            return str.slice(this.detectExt(str) + 1, str.length);
        }
    }
};
swapImg.prototype.options = options;
swapImg.prototype.execute = execute;
var swapImg = new swapImg();


function options(opt) {
    if (opt.length > 0) {
        opt = Array.prototype.slice.call(opt);
        opt.each(function(obj) {
            this.execute(obj);
        }.bindAsEventListener(this));
    }
};

function execute(obj) {
    var idObj = $(obj);
    if (idObj) {
        this.evt(idObj);
    } else {
        var elm;
        if (/(#|\.)/.test(obj)) {
            elm = $$(obj);
        } else {
            var classObj = document.getElementsByClassName(obj);
            elm = Array.prototype.slice.call(classObj);
        };
        if (elm.length > 0) {
            elm.each(function(obj) {
                this.evt(obj);
            }.bindAsEventListener(this));
        }
    };
};

function getTarget(evt) {
    var evt = evt || window.event;
    var target = evt.target || evt.srcElement;
    if (target.nodeType == 3) {
        target = target.parentNode;
    }
    return target;
};

function browserSize() {
    if (document.body.clientHeight) {
        browserW = document.body.clientWidth;
        browserH = document.body.clientHeight;
    }
    if (document.documentElement.clientHeight) {
        browserW = document.documentElement.clientWidth;
        browserH = document.documentElement.clientHeight;
    }
    if (window.innerHeight) {
        browserW = window.innerWidth;
        browserH = window.innerHeight;
    }
    
    screenW = screen.width;
    screenH = screen.height;
}


