
var isIE = testIE();
var checkInterval = 200;
var preloadDistance = 1500;
var isUpdating = false;
var mouseState = "up";
var id;

$(document).ready(function() { 
	
	
});

function testIE() {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) return true;
	else return false;
}
		
function getMoreHistory() {
	var indexVal = parseInt($("#current_page").val());
	var url = $("#url").val();
	var cat= $("#cat").val();
	if (indexVal <= parseInt($("#total_pages").val())) {
		$.post("/templates/feed/entry-detail.php", { url: url, cat: cat, startRow: indexVal  },addMoreHistory);
		$("#moreload").show();
	}
	return false;
}

function failure() {
	$("#new_page_content").innerHTML = "<p><strong>Could not contact the server.</strong><br />Please wait awhile and try again. <br /><br />We apologize for the inconvenience.</p>";
}

function addMoreHistory(data) {
	response = data.substring(0, data.length)
	if (response == "No results") {
		$("#moreload").hide();
	} else {
		//$("#new_page_content").html($("#new_page_content").html() + response);
		$("#new_page_content").before(response);
		$("#moreload").hide();
		$("#current_page").val(parseInt($("#current_page").val()) + parseInt($("#page_limit").val()));
	}
	if (navigator.userAgent.indexOf("Safari") == -1 && jQuery.isFunction($.fn.lazyload)) {
		$("#content_container img").each(function() {
			if($(this).attr("src") == "_gfx/whiterati.gif") $(this).attr("src",$(this).attr("original"))
		});
		$("#content_container img").lazyload({ placeholder : "_gfx/whiterati.gif",  threshold : 100, effect : "fadeIn" });
	}
	isUpdating = false;
}

function updatePage(){
	if ((isIE && isUpdating == false && getPageHeight() - getScrollHeight() < preloadDistance) || (!isIE && mouseState == "up" && isUpdating == false && getPageHeight() - getScrollHeight() < preloadDistance)) {
		
		isUpdating = true;
		getMoreHistory();
	}
	
	if (parseInt($("#current_page").val()) < parseInt($("#total_pages").val()) && parseInt($("#total_pages").val()) != 1) {
		setTimeout("updatePage()", checkInterval);
	}
	
}

function onMouseDown(){
	mouseState = "down";
}

function onMouseUp(){
	mouseState = "up";
}


function init_feed( ){
	document.onmousedown = onMouseDown;
	document.onmouseup = onMouseUp;

	id = $("page_id").value;

	var fromBackButton = false;
	setTimeout("updatePage()", checkInterval);

}

function checkPaginate() {
	if ((isIE && isUpdating == false && getPageHeight() - getScrollHeight() < preloadDistance) || (!isIE && mouseState == "up" && isUpdating == false && getPageHeight() - getScrollHeight() < preloadDistance)) {
		isUpdating = true;
		getMoreHistory();
	}
}



function getPageHeight(){
  var y;
  var test1 = document.body.scrollHeight;
  var test2 = document.body.offsetHeight
  if (test1 > test2) {
    y = document.body.scrollHeight;
  } else {
	  y = document.body.offsetHeight;
  }
  return parseInt(y);
}

function _getWindowHeight(){
  if (self.innerWidth) {
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientWidth) {
    frameWidth = document.documentElement.clientWidth;
    frameHeight = document.documentElement.clientHeight; 
  } else if (document.body) {
    frameWidth = document.body.clientWidth;
    frameHeight = document.body.clientHeight;
  }
  return parseInt(frameHeight);
}

function getScrollHeight(){
  var y;
  // all except Explorer
  if (self.pageYOffset) {
      y = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
      y = document.documentElement.scrollTop;
  } else if (document.body)	{
      y = document.body.scrollTop;
  }
  return parseInt(y)+_getWindowHeight();
}

$(document).ready(function() { init_feed(); });