function Template(str, data) {
    var fnText = "var __p__=[]; with(obj){__p__.push('" + str.replace(/[\r\t\n]/g, " ").replace(/\'/g, '"').split("<%").join("\t").replace(/((^|%>)[^\t]*)'/g, "$1\r").replace(/\t=(.*?)%>/g, "',$1,'").split("\t").join("');").split("%>").join("__p__.push('").split("\r").join("\\'") + "');}return __p__.join('');";
    var fn = new Function("obj", fnText);
    return fn(data);
}
function makeColorBall(d) {	
    var cls = d.type.indexOf('足彩') === -1 ? 'red': 'small';
    var numArr = d.WareResult.split(':'),
    html = ['<span class="' + cls + '">', numArr[0].replace(/,/g, '</span><span class="' + cls + '">'), '</span>'];
    if (numArr[1]) {
        html = html.concat(['<span class="blue">', numArr[1].replace(/,/g, '</span><span class="blue">'), '</span>']);
    }
    return html.join('');
}
function isLast(key, obj) {
    var temp = [];
    for (var p in obj) {
        temp.push(p);
    }
    return key === temp[temp.length - 1];
}
var caipiao = function() {
    var url = G_dirUrl+'images/caipiao/Ajax_value.asp?v=1',
    tpl = $('#table_template').html(),
    tplMap = {
        '超级大乐透': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=23',
            'date': '一、三、六',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://kaijiang.500wan.com/dlt.shtml',
                '走势': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://trade.500wan.com/dlt/',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=23'
            },
            'type': '体彩'
        },
        '七星彩': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=32',
            'date': '二、五、日',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://kaijiang.500wan.com/qxc.shtml',
                '走势': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://datachart.500wan.com/qxc/zoushi/jbzs.shtml',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=32'
            },
            'type': '体彩'
        },
        '排列五': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=31',
            'date': '每日',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://kaijiang.500wan.com/plw.shtml',
                '走势': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://datachart.500wan.com/plw/zoushi/jbzs.shtml',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=31'
            },
            'type': '体彩'
        },
        '排列三': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=30',
            'date': '每日',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://kaijiang.500wan.com/pls.shtml',
                '走势': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://datachart.500wan.com/pls/zoushi/jbzs.shtml',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=30'
            },
            'type': '体彩'
        },
        '双色球': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=20',
            'date': '二、四、日',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://kaijiang.500wan.com/ssq.shtml',
                '走势': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://datachart.500wan.com/ssq/zoushi/jbzs_redblue.shtml',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=20'
            },
            'type': '福彩'
        },
        '福彩3D': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=28',
            'date': '每日',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://kaijiang.500wan.com/sd.shtml',
                '走势': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://datachart.500wan.com/sd/zoushi/jbzs.shtml',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=28'
            },
            'type': '福彩'
        },
        '七乐彩': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=29',
            'date': '一、三、五',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://kaijiang.500wan.com/qlc.shtml',
                '走势': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://datachart.500wan.com/qlc/zoushi/jbzs_sanf.shtml',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=29'
            },
            'type': '福彩'
        },
        '足彩胜负彩': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=34',
            'date': '一、三、五',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://zx.500wan.com/zc/',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=34'
            },
            'type': '足彩'
        },
        '半全场胜负彩': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=50',
            'date': '一、三、五',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://zx.500wan.com/zc/',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=50'
            },
            'type': '足彩'
        },
        '足彩任选九场': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=35',
            'date': '一、三、五',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://zx.500wan.com/zc/',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=35'
            },
            'type': '足彩'
        },
        '四场进球彩': {
            'link': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=51',
            'date': '一、三、五',
            'opt': {
                '详情': 'http://www.500wan.com/pages/info/public/interfacehezuo.php?regfrom=1616net&tourl=http://zx.500wan.com/zc/',
                '立即购买': 'http://union.500wan.com/pages/interfacehezuo.php?coopid='+G_lm_500wan_s+'&adid=51'
            },
            'type': '足彩'
        }
    };
    return {
        init: function() {
            var lists = $('#cai>li:not(:last)'),
            timer;
            lists.click(function() {
                if (this.className === 'on') return false;
                var index = lists.index(this);
                $('.day').html(index == 2 ? '': '开奖日');
                lists.removeClass('on').eq(index).addClass('on');
                $('#piao>table').hide().eq(index).show();
            }).mouseover(function() {
                var _this = $(this);
                timer = setTimeout(function() {
                    _this.click();
                },
                300);
            }).mouseout(function() {
                clearTimeout(timer);
            });
            this.getData();
        },
        getData: function() {
            var _this = this;
            $.getJSON(url,
            function(data) {
                data = $.extend(true, tplMap, data);
                var result = {};
                for (var p in data) {
                    var type = data[p]['type'];
                    if (type) {
                        if (!result[type]) result[type] = {};
                        result[type][p] = data[p];
                    }
                }
                _this.renderer(result);
            });
        },
        renderer: function(d) {
            d = Template(tpl, {
                data: d
            });
            $('#piao').html(d).find('table').hide().eq(0).show();
        }
    }
} ();
parent === self ? caipiao.init() : parent.J.add('caipiao', caipiao, window);