; (function ($) {
    var wxshareDefaults = {
        direImg: "",
        guideStr:"赶快分享吧",
        link:"",
        title:"",
        desc:"",
        imgUrl:"",
        appId:"",
        timestamp:"",
        nonceStr:"",
        signature: ""
    };
    
    $.fn.wxshare = function () {

        var options = arguments[0]; 
        var settings = $.extend({}, wxshareDefaults,options);

        guidestr = settings.guideStr;
        link = settings.link;
        desc = settings.desc;
        imgUrl = settings.imgUrl;
        title = settings.title;
        appId = settings.appId;
        timestamp = settings.timestamp;
        nonceStr = settings.nonceStr;
        signature = settings.signature;

        var $sharebtn = this;
        
        if (!settings.direImg) {
            settings.direImg = "/images/guide/dicr.png";
        }

        var htmlstr = '<div class="wxshare_mark"></div>';
        htmlstr += '<div class="wxshare_modalContent">';
        htmlstr += '<div class="wxshare_guide">';
        htmlstr += '<div class="wxshare_text">点击 <span class="blue">右上角菜单按钮</span></div> <div class="wxshare_text">选择 <span class="blue">分享到朋友圈</span></div><div  class="wxshare_text">' + guidestr + '!</div>';
        htmlstr += '</div>';
        htmlstr += '<img class="wxshare_dicr" src="' + settings.direImg + '" />';
        htmlstr += '</div>';

        $("body").append($(htmlstr));

        $sharebtn.click(function () {
            $(".wxshare_mark").show();
            $(".wxshare_modalContent").show();
        });

        $(".wxshare_mark").click(function () {
            $(".wxshare_mark").hide();
            $(".wxshare_modalContent").hide();
        });

        $(".wxshare_modalContent").click(function () {
            $(".wxshare_mark").hide();
            $(".wxshare_modalContent").hide();
        });

        wx.config({
            //debug: false,
            appId: appId,
            timestamp: timestamp,
            nonceStr: nonceStr,
            signature: signature,
            jsApiList: [
              'checkJsApi',
              'onMenuShareTimeline',
              'onMenuShareAppMessage',
              'onMenuShareQQ',
              'onMenuShareWeibo',
              'hideMenuItems',
              'showMenuItems',
              'hideAllNonBaseMenuItem',
              'showAllNonBaseMenuItem',
              'translateVoice',
              'startRecord',
              'stopRecord',
              'onRecordEnd',
              'playVoice',
              'pauseVoice',
              'stopVoice',
              'uploadVoice',
              'downloadVoice',
              'chooseImage',
              'previewImage',
              'uploadImage',
              'downloadImage',
              'getNetworkType',
              'openLocation',
              'getLocation',
              'hideOptionMenu',
              'showOptionMenu',
              'closeWindow',
              'scanQRCode',
              'chooseWXPay',
              'openProductSpecificView',
              'addCard',
              'chooseCard',
              'openCard',
              'profile'
            ]
        });
        wx.ready(function () {
            wx.showOptionMenu();
            wx.onMenuShareTimeline({
                title: title, // 分享标题
                desc: desc,
                link: link,
                imgUrl: imgUrl, // 分享图标
                //trigger: function (res) {
                //    alert('用户点击分享到朋友圈');
                //},
                success: function (res) {
                    //$(".wxshare_btnCancel").click();
                    $(".wxshare_mark").click();
                    alert("已分享到朋友圈");
                }
            });

            wx.onMenuShareAppMessage({
                title: title, // 分享标题
                desc: desc,
                link: link,
                imgUrl: imgUrl,
                //trigger: function (res) {
                //    alert('用户点击发送给朋友');
                //},
                success: function (res) {
                    //$(".wxshare_btnCancel").click();
                    $(".wxshare_mark").click();
                    alert("已分享");
                }
            });

            wx.onMenuShareQQ({
                title: title, // 分享标题
                desc: desc,
                link: link,
                imgUrl: imgUrl,
                //trigger: function (res) {
                //    alert('用户点击分享到QQ');
                //},
                success: function (res) {
                    $(".wxshare_mark").click();
                    alert("已分享到QQ");
                }
            });
            wx.onMenuShareWeibo({
                title: title, // 分享标题
                desc: desc,
                link: link,
                imgUrl: imgUrl,
                //trigger: function (res) {
                //    alert('用户点击分享到微博');
                //},
                success: function (res) {
                    $(".wxshare_mark").click();
                    alert("已分享到微博");
                }
            });
        });
        wx.error(function (res) {
            // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，
            //也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
            //alert("error:" + res.errMsg);
        });

    }

})(jQuery);