function WX_share(b,c,d,e){var f=navigator.userAgent,g=-1<f.indexOf("Android")||-1<f.indexOf("Adr");f.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)&&(function(a){window.WebViewJavascriptBridge?a(WebViewJavascriptBridge):document.addEventListener("WebViewJavascriptBridgeReady",function(){a(WebViewJavascriptBridge)},!1)}(function(a){a.init(function(a,b){alert("Received message: "+a);b&&b("Right back atcha")});a.send({url:b,typeid:8,depict:c,title:d,picture:e});a.send("Please respond to this",function(a){alert("Javascript got its response",
a)})}),window.webkit.messageHandlers.JS_Call_OC_login.postMessage({url:b,typeid:8,depict:c,title:d,picture:e}));g&&Android.share(b,c,d,e)};


;(function($, window, document, undefined) {
    function regist(mName, module) {
        if (typeof window.brickjs === "undefined") {
            window.brickjs = {};
        }
        brickjs[mName] = module;
    }
;/** 
 * @file 微信,微博分享
 * @author jie.qin@condenast.com.cn
 * @version 0.1.0
 * @requires http://js.selfimg.com.cn/jquery/jquery-1.7.1.min.js|http://js.selfimg.com.cn/gallery/zepto/1.1.4/zepto.min.js
 * @requires http://js.selfimg.com.cn/lib/jweixin-1.0.0.js
 * @example <a href="../mshare/examples/demo.html">demo.html</a>
 * @see http://js.selfimg.com.cn/brickjs/mshare/0.1.0/mshare.min.js
 */
var isWeixin = navigator.userAgent.toLowerCase().indexOf("micromessenger") > -1;
//isWeixin = true;
/** 
 * 微信,微博分享定义组件。(微博分享功能,可以在按钮上配置属性实现动态替换分享属性,属性名称:data-desc,data-sUrl,data-imgUrl)
 * @class
 * @author jie.qin@condenast.com.cn
 * @version 0.1.0
 * @requires http://js.selfimg.com.cn/jquery/jquery-1.7.1.min.js|http://js.selfimg.com.cn/gallery/zepto/1.1.4/zepto.min.js
 * @requires http://js.selfimg.com.cn/lib/jweixin-1.0.0.js
 * @example <a href="../mshare/examples/demo.html">demo.html</a>
 * @see http://js.selfimg.com.cn/brickjs/mshare/0.1.0/mshare.min.js
 * @param {Object} cfgs 配置项
 * @param {String} [cfgs.site=""] 1、vogue 2、self 3、gq 4、adstyle 5、cnt 6、subscribe (必须,用于微信获得签名和统计使用)
 * @param {String} [cfgs.catalog=""] 栏目id(仅对微博有效)
 * @param {String} [cfgs.sUrl=window.location.href] 分享地址url
 * @param {String} [cfgs.title=document.title] 分享标题(仅对微信有效,微博分享使用desc)
 * @param {String} [cfgs.desc=""] 分享说明
 * @param {String} [cfgs.imgUrl=""] 分享时携带图片地址
 * @param {String} [cfgs.ralateUid=""] (仅对微博有效)
 * @param {String} [cfgs.timelineCallback] 分享到朋友圈回调(仅对微信有效)
 * @param {String} [cfgs.sendMessageCallback] 发送给朋友回调(仅对微信有效)
 * @param {String} [cfgs.appKey] 分享到微信和微博对应的appkey
 * @param {String} [cfgs.appKey.tsina] 分享到微博对应的appkey
 * @param {String} [cfgs.wxGuide] 分享到微信引导(仅对微信有效)
 * @param {String} [cfgs.wxGuide.tpl] 分享到微信引导弹层自定义模板
 * @param {String} [cfgs.wxGuide.imgUrl] 分享到微信引导图片地址
 * @param {String} [cfgs.wxGuide.width] 分享到微信引导图片宽度
 * @param {String} [cfgs.wxGuide.height] 分享到微信引导图片高度
 * @param {Object} [cfgs.wxGuide.style={top:0,right:0}] 分享到微信引导弹层样式
 * @param {Object} selector 绑定的对象
 */
function MShare(cfgs, selector) {
    this._cfgs = {
        site: "",
        catalog: "",
        sUrl: window.location.href,
        title: document.title,
        imgUrl: "",
        desc: "",
        appKey: "",
        ralateUid: "",
        source_id:"",
        source_table :"",
        timelineCallback: function() {},
        sendMessageCallback: function() {},
        shareQQCallback: function() {},
        wxGuide: {
            tpl: '',
            imgUrl: 'http://css.selfimg.com.cn/vogue/mdiscovery/images/weixin-guide.png',
            width: 295,
            height: 273,
            style: {
                right: 0,
                top: 0
            }
        }
    };
    this.weixinSigned = 0;
    this.trigger = $(selector);
    this._baseUrl = "/collect.";
    this._tsinaParam = "get?platform=tsina&title={title}&url={url}&pic={pic}&site={site}&source_id={source_id}&source_table={source_table}&ralateUid={ralateUid}&appkey={appkey}&hash={hash}";
    this._init(cfgs, selector);
    this._reflux();
}
MShare.prototype = {
    _init: function(cfgs, selector) {
        var self = this;
        $.extend(true, this._cfgs, cfgs)

        if (selector) {
            $(selector).on('click', function() {
                if (isWeixin) {
                    self._weixinGuide();
                } else {
                    self._weiboShare($(this));
                }
            })
        }
        if (isWeixin) {
            this.weixinInit();
        }
    },
    _weixinGuide: function() {
        var cfg = this._cfgs,
            weixinCfg = cfg.wxGuide,
            defImg = weixinCfg.imgUrl,
            tpl = weixinCfg.tpl || '<div id="weixin_guide" style="position: fixed;background:url(http://css.selfimg.com.cn/cnt/mobile/images/blk80.png) repeat;left:0;top:0;right:0;bottom:0;z-index:1000;"><img src="' + defImg + '" style="position: absolute;z-index:100;" width="' + weixinCfg.width + '" height="' + weixinCfg.height + '"/></div>';

        $('#weixin_guide').remove();
        $(tpl).attr('id', 'weixin_guide').appendTo($('body'));

        $('#weixin_guide img').css(weixinCfg.style || {})
        setTimeout(function() {
            $('#weixin_guide').click(function() {
                $(this).remove();
            })
        }, 300)
    },
    /**
     * 对微信分享自定义配置
     * @param {Object} cfgs 配置项
     * @param {String} [cfgs.site=""] 1、vogue 2、self 3、gq 4、adstyle 5、cnt 6、subscribe(必须,用于微信获得签名和统计使用)
     * @param {String} [cfgs.sUrl=window.location.href] 分享地址url
     * @param {String} [cfgs.title=document.title] 分享标题
     * @param {String} [cfgs.desc=""] 分享说明
     * @param {String} [cfgs.imgUrl=""] 分享时携带图片地址
     * @param {String} [cfgs.timelineCallback] 分享到朋友圈回调
     * @param {String} [cfgs.sendMessageCallback] 发送给朋友回调
     * @param {String} [cfgs.shareQQCallback] 发送到QQ回调
     */
    weixinInit: function(cfgs) {
        var self = this,
            wxCfgs;
        cfgs && $.extend(true, this._cfgs, cfgs);

        wxCfgs = {
            "site": this._cfgs.website,
            "img_url": this._cfgs.imgUrl || "",
            "link": this._cfgs.sUrl || location.href,
            "desc": this._cfgs.desc || "",
            "title": this._cfgs.title || document.title,
            timelineCallback: this._cfgs.timelineCallback,
            sendMessageCallback: this._cfgs.sendMessageCallback,
            shareQQCallback: this._cfgs.shareQQCallback
        }
        //var url = VG.conf.root_domain + '?c=WeixinJsSDK&a=AjaxConfigData&site=' + wxCfgs.site + '&thisPageUrl=' + encodeURIComponent(location.href) + '&callback=?';
        var url = VG.conf.root_domain + '?c=WeixinJsSDK&a=AjaxConfigData&thisPageUrl=' + location.href.split('#')[0];
        var state = false;
        if (location.href.indexOf('test') > -1) {
            var state = true;
        }
        //alert(url)
        $.ajax({
            type:'post',
            dataType:'jsonp',
            jsonp: 'jsonp_cb',
            url: url,
            //data: {"thisPageUrl":location.href.split('#')[0]},
            success:function(data){
                var d = data.msg;
                //alert(d.appId)
                wx.config({
                    //debug:true,
                    appId: d.appId,
                    timestamp: d.timestamp,
                    nonceStr: d.nonceStr,
                    signature: d.signature,
                    jsApiList: [
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage',
                        'onMenuShareQQ'
                    ]
                });
                //alert(wxCfgs.link);
                wx.ready(function() {
                    //alert(wxCfgs.img_url)
                    if (state) {
                        //alert(wxCfgs.img_url);
                    }
                    wx.onMenuShareAppMessage({
                        title: wxCfgs.title,
                        desc: wxCfgs.desc,
                        link: wxCfgs.link,
                        imgUrl: wxCfgs.img_url,
                        success: function(res) {
                            if (state) {
                                alert(1);
                            }
                            wxCfgs.sendMessageCallback('success', res)
                            self._shareLog(0, 1);
                        },
                        cancel: function(res) {
                            if (state) {
                                alert(2);
                            }
                            wxCfgs.sendMessageCallback('cancel', res)
                            self._shareLog(0, 2);
                        },
                        fail: function(res) {
                            if (state) {
                                alert(3);
                            }
                            wxCfgs.sendMessageCallback('fail', res)
                            self._shareLog(0, 0);
                        }
                    });
                    wx.onMenuShareTimeline({
                        title: wxCfgs.title,
                        link: wxCfgs.link,
                        imgUrl: wxCfgs.img_url,
                        success: function(res) {
                            if (state) {
                                alert(1);
                            }
                            wxCfgs.timelineCallback('success', res)
                            self._shareLog(1, 1);
                        },
                        cancel: function(res) {
                            if (state) {
                                alert(2);
                            }
                            wxCfgs.timelineCallback('cancel', res)
                            self._shareLog(1, 2);
                        },
                        fail: function(res) {
                            if (state) {
                                alert(3);
                            }
                            wxCfgs.timelineCallback('fail', res)
                            self._shareLog(1, 0);
                        }
                    });
                    wx.onMenuShareQQ({
                        title: wxCfgs.title,
                        link: wxCfgs.link,
                        imgUrl: wxCfgs.img_url,
                        success: function(res) {
                            wxCfgs.shareQQCallback('success', res)
                            self._shareLog(2, 1);
                        },
                        cancel: function(res) {
                            wxCfgs.shareQQCallback('cancel', res)
                            self._shareLog(2, 2);
                        },
                        fail: function(res) {
                            wxCfgs.shareQQCallback('fail', res)
                            self._shareLog(2, 0);
                        }
                    });
                });
            },
            async:'true',
            timeout: 3000            
        });

        /*
        $.getJSON(VG.conf.root_domain + '?c=WeixinJsSDK&a=AjaxConfigData&site=' + wxCfgs.site + '&thisPageUrl=' + encodeURIComponent(location.href) + '&callback=?', function(d) {
            var d = d.msg;
            if (d) {
                wx.config({
                    //debug:true,
                    appId: d.appId,
                    timestamp: d.timestamp,
                    nonceStr: d.nonceStr,
                    signature: d.signature,
                    jsApiList: [
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage',
                        'onMenuShareQQ'
                    ]
                });
                wx.ready(function() {
                    wx.onMenuShareAppMessage({
                        title: wxCfgs.title,
                        desc: wxCfgs.desc,
                        link: wxCfgs.link,
                        imgUrl: wxCfgs.img_url,
                        success: function(res) {
                            wxCfgs.sendMessageCallback('success', res)
                            self._shareLog(0, 1);
                        },
                        cancel: function(res) {
                            wxCfgs.sendMessageCallback('cancel', res)
                            self._shareLog(0, 2);
                        },
                        fail: function(res) {
                            wxCfgs.sendMessageCallback('fail', res)
                            self._shareLog(0, 0);
                        }
                    });
                    wx.onMenuShareTimeline({
                        title: wxCfgs.title,
                        link: wxCfgs.link,
                        imgUrl: wxCfgs.img_url,
                        success: function(res) {
                            wxCfgs.timelineCallback('success', res)
                            self._shareLog(1, 1);
                        },
                        cancel: function(res) {
                            wxCfgs.timelineCallback('cancel', res)
                            self._shareLog(1, 2);
                        },
                        fail: function(res) {
                            wxCfgs.timelineCallback('fail', res)
                            self._shareLog(1, 0);
                        }
                    });
                    wx.onMenuShareQQ({
                        title: wxCfgs.title,
                        link: wxCfgs.link,
                        imgUrl: wxCfgs.img_url,
                        success: function(res) {
                            wxCfgs.shareQQCallback('success', res)
                            self._shareLog(2, 1);
                        },
                        cancel: function(res) {
                            wxCfgs.shareQQCallback('cancel', res)
                            self._shareLog(2, 2);
                        },
                        fail: function(res) {
                            wxCfgs.shareQQCallback('fail', res)
                            self._shareLog(2, 0);
                        }
                    });
                });
            }
        })
        */
        
    },
    /**
     * 重置分享自定义配置
     * @param {Object} cfg 自定义配置对象
     * @param {String} [cfg.sUrl=window.location.href] 分享地址url
     * @param {String} [cfg.title=document.title] 分享标题
     * @param {String} [cfg.desc=""] 分享说明
     * @param {String} [cfg.imgUrl] 分享时携带图片地址
     */
    reset: function(cfg) {
        var self = this;
        if (!isWeixin) {
            cfg.sUrl && self.trigger.data('sUrl', cfg.sUrl);
            cfg.desc && self.trigger.data('desc', cfg.desc);
            cfg.imgUrl && self.trigger.data("imgUrl", cfg.imgUrl);
        } else {
            this.weixinInit({
                sUrl: cfg.sUrl || self._cfgs.sUrl,
                desc: cfg.desc || self._cfgs.desc,
                imgUrl: cfg.imgUrl || self._cfgs.imgUrl,
                title: cfg.title || self._cfgs.title
            });
        }

    },
    _reflux: function() {
        var refer = document.referrer,
            url = document.location.href,
            param = location.search.substr(1),
            ua = navigator.userAgent;

        if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
            $.getJSON(this._baseUrl + "reflux?" + "callback=?" + "&url=" + encodeURIComponent(url) + '&ua=' + ua + '&param=' + encodeURIComponent(param));
        }
    },
    _shareLog: function(actType, actResult) {
        var site = this._cfgs.website;
        //$.get('http://application.self.com.cn/wxapi/interface/sharecollect/collectdata?mp=' + site + '&url=' + encodeURIComponent(location.href) + '&ac=' + actType + '&sc=' + actResult + '&ua=' + encodeURIComponent(navigator.userAgent))
    },
    _weiboShare: function(trigger) {
        var self = this;
        var cfg = this._cfgs,
            params = {
                title: cfg.desc,
                url: cfg.sUrl,
                pic: cfg.imgUrl,
                site: cfg.site,
                cat: cfg.catalog,
                source_id: cfg.source_id,
                source_table: cfg.source_table,
                ralateUid: cfg.ralateUid,
                appkey: cfg.appKey.tsina
            },
            tgUrl = trigger.data('sUrl'),
            tgTitle = trigger.data('desc'),
            tgPic = trigger.data("imgUrl");

        params.title = encodeURIComponent(tgTitle ? tgTitle : params.title);
        params.url = encodeURIComponent(tgUrl ? tgUrl : params.url);
        params.pic = encodeURIComponent(tgPic ? tgPic : params.pic);

        var cmdUrl = this._baseUrl + self._formatUri(this._tsinaParam, params);
        window.open(cmdUrl);
    },
    _formatUri: function(uri, params) {
        var reg, uri = uri;
        for (var k in params) {
            reg = new RegExp("{" + k + "}", "g");
            uri = uri.replace(reg, params[k]);
        }
        return uri;
    }
}
;regist("MShare",MShare);
})((typeof Zepto !== 'undefined' ? Zepto : jQuery), window, document)