// ------------------------------------------------------------
// 
// ------------------------------------------------------------
var g_sidenoteId = 1;

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
$(document).ready(
	function ()
	{	
		$("dl.post dd.copy a[@title='link'],dl.post dd.copy a[@title='note']")
			.hide()
			.addClass("sidenote")
			.each(createSidenote);
			
		$(document).click(handleDocumentClick);
	}
);

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function createSidenote ()
{
	var sidenote = $("<div></div>");
	var content = $(this).html();
	
	if ($(this).is("a[@title='link']"))
	{
		var externalLink = $("<a href=\"" + $(this).attr("href") + "\" class=\"visit\" target=\"_blank\">visit this referenced source</a>");
		
		// externalLink.click();
		
		sidenote.append(externalLink);
				
		// console.log($(this).attr("href").match(/(http\:\/\/[^\/]*)/)[0] + ":" + $(this).attr("href"));
		
		var baseUrl = $(this).attr("href").match(/(https?\:\/\/[^\/]*)/)[0];
		
		sidenote.append("<img src=\"" + baseUrl + "/favicon.ico?d=2\" class=\"favicon\" />");
		
		sidenote.append("<h3>" + content.substring(0, content.indexOf("|")) + "</h3>");
		
		sidenote.addClass("sidenote-link");
		
		// alert(baseUrl);
		
		$("img.favicon", sidenote).error(handleFaviconFailure);
		
		content = content.substring(content.indexOf("|") + 1);
		
		$(this).addClass("sidenote-link");

		$(this).html("<img src=\"http://researchpuzzle.com/_resources/img/post/icon_sidenote_link.gif\" />");
	}
	else
	{
		sidenote.addClass("sidenote-note");

		$(this).addClass("sidenote-note");

		$(this).html("<img src=\"http://researchpuzzle.com/_resources/img/post/icon_sidenote_note.gif\" />");
	};
	
	var closeLink = $("<a href=\"#\" class=\"close\">close</a>");
	
	closeLink.click(handleSidenoteClose);

	sidenote.append("<p>" + content + "</p>");
	
	sidenote.append(closeLink);
	
	sidenote.addClass("sidenote");
	
	sidenote.attr("id", "sidenote-" + g_sidenoteId);
	
	sidenote.css({
		"top": 0,
		"left": 0
	});
	
	sidenote.click(handleSidenoteClick);
	
	$(this).removeAttr("title");
	
	$(this).attr("sidenoteid", g_sidenoteId);
	
	$(this).click(handleSidenoteLinkClick);
	
	$(this).show();
	
	$("body").append(sidenote);
	
	g_sidenoteId++;
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function handleFaviconFailure (e)
{
	$(this).css("visibility", "hidden");
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function handleSidenoteClick (e)
{
	return $(e.target).is("a.visit") ? true : false;
};

function handleDocumentClick ()
{
	$("div.sidenote").hide();
};

function handleSidenoteLinkClick ()
{
	this.blur();
	
	$("div.sidenote").hide();
	
	var o = $(this).offset();
		
	var sidenote = $("#sidenote-" + $(this).attr("sidenoteid"));
	
	sidenote.css("top", o.top - ($.browser.msie ? 5 : 3));

	sidenote.css("left", o.left - 5);
	
	sidenote.show();
	
	return false;
};

function handleSidenoteClose ()
{
	this.blur();

	$(this).parent().hide();
	
	return false;
};

