$(document).ready(function(){
	/*/ Debugging. Prepend a "/" to enable
	$('#dropMessage').show();
	/**/
	
	$("#bookmarkSource").draggable();
	$("#bookmarkUsed").draggable();
	$("#content").droppable({
		drop: function(event, ui) {
			// Find position relative to #content
			var content = $("#content");
			var co = content.offset();
			var left = Math.round(ui.offset.left - co.left);
			var top = Math.round(ui.offset.top - co.top);
			var location = left + ' ' + top;
			
			// Save position and URL in cookie
			// Create URL object from the string. Otherwise changes to the URL would cause a document reload.
			var url = $.url(document.location.href);
			url.param('left', left);
			url.param('top', top);
			$("#dropMessage").html('img=' + ui.offset.left + ' ' + ui.offset.top + '<br />\n'
			 + 'content=' + co.left + ' ' + co.top + '<br />\n'
			 + 'location=' + location + '<br />\n'
			 + 'url ' + url.toString());
			$.cookie(BOOKMARK_COOKIE, url);
			
			// Hide the source bookmark and reset its position
			$('#bookmarkParent').hide();
			$('#bookmarkSource').css({'left': '0', 'top': '0', 'position': 'relative'}); 
			
			// Show the bookmark links
			$('#bookmarkLinks').show();

			// Move the "used bookmark" to the drop location and show it
			var bookmark = $('#bookmarkUsed');
			bookmark.css({'left': left+'px', 'top': top+'px'});
			bookmark.show();
		}
	});
	
	// Is there a cookie?
	var url = $.cookie(BOOKMARK_COOKIE);
	if (url) {
		// Analyze the URL
		url = $.url(url);
		var left = url.param('left');
		var top = url.param('top');
		url.attr('query', null);
		url.attr('hash', null);
		var withoutQuery = url.toString();
		
		// Analyze the URL of the current document
		var currentUrl = $.url(document.location.href);
		var scroll = !(currentUrl.param('left') === null) && !(currentUrl.param('top') === null);
		currentUrl.attr('query', null);
		currentUrl.attr('hash', null);
		currentUrl = currentUrl.toString();
		
		// If the bookmark is for the current page
		var bookmark = $("#bookmarkUsed");
		if (currentUrl == withoutQuery) {
			// Move the "used bookmark and show it"
			bookmark.css({'left': left+'px', 'top': top+'px'});
			bookmark.show();
			$("#dropMessage").html('Moved to ' + left + ' ' + top);

			// Hide the source bookmark and show the links
			$('#bookmarkParent').hide();
			$('#bookmarkLinks').show();
			
			// If we came from a different page, then scroll to the used bookmark, now
			if (scroll) {
				var offset = bookmark.offset();
				$("#dropMessage").html('Scroll to ' + 0 + ' ' + offset.top);
				// Can't scroll right away
				bookmark.oneTime(500, function() {
					window.scrollTo(0, offset.top - 40);
				});
			}
		} else {
			// There is a bookmark but it points to a different page.
			// Hide the source bookmark (the used bookmark is already invisible)
			// and show the links
			$('#bookmarkParent').hide();
			$('#bookmarkLinks').show();
			$("#dropMessage").html('Bookmark is on a different page<br />cookie=' + withoutQuery+"<br />document="+currentUrl);
		}
	} else {
		$("#dropMessage").html('No cookie');
	}
});

function goToBookmark() {
	var bookmark = $("#bookmarkUsed");
	if (bookmark.is(':visible')) {
		var offset = bookmark.offset();
		$("#dropMessage").html('Scroll to ' + offset.left + ' ' + offset.top);
		window.scrollTo(offset.left, offset.top - 40);
	} else {
		document.location = $.cookie(BOOKMARK_COOKIE);
	}
	return false;
}

function resetBookmark() {
	$.cookie(BOOKMARK_COOKIE, null);
	$("#bookmarkUsed").hide();
	$('#bookmarkParent').show();
	$('#bookmarkSource').css({'left': '0', 'top': '0', 'position': 'relative'}); 
	$('#bookmarkLinks').hide();
	$("#dropMessage").html('Reset.');
	return false;
}

