(function($) {
  $.fn.isPointed = function(x, y) {
    var self = $(this);
    var left = self.position().left;
    var top = self.position().top;
    var right = left + self.outerWidth();
    var bottom = top + self.outerHeight();
    return left <= x && x < left + self.outerWidth() && top <= y && y < top + self.outerHeight();
  };
})(jQuery);

var mouseenterPopup = function(buttonId, popupId, effect, callback) {

  var button = $('#' + buttonId);
  var popup = $('#' + popupId);
  var isPopupNow = false;
  
  button.bind('mouseenter', function() {
    if (!isPopupNow) {
      isPopupNow = true;
      popup.show(effect, 300, callback);
    }
  });

  button.bind('mouseleave', function(e) {
    if (!button.isPointed(e.pageX, e.pageY) && !popup.isPointed(e.pageX, e.pageY) && isPopupNow) {
      isPopupNow = false;
      popup.hide(effect, 300);
    }
  });
  
  popup.bind('mouseleave', function(e) {
    if (!button.isPointed(e.pageX, e.pageY) && !popup.isPointed(e.pageX, e.pageY) && isPopupNow) {
      isPopupNow = false;
      popup.hide(effect, 300);
    }
  });

};

var SbsItem = function(name, url, displayName, displayIconUrl) {
  this.name = name;
  this.url = url;
  this.displayName = displayName;
  this.displayIconUrl = displayIconUrl;
};

var Sbs = function(title, url) {
  this.title = encodeURIComponent(title);
  this.url = encodeURIComponent(url);
};

Sbs.hatena = new SbsItem(
  "hatena",
  "http://b.hatena.ne.jp/add?mode=confirm&title=#{title}&url=#{url}",
  "はてな",
  "/images/common/sbs_hatena.gif"
);

Sbs.google = new SbsItem(
  "google",
  "https://www.google.com/bookmarks/mark?op=add&bkmk=#{url}&title=#{title}",
  "Google",
  "/images/common/sbs_google.gif"
);

Sbs.livedoor = new SbsItem(
  "livedoor",
  "http://clip.livedoor.com/clip/add?link=#{url}&title=#{title}&ie=utf8",
  "livedoor",
  "/images/common/sbs_livedoor.gif"
);

Sbs.yahoo = new SbsItem(
  "yahoo",
  "http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t=#{title}&u=#{url}&opener=bm&ei=UTF-8",
  "Yahoo!",
  "/images/common/sbs_yahoo.gif"
);

$.extend(Sbs.prototype, {

  createPanel: function(names) {
  
    var self = this;
  
    var createItemRecord = function(item) {
      var bookmarkUrl = _T(item.url, { title: self.title, url: self.url } );
      var openWindow = _T("javascript:window.open('#{url}', '', 'scrollbars=yes,menubar=no,width=600,height=530,resizable=yes,toolbar=no,location=no,status=no');void(0)", { url: bookmarkUrl });
      var icon = $(_T('<img class="icon" src="#{s}" alt="#{a}" />', { s: item.displayIconUrl, a: item.displayName }));
      var name = $(_T('<span class="name">#{n}</span>', { n: item.displayName }));
      var link = $(_T('<a class="link" href="#{openWindow}"></a>', { openWindow: openWindow }));
      var td = $('<td class="item"></td>');
      link.append(icon).append(name).appendTo(td);
      return td;
    };
  
    var tab = $('<table>');
    for (var i = 0; i < names.length; i++) {
      var tr = $('<tr></tr>');
      tr.append(createItemRecord(Sbs[names[i]]));
      if (i < names.length - 1) {
        tr.append(createItemRecord(Sbs[names[++i]]));
      }
      tab.append(tr);
    }
    
    return $('<div class="sbs"></div>').append(tab);
  },
  
  bookmark: function(name) {
    location.href = _T(Sns[name].url, { title: this.title, url: this.url });
  }
  
});
