var spamLock = false;

var spamAjax = new SimpleAjax("http://nigmaru.com/ajax/spamwar.ajax/report");

function SimpleAjax(url) {
	this.url = url;
	this.request = function(post, callback) {
		var xmlHttpRequest = this.getXMLHttpRequest();
		xmlHttpRequest.open('POST', this.url, true);
		xmlHttpRequest.onreadystatechange = function() {
			callback(xmlHttpRequest);
		}
		xmlHttpRequest.send(post);
	}
	this.getXMLHttpRequest = function() {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	this.serialize = function(xmlDoc) {
		try {
			return (new XMLSerializer()).serializeToString(xmlDoc);
		} catch (e) {
			try {
				return xmlDoc.xml;
			} catch (e) {
				return "Can`t serialize";
			}
		}
	}
}

function getXMLDocIE() {
	var versions = [ "Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.5.0",
			"Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0",
			"Msxml.DOMDocument" ];

	for (i = 0; i < versions.length; i++) {
		try {
			var ieXML = new ActiveXObject(versions[i]);
			return ieXML;
		} catch (e) {
		}
	}
}

function swapAjaxIcon() {
	var d = document.getElementById('nhl_spam_war');
	d.innerHTML = '( заявка отправлена )';

	var t = setTimeout( function() {
		d.parentNode.removeChild(d);
	}, 2000);
}

function reportSpam(url, query, uid) {
	if (spamLock == false) {
		swapAjaxIcon();
		spamLock = true;

		var callback = function(xml) {
			if (xml.readyState == 4) {
				//nop
			}
		}

		try {
			var doc = document.implementation.createDocument("", "", null);
		} catch (e) {
			var doc = getXMLDocIE();
		}
		var urlNode = doc.createElement("url");
		urlNode.appendChild(doc.createTextNode(url));
		var queryNode = doc.createElement("query");
		queryNode.setAttribute("entities", "true");
		queryNode.appendChild(doc.createTextNode(query));
		var uidNode = doc.createElement("uid");
		uidNode.appendChild(doc.createTextNode(uid));
		var posNode = doc.createElement("pos");
		posNode.appendChild(doc.createTextNode("0"));
		var modNode = doc.createElement("ban");
		modNode.appendChild(urlNode);
		modNode.appendChild(queryNode);
		modNode.appendChild(uidNode);
		modNode.appendChild(posNode);
		doc.appendChild(modNode);
		spamAjax.request(spamAjax.serialize(doc), callback);
	}
	return false;
}

function nigmaHighlightJump(obj) {
	var indx = obj.id.lastIndexOf("nhl_");
	var id = obj.id.substr(indx, obj.id.length - indx);
	nigmaHighlightJumpById(id);
}

function nigmaHighlightJumpById(id) {
	var x = y = 0;
	var target = document.getElementById(id);
	var el = target;
	while(el && el != document.body) {
		x += el.offsetLeft;
		y += el.offsetTop;
		el = el.offsetParent;
	}
	scrollTo(x, y);
	nigmaHighlightBlink(id, 0);
}

function nigmaHighlightBlink(id, num) {
	if(num % 2)
		document.getElementById(id).className = document.getElementById(id).className.replace(' NigmaHighlightBlinkSpan', '');
	else
		document.getElementById(id).className += ' NigmaHighlightBlinkSpan';

	if (num < 7)
		setTimeout('nigmaHighlightBlink("' + id + '",' + (num + 1) + ')', 100);
}

function waitSpamWar() {
	if(document.getElementById("nhl_spam_war"))
		nhlInitialize();
	else
		setTimeout(waitSpamWar, 750);
}

function nhlInitialize() {
	var spamWar = document.getElementById("nhl_spam_war");

	if(userId != 0)
		spamWar.style.visibility = "visible";
	else
		spamWar.parentNode.removeChild(spamWar);
}

