/*
 * Joinpool dragToShare
 * Copyright 2009 Sven Kaiser
 * www.joinpool.de
 * competence network
 *
 * Version 1.3   -   Updated: Nov. 18, 2009
 */
$(function() {
	var shareArea = $("<div><div>").attr("id", "shareArea").css("display", "none").css("height", "0px").prependTo("body");
	$(shareArea).html("<div id='shareContent'><strong class='shareInfo'></strong><div class='dragHere'></div><ul id='targets'><li id='Facebook'><a href='http://www.facebook.com'></a><span>Facebook</span></li><li id='Twitter'><a href='http://twitter.com'></a><span>Twitter</span></li><li id='Delicious'><a href='http://delicious.com'></a><span>Delicious</span></li><li id='EMail'><a href='mailto:'></a><span>E-Mail</span></li></ul></div>");

	var images = $(".share img"),
	$imageTitle = '';
	if (images.length == 0) {
		$("h1").before('<a href="javascript:;" class="share"><img src="/image/x.png" width="890" height="195" style="z-index: 90; position: absolute" alt="Webdesign, Entwicklung und Refactoring bei http://www.joinpool.de" rel="#webdesign #php #refactoring" /></a>');
		var images = $(".share img");
		var h1 = true;
	}
	
  title = 'Diese Seite bookmarken' //$("title").text() || document.title;
  
  $(shareArea).css("height", "0px");

  //make images draggable
  images.draggable({
	  //create draggable helper
	  helper: function() {
	  	if (h1) var helperThumb = "http://www.joinpool.de/image/logo-small.png"; else var helperThumb = $(this).attr("src");
			return $("<div>").attr("id", "helper").html("<span class='arrow'></span><img id='thumb' src='" + helperThumb + "'>").appendTo("body"); //" + title + "
		},
	  cursor: "pointer",
	  cursorAt: { left: -28, top: 18 },
	  zIndex: 99999,
	  
	  //show overlay and targets
	  start: function() {
	  	var scrollTop = $(window).scrollTop();
	  	if (scrollTop == 0) {
	  		$("#shareArea").css("position", "relative");
	  		$("#shareArea").css({"-webkit-box-shadow":"none", "-moz-box-shadow":"none", "box-shadow":"none"});
	  	} else {
	  		$("#shareArea").css("position", "fixed");
	  		$("#shareArea").css({"-webkit-box-shadow":"0 0px 8px #3F3F3F", "-moz-box-shadow":"0 0px 8px #3F3F3F", "box-shadow":"0 0px 8px #3F3F3F"});
	  	}
	  	
	  	$imageTitle = $(this).attr('alt');
	  	$hashTags = $(this).attr('rel'); 
	  	if ($hashTags == undefined) $hashTags = '';
	  	
			$("#tip").remove();
			$(this).unbind("mouseenter");
			
			$("#shareArea").animate({height: "137px"}, {queue:false, duration: 700, easing: 'easeOutSine'}); //easeOutBounce
			$("#targets").css("left", ($("body").width() / 2) - $("#targets").width() / 2).fadeIn("fast");
	  },
	  
	  //remove targets and overlay
	  stop: function() {
	    $("#targets").fadeOut();
			$(".shareInfo", "#targets").remove();
			$(".shareShadow").remove();
	    $("#overlay").remove();
			$(this).bind("mouseenter", createTip);
			
			$("#shareArea").animate({height: "0px"}, {queue:false, duration: 1700, easing: 'easeOutBounce'})  
		}
	});

	//make targets droppable
	$("#targets li").droppable({
	  tolerance: "pointer",
	  //show info when over target
	  over: function() {
	    $(".shareInfo", "#targets").remove();
	    var id = $(this).attr("id");
	    if (id == "Twitter") {
	    	var title = "Twittern";
	    	$(this).css("background-position", "-68px -69px");
	    }
	    else if (id == "Facebook") {
	    	var title = "Bei Facebook posten";
	    	$(this).css("background-position", "-68px 0px");
	    }
	    else if (id == "Delicious") {
	    	var title = "Bei Delicious posten";
	    	$(this).css("background-position", "-68px -138px");
	    }
	    else if (id == "EMail") {
	    	var title = "Per E-Mail senden";
	    	$(this).css("background-position", "-68px -207px");
	    }
	    //$("<span>").addClass("share").text(title).addClass("active").appendTo($(this)).fadeIn();
	    $("#shareArea strong").addClass("shareInfo").text(title).fadeIn();
	  },
	  
	  out: function() {
	  	$(".shareInfo").text('');
	    $("#Facebook").css("background-position", "0px 0px");
	    $("#Twitter").css("background-position", "0px -69px");
	    $("#Delicious").css("background-position", "0px -138px");
	    $("#EMail").css("background-position", "0px -207px");
	  },
	  
	  drop: function() {
	    var id = $(this).attr("id"),
		  currentUrl = window.location.href,
		  baseUrl = $(this).find("a").attr("href");
		  //$imageTitle = escape($imageTitle);
		  //$hashTags = escape($hashTags);
		  //currentUrl = escape(currentUrl);

		  $("#Facebook").css("background-position", "0px 0px");
	    $("#Twitter").css("background-position", "0px -69px");
	    $("#Delicious").css("background-position", "0px -138px");
	    $("#EMail").css("background-position", "0px -207px");
		  
			if (id.indexOf("Twitter") != -1) {
			  window.open(baseUrl + "/home?status=" + $imageTitle + ' ' + escape($hashTags), 'Twitter', 'width=800,height=600,scrollbars=yes');
			}
			else if (id.indexOf("Delicious") != -1) {
			  window.open(baseUrl + "/save?url=" + currentUrl + "&title=" + $imageTitle, 'Delicious', 'width=950,height=600,scrollbars=yes');
			}
			else if (id.indexOf("Facebook") != -1) {
			  //window.location.href = baseUrl + "/sharer.php?u=" + currentUrl + "&t=" + $imageTitle;
			  window.open(baseUrl + "/sharer.php?u=" + currentUrl + "&t=" + $imageTitle, 'Facebook', 'width=800,height=600,scrollbars=yes');
			}
			else if (id.indexOf("EMail") != -1) {
			  window.location.href = "mailto:?&subject=" + $imageTitle + "&body=Gefunden%20auf%20" + currentUrl;
			}
		}		  
	});

  var createTip = function(e) {
		//create tool tip if it doesn't exist
		($("#tip").length === 0) ? $("<div>").html("<span>Klicken und ziehen<\/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();
	});
});
