/* HELPER FUNCTIONS */

function isFunction(a) {
    return typeof a == 'function';
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function htSetText(elem, text) {
        elem['innerText' in elem ? 'innerText' : 'textContent'] = text || '';
}

function htGetText(elem) {
        return elem['innerText' in elem ? 'innerText' : 'textContent'] || '';
}

var sContainers = new Array();

/* ATTACH KARMA-RELATED EVENTS TO COMMENTS */
addLoadEvent(function() { 
	var comments = document.getElementsByClassName('comment', $('comments'));

	if (!isArray(comments)) return;

	var comment;

	while (comment = comments.pop()) {
		/* mouseover for hidden comments */
		if (comment.className.indexOf('hidden') > -1) {
			attachKarmaMouseOvers(comment);
		}

		/* karma voting events */
		var inputs = comment.getElementsByTagName('input');
		var ti = getTargetInfo(inputs);
		if (!ti) continue;

		// get the score container
		var containers = document.getElementsByClassName('score', comment);
		if (!containers || containers.length < 1) continue;

		var target_id = ti[0];
		sContainers[target_id] = containers[0];

		// voting controls for this comment
		comment.voteControls = new Array();

		var input;
		for (var a = 0; a < inputs.length; a++) {
			input = inputs[a];
			if (input.type != 'image') continue;
			input.ownComment = comment;
			// target info could also be fetched when voting..?
			input.targetInfo = ti;

			if (input.name == 'minus') {
/*				input.onclick = function() {
					attachKarmaMouseOvers(this.ownComment);
					vote(this, -1);
					return false;
				}*/
				Event.observe(input, 'click', function(e) {
					attachKarmaMouseOvers(Event.element(e).ownComment);
					vote(Event.element(e), -1);
					Event.stop(e);
				}, false);
			} else if (input.name == 'plus') {
/*				input.onclick = function() {
					clearKarmaMouseOvers(this.ownComment);
					vote(this, 1);
					return false;
				}*/
				Event.observe(input, 'click', function(e) {
					clearKarmaMouseOvers(Event.element(e).ownComment);
					vote(Event.element(e), 1);
					Event.stop(e);
				}, false);
			} else {
				continue;
			}
			comment.voteControls.push(input);
		}
	}
});

function attachKarmaMouseOvers(ob) {
/*	ob.onmouseover = function() {
		this.className = 'comment';
	}
	ob.onmouseout = function() {
		this.className = 'comment hidden';
	}*/

	ob.style.cursor = 'pointer';

	ob.onclick = function() {
		this.className = (this.className == 'comment' ? 'comment hidden' : 'comment');
	}
	ob.className = 'comment hidden';
}

function clearKarmaMouseOvers(ob) {
//	ob.onmouseover = null;
//	ob.onmouseout = null;
	ob.onclick = null;
	ob.className = 'comment';
}

/* GET TARGET ID & TYPE FROM A LIST OF INPUTS*/
function getTargetInfo(inputs) {
	var ret = new Array();
	for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].name == 'target_id') ret[0] = inputs[i].value;
		else if (inputs[i].name == 'target_type') ret[1] = inputs[i].value;
	}
	if (ret.length == 2) return ret;
	return false;
}

function updateScore(t) {
        if (!t.responseXML) return false;

        // find the root node
        var root = null;
        for (var i = 0; i < t.responseXML.childNodes.length; i++) {
                if (t.responseXML.childNodes[i].nodeName == 'vote') {
                        root = t.responseXML.childNodes[i];
                        break;
                }
        }

        if (!root) return false;

        var id = 0;
        var score = 0;

        var kids = root.childNodes;
        for (var i = 0; i < kids.length; i++) {
                var node = kids[i];

                // IE & Safari fix..
		var val;

		if (node.textContent) {
			val = node.textContent;
		} else if (node.firstChild && node.firstChild.nodeValue) {
			val = node.firstChild.nodeValue;
		}

//		var val = node.textContent ? node.textContent : node.firstChild.nodeValue;
//                var val = document.all ? node.firstChild.nodeValue : node.textContent;
                if (node.nodeName == 'target-id') id = val;
                else if (node.nodeName == 'score') score = val;
        }

	id = parseInt(id);
        if (!id) return false;
        score = parseInt(score);

        container = sContainers[id];
        if (!container) return;

	var str = '';

        if (score < 0) {
                container.className = 'container negative';
        } else if (score > 0) {
                container.className = 'container positive';
		str = '+';
        } else {
                str.className = 'container';
        }
	htSetText(container, str+score);

	new Effect.Pulsate(container);
}

function vote(input, score) {
	var target_id = input.targetInfo[0];
	var target_type = input.targetInfo[1];

        new Ajax.Request('/wordpress/wp-content/plugins/ht_karma/vote_js.php?<?=SID?>', {parameters:'target_id='+target_id+'&target_type='+target_type+'&score='+score, asynchronous:true, onSuccess:updateScore});

	// hide the voting controls
	for (var i = 0; i < input.ownComment.voteControls.length; i++) {
		new Effect.Squish(input.ownComment.voteControls[i]);
	}
}
