var xmlHttp = createXmlHttpRequestObject();  
 
function createXmlHttpRequestObject() {
	var xmlHttp;
	
	if (window.ActiveXObject) { 
		try { 
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		catch (e) {
			xmlHttp = false; 
		}
	}
	else {
		try {
			xmlHttp = new XMLHttpRequest(); 
		}
		catch (e) {
			xmlHttp = false; 
		}
	}
	
	if (!xmlHttp) {
		alert("Error creating the XMLHttpRequest object.");
	}
	else {
		return xmlHttp;
	}
}
 
function process(thread_date, direction, type) {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { 
		if (direction == 'setURL') {
			xmlHttp.open("GET", 'setURL.php?url=' + thread_date, true);
		}
		else {
			xmlHttp.open("GET", 'vote_responder.php?direction=' + direction + '&thread_date=' + thread_date + '&type=' + type, true);
		}
		
		xmlHttp.onreadystatechange = function() { 
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					var id = direction + '_' + thread_date;
					handleServerResponse(direction, thread_date);
				}
			}
		}
		xmlHttp.send(null);
	} 
	else {
		timeoutString = 'process("' + thread_date + '", "' + direction + '")';
		window.setTimeout(timeoutString, 1000);
	}
} 
 
function handleServerResponse(direction, thread_date) {
	if (xmlHttp.readyState == 4) { 
		if (xmlHttp.status == 200) { 
			var html = xmlHttp.responseText;
		
			if (html == 0) {
				if (direction != 'setURL') {
					noVote(direction + '_' + thread_date);
				}
			}
			else {
				document.getElementById(direction + '_' + thread_date + '_s').innerHTML = html;
			}
		}  
		else { 
			alert("There was a problem accessing the server: " + xmlHttp.statusText); 
		} 
	}
}

function noVote(id) {
	span = document.getElementById(id);
	span.style.left = '-5px';
	var animation = "putBack('" + id + "')";
	setTimeout(animation, 60);
}

function goRight(id) {
	document.getElementById(id).style.left = '5px';	
	var animation = "putBack('" + id + "')";
	setTimeout(animation, 60);
}

var right = 1;

function putBack(id) {
	document.getElementById(id).style.left = '0px';
	if (right) {
		var animation = "goRight('" + id + "')";
		setTimeout(animation, 60);
		right = 0;
	}
	else {
		right = 1;	
	}
}