//点击复制
var ClipBoard = function (obj) {
    this.handlerID = obj.handlerID || null;
    this.textID = obj.textID || null;
    this.type = obj.type || 'copy';
    this.isAttr = obj.isAttr || false;
    this.isPlugin = true;
    this.isActive = false;

    var ua = window.navigator.userAgent;
    var is_IE = ua.match(/(rv:|msie )\d+/i);
    var IE_Version = is_IE ? parseInt(is_IE[0].split(/:| /g)[1]) : 9;
    if (IE_Version <= 8) {
        this.isPlugin = false;
    }
    var handlerObj = document.getElementById(obj.handlerID);
    if (typeof this.type === 'string') {
        handlerObj.setAttribute('data-clipboard-action', this.type)
    } else {
        throw error('type类型错误!');
    }
    if (!obj.isAttr && obj.textID) {
        handlerObj.setAttribute('data-clipboard-target', '#' + obj.textID);
    }
}
ClipBoard.prototype.attach = function () {
    if (this.isActive) { // 对象已经被实例化
        return;
    }
    var tip = '复制';
    if (this.type === 'cut') {
        tip = '剪切';
    }
    this.isActive = true;
    if (this.isPlugin) {
        var clip = new Clipboard('#' + this.handlerID);
        clip.on('success', function (e) {
            alert(tip + '成功!');
        });
        clip.on('error', function (e) {
            //alert(e.action + '操作失败!');
            alert('当前系统不支持,请手动复制(长按文字选择“拷贝”)');
        });
    } else if (window.attachEvent) {
        var self = this;
        var handlerObj = document.getElementById(this.handlerID);
        handlerObj.attachEvent('onclick', function () {
            var text = '';
            if (self.isAttr) {// 复制属性值
                text = handlerObj.getAttribute('data-clipboard-text');
            } else {
                var textObj = document.getElementById(self.textID);
                text = textObj.value || textObj.innerHTML;
            }
            window.clipboardData.setData('Text', text);
            alert(tip + '成功,可通过Ctrl+V进行粘贴!');
        });
    } else {
        alert('浏览器版本过低,不支持该插件!');
    }
}

var c1 = new ClipBoard({
    handlerID: 'iosCopyBtn',
    textID: 'link',
    isAttr: false,
    type: 'copy'
});
c1.attach(); // 触发复制/剪切功能

$(function () {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (bIsAndroid) {
        $('#iosCopyBtn').hide();
        $('#androidCopyBtn').show();
    } else {
        $('#androidCopyBtn').hide();
    };
    if (bIsAndroid || bIsIphoneOs || bIsIpad || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM) {
        
    }
    
});
var CouponFun = function () {
    //基本
    var Coupon_fun = function () {

        //复制
        $("#androidCopyBtn").click(function () {
            var e = document.getElementById("link");
            e.select();
            document.execCommand("Copy");
            alert("已复制");
        });
    };
    return {
        init: function () {
            Coupon_fun();
        }
    }
}();
CouponFun.init();