// ==UserScript==
// @name           Delicious.com Private Bookmarks Only
// @namespace      http://blog.jacobodevera.com
// @description    Adds links to hide/show shared bookmarks in a user's page in delicious.com. This allows the user to see a list of their private bookmarks, a functionality that delicious.com does not provide yet.
// @author         Jacobo de Vera
// @include        http://delicious.com/*
// @require        http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==


// ***********************************************************************
// Some functions
// ***********************************************************************

function isUserPage()
{
	return $("body").hasClass("userposts");
}


function getAllBookmarks()
{
	return $("li.post");
}


function isPrivate(bookmark)
{
	return $(bookmark).hasClass("isPrivate")
}


function hasDate(bookmark)
{
	return $("div.dateGroup", bookmark).size() != 0;
}


function setDate(bookmark, theDate)
{
	$("div.bookmark", bookmark).append(theDate);
}


function getDate(bookmark)
{
	return $(".dateGroup", bookmark);
}


/**
 * Adjust dates so that the correct date will be shown in the first bookmark
 * of the day when all public bookmarks are hidden.
 *
 * In Delicious, only the first bookmark saved in a specific day carries the
 * date. The following bookmarks are assumed to have been saved the same day,
 * until a new date is specified.
 *
 * This is a problem when the bookmark carrying the date is public and it is
 * hidden, as any private bookmark saved in the same day will be grouped to
 * whichever the last date was.
 *
 * This function reads every date and assigns it to the first private bookmark
 * saved in that particular day, marking it as a duplicate for future deletion
 * (if the public bookmarks are shown again).
 */
function setDateToFirstPrivateBookmark()
{
	var isLastDatePrivate = false;
	var lastDate = 0;
	var lis = getAllBookmarks();

	for (var i = 0; i < lis.size(); i++) {
		item = lis[i];
		if (hasDate(item)) {
			lastDate = getDate(item); 
			isLastDatePrivate = isPrivate(item);
		}
		else {
			// Only set a date to an item if it is private and there isn't a
			// previous private bookmarks with the same date:
			if (isPrivate(item) && !isLastDatePrivate) {
				// Mark duplicate dates to remove them later
				setDate(item, lastDate.clone().addClass("dupDate"));
				isLastDatePrivate = true;
			}
		}
	}
}


/**
 * Remove dates from private bookmarks that had one copied from the first
 * public bookmarks of the same day.
 *
 * This is used to undo the efects of setDateToFirstPrivateBookmark, brings
 * everything back to normal when both private and public bookmarks are
 * shown again.
 */
function removeDupDates()
{
	$(".dupDate").remove();
}


function hidePublicBookmarks()
{
	setDateToFirstPrivateBookmark()
	$('ul.bookmarks li.post:not(.isPrivate)').hide();
}

function showPublicBookmarks()
{
	removeDupDates();
	getAllBookmarks().show();
}

// ***********************************************************************
// End of functions
// ***********************************************************************

// All those changes only make sense in a user's page
if (isUserPage()) {

	// Insert filter menu
	$("p#alt_message").after('<p id="filters" class="altMsg">Filters: </p>')
	var filters = $("p#filters");

	// Appy some style (copied from surrounding areas)
	filters.css({'border-top':'1px solid #DDDDDD', 'height' : '1.4em', 'padding': '2px 0 0'})

	// Add links
	filters.append('<a href="#" id="showprivate">Show only private bookmarks</a>');
	$("a#showprivate").click( function() { hidePublicBookmarks(); });

	filters.append('<em>|</em>'); // Separator

	filters.append('<a href="#" id="showpublic">Show public & private bookmarks</a>');
	$("a#showpublic").click(  function() { showPublicBookmarks(); });

}
