var feeds = new Array();

function addToFeeds (url) {
  feeds[feeds.length] = {
    lastupdated: 0,
    url: url
  };
  return true;
}

function updateFeedTime (index) {
  feeds[index]['lastupdated'] = parseInt(new Date().getTime().toString().substring(0, 10));
  return true;
}

function hideChangeFeed () {
  $('#changeFeedBox').hide ();
}

var keepScrolling = false;

function scrollNow (scrollBy) {
  if (keepScrolling) {
    window.scrollBy(0,scrollBy);
    scrolldelay = setTimeout('scrollNow('+scrollBy+')',100);
  }
}

function countNumberEntirelyOnScreen () {
  var numAcross = parseInt ($(window).width() / 200);
  var numHeight = parseInt ($(window).height() / 150);
  
  var totalOnScreen = numAcross * numHeight;
  
  //  Make sure that this number isn't higher than the number of .items
  if ($('#allItems > .item').length < totalOnScreen) totalOnScreen = $('#allItems > .item').length;
  
  return totalOnScreen;
}

function fixItemHeights () {
	for (var i = 0, numItems = $('#allItems > .item').length; i < numItems; i++) {
		while ($('#allItems > .item:eq(' + i + ')').height() > 150) {
			$('#allItems > .item:eq(' + i + ') > a').css ('font-size', parseInt ($('#allItems > .item:eq(' + i + ') > a').css ('font-size')) - 1);
		}
	}
}

function updateDynFeed () {
	for (var q = 0, numFeeds = feeds.length; q < numFeeds; q++) {
		$.get ('dyn-feed.php', { f: feeds[q]['url'], newestfeed: feeds[q]['lastupdated'] }, function(data){
			for (var i = 0, itemLen = data.childNodes[0].childNodes.length; i < itemLen; i++) {
				prependToItem = Math.floor(Math.random()*countNumberEntirelyOnScreen());
				var newDivForItem = document.createElement ('div');
				newDivForItem.setAttribute ('class', 'item');
				var newANode = document.createElement ('a');
				newANode.setAttribute ('href', data.childNodes[0].childNodes[i].firstChild.getAttribute ('href'));
				newANode.setAttribute ('title', data.childNodes[0].childNodes[i].firstChild.getAttribute ('title'));
				newANode.setAttribute ('target', data.childNodes[0].childNodes[i].firstChild.getAttribute ('target'));
				var newPNodeForANode = document.createTextNode (data.childNodes[0].childNodes[i].firstChild.firstChild.nodeValue);
				newANode.appendChild (newPNodeForANode);
				newDivForItem.appendChild (newANode);
				$('#allItems > .item:eq('+prependToItem+')').before (newDivForItem);
				$('#allItems > .item:eq('+prependToItem+')').css ('font-style', 'italic');
				newestFeed = parseInt(new Date().getTime().toString().substring(0, 10));
			}
		}, "xml");
		updateFeedTime (q);
		fixItemHeights ();
	}
}
  
$(document).ready (function () {
  $('#showInfo1').click(function(){
    $('#allItems, #justSomeNotes').toggle();
  });
  
  $('#navigation,#changeFeedBox,#addFeedBox').css ('position', 'fixed');
  $('#navigation,#changeFeedBox,#addFeedBox').css ('top', '12px');
  $('#navigation').css ('left', $(document).width() - 30);
  $('#changeFeedBox').css ('left', $(document).width() - 60 - $('#changeFeedBox').width());
  $('#addFeedBox').css ('left', $(document).width() - 60 - $('#addFeedBox').width());
  
  $(window).bind ("resize", function () { $('#navigation').css ('left', $(document).width() - 30); });
  
  $('#scrollDown').bind ("mousedown", function () { keepScrolling = true; scrollNow (20); });
  $('#scrollDown').bind ("mouseup", function () { keepScrolling = false; });
  
  $('#scrollUp').bind ("mousedown", function () { keepScrolling = true; scrollNow (-20); });
  $('#scrollUp').bind ("mouseup", function () { keepScrolling = false; });
  
  $('#changeFeed').bind ("click", function () { $('#changeFeedBox').show (); });
  $('#addFeed').bind ("click", function () { $('#addFeedBox').show (); });
  
  setInterval ('updateDynFeed()', 300001);
  
  fixItemHeights ();
});
