$(document).ready(function()
{
	$("#shareThis").append('<a href="#" id="tweetThis"><img src="/image/ui/twitter-icon.png" alt="Tweet This" /></a>');
	
	twttr.anywhere(function(twitter)
	{
		twitter.hovercards();
	});
	
	$("#tweetThis").click(function(event)
	{
		event.preventDefault();
		$("#tweetBox").remove();
		$("body").append("<div id=\"tweetBox\"></div>");
		viewportWidth = $("html").innerWidth();
		leftPos = (viewportWidth/2)-263;
		$("#tweetBox").css({left: leftPos+"px"});
		$("#tweetBox").prepend('<div class="menuBar"><img src="/image/ui/twitter-icon.png" alt="Tweet" /><img src="/image/ui/16x16_close_white.png" alt="Close" class="twitterClose" /></div>');
		$("#tweetBox").draggable({handle:".menuBar", cursor: "default"});
		twttr.anywhere(function(twitter)
		{
			twitter("#tweetBox").tweetBox({label: "Tweet this:", defaultContent: $("h1").text() + " " + bitlyShortUrl});
		});
		focusTweet();
	});
	
	$("#tweetBox .twitterClose").live("click",function()
	{
		$("#tweetBox").remove();
	});
	$("#linkedInBox .linkedInClose").live("click",function()
	{
		$("#linkedInBox").remove();
	});
	$("#emailBox .emailClose").live("click",function()
	{
		$("#emailBox").remove();
	});
	
	BitlyCB.shortenResponse = function(data)
	{
		var first_result;
		for (var r in data.results)
		{
			first_result = data.results[r]; break;
		}
		bitlyShortUrl = first_result.shortUrl;
	}
	BitlyClient.shorten(window.location, 'BitlyCB.shortenResponse');
});

function focusTweet()
{
	if($(".twitter-anywhere-tweet-box").length > 0)
	{
		$(".twitter-anywhere-tweet-box").contents().find("#tweet-box").focus();
	}
	else
	{
		setTimeout("focusTweet()",100);
	}
}

$(document).ready(function()
{
	var linkedInUrl = "http://www.linkedin.com/shareArticle?mini=true"
		+ "&url=" + escape(window.location.href).replace(/\+/g,"%252B")
		+ "&title=" + escape($("h1").text())
		+ "&summary=" + escape($("#prContent").text().substring(0,300)+"...")
		+ "&source=" + escape("ABI Research");
	$("#shareThis").append('<a href="'+linkedInUrl+'" id="linkedIn"><img src="/image/ui/linkedin-icon.png" alt="LinkedIn" /></a>');
	$("#linkedIn").click(function(event)
	{
		event.preventDefault();
		var linkedInUrl = "http://www.linkedin.com/shareArticle?mini=true"
			+ "&url=" + escape(window.location.href).replace(/\+/g,"%252B")
			+ "&title=" + escape($("h1").text())
			+ "&summary=" + escape($("#prContent").text().substring(0,300)+"...")
			+ "&source=" + escape("ABI Research");
		$("#linkedInBox").remove();
		$("body").append("<div id=\"linkedInBox\"></div>");
		viewportWidth = $("html").innerWidth();
		leftPos = (viewportWidth/2)-263;
		$("#linkedInBox").css({left: leftPos+"px"});
		$("#linkedInBox").prepend('<div class="menuBar"><img src="/image/ui/linkedin-icon.png" alt="LinkedIn" /><img src="/image/ui/16x16_close_white.png" alt="Close" class="linkedInClose" /><iframe id="linkedInContent" style="position: absolute; top: 40px; left: 20px;" height="570" width="520" scrolling="no" src="'+linkedInUrl+'"></iframe></div>');
		$("#linkedInBox").draggable({handle:".menuBar", cursor: "default"});
	});
});

$(document).ready(function()
{
	$("#emailThis").click(function(event)
	{
		var url = escape(window.location.href);
		$("#emailBox").remove();
		$("body").append("<div id=\"emailBox\"></div>");
		viewportWidth = $("html").innerWidth();
		leftPos = (viewportWidth/2)-263;
		$("#emailBox").css({left: leftPos+"px"});
		$("#emailBox").prepend('<div class="menuBar"><img src="/image/emailicon.gif" alt="Email" /><img src="/image/ui/16x16_close_white.png" alt="Close" class="emailClose" /></div>');
		$("#emailBox").append('<p>Your e-mail address:</p><input type="text" id="senderEmail" /><br /><p>Your name:</p><input type="text" id="name" /><br /><p>Recipient\'s e-mail address:</p><input type="text" id="recipientEmail" />');
		$("#emailBox").append('<br /><br /><a href="#" class="button" id="sendEmail">Send e-mail</a>');
		$("#emailBox").draggable({handle:".menuBar", cursor: "default"});
		$("#senderEmail").focus();
		
		$("#sendEmail").click(function()
		{
			recipient = $("#recipientEmail").val();
			sender = $("#senderEmail").val();
			name = $("#name").val();
			$("#emailBox").remove();
			createWaitMessage("Sending e-mail...");
			
			$.ajax(
			{
				type: "POST",
				url: "/mailpage.jsp",
				data: "url="+encodeURIComponent(url) +
					"&recipientEmail="+encodeURIComponent(recipient) +
					"&senderEmail="+encodeURIComponent(sender) +
					"&name="+encodeURIComponent(name) +
					"&subject="+encodeURIComponent($("h1").text()),
				success: function(response)
				{
					createMessage(response);
				}
			});
		});
	});
});