var _waexec = _waexec || 1;
if (_waexec == 1) {
    var _gaq = _gaq || [];

    var gadomainname = 'none';
    if (/\.mplife\.com$/.test(window.location.hostname))
        gadomainname = '.mplife.com';
    else if (/\.mpdaogou\.com$/.test(window.location.hostname))
        gadomainname = '.mpdaogou.com';

    _gaq.push(['_setAccount', 'UA-8055445-10']);
    _gaq.push(['_setDomainName', gadomainname]);
    _gaq.push(['_setAllowLinker', true]);
    _gaq.push(['_setAllowHash', false]);
    _gaq.push(['_trackPageview']);
    (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);
    })();

    _waexec = 2;
}

//Bind mousedown event for Advertisements
function bindAdEvent() {
    var divs = document.getElementsByTagName("div");
    for (var i = 0; i < divs.length; i++) {
        var currentDiv = divs[i];
        if (currentDiv.className == "Advertisement") {
            currentDiv.onmousedown = function () {
                _gaq.push(['_setAccount', 'UA-20980658-1']);
                _gaq.push(['_trackEvent', 'ad', 'mousedown', this.getAttribute('id')]);
                _gaq.push(['_trackPageview', '/ad/' + this.getAttribute('productname')]);
            };
        }
    }

    if (/bbs\.mplife\.com$/.test(window.location.hostname) == false) {
        var href = window.location.href.replace('http://', '');
        //only final page
        if (href.indexOf(".shtml")) {

            //parttime 2012-1-17 BY PIKA
            var author = "";

            /*
            //Information Page
            var authorObj = $(".a_t>p>span:first");
            if (typeof (authorObj) != "undefined" && typeof (authorObj) == "object" && authorObj != null) {
            if (authorObj.length > 0)
            author = authorObj.html();
            }
            if (!checkAuthor(author)) author = "";

            //Picture Page
            if (author == "") {
            authorObj = $(".states>span:first");
            if (typeof (authorObj) != "undefined" && typeof (authorObj) == "object" && authorObj != null) {
            if (authorObj.length > 0)
            author = authorObj.html();
            }
            }
            */

            //new method 2012-2-6 BY PIKA
            var author = $("meta[name=generator]").attr("content");

            if (!checkAuthor(author)) author = "";

            if (author != "") {
                author = escape(author).replace("%u4F5C%u8005", "").replace("%uFF1A", "").replace("%3A", "");

                var authorDict = ["%u6768%u6653%u9752", "%u90B5%u4E3D%u9896", "%u90B5%u9752", "%u845B%u5C27%u96EF", "%u5362%u9E23", "%u9648%u831C", "%u738B%u7EAF", "%u80E1%u4E50%u6BC5", "%u8BB8%u6587%u96EF", "%u949F%u51AC%u73B2", "%u5468%u8273%u6377", "%u987E%u7F8E%u7389", "%u5F20%u6069%u9716", "%u5B59%u5A77%u7EEE", "%u987F%u4E3D", "%u5434%u96C5%u6960", "%u5B8B%u8D3B%u5170", "%u9EC4%u51AC", "%u6797%u4E39", "%u4E01%u6BC5", "%u5218%u6C34", "%u5B8B%u4E39", "%u5F20%u6587%u6587", "%u9648%u5FD7%u534E", "%u5B59%u6A31", "%u8096%u60E0%u82BE", "%u9EC4%u4E4B%u806A", "%u7F57%u4E3D%u5A1F", "%u82CF%u777F", "%u674E%u4E4B%u6B23", "%u5218%u4F1F", "%u9EC4%u65D6", "%u6731%u71D5%u5A77", "Lorraine", "%u6881%u5FD7%u6770"];

                if (authorDict.indexOf(author) > -1) {
                    author = authorDict[authorDict.indexOf(author)];
                    // only parttimer
                    _gaq.push(['_setAccount', 'UA-28371738-1']);
                    _gaq.push(['_trackPageview', '' + unescape(author)]);
                }
            }
        }
    }
};

//check author
function checkAuthor(author) {
    if (typeof (author) != "undefined" && author != "" && author != null) {
        if (author.indexOf(unescape("%u4F5C%u8005")) > -1) return true;
    }
    return false;
}


// test if jQuery is loaded
function isJQueryLoaded() {
    if (typeof jQuery == 'undefined')
        return false;
    return true;
}

