
function VisitStatClass(){   
    this.currentDomain = ".chinagames.net";//document.domain;
    this.currentDomain ="";
    this.cookieDomain = "";
    //this.VisitStatClass = function(){
       
            this.cookieDomain = document.domain;
            p = this.cookieDomain.lastIndexOf(".");
            p2 = this.cookieDomain.lastIndexOf(".",p-1);     
            this.currentDomain = this.cookieDomain.substring(p2) 
          
   // }
 
    


    this.GetCookieVal = function(offset)
    //获得Cookie解码后的值
    {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
    }
    
    
     this.SetCookie = function(name, value,myexpires,mypath,mydomain)
    //设定Cookie值
    {
    var expdate = new Date();
    var argv = this.SetCookie.arguments;
    var argc = this.SetCookie.arguments.length;
    if (myexpires!=""){
	    expires = myexpires;
    }else{expires=null;}
    if (mypath!=""){
	    path = mypath;
    }else{path=null;}
    if (mydomain!=""){
	    domain = mydomain;
    }else{domain=null}
    /*
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;*/
    var secure = (argc > 5) ? argv[5] : false;

    if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
    document.cookie = name + "=" +  (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
    +((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
    +((secure == true) ? "; secure" : "");
    }
    
    
    
    
    this.DelCookie = function(name)
    //删除Cookie
    {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = this.GetCookie (name);
    document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString()+";path=/;domain="+this.currentDomain+";";
    }
    
    
    
    
    this.GetCookie = function(name)
    //获得Cookie的原始值
    {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)
    {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return this.GetCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
    }

    this.trim = function (text)
    {
        if (typeof (text) == 'undefined' || !text.toString)
            {
            return '';
            }
        return text.toString().replace(/^\s*|\s*$/g, '');
    }


    this.setTo2Num = function(pStr){
	    if(pStr.length<2){
		    return "0"+pStr;
	    }else{
		    return pStr;
	    }
    }


    this.del_allcookie = function (){
	    this.DelCookie("VisitStatCookie");
    	

    }
    this.init_cookie = function (){
	    //DelCookie("UserInfo[CookieID]");
	    if(this.GetCookie("VisitStatCookie")==null ||  this.GetCookie("VisitStatCookie")==""){
		    this.del_allcookie();
		    rndstr = parseInt(9999*Math.random());
		    while(rndstr<1000){
			    rndstr = parseInt(9999*Math.random());
		    }
		    d = new Date();
		    y=d.getYear().toString();
		    m=(d.getMonth()+1).toString();
		    dt=d.getDate().toString();
		    h=d.getHours().toString();
		    mm=d.getMinutes().toString();
		    s=d.getSeconds().toString();		
		    value=y+this.setTo2Num(m)+this.setTo2Num(dt)+this.setTo2Num(h)+this.setTo2Num(mm)+this.setTo2Num(s)+rndstr.toString();
		    this.SetCookie("VisitStatCookie", value,60*60*24,"/",this.currentDomain);
		    //alert(document.cookie)
		    return value;
	    }else{
	        return this.GetCookie("VisitStatCookie");
	    };
    	
    	
    }









    this.sUserAgent = navigator.userAgent;
    this.fAppVersion = parseFloat(navigator.appVersion);


    this.compareVersions = function(sVersion1, sVersion2) {

        var aVersion1 = sVersion1.split(".");
        var aVersion2 = sVersion2.split(".");
       
        if (aVersion1.length > aVersion2.length) {
            for (var i=0; i < aVersion1.length - aVersion2.length; i++) {
                aVersion2.push("0");
            }
        } else if (aVersion1.length < aVersion2.length) {
            for (var i=0; i < aVersion2.length - aVersion1.length; i++) {
                aVersion1.push("0");
            }   
        }
       
        for (var i=0; i < aVersion1.length; i++) {
     
            if (aVersion1[i] < aVersion2[i]) {
                return -1;
            } else if (aVersion1[i] > aVersion2[i]) {
                return 1;
            }   
        }
       
        return 0;

    }

    this.DetectUserAgent="";
    this.DetectUserAgentVersion = "";
    this.UserPlatform = "";
    this.UserPlatformVersion = "";
    this.isOpera = this.sUserAgent.indexOf("Opera") > -1;
    this.DetectUserAgent = "Opera";
    this.isMinOpera4 = this.isMinOpera5 = this.isMinOpera6 = this.isMinOpera7 = this.isMinOpera7_5 = false;
    this.DetectUserAgent = "MiniOpera";

    this.getBrowserVersion = function(){
        if (this.isOpera) {
            this.DetectUserAgent = "Opera";
            var fOperaVersion;
            if(navigator.appName == "Opera") {
                fOperaVersion = fAppVersion;
            } else {
                var reOperaVersion = new RegExp("Opera (//d+//.//d+)");
                reOperaVersion.test(this.sUserAgent);
                fOperaVersion = parseFloat(RegExp["$1"]);
            }

            this.isMinOpera4 = fOperaVersion >= 4;
            this.isMinOpera5 = fOperaVersion >= 5;
            this.isMinOpera6 = fOperaVersion >= 6;
            this.isMinOpera7 = fOperaVersion >= 7;
            this.isMinOpera7_5 = fOperaVersion >= 7.5;
            this.DetectUserAgentVersion = fOperaVersion;
        }

        var isKHTML = this.sUserAgent.indexOf("KHTML") > -1
                      || this.sUserAgent.indexOf("Konqueror") > -1
                      || this.sUserAgent.indexOf("AppleWebKit") > -1;
                     
        var isMinSafari1 = isMinSafari1_2 = false;
        var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false;

        if (isKHTML) {
            isSafari = this.sUserAgent.indexOf("AppleWebKit") > -1;
            isKonq = this.sUserAgent.indexOf("Konqueror") > -1;

            if (isSafari) {
                this.DetectUserAgent = "Safari";
                var reAppleWebKit = new RegExp("AppleWebKit///(//d+(?://.//d*)?)");
                reAppleWebKit.test(this.sUserAgent);
                var fAppleWebKitVersion = parseFloat(RegExp["$1"]);

                isMinSafari1 = fAppleWebKitVersion >= 85;
                isMinSafari1_2 = fAppleWebKitVersion >= 124;
                this.DetectUserAgentVersion = "";
            } else if (isKonq) {
                this.DetectUserAgent = "Konq";
                var reKonq = new RegExp("Konqueror///(//d+(?://.//d+(?://.//d)?)?)");
                reKonq.test(this.sUserAgent);
                isMinKonq2_2 = this.compareVersions(RegExp["$1"], "2.2") >= 0;
                isMinKonq3 = this.compareVersions(RegExp["$1"], "3.0") >= 0;
                isMinKonq3_1 = this.compareVersions(RegExp["$1"], "3.1") >= 0;
                isMinKonq3_2 = this.compareVersions(RegExp["$1"], "3.2") >= 0;
                this.DetectUserAgentVersion = "";
            }
           
        }

        var isIE = this.sUserAgent.indexOf("compatible") > -1
                   && this.sUserAgent.indexOf("MSIE") > -1
                   && !this.isOpera;
                  
        var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = false;

        if (isIE) {
            this.DetectUserAgent = "IE";
           /* var reIE = new RegExp("MSIE (d+.d+);");
            reIE.test(this.sUserAgent);
            alert(RegExp["$1"]);
            var fIEVersion = parseFloat(RegExp["$1"]);
            */
            
            var fIEVersion = this.sUserAgent.substring(this.sUserAgent.indexOf("MSIE"));
            fIEVersion =fIEVersion.substring(4,fIEVersion.indexOf(";"));
            
            fIEVersion = parseFloat(fIEVersion);
            
            isMinIE4 = fIEVersion >= 4;
            isMinIE5 = fIEVersion >= 5;
            isMinIE5_5 = fIEVersion >= 5.5;
            isMinIE6 = fIEVersion >= 6.0;
            this.DetectUserAgentVersion = fIEVersion;
        }

        var isMoz = this.sUserAgent.indexOf("Gecko") > -1
                    && !isKHTML;

        var isMinMoz1 = sMinMoz1_4 = isMinMoz1_5 = false;

        if (isMoz) {
            this.DetectUserAgent = "Mozilla";
            var reMoz = new RegExp("rv:(//d+//.//d+(?://.//d+)?)");
            reMoz.test(this.sUserAgent);
            isMinMoz1 = this.compareVersions(RegExp["$1"], "1.0") >= 0;
            isMinMoz1_4 = this.compareVersions(RegExp["$1"], "1.4") >= 0;
            isMinMoz1_5 = this.compareVersions(RegExp["$1"], "1.5") >= 0;
            this.DetectUserAgentVersion = "";
        }

        var isNS4 = !isIE && !this.isOpera && !isMoz && !isKHTML
                    && (this.sUserAgent.indexOf("Mozilla") == 0)
                    && (navigator.appName == "Netscape")
                    && (fAppVersion >= 4.0 && fAppVersion < 5.0);

        var isMinNS4 = isMinNS4_5 = isMinNS4_7 = isMinNS4_8 = false;

        if (isNS4) {
            this.DetectUserAgent = "NS4";
            isMinNS4 = true;
            isMinNS4_5 = fAppVersion >= 4.5;
            isMinNS4_7 = fAppVersion >= 4.7;
            isMinNS4_8 = fAppVersion >= 4.8;
            this.DetectUserAgentVersion = fAppVersion;
        }

        var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
        var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC")
                    || (navigator.platform == "Macintosh");

        var isUnix = (navigator.platform == "X11") && !isWin && !isMac;

        var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = false;
        var isMac68K = isMacPPC = false;
        var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false;

        if (isWin) {
            isWin95 = this.sUserAgent.indexOf("Win95") > -1
                      || this.sUserAgent.indexOf("Windows 95") > -1;
            isWin98 = this.sUserAgent.indexOf("Win98") > -1
                      || this.sUserAgent.indexOf("Windows 98") > -1;
            isWinME = this.sUserAgent.indexOf("Win 9x 4.90") > -1
                      || this.sUserAgent.indexOf("Windows ME") > -1;
            isWin2K = this.sUserAgent.indexOf("Windows NT 5.0") > -1
                      || this.sUserAgent.indexOf("Windows 2000") > -1;
            isWinXP = this.sUserAgent.indexOf("Windows NT 5.1") > -1
                      || this.sUserAgent.indexOf("Windows XP") > -1;
            isWinNT4 = this.sUserAgent.indexOf("WinNT") > -1
                      || this.sUserAgent.indexOf("Windows NT") > -1
                      || this.sUserAgent.indexOf("WinNT4.0") > -1
                      || this.sUserAgent.indexOf("Windows NT 4.0") > -1
                      && (!isWinME && !isWin2K && !isWinXP);
                      
             isWin2003 = this.sUserAgent.indexOf("Windows NT 5.2") > -1  && (!isWinME && !isWin2K && !isWinXP && isWinNT4);
                            
             isWinVista =  this.sUserAgent.indexOf("Windows NT 6.0") > -1   && (!isWinME && !isWin2K && !isWinXP && isWinNT4);
            
            
            this.UserPlatform = "Windows";
            if (isWin95)
            {
                this.UserPlatform+="95";
            }else
            if (isWin98)
            {
                this.UserPlatform+="98";
            }else
            if (isWinME)
            {
               this.UserPlatform+="ME";
            }else
            if (isWin2K)
            {
                this.UserPlatform+="2000";
            }else
            if (isWinXP)
            {
                this.UserPlatform+="XP";
            }else
            if (isWin2003)
            {
                this.UserPlatform+="2003";
            }else
            if (isWinVista)
            {
                this.UserPlatform+="Vista";
            }else
            if (isWinNT4)
            {
                this.UserPlatform+="NT";
            }
        }

        if (isMac) {
            isMac68K = this.sUserAgent.indexOf("Mac_68000") > -1
                       || this.sUserAgent.indexOf("68K") > -1;
            isMacPPC = this.sUserAgent.indexOf("Mac_PowerPC") > -1
                       || this.sUserAgent.indexOf("PPC") > -1; 
                       
            this.UserPlatform = "Mac";
        }

        if (isUnix) {
            isSunOS = this.sUserAgent.indexOf("SunOS") > -1;

            if (isSunOS) {
                var reSunOS = new RegExp("SunOS (//d+//.//d+(?://.//d+)?)");
                reSunOS.test(this.sUserAgent);
                isMinSunOS4 = this.compareVersions(RegExp["$1"], "4.0") >= 0;
                isMinSunOS5 = this.compareVersions(RegExp["$1"], "5.0") >= 0;
                isMinSunOS5_5 = this.compareVersions(RegExp["$1"], "5.5") >= 0;
            }
            this.UserPlatform = "Unix";
        }
    
    }
    
    
 
    this.oriRefSite = "";
    this.oriRefUrl = "";
    

    this.CountDownLoad = function(downloadLink){
        downloadLink = this.trim(downloadLink);
        intuserid = this.GetCookie("chinaGamesUserId");
        newVisitStatCookie = this.init_cookie();
        this.getBrowserVersion();
        hiddenframe = document.getElementById("hiddenframefordowncount");
        if (hiddenframe==null)
        {
            iframe = document.createElement("iframe");
            iframe.id = "hiddenframefordowncount";
            iframe.name="hiddenframefordowncount";
            iframe.style.width="0px";
            iframe.style.height="0px";
            document.body.appendChild(iframe);
           // document.body.innerHTML+="<iframe width=0 height=0 frameborder=0 id='hiddenframefordowncount' name='hiddenframefordowncount'></iframe>";
            hiddenframe = document.getElementById("hiddenframefordowncount");
        }
        
        if (this.GetCookie("VisitStatOriRefUrl")==null || this.GetCookie("VisitStatOriRefUrl")=="") {
            if(document.referrer == ""){
                this.oriRefUrl="";
                this.SetCookie("VisitStatOriRefUrl", "",60*60*24,"/",this.cookieDomain);// 设置cookie两天
            }else{
                this.oriRefUrl=escape(document.referrer);
                this.SetCookie("VisitStatOriRefUrl", this.oriRefUrl,60*60*24,"/",this.cookieDomain);// 设置cookie两天
            }
        }else{
            this.oriRefUrl =  escape(this.GetCookie("VisitStatOriRefUrl"));
            this.SetCookie("VisitStatOriRefUrl", this.oriRefUrl ,60*60*24,"/",this.cookieDomain);// 设置cookie两天
        };

        if (this.GetCookie("VisitStatOriRefSite")==null || this.GetCookie("VisitStatOriRefSite")=="") {
            if(document.referrer.toString() == ""){
                this.oriRefSite = "";
                this.SetCookie("VisitStatOriRefSite", "",60*60*24,"/",this.cookieDomain);// 设置cookie两天
            }else{
                p = document.referrer.indexOf("://")+3;
                site = document.referrer.substring(p);
                p = site.indexOf("/");
                this.oriRefSite = escape(site.substring(0,p));
                this.SetCookie("VisitStatOriRefSite", this.oriRefSite,60*60*24,"/",this.cookieDomain);// 设置cookie两天
            }
        }else{
            this.oriRefSite = escape(this.GetCookie("VisitStatOriRefSite"));
            this.SetCookie("VisitStatOriRefSite", this.oriRefSite,60*60*24,"/",this.cookieDomain);// 设置cookie两天
        };
        //alert(hiddenframe)        
        hiddenframe.src = "http://union10.chinagames.net/VisitStat/DownloadCount.aspx?Site=1&SubDomain=4&Cookie="+newVisitStatCookie+"&UserAgent="+this.DetectUserAgent+this.DetectUserAgentVersion+"&UserPlatform="+this.UserPlatform+"&UserScreenWidth="+screen.width+"X"+screen.height+"&UserReference="+escape(document.referrer)+"&UserID="+intuserid+"&r="+Math.random()+"&DownloadLink="+downloadLink;
        document.location.href=downloadLink;
        //alert("aaaaa");
    }
   /* 
    this.clickDownLoad = function (){
        if(typeof(CountDownLoad)=="undefined"){
            setTimeout(function(){clickDownLoad()},1000);
            return;
        };
        CountDownLoad();
    }*/
    
    
    this.RegStat  = function (userid,resourceid){    
		//alert("bb");
        intuserid = userid;
        newVisitStatCookie = this.init_cookie();
        this.getBrowserVersion();
        /*hiddenframe = document.getElementById("hiddenframefordowncount");
        if (hiddenframe==null)
        {
            document.body.innerHTML+="<iframe width=0 height=0 frameborder=0 id='hiddenframefordowncount' name='hiddenframefordowncount'></iframe>";
            hiddenframe = document.getElementById("hiddenframefordowncount");
        }*/
        
        if (this.GetCookie("VisitStatOriRefUrl")==null) {
            if(document.referrer == ""){
                this.oriRefUrl="";
                this.SetCookie("VisitStatOriRefUrl", "null",60*60*24,"/",this.cookieDomain);// 设置cookie两天
            }else{
                this.oriRefUrl=escape(document.referrer);
                this.SetCookie("VisitStatOriRefUrl", this.oriRefUrl,60*60*24,"/",this.cookieDomain);// 设置cookie两天
            }
        }else{
            this.oriRefUrl =  escape(this.GetCookie("VisitStatOriRefUrl"));
            this.SetCookie("VisitStatOriRefUrl", this.oriRefUrl ,60*60*24,"/",this.cookieDomain);// 设置cookie两天
        };

        if (this.GetCookie("VisitStatOriRefSite")==null) {
            if(document.referrer.toString() == ""){
                this.oriRefSite = "";
                this.SetCookie("VisitStatOriRefSite", "null",60*60*24,"/",this.cookieDomain);// 设置cookie两天
            }else{
                p = document.referrer.indexOf("://")+3;
                site = document.referrer.substring(p);
                p = site.indexOf("/");
                this.oriRefSite = escape(site.substring(0,p));
                this.SetCookie("VisitStatOriRefSite", this.oriRefSite,60*60*24,"/",this.cookieDomain);// 设置cookie两天
            }
        }else{
            this.oriRefSite = escape(this.GetCookie("VisitStatOriRefSite"));
            this.SetCookie("VisitStatOriRefSite", this.oriRefSite,60*60*24,"/",this.cookieDomain);// 设置cookie两天
        };
                
        //hiddenframe.src = "http://union10.chinagames.net/VisitStat/DownloadCount.aspx?Site=1&SubDomain=4&Cookie="+newVisitStatCookie+"&UserAgent="+this.DetectUserAgent+this.DetectUserAgentVersion+"&UserPlatform="+this.UserPlatform+"&UserScreenWidth="+screen.width+"X"+screen.height+"&UserReference="+escape(document.referrer)+"&UserID="+intuserid+"&r="+Math.random()+"&DownloadLink="+downloadLink;
        document.write("<script src='http://union10.chinagames.net/VisitStat/Reg.aspx?UserID="+intuserid+"&Site=1&SubDomain=4&Cookie="+newVisitStatCookie+"&UserReference="+escape(document.referrer)+"&PartID="+resourceid+"&UserAgent="+this.DetectUserAgent+this.DetectUserAgentVersion+"&UserPlatform="+this.UserPlatform+"&UserScreenWidth="+screen.width+"X"+screen.height+"&r="+Math.random()+"'><\/script>");
        //alert("aaa");
    }
}