var DB_Analyse = DB_Analyse || {};
DB_Analyse.rand = function(a) {
	return Math.floor(Math.random() * a) + 1;
}

// 获取浏览器类型
DB_Analyse.getBrowser = function() {
	var Sys = Sys || {};
	var t;
	// 1:IE5；2:IE6；3:IE7；4:Firefox；5:Maxthon；10:其他
	var ua = navigator.userAgent.toLowerCase();
	(navigator.userAgent.indexOf("Maxthon") > -1) ? Sys.maxthon = 'maxthon'
			: (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] : (s = ua
					.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] : (s = ua
					.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] : (s = ua
					.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] : (s = ua
					.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1]
					: 0;
	if (Sys.ie) {
		var version = parseInt(Sys.ie);
		if (version == 5) {
			t = 1;
		} else if (version == 6) {
			t = 2;
		} else if (version == 7) {
			t = 3;
		} else {
			t = 6;
		}
	} else if (Sys.firefox) {
		t = 4;
	} else if (Sys.maxthon) {
		t = 5;
	} else {
		t = 6;
	}
	return t;
}
// 获取参数
DB_Analyse.getVar = function(param) {
	var strHref = top.location.search.substr(1);
	var intPos = strHref.indexOf("?");
	var strRight = strHref.substr(intPos + 1);

	var arrTmp = strRight.split("&");
	for ( var i = 0; i < arrTmp.length; i++) {
		var arrTemp = arrTmp[i].split("=");
		if (arrTemp[0].toUpperCase() == param.toUpperCase()) {
			return arrTemp[1];
		}
	}
	return 0;
}

DB_Analyse.sendColection = function(page) {
	var protocol = top.location.protocol;
	// 目前只支持http一种,以后可扩展为https
	// var record_api = ((protocol == 'https:') ? 'https:' : 'http:') + '//' +
	// 'analyse.duba.net/server.php';
	var record_api = ((protocol == 'https:') ? 'http:' : 'http:') + '//' + 'hd.duba.net/analyse/server.php';
	// Uid Sid
	var Uid = this.Uid || 0;
	var Sid = this.Sid || 0;
	var u = DB_Analyse.getVar('uid');
	var s = DB_Analyse.getVar('sid');
	if (u != 0) {
		Uid = u;
	}
	if (s != 0) {
		Sid = s;
	}

	// 点击位置
	var type = 1;
	var pg = top.location.pathname + document.location.search;
	if (page && page != "") {
		pg = page;
		type = 2;
	}

	//来源页的广告位处理
	var pos = DB_Analyse.getVar('pos');
	if (pos != 0) {
		pg = pos;
		type = 1;
	} else {
		pg = 0;
	}

	// 浏览器
	var browser = DB_Analyse.getBrowser();
	// referer
	var referer = window.document.referrer;
	// height,width,colorDepth
	var sw = screen.width;
	var sh = screen.height;
	var sc = screen.colorDepth;
	var img = new Image(1, 1);
	var src = record_api + "?Uid=" + encodeURIComponent(Uid) + "&Sid="
			+ encodeURIComponent(Sid) + "&SourceUrl="
			+ encodeURIComponent(referer) + "&PageUrl="
			+ encodeURIComponent(top.location.href) + "&BrowserCode=" + browser
			+ "&Browser=" + encodeURIComponent(navigator.userAgent) + "&page="
			+ encodeURIComponent(pg) + "&type=" + encodeURIComponent(type)
			+ "&rd=" + DB_Analyse.rand(99999999999) + DB_Analyse.rand(99999999999) + DB_Analyse.rand(99999999999);
	;
	img.src = src;
}
