var _gaq = _gaq || [];
var pluginUrl = (('https:' == document.location.protocol) ?
   'https://ssl.' : 'http://www.') +
   'google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'UA-33829170-2']);
_gaq.push(['_addOrganic', 'baidu', 'word']);
_gaq.push(['_addOrganic', 'm.baidu', 'word']);
_gaq.push(['_addOrganic', 'wap.baidu', 'word']);
_gaq.push(['_addOrganic', 'baidu', 'kw']);
_gaq.push(['_addOrganic', 'baidu', 'q1']);
_gaq.push(['_addOrganic', 'baidu', 'q2']);
_gaq.push(['_addOrganic', 'baidu', 'q3']);
_gaq.push(['_addOrganic', 'baidu', 'q4']);
_gaq.push(['_addOrganic', 'baidu', 'q5']);
_gaq.push(['_addOrganic', 'baidu', 'q6']);
_gaq.push(['_addOrganic', 'soso', 'w']);
_gaq.push(['_addOrganic', 'vnet', 'kw']);
_gaq.push(['_addOrganic', 'sogou', 'query']);
_gaq.push(['_addOrganic', 'youdao', 'q']);
_gaq.push(['_addOrganic', 'so.360.cn', 'q']);
_gaq.push(['_addOrganic', '360so', 'q']);
_gaq.push(['_addOrganic', '360sou', 'q']);
_gaq.push(['_addIgnoredOrganic', 'pztuan.com']);
_gaq.push(['_addIgnoredOrganic', 'www.pztuan.com']);
_gaq.push(['_addIgnoredOrganic', 'pztuan']);
_gaq.push(['_addIgnoredOrganic', '品质团']);
_gaq.push(['_setDomainName', 'pztuan.com']);
_gaq.push(['_setAllowHash', false]);
_gaq.push(['_trackPageview']);
//PHPStat电商数据分析统计JS代码布署
var _trackData = _trackData || [];
//加载jquery
var JSInit = JSInit || [];

function initjsfunction() {
    while (true) {
        try {
            var func = JSInit.pop();
            if (typeof (func) == "function") {
                func();
            }
            else if (typeof (func) == "string") {
                eval(func);
            }
            else {
                break;
            }
        }
        catch (e) { 
            
        }

    } 
}


function initjsfunc() {
    
    $(document).ready(function() { initjsfunction(); });
}


///跟踪时间
//_gaq.push(['_trackTiming', '类别：如外部javascript', '加载index.js', timeSpent, 'CDN']);
///采样时间
_gaq.push(['_setSiteSpeedSampleRate', 10]);

//(function () {
//    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
//    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
//    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
//})();

//JSInit.push(function () {
//    loadJs("/upfile/js/ga.js", null, "utf-8", "ga.js", "local");
//});


function loadJs(url, callback, charset, variable, opt_label) {
    var tt = new TrackTiming("javascript", variable, opt_label);
    tt.startTime();
    var script = document.createElement('script');
    script.onload = script.onreadystatechange = function () {
        if (script && script.readyState && /^(?!(?:loaded|complete)$)/.test(script.readyState)) return;
        if (script.readyState === 'loaded' || script.readyState === 'complete') {
//            script.dd.toString();
//            alert(url);
//            alert(event.reason);
        }
        script.onload = script.onreadystatechange = null;
        script.src = '';
        script.parentNode.removeChild(script);
        script = null;
        if (callback) callback();
    };
    if (script.addEventListener) {
        script.onerror = function (msg) {
            PushError("异步加载js错误", url, "");
        }
    }
    script.charset = charset || document.charset || document.characterSet;
    script.async = true;
    script.type = "text/javascript";
    script.src = url;
    try { document.getElementsByTagName("head")[0].appendChild(script); } catch (e) { }
    tt.endTime().send();
}
function TrackTiming(category, variable, opt_label) {
    this.category = category;
    this.variable = variable;
    this.label = opt_label ? opt_label : undefined;
    this.startTime;
    this.endTime;
    return this;
}

TrackTiming.prototype.startTime = function () {
    this.startTime = new Date().getTime();
    return this;
}

TrackTiming.prototype.endTime = function () {
    this.endTime = new Date().getTime();
    return this;
}


TrackTiming.prototype.send = function () {
    var timeSpent = this.endTime - this.startTime;
    var hourInMillis = 1000 * 60 * 60;
    if (0 < timeSpent && timeSpent < hourInMillis) {
        window._gaq.push(['_trackTiming', this.category, this.variable, timeSpent, this.label]);
    }
    return this;
}
function PushError(msg, url, line) {
    var bcon = new Image(),
        param = { type: "jsdebug", level: "fatal", src: "page", msg: msg, url: url, line: line, time: (new Date()).getTime() },
        toData = function (o) {
            if (!o) return; var i, res = [];
            for (i in o) { if (o.hasOwnProperty(i)) { res.push(encodeURIComponent(i) + "=" + encodeURIComponent(o[i])); } }
            return res.join("&");
        };
    bcon.src = "/jserror.js?" + toData(param);
}

//window.onerror = function (msg, url, line) {
//    PushError(msg, url, line);
//    return true;
//};
//异步加载css
function loadCss(src, target, callback, variable, opt_label) {
    var tt = new TrackTiming("css", variable, opt_label);
    tt.startTime();
    var node = document.createElement('link'),
    outEl;
    switch (target) {
        case 'body':
            outEl = document.body;
            break;
        case 'head':
            outEl = document.getElementsByTagName('head')[0];
            break;
        default:
            outEl = document.getElementsByTagName('head')[0];
    }
    node.rel = "stylesheet";
    node.type = 'text/css';
    node.onerror = function (obj) {
        PushError("异步加载css错误", src, "");
    }

    if (node.addEventListener) {
        node.addEventListener('load', callback, false);
    }
    else { // for IE6-8
        node.onreadystatechange = function () {
            var rs = node.readyState;
            if (rs === 'loaded' || rs === 'complete') {
                node.onreadystatechange = null;
                if (callback)
                    callback();
            }
            else {

            }
        };
    }
    node.href = src;
    outEl.appendChild(node);
    tt.endTime().send();
}
