/*
 * ShareIt - jQuery Plugin
 * Share content on facebook and iwiw
 *
 * Author: Matyas Juhasz
 * Documentation: http://joohi.hu/shareit
 * No license. Use it however you want. Just keep this notice included.
 *
 * Version: 1.1.1 (2010-07-12)
 * Requires: jQuery v1.3+
 *
 */

(function($) {
  $.fn.extend({
  shareIt: function(givenOptions) {
    var self = $(this),
      element,
      options = $.extend({
        title: "Share this page",
        facebook_icon: "images/facebook.png",
        iwiw_icon: "images/iwiw.png",
        icons: [],
        iwiw: true,
        facebook: true
      }, givenOptions);

    var shareitbox;

    function showShareBox() {
      $("body").append(
        shareitbox = $('<div id="shareit-box" class="shareit-box"><h2>'+options.title+'</h2></div>')
      );

      var left_position = $(element).offset().left,
          top_position = $(element).offset().top+$(element).innerHeight()+6;

      shareitbox.css("left", left_position+"px");
      shareitbox.css("top", top_position+"px");

      if (options.facebook)
        shareitbox.append(facebook_button = '<a href="http://www.facebook.com/sharer.php?u='+document.location+'" target="_blank"><img src="'+options.facebook_icon+'" /></a>');
      if (options.iwiw)
        shareitbox.append(iwiw_button = '<a href="http://iwiw.hu/pages/share/share.jsp?u='+document.location+'" target="_blank"><img src="'+options.iwiw_icon+'" /></a>');
      $(options.icons).each(function() {
        shareitbox.append('<a href="'+this[0]+'" target="_blank"><img src="'+this[1]+'" /></a>');
      });

      if ($(window).height() < top_position + shareitbox.outerHeight() - $(window).scrollTop()) {
        shareitbox.css("top", top_position - shareitbox.outerHeight() - 12 - $(element).innerHeight())
      }

      $("*").bind("click.shareit", function() {
        $(".shareit-box").remove();
        $("*").unbind("click.shareit");
      });

    }

    function doIt() {
      self.click(function(e) {
        element = e.target;
        showShareBox();
        return false;
      });
    }
    doIt();
  }
  });
})(jQuery);
