var pkcms = {};
//检测浏览器的类型及版本
pkcms.checkBrowser = function() {
    var ug = navigator.userAgent.toLowerCase();
    var userAgent = document.getElementById("userAgent");

    //检测IE及版本
    var IE = ug.match(/msie\s*\d\.\d/); //提取浏览器类型及版本信息，注match()方法返回的是数组而不是字符串
    var isIE = check(/msie/);
    if (isIE) {
        pkcms.browserType = "Internet Explorer";
        pkcms.browserVer = IE.join(" ").match(/[0-9]/g).join("."); //先用join()方法转化为字符串，然后用match()方法匹配到版本信息，再用join()方法转化为字符串
    }

    //检测chrome及版本
    var chrome = ug.match(/chrome\/\d\.\d/gi);
    var isChrome = check(/chrome/);
    if (isChrome) {
        pkcms.browserType = "Chrome";
        pkcms.browserVer = chrome.join(" ").match(/[0-9]/g).join(".");
    }

    //检测firefox及版本
    var firefox = ug.match(/firefox\/\d\.\d/gi);
    var isFirefox = check(/firefox/);
    if (isFirefox) {
        pkcms.browserType = "Firefox";
        pkcms.browserVer = firefox.join(" ").match(/[0-9]/g).join(".");
    }
};
//复制到剪贴板
pkcms.copyToClipboard = function(txt) {
    if (window.clipboardData) {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text", txt);
    } else if (navigator.userAgent.indexOf("Opera") !== -1) {
        window.location = txt;
    } else if (window.netscape) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
            alert($.L('copy_msg'));
        }
        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip)
            return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans)
            return;
        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext = txt;
        str.data = copytext;
        trans.setTransferData("text/unicode", str, copytext.length * 2);
        var clipid = Components.interfaces.nsIClipboard;
        if (!clip)
            return false;
        clip.setData(trans, null, clipid.kGlobalClipboard);
        alert($.L('copy_ok'));
    }
};
//动态加载 JavaScript 文件
pkcms.loadScript = function(file_name){
    var url = location.protocol + '//' + location.hostname
        + (location.port == '' ? '' : ':'+location.port) + '/statics/js/' + file_name + ".js";
    document.write('<scr' + 'ipt type="text/javascript" src="' + url + '"><\/scr' + 'ipt>');
}
// 判断 jquery 是否存在
pkcms.jqueryExists = function() {
    var jQueryIsLoaded = typeof jQuery != 'undefined';
    if (! jQueryIsLoaded)
        pkcms.loadScript('jquery-1.8.3.min');
}
//浏览计数（catid：栏目id,id:内容id)
pkcms.hits = function(catid, id) {
    if ($('#pk_hits').length > 0) {
        $('#pk_hits').html('loading_');
        $.get('/index.php/content/asynchronous/', 'actionType=hits&catid=' + catid + '&id=' + id, function(result) {
            $('#pk_hits').html(result);
        });
    }
};
//下载计数（catid：栏目id,id:内容id)
pkcms.down = function(catid, id) {
    if ($('#pk_down').length > 0) {
        $('#pk_down').html($.L('loading'));
        $.get('/index.php', 'app=content&c=ajax&action=down&catid=' + catid + '&id=' + id, function(result) {
            $('#pk_down').html(result);
        });
    }
};
//收藏计数（catid：栏目id,id:内容id)
pkcms.favorites = function(catid, id) {
    art.dialog.tips($.L('loading'), 5);
    $.get('/index.php', 'app=content&c=ajax&action=favorites&catid=' + catid + '&id=' + id, function(result) {
        art.dialog.tips(result, 2);
    });
};
//推荐计数（catid：栏目id,id:内容id)
pkcms.recommend = function(catid, id) {
    art.dialog.tips($.L('loading'), 5);
    $.get('/index.php', 'app=content&c=ajax&action=recommend&catid=' + catid + '&id=' + id, function(result) {
        art.dialog.tips(result, 2);
    });
};
//加入购物车
pkcms.add_cart = function(obj, id, modelid, buy) {
    var os = $(obj);
    $.get('index.php', {app: 'order', c: 'shopping', action: 'add', id: id, modelid: modelid, random: Math.random()}, function(data) {
        //alert(data);
        if (data === '1') {
            alert($.L('cart_product_exits'));
        } else if (data === '2') {
            alert($.L('no_buy_me_product'));
        } else if (data === '3') {
            if (buy) {
                window.location.href = "index.php?app=order&c=shopping&action=cart";
            } else {
                alert($.L('cart_add_ok'));
            }
        } else {
            if (buy) {
                window.location.href = "index.php?app=order&c=shopping&action=cart";
            } else {
                alert($.L('cart_add_ok'));
            }
        }
    });
};
//异步登陆
pkcms.ajaxlogin = function(htmlid,formId) {
    $('#'+htmlid).html('loading……');
    $.ajax({
        type: "Get",
        url: '/index.php/member/GetUserLoginInfo',
        success: function(data) {
            $('#'+htmlid).html('Hello！ '+data+'，<a href="/index.php/member/PublicLogin/Out">LogOut</a>').show();
            $('#'+formId).remove();
        },
        error: function (xhr, status, error) {
            if (xhr.status === 401) {
                $('#'+htmlid).hide();
            } else {
                var result = $.parseJSON(xhr.responseText);
                console.log(result);
            }
        }
    });
};
// 跟踪来访者
pkcms.guestIp = function() {
    var string_url = encodeURI(location.href);
    pkcms.jqueryExists();
    $.get('index.php','app=content&c=ajax&action=ip&url='+string_url);
}
//验证码异步调取
pkcms.auth_ask = function() {
    if ($('#authask').html() === '') {
        $.get('index.php?app=member&c=auth&action=askreply', function(result) {
            $('#authask').html(result);
        });
    }
};
//页面宽度
pkcms.pageWidth = function() {
    //获得当前document宽度
    if ($.browser.msie) {
        return document.compatMode === "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
    } else {
        return self.innerWidth;
    }
};
//页面高度
pkcms.pageHeight = function() {
    //获得当前document高度
    if ($.browser.msie) {
        return document.compatMode === "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
    } else {
        return self.innerHeight;
    }
};