/*
 * Copyright (c) 2010, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

/**
 * Small jQuery plugin to add facebook like-button-support to any dom-element.
 * 
 * This is very useful, if a website should have a free styled like-button and 
 * it gives a better tracking-protection for the user. 
 * 
 * Features:
 * - Any dom-element can be used as a like button. Typically you add this to an
 *   image. 
 * - Facebook code will not be fetched, unless the user moves his mouse over 
 *   the like-button element. This gives the user a far better tracking 
 *   protection and the like is still one click away.
 * 
 * Disadvantages:
 * Some Facebook-features will not work with this implementation:
 * - The count of likes will not be visible, because the real facebook-like 
 *   button is not shown and facebook does not provide this information via API
 * - It is not possible to show, if the user likes this page already. Therefore
 *   dislike can not be shown. But if the user already likes the page, the 
 *   funktionality is dislike, though.  
 * 
 * Usage:
 * $(SELECTOR).addLikeSupport(URL);
 * 
 * Example:
 * $('#likeImage').addLikeSupport('http://www.google.de');
 */ 
(function($) {
	
	$.fn.addLikeSupport = function(src) {
		if (!$(this).length) return this;

		var tester = $('<div style="position: absolute; width: 1px; height: 1px; top: 0; left: 0; background: transparent;" />');
		$('body').append(tester);
		var offset = tester.offset();
		tester.remove();
		
		var button;
    
    if (!button) {
      src = urlencode(src);
      // button = $('<div style="position: absolute; width: 5px; height: 5px; background: transparent; left: 0; top: 0px; z-index: 10000;"><iframe allowTransparency="true" frameborder="0" scrolling="no" style="position: absolute; border: none; overflow:hidden; width: 5px; height: 5px; opacity: 0;filter: alpha(opacity = 0);" src="http://www.facebook.com/plugins/like.php?href='+src+'&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like"></iframe></div>');
      button = $('<div style="position: absolute; width: 5px; height: 5px; background: transparent; left: 0; top: 0px; z-index: 10000;"><iframe src="http://www.facebook.com/plugins/like.php?href='+src+'&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=20" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:5px; height:5px; opacity: 0;filter: alpha(opacity = 0);" allowTransparency="true"></iframe></div>');
      $('body').append(button);
    }
		
		$(this).css('cursor', 'pointer').mousemove(function(e) {
/*
			if (!button) {
				src = urlencode(src);
				button = $('<div style="position: absolute; width: 5px; height: 5px; background: transparent; left: 0; top: 0px; z-index: 10000;"><iframe allowTransparency="true" frameborder="0" scrolling="no" style="position: absolute; border: none; overflow:hidden; width: 5px; height: 5px; opacity: 0;filter: alpha(opacity = 0);" src="http://www.facebook.com/plugins/like.php?href='+src+'&amp;layout=button_count&amp;show_faces=false&amp;action=like"></iframe></div>');
				$('body').append(button);
			}
*/
			button.css({'left': e.pageX-offset.left-2+'px', 'top': e.pageY-offset.top-2+'px'});
		});
    
/*
    $(this).click(function() {
      button.hide();
      $(this).hide();
    });
*/
    
	}
	
	urlencode = function(str) {
	    // URL-encodes string  
	    // version: 1102.614
		// function from http://phpjs.org/functions/urlencode:573
	    str = (str + '').toString();
	    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
	}	
	
})(jQuery);


