$(function() {
    var report = {
        getBrowserInfo : function () {
            var explorer = window.navigator.userAgent.toLowerCase();
            //ie
            if (explorer.indexOf("msie") >= 0) {
                var ver = explorer.match(/msie ([\d.]+)/)[1];
                return { type: "IE", version: ver };
            } else if (explorer.indexOf("trident") >= 0) {
                return { type: "IE", version: 'unknow' };
            }
    
            //firefox
            else if (explorer.indexOf("firefox") >= 0) {
                var ver = explorer.match(/firefox\/([\d.]+)/)[1];
                return { type: "Firefox", version: ver };
            }
            //Chrome
            else if (explorer.indexOf("chrome") >= 0) {
                var ver = explorer.match(/chrome\/([\d.]+)/)[1];
                return { type: "Chrome", version: ver };
            }
            //Opera
            else if (explorer.indexOf("opera") >= 0) {
                var ver = explorer.match(/opera.([\d.]+)/)[1];
                return { type: "Opera", version: ver };
            }
            //Safari
            else if (explorer.indexOf("Safari") >= 0) {
                var ver = explorer.match(/version\/([\d.]+)/)[1];
                return { type: "Safari", version: ver };
            } else {
                return { type: "unknow", version: "unknow" };
            }
        },
    
        /**上传数据 
         * reportType : 1为展示 2为点击
        */
        reportData : function (type, param) {
            var reportType = type,
                reportUrl = 'http://dastat.duowan.com/?r=api/stat',
    
                browserInfo = this.getBrowserInfo(),
                data = {
                    loc: param.locid,
                    pid: param.pid,
                    type: reportType,
                    url: window.location.href,
                    os: window.navigator.platform,
                    browser: browserInfo.type + browserInfo.version,
                    px: window.screen.width + "*" + window.screen.height
                };
    
    
            if (typeof data.loc != 'undefined' && typeof data.pid != 'undefined' && data.type) {
                this.sendData(reportUrl, data);
            }
    
        },
    
        sendData : function (url, data) {
            var img = new Image();
    
            for (var key in data) {
                url += "&" + key + "=" + data[key];
            }
    
            img.src = url;
    
            img = null;
        },
    
        /**绑定点击上报数据事件 
         * ele : 被点击的选择器
        **/
        bindEvent: function(ele) {
            var self = this
            $('body').on('click',ele, function() {
                var param = {
                    locid: $(this).attr('locid'),
                    pid: $(this).attr('pid')
                }
                self.reportData(2,param)
            })
        }
    }
    
    function getURLParam(name) {
		var value = location.search.match(new RegExp("[?&]" + name + "=([^&]*)(&?)", "i"));
		return value ? decodeURIComponent(value[1]) : value;
    }
    
    var listTpl = __inline("../tpl/yylive.tmpl")
    var locid1, locid2;
    var gtype = getURLParam('gtype')
    if(gtype == 'lol') {
        locid1 = 154
        locid2 = 155
    } else if(gtype == 'dnf') {
        locid1 = 156
        locid2 = 157
    }

    var boxtype = getURLParam('boxtype') 
    if(boxtype == 'newdnf') {
        $('.o-wrap').addClass("newdnf")
    }

    $.ajax({
        url: 'http://da.duowan.com/loc/'+locid1,
        type: 'GET',
        dataType: 'jsonp',
        success: function(ret) {
            $(".loading-tips").hide()
            var hasBanner = false
            if(ret.extraJson && ret.extraJson.list.length > 0) {
                hasBanner = true
                var item = ret.extraJson.list[0]
                var img = item.image ? item.image : item.img
                $(".yylive-banner").html('<a href="'+item.url+'" title="'+item.title+'" locid="'+ret.locid+'" pid="'+ret.pid+'" target="_blank"><img src="'+img+'"/></a>')

                report.reportData(1,{locid: ret.locid,pid: ret.pid})
                report.bindEvent('.yylive-banner a')
                
            }
            $.ajax({
                url: 'http://da.duowan.com/loc/'+locid2,
                type: 'GET',
                dataType: 'jsonp',
                success: function(ret2) {
                    $(".loading-tips").hide()
                    var dom = listTpl({
                        list: hasBanner ? ret2.extraJson.list.slice(0, 8) : ret2.extraJson.list,
                        locid: ret2.locid,
                        pid: ret2.pid,
                        more: ret2.extraJson.more
                    })
                    $(".yylive-list").html(dom)
                    if(!hasBanner) {
                        $(".yylive-list").addClass("tp2")
                    }
                    report.reportData(1,{locid: ret2.locid,pid: ret2.pid})
                    report.bindEvent('.yylive-list a')
                    
                }
            })
        }
    })
    
})