var obj;
var obj2;
var id = 0;
var reload_interval = null;
var last_reload = 0;

$(document).ready(function() {
	if (is_mobile_device())
		$("#comment_scroller").css("max-height", "none");
});

function reload_comments(area, console, contentID) {
	curtime = (new Date()).getTime();
	if (last_reload + 10000 < curtime) {
		url = 'http://www.psxextreme.com/users/reload.asp?area=' + area + '&console=' + console + '&contentID=' + contentID + '&auto=' + (reload_interval == null ? 'Off' : 'On');
		query = '';
		xmlhttpPost(url, query, 'reload', null);
		last_reload = (new Date()).getTime();
	}
}

function auto_reload_comments(area, console, contentID) {
	if (reload_interval == null) {
		//reload_comments(area, console, contentID);
		reload_interval = setInterval("reload_comments('" + area + "', " + console + ", " + contentID + ")", 60000);
		$("#auto_reload_comments").text("Auto: On");
	} else {
		clearInterval(reload_interval);
		reload_interval = null;
		$("#auto_reload_comments").text("Auto: Off");
	}
}

function toggle_scrollcomments() {
	url = 'http://www.psxextreme.com/users/scrollcomments.asp';
	query = '';
	xmlhttpPost(url, query, 'scrollcomments', null);
}

function do_scrollcomments(msg) {
	if (msg == "False") {
		$("#comment_scroller").css({
			"max-height": "none",
			"overflow": "visible"
		});
	} else {
		$("#comment_scroller").css({
			"max-height": "600px",
			"overflow": "auto"
		});
	}
}

function is_mobile_device() {
	return navigator.userAgent.match(/(iPhone|iPod|Android|j2me|WebOS|PalmOS|BlackBerry|Windows CE|NetFront|Opera Mini)/i);
}

function xmlhttpPost(strURL, sendQuery, mode, id) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			if (mode == 'delete')
				deleteComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'hide')
				hideComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'unhide')
				unhideComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'show')
				showComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'edit')
				editComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'block')
				userBlocked(self.xmlHttpReq.responseText, id);
			else if (mode == 'getpost')
				onPostGet(self.xmlHttpReq.responseText, id);
			else if (mode == 'agree' || mode == 'disagree')
				onAgreedOrDisagreed(self.xmlHttpReq.responseText, id);
			else if (mode == 'report')
				spamReported(self.xmlHttpReq.responseText, id);
			else if (mode == 'scrollcomments')
				do_scrollcomments(self.xmlHttpReq.responseText);
			else
				updatePage(self.xmlHttpReq.responseText);
		}
	}
    self.xmlHttpReq.setRequestHeader("Content-length", sendQuery.length);
    self.xmlHttpReq.setRequestHeader("Connection", "close");
	self.xmlHttpReq.send(sendQuery);
}

function updatePage(str) {
	if (str.substr(0, 5) == 'Error') {
		document.getElementById('comment_error').innerHTML = str;
	} else {
		arr = str.split("|||||")
		str = arr[0];
		id = arr[1];
		//document.getElementById('comment_error').innerHTML = 'Comment successfully posted.';
		//document.getElementById('nocomments').style.display = 'none';
		document.getElementById('viewcomments').innerHTML = str;
		//document.getElementById('comment_body').value = '';
		
		if (window.resize_height) {
			resize_height();
		}
		
		if (id != 0) {
			$('#comment_scroller').animate({scrollTop: $('#usercomment_' + id).position().top});
			setTimeout("changeCommentColor()", 2000)
		} else {
			$("#comment_scroller").animate({ scrollTop: $("#comment_scroller").attr("scrollHeight") + 15000 }, 500);
			//$("#comment_scroller").attr({ scrollTop: $("#comment_scroller").attr("scrollHeight") });
		}
	}
}

function editComment(str, id) {
	document.getElementById('usercomment_edit_' + id).style.display = 'none';
	obj = document.getElementById('usercomment_' + id);
	obj.style.display = '';
	obj.innerHTML = str;
	resize_height();
}

function hideComment(str, id) {
	//alert(str);
	obj = document.getElementById('usercomment_' + id);
	obj.innerHTML = 'You hid this comment';
	resize_height();
}

function showComment(str, id) {
	obj = document.getElementById('usercomment_' + id);
	obj.innerHTML = str;
	resize_height();
}

function unhideComment(str, id) {
	obj = document.getElementById('usercomment_' + id);
	obj.innerHTML = str;
	resize_height();
}

function deleteComment(str, id) {
	alert(str);
	document.getElementById('usercomment_parent_' + id).style.display = 'none';
	resize_height();
}

function userBlocked(str, id) {
	alert(str);
}

function changeCommentColor() {
	document.getElementById('usercomment_' + id).style.color = '#000';
	document.getElementById('usercomment_' + id).style.backgroundColor = '#eee';
}

