function _init(pmatch, pmsg) {
    rp = 10;
    _hvrimg = {
        prev: '/images/match/last_over.gif',
        next: '/images/match/next_over.gif'
    };
    ptotal = {
        top: Math.min(rp, pmatch),
        recent: Math.min(rp, pmatch),
        msg: pmsg
    };
    cp = {
        top: 1,
        recent: 1,
        msg: 1
    };

    hvrimg = {
        top_prev: '',
        top_next: pmatch > cp['top'] ? _hvrimg.next : '',
        recent_prev: '',
        recent_next: pmatch > cp['recent'] ? _hvrimg.next : '',
        msg_prev: '',
        msg_next: pmsg > cp['msg'] ? _hvrimg.next : ''
    };
    $(document).ready(function() {
        $('img[id]').bind('mouseover', function() {
            if (hvrimg[this.id])
                this.src = hvrimg[this.id];
        }).bind('click', function() {
            var type, dir, what;
            if (hvrimg[this.id]) {
                what = this.id.split('_');
                type = what[0];
                dir = what[1];
                if (dir == 'next' && cp[type] < ptotal[type])
                    p = parseInt(cp[type]) + 1;
                else if (dir == 'prev' && cp[type] > 1)
                    p = parseInt(cp[type]) - 1;
                repage(type, p);
                loadContent(type, p);
            }
        });
        for (type in cp)
            repage(type, 1);
    });
}

function repage(type, pgno) {
    cp[type] = parseInt(pgno);
    prev = cp[type] > 1;
    next = cp[type] < ptotal[type];
    hvrimg[type + '_prev'] = prev ? _hvrimg.prev : '';
    hvrimg[type + '_next'] = next ? _hvrimg.next : '';
    $('img#' + type + '_prev').css('cursor', prev ? 'pointer' : 'auto');
    $('img#' + type + '_next').css('cursor', next ? 'pointer' : 'auto');
    rebar(type, pgno);
}

function rebar(type, pgno) {
    var ps, pe, i, html = '';
    if (pgno % rp == 0)
        ps = pgno - rp + 1;
    else
        ps = pgno - pgno % rp + 1
    pe = Math.min(ps + rp - 1, ptotal[type]);
    for (i = ps ; i <= pe && i <= ptotal[type] ; ++i)
        html += '<a href="#' + type + '_p' + i + '" rel="paginate" onclick="loadContent(\'' + type + '\', ' + i + ');repage(\'' + type + '\', \'' + i + '\');return false;">' + ('<span' + (i == cp[type] ? ' style="font-weight: bold; text-decoration: underline;"' : '') + '>' + i + '</span>') + '</a>' + (i == pe ? '' : ' ');
    $('#' + type + '_pbar').html(html);
}

function loadContent(type, pgno) {
    var ani, data = {
        ajax: 'jquery.load',
        action: 'content',
        page: pgno,
        what: type
    };
    $('#' + type + '_pbar').css('cursor', 'wait');
    ani = new YAHOO.util.Anim(document.getElementById(type + '_table'), {
        opacity: { to: 0 }
    }, 1, YAHOO.util.Easing.easeOut);
    ani.onComplete.subscribe(function() {
        $('#' + type + '_table').load('/ajax/match.php' + location.search, data, function() {
            var an;
            $('#' + type + '_pbar').css('cursor', 'auto');
            an = new YAHOO.util.Anim(type + '_table', {
                opacity: { to: 1 }
            }, 1, YAHOO.util.Easing.easeOut);
            an.animate();
        });
    });
    ani.animate();
}

function submitMessage() {
    var guest_name, guest_name_custom, msg_content;
    guest_name = $('#guest_name').val();
    guest_name_custom = $('#guest_id0').attr('checked') ? 1 : 0;
    if (guest_name_custom && !guest_name) {
        alert('請輸入留言暱稱');
        return false;
    }
    msg_content = $('#msg_content').val();
    if (!msg_content) {
        alert('請輸入留言內容');
        return false;
    }
    var data = {
        ajax: 'jquery.post',
        action: 'message',
        guest_id: $('#guest_id').val(),
        guest_name: guest_name,
        msg_content: msg_content,
        guest_name_custom: guest_name_custom
    };
    $.post('/ajax/match.php' + location.search, data, _submitted);
    return true;
}

function _submitted(_) {
    var r;
    eval('r=' + _);
    if (!r.success) {
        alert('留言失敗');
        return false;
    }
    $('#guest_id').attr('checked', true);
    $('#guest_name').val('');
    $('#msg_content').val('');
    loadContent('msg', cp['msg']);
    repage('msg', cp['msg']);

    return true;
}

function deleteMatchRecord(id) {
    var data = {
        ajax: 'jquery.post',
        action: 'delrec',
        id: id
    };
    $.post('/ajax/match.php' + location.search, data, _deleted);
    return true;
}

function _deleted(_) {
    var r;
    eval('r=' + _);
    if (!r.success) {
        alert('刪除失敗');
        return false;
    }
    loadContent('top', cp['top']);
    loadContent('recent', cp['recent']);
}

function deleteMatchMessage(id) {
    var data = {
        ajax: 'jquery.post',
        action: 'delmsg',
        id: id
    };
    $.post('/ajax/match.php' + location.search, data, _mdeleted);
    return true;
}

function _mdeleted(_) {
    var r;
    eval('r=' + _);
    if (!r.success) {
        alert('刪除失敗');
        return false;
    }
    loadContent('msg', cp['msg']);
}

var _hvrimg, hvrimg, cp, ptotal, rp;
var confirmDeleteMessage = '是否確定刪除？';
