
var isIE = testIE();
var checkInterval = 200;
var preloadDistance = 100;
var isUpdating = false;
var mouseState = "up";
var id;

$(document).ready(function() { 
	resizeContainer();
	document.onmousedown = onMouseDown;
	document.onmouseup = onMouseUp;

	id = $("page_id").value;

	var fromBackButton = false;
	setTimeout("updatePage()", checkInterval);
	checkForSound();
	
	var me = (location.host.indexOf("cargocollective.com") >= 0) ? "http://"+location.host+"/"+$("#url").val() : "http://"+location.host;
	shortcut.add("Shift+F",function() { top.document.location.href=me+"/following" },{ 'type':'keydown', 'disable_in_input':true, 'keycode':70 });
});

function resizeContainer() {
	var total = 50; // start size for the loadanim
	
	$(".entry").each(function() { 
		entryid = $(this).attr("id");
		id = entryid.replace("entry_","");
		if($(".slideshow_container_"+id).length) {
			var final_w = img_pad = 0;
			$(".slideshow_container_"+id+" img").each(function() { 
				final_w = ($(this).width() > final_w) ? $(this).width() : final_w;
			});
			
			var pad_r = parseInt($(".project_content").css("padding-right").replace(/[^0-9]/g,""));
			var pad_imr = parseInt($(".project_content img").css("padding-right").replace(/[^0-9]/g,"")) ;
			var marg_r = parseInt($(".project_content").css("margin-right").replace(/[^0-9]/g,""));
			var marg_imr = parseInt($(".project_content img").css("margin-right").replace(/[^0-9]/g,""));
			var sldmar_r = parseInt($(".slideshow_component").css("margin-right").replace(/[^0-9]/g,""));
			var sldpad_imr = parseInt($(".slideshow_component").css("padding-right").replace(/[^0-9]/g,""));
			pad_r = pad_r > -1 ? pad_r : 0;
			pad_imr = pad_imr > -1 ? pad_imr : 0;
			marg_r = marg_r > -1 ? marg_r : 0;
			marg_imr = marg_imr > -1 ? marg_imr : 0;
			sldmar_r = sldmar_r > -1 ? sldmar_r : 0;
			sldpad_imr = sldpad_imr > -1 ? sldpad_imr : 0;
			final_w += pad_r + marg_r + marg_r + marg_imr + sldpad_imr + sldmar_r;
			
			//$(this).width(final_w);
			total += parseInt(final_w);
			
		} else total += parseInt($(this).width());
	});
	$("#content_container").width(total);
}

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();
	var design= $("#design").val();
	if (indexVal <= parseInt($("#total_pages").val())) {
		$.post("/designs/feed/"+$("#page_type").val()+"-pagination.php", { url: url, cat: cat, design: design, 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" });
	}
	resizeContainer();
	isUpdating = false;
}

function updatePage(){
	var container_w = $("#content_container").width();
	var scroll_w = getScrollWidth();
	if ((isIE && isUpdating == false && container_w - scroll_w < preloadDistance) || (!isIE && mouseState == "up" && isUpdating == false && container_w - scroll_w < 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 _getWindowWidth(){
  if (self.innerWidth) {
		frameWidth = self.innerWidth;
  } else if (document.documentElement && document.documentElement.clientWidth) {
    frameWidth = document.documentElement.clientWidth;
  } else if (document.body) {
    frameWidth = document.body.clientWidth;
  }
  return parseInt(frameWidth);
}

function getScrollWidth(){
  var x;
  // all except Explorer
  if (self.pageXOffset) {
      x = self.pageXOffset;

  } else if (document.documentElement && document.documentElement.scrollLeft) {
      x = document.documentElement.scrollLeft;
  } else if (document.body)	{
      x = document.body.scrollLeft;
  }
  return parseInt(x)+_getWindowWidth();
}
