(function (pageTime, timerId, destination, timeList, targetName) {
	try {
		var baseSrc = "/access-log/";
		var sessNs = '___log';
		if (!baseSrc) {
			return;
		} else {
			startTimer(true);
		}
		var sendData = g('location_href', location.href);
		if (document.title) {
			sendData += g('title', document.title);
		}
		if (getCookie(sessNs) === false) {
			var props = ['language', 'browserLanguage', 'userLanguage', 'javaEnabled', 'cookieEnabled', 'height', 'width', 'colorDepth'];
			var prop;
			sendData += g('referer', document.referrer);
			while (prop = props.shift()) {
				sendData += (function (target) {
					switch (prop) {
						case 'height':
						case 'width':
						case 'colorDepth':
							target = 'screen';
							break;
					}
					var value = '';
					try {
						if (prop === 'javaEnabled' || typeof window[target][prop] === "function") {
							value = window[target][prop]();
						} else {
							value = window[target][prop] || '';
						}
					} catch (ee) {
					}
					return g(target + '_'  + prop, value);
				})('navigator');
			}
			var flashVer = 0;
			if (navigator.plugins && navigator.mimeTypes['application/x-shockwave-flash']) {
				var plugin = navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin;
				if (plugin) {
					flashVer = plugin.description;
				}
			} else {
				var flashOCX = '';
				try {
					flashOCX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7").GetVariable("$version");
				} catch (ee) {
					try {
						flashOCX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash").GetVariable("$version");
					} catch (eee) {
					}
				}
				if (flashOCX) {
					flashVer = flashOCX;
				}
			}
			if (flashVer <= 6) {
				flashVer = 0;
			}
			sendData += g('flash_description', flashVer);
			setCookie(sessNs, Math.random().toString(36).substr(2).split('').sort(function(){ return Math.random() - Math.random();}).join(''));
		}
	} catch (e) {
		sendError(baseSrc + 'error', e);
	}
	sendQuery(baseSrc + 'load', sendData, callback);

	function callback()
	{
		if (! document.body) {
			window.setTimeout(arguments.callee, 1);
		} else {
			var hrefs, href, i = 0, elem;
			hrefs = document.getElementsByTagName('a');
			while (href = hrefs[i++]) {
				if (href.target) {
					addEvent(href, 'click', function(){
						if (this.href) {
							sendQuery(baseSrc + 'clicked', g('c', this.href));
						}
					});
				} else {
					addEvent(href, 'touchstart', setDestination);
					addEvent(href, 'mousedown', setDestination);
				}
			}
			addEvent(window, 'beforeunload', sendLeaveTime);
			addEvent(window, 'unload', sendLeaveTime);
			addEvent(window, 'blur', stopTimer);
			addEvent(window, 'pagehide', stopTimer);
			addEvent(window, 'focus', startTimer);
			addEvent(window, 'pageshow', startTimer);
			addEvent(window, 'scroll', countDisplayedTime);
			addEvent(window, 'resize', countDisplayedTime);
			
			var elems = document.body.getElementsByTagName('*');
			i=0;
			while (elem = elems[i++]) {
				if (elem.getAttribute (targetName)) {
					elem.duration = 0;
					elem.timestamp = 0;
					timeList.push(elem);
				}
			}
			countDisplayedTime();
		}
	}

	function countDisplayedTime()
	{
		var t = document.documentElement.scrollTop || document.body.scrollTop;
		var b = t + window.innerHeight;
		var l = document.documentElement.scrollLeft || document.body.scrollLeft;
		var r = l + window.innerWidth;
		var elem, i = 0;
		var ts = ~~(new Date / 1000);
		while (elem = timeList[i++]) {
			if (isShown(elem, t, b, l, r)) {
				if (elem.timestamp) {
					elem.duration += ts - elem.timestamp;
				}
				elem.timestamp = ts;
			} else {
				elem.timestamp = 0;
			}
		}
		function isShown(elem, t, b, l, r)
		{
			var parent = elem, eT = 0, eL = 0;
			while (parent) {
				eT += parent.offsetTop;
				eL += parent.offsetLeft;
				parent = parent.offsetParent;
			}
			var eB = eT + elem.offsetHeight;
			//var eR = eL + elem.offsetWidth;
			return (b > eT && t < eB);
		}
	}

	function startTimer(notCountElem)
	{
		if (!notCountElem) {
			countDisplayedTime();
		}
		if (timerId === false && isNaN(pageTime) === false) {
			timerId = setInterval(function () {
				pageTime += 1;
			}, 1000);
		}
	}

	function stopTimer()
	{
		if (timerId !== false) {
			clearInterval(timerId);
			timerId = false;
		}
		var elem, i = 0;
		var ts = ~~(new Date / 1000);
		while (elem = timeList[i++]) {
			if (elem.timestamp) {
				elem.duration += ts - elem.timestamp;
			}
			elem.timestamp = 0;
		}
		
	}

	function sendLeaveTime()
	{
		if (isNaN(pageTime) === false) {
			var sendData = g('time', pageTime);
			pageTime = '@';
			countDisplayedTime();
			if (timeList && timeList.length > 0) {
				var elem, shownList = [];
				while (elem = timeList.shift()) {
					if (elem.duration > 0) {
						shownList.push(elem.getAttribute(targetName) + '=' + elem.duration);
					}
				}
				if (shownList.length > 0) {
					sendData += g('d', shownList.join('&'));
				}
			}
			sendData += g('c', destination);
			sendQuery(baseSrc + 'unload', sendData);
		}
	}

	function setDestination()
	{
		destination = this.href;
	}

	function addEvent(obj, type, func, capture)
	{
		capture = (capture) ? true : false;
		if (obj.addEventListener) {
			obj.addEventListener(type, func, capture);
		} else if (obj.attachEvent) {
			obj.attachEvent('on' + type, func);
		}
	}

	function removeEvent(obj, type, func)
	{
		if (obj.removeEventListener) {
			obj.removeEventListener(type, func, false);
		} else if (obj.detachEvent) {
			obj.detachEvent('on' + type, func);
		}
	}

	function g(name, value)
	{
		try {
			var v = encodeURIComponent( value );
		} catch(e) {
			v = '';
		}
		return '&' + encodeURIComponent( name ).replace( /%20/g, '+' )
			+ '=' + v.replace( /%20/g, '+' );
	}

	function sendQuery(actionUrl, data, callback)
	{
		var img = new Image();
		img.src = actionUrl + '?t=' + (new Date).getTime() +  data;
		if (callback) {
			callback();
		}
	}

	function sendError(actionUrl, error)
	{
		var img = new Image();
		var newtime = (new Date).getTime();
		var message = error.message;
		if (error.fileName && error.lineNumber) {
			message += "ファイル:" + error.fileName + ", 行:" + error.lineNumber;
		}
		img.src = actionUrl + '?t=' + newtime + '&e=' + message;
	}

	function setCookie(name, value)
	{
		document.cookie = name + '=' + value + '; path=/;';
	}

	function unsetCookie(name)
	{
		document.cookie = name+"=; expires="+(new Date(0)).toGMTString();
	}

	function getCookie(name)
	{
		var value = false;
		if (document.cookie) {
			var cookies = document.cookie.split("; ");
			for (var i = 0; i < cookies.length; i++) {
				var str = cookies[i].split("=");
				if (str[0] == name) {
					value = unescape(str[1]);
					break;
				}
			}
		}
		return value;
	}
})(0, false, '', [], '_timer');