+function($){
  var wikipURL = '/wikipedia/', d = document;
  $(function() {
      var showWikipediaTIPS = function(html, $obj) {
        $(d.body).append(html);
        var offset = $obj.offset();
        var $wikip = $('#wikipediaWidget');
        //                                                          console.log([offset.top, $wikip.height(), offset.left, $wikip.width()]);

        $wikip.css({top: Math.max(offset.top-$wikip.height(), 0),
                    left: Math.max(offset.left-$wikip.width()/2, 0),
                    position: 'absolute'
                   });
//        alert($('#wikipediaWidget p').offset().top);

      };

      // set wikipedia TIPS
      $('div.articleBody .wikipedia').mouseover(function(){
                                                  $('#wikipediaWidget').remove();
                                                  var word = $(this).attr('href').replace(/.+wiki\//, ''), self = this;
                                                  var url = [wikipURL, encodeURIComponent(word)].join('');
                                                  $.get(url, null, function(v){
                                                          if (!v) return;
                                                          showWikipediaTIPS(v, $(self));
                                                        });
                                   }).mouseout(function() {
                                                  $('#wikipediaWidget').remove();
                                               });

      // footnote TIPS
      $('.footnote-identifier-link').mouseover(function(){
                                                 $('#wikipediaWidget').remove();
                                                 var $self = $(this);
                                                 var title = $self.text(), text = $self.attr('title');
                                                 var html = [
                                                             '<div id="wikipediaWidget">',
                                                             '<span class="wikiT"></span>',
                                                             '<div class="wiki">',
                                                             '<strong>', title, '</strong>',
                                                             '<p>&nbsp</p>',
                                                             '<p>', text, '<br /></p>',
                                                             '</div>',
                                                             '<span class="wikiB"></span>',
                                                             '</div>'
                                                            ].join('');
                                                 showWikipediaTIPS(html, $self);
                                               }).mouseout(function() {
                                                  $('#wikipediaWidget').remove();
                                               });

      // set Shadowbox title
      $('#mainBox a > img').each(function(){
                                   if (!this.title) return;
                                   var $parent = $(this).parent();
                                   if (!$parent.attr('title')) $parent.attr('title', $(this).attr('title'));
                                 });

      // flowers
      var hash=location.hash;
      if ($('#onedayflower').size() && hash) {
        var url = $('a[name="'+hash.replace(/#/,'')+'"]').parent().parent().children('p').children('a').attr('href');
        if (!url) return;
        setTimeout(function(){
        Shadowbox.open({
          content: url,
          player: 'img',
          gallery: 'flower'
                       });
                     }, 1000);

      }
  });
}(jQuery);

function openGalleryImage(url, name) {
  name = name||'promo';
  url = url.replace(/\/imagick\/.+\/blog\//, location.href.split('/').slice(0,3).join('/') + '/imagick/imgk_thumbnail/index/thumb/800x600/blog/');
  Shadowbox.open({
    content: url,
    player: 'img',
    gallery: name
      });

}