function getPost(id) {
	url = 'http://www.psxextreme.com/users/getpost.asp';
	query = 'id=' + id
	xmlhttpPost(url, query, 'getpost', id);
}

function onPostGet(str, id) {
	//alert(str);
	obj = document.getElementById('usercomment_edit_' + id);
	if (obj != undefined) {
		obj.style.display = '';
		obj.innerHTML = '<form method="post" action="#" onsubmit="doPostEdit(' + id + '); return false;"><textarea name="comment" id="editcomment_' + id + '" style="width: 400px; height: 150px;">' + str + '</textarea><br /><button type="submit">Edit Comment</button> <button type="button" onclick="cancelEdit(' + id + ');">Cancel</button></form></div>';
	} else
		obj2.innerHTML = obj2.innerHTML + '<div id="usercomment_edit_' + id + '"><form method="post" action="#" onsubmit="doPostEdit(' + id + '); return false;"><textarea name="comment" id="editcomment_' + id + '" style="width: 400px; height: 150px;">' + str + '</textarea><br /><button type="submit">Edit Comment</button> <button type="button" onclick="cancelEdit(' + id + ');">Cancel</button></form></div>';
		
	resize_height();
}

function cancelEdit(id) {
	//alert(id);
	obj = document.getElementById('usercomment_' + id);
	obj.style.display = '';
	obj = document.getElementById('usercomment_edit_' + id);
	obj.style.display = 'none';
	
	resize_height();
}

function editPost(id) {
	obj = document.getElementById('usercomment_' + id);
	obj.style.display = 'none';
	obj2 = document.getElementById('usercomment_parent_' + id);
	getPost(id);
	
	resize_height();
}

function doPostEdit(id) {
	//alert('Sorry, commenting is currently disabled.');
	//return false;
	obj = document.getElementById('editcomment_' + id);
	url = 'http://www.psxextreme.com/users/editcomment.asp';
	query = 'id=' + id + '&comment=' + encodeURIComponent(obj.value);
	xmlhttpPost(url, query, 'edit', id);
}

function doComment(str, area, console, contentID) {
	url = 'http://www.psxextreme.com/users/comment.asp?area=' + area + '&console=' + console + '&contentID=' + contentID
	query = 'parent=' + document.getElementById('comment_parent').value + '&comment=' + encodeURIComponent(str)
	xmlhttpPost(url, query, '', 0);
}

function deletePost(id, ism) {
	forSure = confirm('Are you sure you want to delete this comment?');
	if (forSure) {
		if (ism)
			dockPoints = confirm('Would you like to remove the points that were awarded for this comment?');
		else
			dockPoints = true;
		url = 'http://www.psxextreme.com/users/deletecomment.asp';
		query = 'id=' + id + '&dockpoints=' + dockPoints
		xmlhttpPost(url, query, 'delete', id);
	}
}

function hidePost(id) {
	url = 'http://www.psxextreme.com/users/hidecomment.asp';
	query = 'id=' + id;
	xmlhttpPost(url, query, 'hide', id);
}

function unhidePost(id) {
	url = 'http://www.psxextreme.com/users/unhidecomment.asp';
	query = 'id=' + id;
	xmlhttpPost(url, query, 'unhide', id);
}

function showPost(id) {
	url = 'http://www.psxextreme.com/users/showcomment.asp';
	query = 'id=' + id;
	xmlhttpPost(url, query, 'show', id);
}

function blockUser(id) {
	thedate = prompt('What date should this user be blocked until?', 'mm/dd/yyyy');
	if (thedate != '' && thedate != null) {
		url = 'http://www.psxextreme.com/users/blockuser.asp';
		query = 'id=' + id + '&date=' + thedate
		xmlhttpPost(url, query, 'block', id);
	}
}

function agree(id) {
	url = 'http://www.psxextreme.com/users/agreeordisagree.asp';
	query = 'id=' + id + '&agree=true';
	xmlhttpPost(url, query, 'agree', id);
}

function disagree(id) {
	url = 'http://www.psxextreme.com/users/agreeordisagree.asp';
	query = 'id=' + id + '&agree=false';
	xmlhttpPost(url, query, 'disagree', id);
}

function reportSpam(id) {
	forSure = confirm('Are you sure you want to report this as spam?');
	if (forSure) {
		url = 'http://www.psxextreme.com/users/report.asp';
		query = 'id=' + id;
		xmlhttpPost(url, query, 'report', id);
	}
}

function onAgreedOrDisagreed(str, id) {
	if (str == '' && str == null)
		return;
	else if (str.substring(0, 8) == '<strong>')
		document.getElementById('agdg_' + id).innerHTML = str;
	else
		alert(str);
		
	resize_height();
}

function spamReported(str, id) {
	if (str != '' && str != null)
		alert(str);
}

function stripAnchor(str) {
	str = str + "";
	pos = str.indexOf("#");
	if (pos == -1)
		return str;
	return str.substring(0, pos);
}
