var ResultList = (function(){
    var url = '';
    var resulturl = '';
    var time = 0;
    var page = 1;

    var getGroup6 = function(n) {
        var group = {
            A: ['01', '02', '03', '04', '05', '06', '07'],
            B: ['08', '09', '10', '11', '12', '13', '14'],
            C: ['15', '16', '17', '18', '19', '20', '21'],
            D: ['22', '23', '24', '25', '26', '27', '28'],
            E: ['29', '30', '31', '32', '33', '34', '35'],
            F: ['36', '37', '38', '39', '40', '41', '42', '43']
        }
        for (var name in group) {
            for (var i in group[name]) {
                if (group[name][i] == n) {
                    return name;
                }
            }
        }
        return '';
    }
    var getGroup11 = function(n) {
        var group = {
            A: ['01', '02', '03', '04'],
            B: ['05', '06', '07', '08'],
            C: ['09', '10', '11', '12'],
            D: ['13', '14', '15', '16'],
            E: ['17', '18', '19', '20'],
            F: ['21', '22', '23', '24'],
            G: ['25', '26', '27', '28'],
            H: ['29', '30', '31', '32'],
            I: ['33', '34', '35', '36'],
            J: ['37', '38', '39', '40'],
            K: ['41', '42', '43']
        }
        for (var name in group) {
            for (var i in group[name]) {
                if (group[name][i] == n) {
                    return name;
                }
            }
        }
        return '';
    }


    return {
        setUrl : function(v){
            url = v;
        },
        setResultUrl : function(v){
            resulturl = v;
        },
        setPage : function(v){
            page = v;
            ResultList.getData(true);
        },
        goBack : function(v){
            page--;
            if (page < 1) {
                page = 1;
            }
            ResultList.getData(true);
        },
        goNext : function(v){
            page++;
            ResultList.getData(true);
        },
        selectNums : function(){
            var num = document.getElementById('num');
            if(num.checked == true){
                ResultList.getData();
            } else {
                ResultList.getData(true);
            }
        },
        selectSet : function(){
            var set = document.getElementById('set');
            if(set.checked == true){
                ResultList.getData();
            } else {
                ResultList.getData(true);
            }
        },
        getData : function(reset){
            var now = (new Date()).getTime();
            if (now - time < 400) {
                return false;
            }
            time = now;

            var numlist = document.getElementById('numlist');
            var setlist = document.getElementById('setlist');

            var nums = [];
            for(i = 0; i < numlist.length; i++){
                if (numlist.options[i].selected) nums.push(numlist.options[i].text);
            }
            var numlists = nums.join(",");

            var setballs = [];
            for(i = 0; i < setlist.length; i++){
                if (setlist.options[i].selected) setballs.push(setlist.options[i].text);
            }
            var setball = setballs.join(",");

            if (!reset) {
                page = 1;
            }

            var param = '';

            param += 'limit=' + document.getElementById('limit').value;
            param += '&page=' + page;
            param += '&order=' + document.getElementById('order').value;
            param += '&order_type=' + document.getElementById('order_type').value;
            param += '&set_type=' + document.getElementById('set_type').value;
            param += '&stage=' + document.getElementById('stage').value;
            param += '&week=' + document.getElementById('week').value;

            param += '&odd=' + document.getElementById('odd').checked;
            param += '&co=' + document.getElementById('co').checked;
            param += '&max=' + document.getElementById('max').checked;
            param += '&continue=' + document.getElementById('continue').checked;

            param += '&num=' + document.getElementById('num').checked;
            param += '&set=' + document.getElementById('set').checked;
            param += '&numlist=' + numlists;
            param += '&setball=' + setball;

            $.ajax({
                url: url,
                async: false,
                dataType: 'json',
                data: param,
                beforeSend : function (){
                    $('#display').html('<img src="/images/loading.gif" />データ取得中…');
                },
                error : function(){
                    alert('エラーが発生しました。');
                    $('#display').html('エラーが発生しました。');
                },
                success: function (data) {

                    var html = '<table id="resultlist">';
                    html += '<colgroup class="result_id"></colgroup>';
                    html += '<colgroup class="result_n1"></colgroup>';
                    html += '<colgroup class="result_n2"></colgroup>';
                    html += '<colgroup class="result_n3"></colgroup>';
                    html += '<colgroup class="result_n4"></colgroup>';
                    html += '<colgroup class="result_n5"></colgroup>';
                    html += '<colgroup class="result_n6"></colgroup>';
                    html += '<colgroup class="result_nbo"></colgroup>';
                    html += '<colgroup class="result_set"></colgroup>'
                    html += '<colgroup class="result_sum"></colgroup>';
                    html += '<tr>';
                    html += '<th>回</th>';
                    html += '<th colspan="6">本数字</th>';
                    html += '<th>ボ数字</th>';
                    html += '<th>セット球</th>';
                    html += '<th>合計</th>';
                    html += '</tr>';

                    var status;
                    if (data.length > 0) {
                        status = data[0];
                        delete(data[0]);

                        var view_type = $('#view_type').val();
                        var num;
                        var nbo;

                        var co;
                        var max;

                        for (var i in data) {
                            switch (view_type) {
                            case 'order':
                                num = [data[i].n1, data[i].n2, data[i].n3, data[i].n4, data[i].n5, data[i].n6];
                                nbo = data[i].nbo;
                                break;
                            case 'group6':
                                num = [data[i].n1, data[i].n2, data[i].n3, data[i].n4, data[i].n5, data[i].n6];
                                num.sort();
                                num[0] = getGroup6(num[0]);
                                num[1] = getGroup6(num[1]);
                                num[2] = getGroup6(num[2]);
                                num[3] = getGroup6(num[3]);
                                num[4] = getGroup6(num[4]);
                                num[5] = getGroup6(num[5]);
                                nbo = getGroup6(data[i].nbo);
                                break;
                            case 'group11':
                                num = [data[i].n1, data[i].n2, data[i].n3, data[i].n4, data[i].n5, data[i].n6];
                                num.sort();
                                num[0] = getGroup11(num[0]);
                                num[1] = getGroup11(num[1]);
                                num[2] = getGroup11(num[2]);
                                num[3] = getGroup11(num[3]);
                                num[4] = getGroup11(num[4]);
                                num[5] = getGroup11(num[5]);
                                nbo = getGroup11(data[i].nbo);
                                break;
                            case 'normal':
                            default:
                                num = [data[i].n1, data[i].n2, data[i].n3, data[i].n4, data[i].n5, data[i].n6];
                                nbo = data[i].nbo;
                                num.sort();
                                break;
                            }

                            co = data[i].co > 0 ? 1 : 0;
                            max = (data[i].m1 == 200000000 || data[i].m1 == 400000000) ? 1 : 0;

                            html += '<tr>';
                            html += '<td class="time"><a href="' + resulturl + '/' + data[i].id + '">第' + data[i].id + '回</a><input type="hidden" name="co" value="' + co + '"><input type="hidden" name="max" value="' + max + '"></td>';
                            html += '<td class="n1">' + num[0] + '</td>';
                            html += '<td class="n2">' + num[1] + '</td>';
                            html += '<td class="n3">' + num[2] + '</td>';
                            html += '<td class="n4">' + num[3] + '</td>';
                            html += '<td class="n5">' + num[4] + '</td>';
                            html += '<td class="n6">' + num[5] + '</td>';
                            html += '<td class="nbo">' + nbo + '</td>';
                            html += '<td class="setball">' + data[i].set + '</td>';
                            html += '<td class="sum">' + data[i].sum + '</td>';
                            html += '</tr>';
                        }
                    } else {
                        html += '<tr>';
                        html += '<td colspan="10">該当する結果がありません。</td>';
                        html += '</tr>';
                    }
                    html += '</table>';

                    if (status) {
                        var pager = '<table class="pager"><tr>';
                        if (status.page != 1) {
                            pager += '<td class="prev"><input type="button" id="back" value="最初" onClick="ResultList.setPage(1);this.blur()"></td>';
                        } else {
                            pager += '<td class="prev"></td>';
                        }
                        if (status.page != 1) {
                            pager += '<td class="prev"><input type="button" id="back" value="前へ" onClick="ResultList.goBack();this.blur()"></td>';
                        } else {
                            pager += '<td class="prev"></td>';
                        }
                        if (status.all != status.page) {
                            pager += '<td class="next"><input type="button" id="next" value="次へ" onClick="ResultList.goNext();this.blur()"></td>';
                        } else {
                            pager += '<td class="next"></td>';
                        }
                        if (status.all != status.page) {
                            pager += '<td class="next"><input type="button" id="next" value="最後" onClick="ResultList.setPage(' + status.all + ');this.blur()"></td>';
                        } else {
                            pager += '<td class="next"></td>';
                        }
                        pager += '</tr></table>';
                        html = pager + html + pager;
                    }

                    $('#display').css({opacity: 0});

                    $('#display').html(html);

                    $('#display').animate({opacity: 1}, 800);

                    if ($('#odd:checked').val()) {
                        $('#resultlist .n1').each(function(){
                            if ($(this).text() % 2 == 1) {
                                $(this).addClass('odd');
                            }
                        });
                        $('#resultlist .n2').each(function(){
                            if ($(this).text() % 2 == 1) {
                                $(this).addClass('odd');
                            }
                        });
                        $('#resultlist .n3').each(function(){
                            if ($(this).text() % 2 == 1) {
                                $(this).addClass('odd');
                            }
                        });
                        $('#resultlist .n4').each(function(){
                            if ($(this).text() % 2 == 1) {
                                $(this).addClass('odd');
                            }
                        });
                        $('#resultlist .n5').each(function(){
                            if ($(this).text() % 2 == 1) {
                                $(this).addClass('odd');
                            }
                        });
                        $('#resultlist .n6').each(function(){
                            if ($(this).text() % 2 == 1) {
                                $(this).addClass('odd');
                            }
                        });
                        $('#resultlist .nbo').each(function(){
                            if ($(this).text() % 2 == 1) {
                                $(this).addClass('odd');
                            }
                        });
                    }

                    if ($('#co:checked').val()) {
                        $('#resultlist .time input:hidden[name="co"][value="1"]').parent().addClass('co');
                    }

                    if ($('#max:checked').val()) {
                        $('#resultlist .time input:hidden[name="max"][value="1"]').parent().addClass('money_max');
                    }

                    if ($('#continue:checked').val()) {
                        var loop = $('#resultlist .time').size();
                        var n;
                        for (var i = 0; i < loop; i++) {
                            n = $('#resultlist .n1:eq(' + i + ')').text();
                            if (n == $('#resultlist .n1:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i + 1) + ')').text()) {

                                $('#resultlist .n1:eq(' + i + ')').addClass('con');
                            }
                            if (n == $('#resultlist .n1:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i - 1) + ')').text()) {

                                $('#resultlist .n1:eq(' + i + ')').addClass('con');
                            }
                            n = $('#resultlist .n2:eq(' + i + ')').text();
                            if (n == $('#resultlist .n1:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i + 1) + ')').text()) {

                                $('#resultlist .n2:eq(' + i + ')').addClass('con');
                            }
                            if (n == $('#resultlist .n1:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i - 1) + ')').text()) {

                                $('#resultlist .n2:eq(' + i + ')').addClass('con');
                            }
                            n = $('#resultlist .n3:eq(' + i + ')').text();
                            if (n == $('#resultlist .n1:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i + 1) + ')').text()) {

                                $('#resultlist .n3:eq(' + i + ')').addClass('con');
                            }
                            if (n == $('#resultlist .n1:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i - 1) + ')').text()) {

                                $('#resultlist .n3:eq(' + i + ')').addClass('con');
                            }
                            n = $('#resultlist .n4:eq(' + i + ')').text();
                            if (n == $('#resultlist .n1:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i + 1) + ')').text()) {

                                $('#resultlist .n4:eq(' + i + ')').addClass('con');
                            }
                            if (n == $('#resultlist .n1:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i - 1) + ')').text()) {

                                $('#resultlist .n4:eq(' + i + ')').addClass('con');
                            }
                            n = $('#resultlist .n5:eq(' + i + ')').text();
                            if (n == $('#resultlist .n1:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i + 1) + ')').text()) {

                                $('#resultlist .n5:eq(' + i + ')').addClass('con');
                            }
                            if (n == $('#resultlist .n1:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i - 1) + ')').text()) {

                                $('#resultlist .n5:eq(' + i + ')').addClass('con');
                            }
                            n = $('#resultlist .n6:eq(' + i + ')').text();
                            if (n == $('#resultlist .n1:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i + 1) + ')').text()) {

                                $('#resultlist .n6:eq(' + i + ')').addClass('con');
                            }
                            if (n == $('#resultlist .n1:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i - 1) + ')').text()) {

                                $('#resultlist .n6:eq(' + i + ')').addClass('con');
                            }
                            n = $('#resultlist .nbo:eq(' + i + ')').text();
                            if (n == $('#resultlist .n1:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i + 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i + 1) + ')').text()) {

                                $('#resultlist .nbo:eq(' + i + ')').addClass('con');
                            }
                            if (n == $('#resultlist .n1:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n2:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n3:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n4:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n5:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .n6:eq(' + (i - 1) + ')').text() ||
                                n == $('#resultlist .nbo:eq(' + (i - 1) + ')').text()) {

                                $('#resultlist .nbo:eq(' + i + ')').addClass('con');
                            }
                        }

                    }

                    if (setballs.length > 0) {
                        $('#resultlist .setball').each(function(){
                            for (var i in setballs) {
                                if ($(this).text() == setballs[i]) {
                                    $(this).addClass('set');
                                    break;
                                }
                            }
                        });
                    }

                    if (nums.length > 0) {
                        $('#resultlist .n1').each(function(){
                            for (var i in nums) {
                                if ($(this).text() == nums[i]) {
                                    $(this).addClass('num');
                                    break;
                                }
                            }
                        });
                        $('#resultlist .n2').each(function(){
                            for (var i in nums) {
                                if ($(this).text() == nums[i]) {
                                    $(this).addClass('num');
                                    break;
                                }
                            }
                        });
                        $('#resultlist .n3').each(function(){
                            for (var i in nums) {
                                if ($(this).text() == nums[i]) {
                                    $(this).addClass('num');
                                    break;
                                }
                            }
                        });
                        $('#resultlist .n4').each(function(){
                            for (var i in nums) {
                                if ($(this).text() == nums[i]) {
                                    $(this).addClass('num');
                                    break;
                                }
                            }
                        });
                        $('#resultlist .n5').each(function(){
                            for (var i in nums) {
                                if ($(this).text() == nums[i]) {
                                    $(this).addClass('num');
                                    break;
                                }
                            }
                        });
                        $('#resultlist .n6').each(function(){
                            for (var i in nums) {
                                if ($(this).text() == nums[i]) {
                                    $(this).addClass('num');
                                    break;
                                }
                            }
                        });
                        $('#resultlist .nbo').each(function(){
                            for (var i in nums) {
                                if ($(this).text() == nums[i]) {
                                    $(this).addClass('num');
                                    break;
                                }
                            }
                        });
                    }

                }
            });
        }
    }
})();

