browserHeight = window.innerHeight; // 600
midpoint = browserHeight / 2; // 300
quarterpoint = (midpoint / 2) + midpoint // 450
currentItem = 0;
padding = 5;
previousHeight = 1;
prevItem = 0;
numPosts = 10;
thisHeight = new Array();
prevHeight = new Array();
prevFocus = 0;
heightText = '';
logger = 0;
postOrder = new Array();
scrollPosition = $('#window').scrollTop(); // 0
var http = false;
http = (navigator.appName == "Microsoft Internet Explorer") ? http = new ActiveXObject("Microsoft.XMLHTTP") : http = new XMLHttpRequest();
function log(action, object) {
var url = "ajax.html?action=" + escape(action) + "&object=" + escape(object);
http.open("GET", url, true);
http.onreadystatechange=function() {
//if(http.readyState == 4) { window.location=url; }
}
http.send(null);
}
function ajax(action, object) {
var url = "ajax.html?action=" + escape(action) + "&object=" + escape(object);
http.open("GET", url, true);
http.onreadystatechange=function() {
if(http.readyState == 4) {
if (http.responseText == "Refresh") {
window.location.reload();
}
}
}
http.send(null);
}
function animateScroll(where) {
if ($('#c' + where).offset() != null) {
$('#window').animate({
scrollTop: $('#c' + where).offset().top
}, 2000);
}
else {
var url = "ajax.html?action=" + escape('get post ID from comment ID') + "&object=" + escape(where);
http.open("GET", url, true);
http.onreadystatechange=function() {
if(http.readyState == 4) {
window.location = 'index.html?post_id=' + http.responseText;
}
}
http.send(null);
}
}
function getHeights() {
var total = 0;
for (var i=0; i < numPosts; i++) {
total = total + itemHeight(i);
thisHeight[i] = itemHeight(i);
prevHeight[i] = total - itemHeight(i);
}
}
function itemHeight(div) {
return $('#' + postOrder[div]).outerHeight(); // 100 + 5
}
function whereFocus(scroll) {
var total = 0;
for (var i=0; i < numPosts; i++) {
if (prevHeight[i] < (scroll + browserHeight/4) && prevHeight[i+1] >= (scroll + browserHeight/4)) {
prevItem = currentItem;
focusCurrentItem(i);
}
}
}
function evalScroll() {
newPosition = $('#window').scrollTop(); // 15
prevNum = currentItem - 1;
whereFocus(newPosition);
}
function focusCurrentItem(num) {
$('.post').removeClass('shadow');
$('#' + postOrder[num]).addClass('shadow');
if (currentItem != num) {
log('focus', $('#' + postOrder[num]).attr('id') );
}
currentItem = num;
}
function postComment(post_id) {
var commentText = $('#comment-text-' + post_id).val();
var url = "ajax.html?action=postComment&post_id=" + post_id + "&text=" + escape(commentText);
http.open("GET", url, true);
http.onreadystatechange=function() {
if(http.readyState == 4) {
var newText = commentText.replace("\n\n", "
");
newText = newText.replace("\n", "
");
$('#comment-text-' + post_id).before( '
' + newText + '