var currUsers = new Array() // should get changed by sidebar
var lastUpdate = 0
var working = false

function setCurrUsers(value)
{	
	currUsers = value
}
function getCurrUsers()
{
	return currUsers
}
function setLastUpdate(v)
{
	lastUpdate = v
}
function getLastUpdate()
{
	return lastUpdate
}
function setWorking(value)
{
	working = value
}


function in_array(needle, haystack)
{
	for (var j=0; j<haystack.length; j++)
		if (haystack[j] == needle)
			return true
	return false
}

var thisUser = ""

function checkForUpdates()
{
	if (working) return
	setWorking(true)
	$.get('/updatesidebar/user',{lastupdate:getLastUpdate()}, function(data)
		{
			cu = getCurrUsers()
			var startup = (cu.length==0)
			var newUsers = eval(data)
			if (newUsers.length)
			{
				for (var i=0; i<newUsers.length; i++)
					if (!in_array(newUsers[i].id, cu) && (newUsers[i].id!=thisUser))
					{ 
						if (!cu.length)
							$('#sidebarframe ul')
								.empty() // in case there was a "loading" entry
						cu.unshift(newUsers[i].id)
						newListItem = document.createElement('li')
						$(newListItem)
							.toggle(startup)	// if first time just make visible
							.append('<a href="/view/user/'+newUsers[i].id+'">'+newUsers[i].name+'</a>')
							.prependTo('#sidebarframe ul')
							.slideDown('slow', function() {
								$(this).css('display','list-item')
								} )
					}
			}
			// no users, or not recent (maybe MySQL crashed)
			else
				$('#sidebarframe ul')
					.empty()
					.append('<i>No one</i>')
			if (cu.length > 10)
			{
				$('#sidebarframe li')
					.slice(10)	// grab the ones larger than 10
					.slideUp('slow',function() {
						$(this).remove()
						})
				cu.slice(10)
			}
			setCurrUsers(cu)
			setLastUpdate(new Date().getTime())
			setWorking(false)
			
			// call again in 10 seconds
			setTimeout(checkForUpdates, 10000)
		})
	
	
}

function updateRecentPages()
{
	$.get('/updateRecent/user', function(data) {
		var pages = JSON.parse(data)
		var $ul = $('#recentUL').empty()
		for (var i=0; i< pages.length; i++)
		{
			$ul
				.append(
					$('<li><a href=\"'+pages[i].link+'\">'
						+pages[i].name
						+'</a></li>')
					)
		}
	} )
}

// call once on startup
$(function() {
	checkForUpdates()
} )

var skipAnimation = false

// move footer up & down
$(function() {
	if (!skipAnimation)
	{
	$('.footerleft, .footerright')
		.delay(1500)
		.slideDown('slow', function() {
			$('.footerleft').show()
			} )
	$('.footer')
		.delay(500)
		.slideDown('slow')
		
	$('#closebox')
		.click(function() {
			$('.footerleft, .footerright')
				.slideUp('', function() {
					$('.footer').slideUp('')
					})
		})
	}
	else // skip animation
	$('#closebox')
		.click( function() {
			$('.solidfooter')
				.hide()
			} )
	})




