$(function()
{
	var images = $("#drag2share img");
	var title = $("title").text() || document.title;
	var title2 = title.replace(/ /gi, "+");
	title2 = title2.replace(/"/gi, '');
	title2 = title2.replace(/!/gi, '');
	title2 = title2.replace(/@/gi, '');
	title2 = title2.slice(33);
	if(title2.length > 50)
	{
		title2 = title2.slice(0,50) + "...";	
	}
	
	images.draggable(
	{
	    helper: function()
		{
			return $("<div>").attr("id", "helper").html('Share on ...').appendTo("body");
	  	},
	 	cursor: "pointer",
	  	cursorAt: { left: -10, top: 20 },
	  	scroll: false,
	  	zIndex: 99999,
	  	//show overlay and targets
	  	start: function()
		{
			$("<div>").attr("id", "overlay").css("opacity", 0.7).appendTo("body");
			$("#overlay").css("top", $(window).scrollTop());
			$("#tip").remove();
			$(this).unbind("mouseenter");
			$("#targets").css('z-index', 99999);
			$("#targets").css({left:($("#drag2share img").offset().left - 60), top:($("#drag2share img").offset().top - 60)}).fadeIn(500);
			$("#drag2share img").attr("src","images/empty.png");
	  	},
		//remove targets and overlay
		stop: function()
		{
			$("#targets").fadeOut(500);
			$(".share", "#targets").remove();
			$("#overlay").remove();
			$(this).bind("mouseenter", createTip);
			$("#drag2share img").attr("src","images/drag-to-share.png");
		}
	});
	
	$("#targets li").droppable(
	{
		tolerance: "pointer",
		over: function()
		{
			if($(this).attr("id") != "share")
			{
				$("#helper").html("Share on " + $(this).attr("id"));
				$(this).css("background-image","url(images/" + $(this).attr("id") + "_ro.png)");
			} else
			{
				$("#helper").html("Share on ...");	
			}
		},
		out: function()
		{
			if($(this).attr("id") != "share")
			{
				$(this).css("background-image","url(images/" + $(this).attr("id") + ".png)");
			}
		},
		drop: function()
		{
			$(this).css("background-image","url(images/" + $(this).attr("id") + ".png)");
			var id = $(this).attr("id"),
			currentUrl = window.location.href,
			baseUrl = $(this).find("a").attr("href");
											
			if (id.indexOf("twitter") != -1)
			{
				window.open(baseUrl + "/home?status=" + title2 + "+:+" + currentUrl);
			} else if (id.indexOf("delicious") != -1)
			{
				window.open(baseUrl + "/save?url=" + currentUrl + "&title=" + title);
			} else if (id.indexOf("facebook") != -1)
			{
				window.open(baseUrl + '/sharer.php?u=' + currentUrl + '&t=' + title);
			} else if (id.indexOf("stumbleupon") != -1)
			{
				window.open(baseUrl + "/submit?url=" + currentUrl + "&topic=" + title);	
			} else if (id.indexOf("delicious") != -1)
			{
				window.open(baseUrl + "/save?url=" + currentUrl + "&title=" + title);	
			} else if (id.indexOf("linkedin") != -1)
			{
				window.open(baseUrl + "/faves?add=" + currentUrl + "&title=" + title);
			}
	 	}		  
	});
	var createTip = function(e)
	{
		($("#tip").length == 0) ? $("<div>").html("<span>Drag to share event<\/span><span class='arrow'><\/span>").attr("id", "tip").css({left:e.pageX + 30,top:e.pageY - 16}).appendTo("body").fadeIn(1000):null;
		images.bind("mouseenter", createTip);
		images.mousemove(function(e)
		{
			$("#tip").css({ left:e.pageX +30, top:e.pageY - 16}); 
		});
		images.mouseleave(function()
		{
			$("#tip").remove(); 
		});
	}
	$(document).ready(function(){
		$("#footer img").bind("mouseenter", createTip);
});
});
