﻿var mcStatsHost = 'http://stats.' + location.hostname.replace(/www\./i, '') + '/';
if (window.$) {
	$(document).ready(function() {
		var clean = function(url) { return url.substr(4, url.length - 5); }
		var enc = encodeURIComponent;
		$('body').append('<iframe id="ifr_stats" style="display:none;"></iframe>');
		var handleClick = function(bs, bt) {
			$('#ifr_stats').prop('src', mcStatsHost + '?m=buttonClick&cdt=' + new Date().getTime() + '&bs=' + enc(bs) + '&bt=' + enc(bt));
		}

		$('img, input[type="button"], input[type="submit"], input[type="image"], button').each(function() {
			$(this).click(function() {
				if (!this.parentNode || this.parentNode.tagName != "A") {
					handleClick(this.src || clean(this.style.backgroundImage), this.title || this.alt || this.value || '');
				}
			});
		});

		$('a').each(function() {
			$(this).click(function() {
				var img = $(this).find('img');
				var bs = img.length ? img[0].src || clean(img[0].style.backgroundImage) : '';
				var t = $(this).text();
				var bt = img.length ? t : ($(this).hasClass('segreg') ? 'total counter' : (isNaN(parseInt(t)) ? t : 'page number ' + t));
				handleClick(bs, bt);
			});
		});

	});
}
