(function() {
	var date = new Date(),
	cookiePre = '__tsa_',
	timeout = 1800,
	w = window,
	d = document,
	m = Math;
	
	var now = date.getTime(),
	s = w.screen,
	n = w.navigator,
	f = function() {
		this.params = {};
		this.init();
	};

	f.prototype = {
		init: function() {
			this.initParams();
			var arr = [];
			for (var i in this.params) {
				arr.push(i + '=' + this.params[i]);
			}
			var url = _tsa.protocol + 'seo.topsem.com/tongji/stats.php?' + arr.join('&');
			var img = new Image(0, 0);
			img.src = url;
		},
		setCookie: function (name, value, time) {
			var life = new Date();
			life.setTime(life.getTime() + time * 1000);
			name = cookiePre + name;
			d.cookie = name + "=" + value + ((time == null) ? "": "; expires=" + life.toGMTString()) + "; path=/";
		},
		getCookie: function (name) {
			var reg = new RegExp("(^| )" + cookiePre + name + "=([^;]*)(;|\x24)");
			var arr = reg.exec(d.cookie);
			return arr && arr[2] !== undefined ? arr[2] : '';
		},
		getHostName: function() {
			return d.location.hostname;
		},
		getSr: function() {
			return s ? s.width + 'x' + s.height : '';
		},
		getSc: function() {
			return s && s.colorDepth ? s.colorDepth + '-bit' : '-';
		},
		getUl: function() {
			var ul = '';
			if (n) {
				if (n.systemLanguage) {
					ul = n.systemLanguage;
				}
				else if (n.language) {
					ul = n.language;
				}
				else if (n.browserLanguage) {
					ul = n.browserLanguage;
				}
				else if (n.userLanguage) {
					ul = n.userLanguage;
				}
			}
			return ul ? ul.toLowerCase() : '-';
		},
		getTz: function() {
			return - date.getTimezoneOffset() / 60;
		},
		getCe: function() {
			return n && n.cookieEnabled ? 1 : 0;
		},
		getJe: function() {
			return n && n.javaEnabled() ? 1 : 0;
		},
		getFl: function() {
			var a = "ShockwaveFlash", fl = '', t;
			if (n && n.plugins && n.plugins.length) {
				t = n.plugins['Shockwave Flash'];
				fl = t.description.split('Shockwave Flash ')[1].split(' ')[0];
			}
			else {
				a += '.' + a;
				try {
					t = new ActiveXObject(a + '.7');
				} catch(e) {
					try {
						t = new ActiveXObject(a + ".6");
					} catch(e) {
						try {
							t = new ActiveXObject(a);
						} catch(e) {}
					}
				}
				fl = t.GetVariable("$version");
				if (fl) {
					fl = fl.split(' ')[1].split(',');
					fl = fl[0] + '.' + fl[1];
				}
			}
			return fl ? fl : '-';
		},
		getRf: function() {
			return d.referrer ? d.referrer : '';
		},
		getCs: function() {
			return d.characterSet ? d.characterSet : d.charset ? d.charset : '';
		},
		getDt: function() {
			return d.title ? d.title : '';
		},
		getVn: function() {
			var vn = this.getCookie('vn'),
			lvt = this.getCookie('lvt');
			if (vn) {
				vn = now - lvt > 43200000 ? parseInt(vn) + 1 : vn;
			}
			else {
				vn = 1;
			}
			this.setCookie('vn', vn, 31536000);
			return vn;
		},
		getNd: function() {
			var nvt = this.getCookie('nvt'),
			lvt = this.getCookie('lvt'),
			nd = 0;
			
			if (!nvt) {
				nvt = now;
				nd = 1;
			}
			else if (now - lvt > timeout) {
				var d = new Date();
				d.setTime(nvt);
				nvt = now;
				nd = d.toLocaleDateString() == date.toLocaleDateString() ? 0 : 1;
			}
			this.setCookie('nvt', nvt, 86400);
			
			return nd;
		},
		initParams: function() {
			this.params = {
				id: _tsa.id,
				sr: this.getSr(),
				sc: this.getSc(),
				ul: this.getUl(),
				tz: this.getTz(),
				ce: this.getCe(),
				je: this.getJe(),
				fl: this.getFl(),
				rf: encodeURIComponent(this.getRf()),
				cs: this.getCs(),
				dt: escape(this.getDt()),
				vn: this.getVn(),
				nd: this.getNd(),
				lvt: Math.round(this.getCookie('lvt') / 1000)
			};
			this.setCookie('lvt', now, 31536000);
		},
		addParam: function( name, value ) {
			this.params[name] = value;
		}
	};
	var a = new f();
})();