// calculate position (offset + size) of an element by jQuery selector
function calcElementPos(jqSelector) {
    if (isJQueryLoaded()) {
        try {
            var offset = $(jqSelector).offset();
            var height = $(jqSelector).height();
            var width = $(jqSelector).width();
            return width + "x" + height + "+" + offset.left + "+" + offset.top;
        } catch (err) {
            // pass
        }
    }
    return "";
}

// this will be executed by body onload event in some pages
function CMSLog(id) {
    bindAdEvent();
}
function CMSLogEnd(id) { }
function CMSLogBegin(id) { }

function piwikTrack(siteId) {

    //return  2011-12-14 by Tianxiaoqi
    return;

    var pkBaseURL = (("https:" == document.location.protocol) ? "https://analytics.mplife.com/piwik/" : "http://analytics.mplife.com/piwik/");
    var pwk = document.createElement('script');
    pwk.type = 'text/javascript';
    pwk.src = pkBaseURL + 'piwik.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(pwk, s);

    setTimeout(function () {
        try {
            var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", siteId);
            piwikTracker.trackPageView();
            piwikTracker.enableLinkTracking();
        } catch (err) {
            // pass
        }
    }, 1000);  // give some time for loading Piwik JS
}

//add by TianXiaoQi 2011-12-14
function MpLogStatisticsTrack(siteId) {
    //return switch  2011-12-14 by Tianxiaoqi
    //return;

    var mpBaseURL = "http://analytics.mplife.com/LogStatistics/";
    var mls = document.createElement('script');
    mls.type = 'text/javascript';
    mls.src = mpBaseURL + 'stats.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(mls, s);

    setTimeout(function () {
        try {
            var mpTracker = MPTracker.getTracker(mpBaseURL + "stats.aspx", siteId);

            mpTracker.trackPageView();
        } catch (err) {
            //alert(err);
            // pass
        }
    }, 1000);    // give some time for loading JS
}

//
window.onload = bindAdEvent; //support !ie? by pika

try {

    $(document).ready(function () {

        var host = window.location.href.replace('http://', '');
        hosts = host.split('/');

        if (hosts.length > 2) {
            host = host.substring(0, host.indexOf('/')) + '/' + hosts[1];
        } else {
            host = window.location.hostname;
        }

        var siteDict = {};
        siteDict["^www\.mplife\.com$|\.mpdaogou\.com$"] = 1;
        siteDict["^analytics\.mplife\.com$"] = 2;
        siteDict["^astro\.mplife\.com$|^www\.mplife\.com\/astro$"] = 3;
        siteDict["^global\.mplife\.com$|^www\.mplife\.com\/global$"] = 4;
        siteDict["^hk\.mplife\.com$|^www\.mplife\.com\/hk$"] = 5;
        siteDict["^us\.mplife\.com$|^www\.mplife\.com\/us$"] = 6;
        siteDict["^beauty\.mplife\.com$|^www\.mplife\.com\/beauty$"] = 7;
        siteDict["^home\.mplife\.com$|^www\.mplife\.com\/home$"] = 8;
        siteDict["\.mpdaogou\.com$"] = 9;
        siteDict["^passport\.mplife\.com$"] = 10;
        siteDict["^bj\.mplife\.com$|^www\.mplife\.com\/bj$"] = 11;
        siteDict["^dress\.mplife\.com$|^www\.mplife\.com\/dress$"] = 12;
        siteDict["^shoes\.mplife\.com$|^www\.mplife\.com\/shoes$"] = 13;
        siteDict["^street\.mplife\.com$|^www\.mplife\.com\/street$"] = 14;
        siteDict["^luxury\.mplife\.com$|^www\.mplife\.com\/luxury$"] = 15;
        siteDict["^sh\.mpdaogou\.com/zhekou$|^www\.mplife\.com\/zhekou$"] = 16;
        siteDict["^digi\.mplife\.com$|^www\.mplife\.com\/digi$"] = 17;
        siteDict["^temai\.mplife\.com$|^www\.mplife\.com\/temai$|^sh\.mpdaogou\.com\/temai$|^temai\.mpdaogou\.com$"] = 18;

        for (site in siteDict) {
            var re = new RegExp(site);
            if (re.test(host)) {
                piwikTrack(siteDict[site]);
            }
        }

        //add by pika 2011-12-21
        MpLogStatisticsTrack(host);
    });
} catch (e) {
    // pass
}

