
if(!window.$JCCA) {
	var $JCCA = [];
}

function JCAC() {
	this.jcaIndex = $JCCA.length;
	$JCCA.push(this);
	this.target = arguments.length ? arguments[0] : null;
	this.cr = function(tag) { return document.createElement(tag) }
	var wl = window.location;
	var self = this;

	var trim = function(str) {
		if (typeof(str) != 'string') return '';
		var str = str.replace(/^  */, ''), ws = / /, i = str.length;
		while (ws.test(str.charAt(--i)));
		return str.slice(0, i + 1);
	};
	var initSourceFilter = function(target) {
		var filter = {};
		var sources = {
			'include': target.getAttribute('include-sources'),
			'exclude': target.getAttribute('exclude-sources')
		};
		if (sources.include) {
			filter = {'type': 'include', 'sources': sources.include};
		} else if (sources.exclude) {
			filter = {'type': 'exclude', 'sources': sources.exclude};
		} else return;
		var acc = {'hash': {}, 'list': []};
		if (filter.type) {
			var sources = filter.sources.split(',');
			for(var i=0; i < sources.length; i++) {
				var name = trim(sources[i].toLowerCase());
				if (!name || acc.hash[name]) break;
				acc.hash[name] = filter.type == 'include';
				acc.list.push(name);
			}
			if (!acc.list.length) return;
			filter.sources = acc.list.join(',');
		}
		return filter;
	};

	if(this.target) {
		var path = String(this.target.getAttribute('path') || '');
		var uniq = String(this.target.getAttribute('uniq') || '');
		var plus = true;
		if (uniq) {
			plus = uniq.match(/^\+\/*(.*)/);
			if (plus) path = plus[1];
			else path = uniq;
		}
		this.domain = String(this.target.getAttribute('domain') || wl.host);
		var po = undefined;
		if(path) {
			var ar = path.match(/^https?:\/\/[^\/]+(.*)/);
			if(ar) po = ar[1];
			else po=path.replace(/^([^\/]+)/, (plus ? wl.pathname : '') + '/$1');
			this.path = po;
		}
		this.path = po || wl.pathname;
		this.sourceFilter = initSourceFilter(this.target);
		this.target.JCAC = true;
	} else {
		$JCCA.shift();
		var multiQ = {};
		var reqMulti = function(domain, atext) {
			var num = $JCCA.length-1;
			if(!atext.length) return;
			var sc = self.cr('script');
			var ref = wl.protocol + '//' + domain + wl.pathname;
			sc.src = wl.protocol + '//js-kit.com/comments-count.js?ref='+encodeURIComponent(ref) + atext;
			$JCCA[num].target.appendChild(sc);
		}
		var els = document.getElementsByTagName('span') || [];
		/* Iterate and find all counter */
		for(var i = 0; i < els.length; i++) {
			if(!els[i].className.match(/js-kit-comments-count/)
			|| els[i].JCAC)
				continue;
			var r = new JCAC(els[i]);
			var d = r.domain;
			var f = r.sourceFilter;
			if (!multiQ[d]) {
				multiQ[d] = {query: '', index: 0};
			}
			multiQ[d].query += '&p[' + multiQ[d].index + ']='
				+ encodeURIComponent(r.path)
				+ '&jx[' + multiQ[d].index + ']=' + r.jcaIndex
				+ (f ? '&' + f.type + '[' + multiQ[d].index + ']=' + encodeURIComponent(f.sources)  : '');
			if(multiQ[d].query.length > 1000) {
				reqMulti(d, multiQ[d].query);
				multiQ[d] = {query: '', index: 0};
			} else {
				multiQ[d].index++;
				window.$JCCAI = true;
			}
		}
		for (var d in multiQ) {
			if (multiQ.hasOwnProperty(d))
				reqMulti(d, multiQ[d].query);
		}
	}
}
new JCAC();

JCAC.prototype.newCount = function(count) {
    this.target.innerHTML = window.JSKitCommentsCountFilter ? window.JSKitCommentsCountFilter(count) : count;
}

