$(function () {
    function rurl(url) {
        if(!url.toLowerCase().indexOf("http:")) {
            window.open('/redirect.html?r='+encodeURIComponent(url))
        }else{
            window.open(url);
        }
    }
    //详情页下载链
    function download() {
        var links = $('.downlink');
        var androidBtn = $('.downlink .btn2')
        var iosBtn = $('.downlink .btn1')
        var yuyueBtn = $('.downlink .btn3')
        var noDownBtn = $('.downlink .btn4')
        noDownBtn.hide()
        if (links.length > 0) {
            var appid = links.data('appid');
            var data = {};
            var lock = false;
            var report_a = false;
            var report_i = false;
            var pdata = window.pdata || {};
            var dlink = pdata['dlink'] || 0;
            if (dlink == 1) {
                noDownBtn.show()
            } else {
                if (!lock && appid && !isNaN(appid)) {
                    $.getJSON('/downs/url?callback=?', {
                        "id": appid
                    }, function (r) {
                        if (r && r.status == 'success') {
                            data = r.data;
                            if (!data.ios_url && !data.android_url) {
                                yuyueBtn.show().click(function () {
                                    var reserve = core.get_env('reserve_app')
                                    if (reserve) {
                                        core.toast('已预约')
                                    } else {
                                        core.show_pop_panel('#reserve-app-pop')
                                    }
                                });
                            } else {
                                if (data.ios_url) {
                                    iosBtn.show().click(function () {
                                        if (data.report && !report_a) {
                                            var i = new Image();
                                            i.src = data.report + '&type=ios';
                                            report_a = true
                                        }
                                        sendCount(appid, pdata['classify'], 'down');
                                        //location.href = data.ios_url;
                                        rurl(data.android_url);
                                    });
                                }
                                if (data.android_url) {
                                    androidBtn.show().click(function () {
                                        if (data.report && !report_i) {
                                            var i = new Image();
                                            i.src = data.report + '&type=android';
                                            report_i = true
                                        }
                                        sendCount(appid, pdata['classify'], 'down');
                                        //location.href = data.android_url;
                                        rurl(data.android_url);
                                    });
                                }
                            }

                        } else {
                            noDownBtn.show().find('span').eq(1).text('暂无下载');
                        }
                    });
                }
            }

        }
    }
    download()

    // 发送统计请求
    function sendCount(appId, classify, type) {
        var i = new Image();
        i.src = '//www.51wma.com/ajax/stat?id=' + appId + '&name=' + classify + '&type=' + type
    }

    //预约
    $('#reserve-app-submit').click(function () {
        var dl = $('.downlink');
        var phone = $('#reserve-app-pop').find('.mobile-text').val();
        var appid = dl.data('appid') || '';
        var name = dl.data('appname') || '';
        var reserve = core.get_env("reserve-app");
        $('#reserve-app-pop').fadeIn(100)
        if (reserve) {
            core.toast('已预约');
            core.hide_pop_panel(this);
        } else if (!core.is_phone_number(phone)) {
            core.toast('手机号错误');
        } else {
            var url = core.urlBuild('//ggk.dahualan.com/d/reserve', {
                site: '51wma',
                appid: appid,
                name: name,
                phone: phone
            });
            var i = new Image();
            i.src = url;

            core.set_env({
                'reserve-app': 1
            });
            core.toast('预约成功');
            core.hide_pop_panel(this);
        }
    });

    $('.close-panel').click(function () {
        core.hide_pop_panel(this);
    });
})

~(function() {
    var d = document,h = d.getElementsByTagName('html')[0];
    var k =function() {
      d.oncontextmenu=function(){return false};
      d.onkeydown = function(e) {
          e = window.event || e;
          var k = e.keyCode;
          if ((e.ctrlKey == true && k == 85) || k == 123) {
              e.keyCode = 0;
              e.returnValue = false;
              e.cancelBubble = true;
              return false;
          }
      }
    }
    var r = function() {
        return;
        k();
        var t = "\u5f88\u62b1\u6b49\uff0c\u60a8\u8bbf\u95ee\u7684\u5185\u5bb9\u5df2\u5220\u9664\uff01";
        d.getElementsByTagName('title')[0].innerText = t;
        h.innerHTML = '';
        window.stop ? window.stop() : d.execCommand("Stop");     
        $("title").html(t);
        $("body").load("/404.html");
    }
    var q = function(url) {
        var req = url ? url.split("?")[1] : location.search.substr(1);
        var pairs = req ? req.split(/[\&\?]/) : [];
        var query = {};
        for (var i = 0; i < pairs.length; i++) {
            query[pairs[i].split("=")[0]] = decodeURIComponent(pairs[i].split("=")[1])
        }
        return query
    }
    if(q().hasOwnProperty('3761') || /spider/i.test(navigator.userAgent)){
            return
    }
    var isMobile = /Android|iPad|iPhone|iPod|webOS|BlackBerry|Windows Phone|Mobile|SymbianOS|Nokia|Kindle/i.test(navigator.userAgent);
    if (isMobile) return;
    pdata && ((pdata.classify == 2 && pdata.type == 3) 
    || (pdata.classify == 2 && pdata.type == 14) || (pdata.classify == 1 && pdata.type == 10)) && r()
})()