$(function() {

//cache selector
var images = $("a.shareable, td.shareable"),
  title = $("title").text() || document.title;
var page='';
var domain= "dev.femaleplasticsurgeons.com";

//make images draggable
images.draggable({
  //create draggable helper
  helper: function() {
    return $("<div>").attr("id", "helper").html("<span>" + title + "</span><img id='thumb' src='" + $(this).children().attr("src") + "'>").appendTo("body");
    
  },
  cursor: "pointer",
  cursorAt: { left: -15, top: 20 },
  zIndex: 99999,
  //show overlay and targets
  start: function() {
    $("<div>").attr("id", "shareoverlay").css("opacity", 0.7).appendTo("body");
	$("#tip").remove();
	$(this).unbind("mouseenter");
	$("#targets").css("left", ($("body").width() / 2) - $("#targets").width() / 2).slideDown();
	$("<div>").addClass("draghelp").text("Drag to share!").addClass("active").appendTo($('#targets')).fadeIn();
	page= $(this).attr('href');
  },
  //remove targets and overlay
  stop: function() {
    $("#targets").slideUp();
	$(".share", "#targets").remove();
    $("#shareoverlay").remove();
	$(this).bind("mouseenter", createTip);
	$('.draghelp').remove();

  }
});

//make targets droppable
$("#targets li").droppable({
  tolerance: "pointer",
  //show info when over target
  over: function() {
    $(".share", "#targets").remove();
    $("<span>").addClass("share").text("Share on " + $(this).attr("id")).addClass("active").appendTo($(this)).fadeIn();
  },
  drop: function() {
    var id = $(this).attr("id"),
      //This is the HREF that will be shared
	  //currentUrl = window.location.href,
	  shareURL="http://"+domain+page;
	  baseUrl = $(this).find("a").attr("href");

	if (id.indexOf("twitter") != -1) {
	  //Make a hash and return that as the link
	  $.get("tinyurl.php", {uri: page}, function(data){
		window.location.href = baseUrl + "/home?status=" + title + ": " + data;
	  });
	  
	}
	 else if (id.indexOf("facebook") != -1) {
	  window.location.href = baseUrl + "/sharer.php?u=" + shareURL;	// + "&t=" + title;
	}
  }		  
});

var createTip = function(e) {
  //create tool tip if it doesn't exist
  ($("#tip").length === 0) ? $("<div>").html("<span>Drag to share this image<\/span><span class='arrow'><\/span>").attr("id", "tip").css({ left:e.pageX + 30, top:e.pageY - 16 }).appendTo("body").fadeIn(2000) : null;
};

images.bind("mouseenter", createTip);

images.mousemove(function(e) {

  //move tooltip
  $("#tip").css({ left:e.pageX + 30, top:e.pageY - 16 });
});

images.mouseleave(function() {

  //remove tooltip
  $("#tip").remove();
});
});


