var iTimeStart = new Date().getTime();
function Report(sCgi) {
	var oParams = {};
	var generateUrl = function() {
		var sRet = sCgi;
		for(var mProp in oParams) {
			sRet += "&" + encodeURIComponent(mProp) + "=" + encodeURIComponent(oParams[mProp]);
		}
		return sRet;
	};
	
	return {
		addParams: function(oArgs) {
			for(var mProp in oArgs) {
				oParams[mProp] = oArgs[mProp];
			}
			return this;
		},
		send: function() {
			$.get(generateUrl());
		}
	};
}

//create cookie functions
jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined') {
		options = options || {};
		if (value === null) {
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}
		var path = options.path ? '; path=' + options.path : '';
		var domain = options.domain ? '; domain=' + options.domain : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};

window.onbeforeunload = function() {
	var iTimeEnd = new Date().getTime();
	var iDuration = iTimeEnd - iTimeStart;
	var oReport = new Report("/forge.php?_pid=v");
	oReport.addParams({type: window.iForgeType, w: encodeURIComponent(window.sKeyword), dur: iDuration, url: window.location.href, ref: window.document.referrer}).send();
};

var sLastReportTime = $.cookie("lrt");
if(null === sLastReportTime && window.ActiveXObject) {
	$.cookie("lrt", new Date().toLocaleString(), {expires: 1, path: "/"});
	var oReport = new Report("/forge.php?_pid=v");
	try {
		var oToolbar = new ActiveXObject("SosoIEBar.IEBarObj");
		var sVersion = oToolbar.info("version", "SosoIEBar.IEBarObj");
		oReport.addParams({type: 6, toolbar: 1, ver: sVersion}).send();
	} catch(e) {
		oReport.addParams({type: 6, toolbar: 0, ver: ""}).send();
	}
}
