(function () {
  jQuery('table.products td').each(function () {
    var $this = jQuery(this), popup;

    if ($this.find('span.image img').length === 0) {
      return;
    }

    popup = jQBoxa({
      container : '#site'
    });

    popup.subscribe({
      created : function () {
        var src, pos, moreInfo;
        src = $this.find('span.image img').attr('src');
        pos = src.lastIndexOf('/');
        src = src.substr(0, pos) + src.substr(pos);
        src = src.replace("medium", "large");
        src = src.replace("small", "large");
        jQuery('<div></div>').addClass('popupImageTop').append(jQuery('<img />').attr({
          src : src,
          alt : ''
        })).appendTo(this.element);
        moreInfo = $this.children('div.moreInfo').html();
        if (moreInfo === null) {
          moreInfo = '&nbsp;';
        }
        jQuery('<div></div>').addClass('popupContent').html(moreInfo).appendTo(this.element);
      },
      preShow : function () {
        this.element.stop(true).css('opacity', '').show();
      },
      postShow : function () {
        this.element.hide().fadeIn(300);
      },
      preHide : function () {
        var popup = this;
        this.element.stop(true).fadeOut(300, function () {
          if (popup.isActive()) {
            this.parentNode.removeChild(this);
          }
        });
        return false;
      }
    });

    jQBoxa.setRelative(popup, $this, {
      enforce : true,
      minimizeOverflow : true,
      positionCollection : {
        context : '#site',
        left : {
          important : true,
          position : {
            left  : 0,
            top    : -241
          },
          offset : {
            right   : -100,
            top    : 100
          }
        },
        right : {
          position : {
            right : 0,
            top   : -241
          },
          offset : {
            left  : -100,
            top   : 100
          }
        }
      }
    });

    jQBoxa.enableMouseover(popup).addActivator($this);
  });
}());
