if (Drupal.jsEnabled) {
  //debugger;
  $(document).ready(function() {

    function fr_bubble_insert_infoicon(elem, target_class_prefix) {
      // dynamically insert info icon image if JS is enabled but only if the data exists

      // check to see if infoicon already exists, if so exit gracefully
      var infoimgCount = $(elem).find(".infoicon").length;
      if (infoimgCount > 0) {
        //alert('This link has ' + infoimgCount + ' info icons');
        return false;
      }

      oRE = new RegExp("^" + target_class_prefix + ".*")
      var content_div;
      var varclass=elem.className.split(' ');
      for (var vc in varclass) {
        if (oRE.test(varclass[vc])) {
          content_div = $('div#' + varclass[vc] + ' div.aflyout-product-info-cluetip').length;
          if (content_div) {
            full_text = $(elem).text();
            full_text.replace(/^\s+|\s+$/g, ''); //trim ws
            last_ws_pos = full_text.lastIndexOf(" ");
            if (last_ws_pos >= 0) {
              part1_text = full_text.substring(0,last_ws_pos + 1); // include the space
              part2_text = full_text.slice(last_ws_pos+1);
            }
            else {
              part1_text = "";
              part2_text = full_text;
            }
            $(elem).text(part1_text);
            $('<span class="infoicon">' + part2_text + '<img class="infoicon" width="1" height="1" src="/sites/all/themes/cs6/public/images/backgrounds/spacer.gif"/></span>').appendTo(elem);
          }
        }
      }
    }

    $('a.bubbleInfo.product').each(function() {
      fr_bubble_insert_infoicon(this, "for-product-");
    });

    $('a.bubbleInfo.anchor').each(function() {
      fr_bubble_insert_infoicon(this, "anchor-for-product-");
    });

    $('a.bubbleInfo.product:has(img), a.bubbleInfo.anchor:has(img)').each(function(index) {

      // get the flyout position of the link
      var frbLinkTop = $(this).offset().top;
      var frbLinkLeft = $(this).offset().left + this.offsetWidth;

      //get the position of the infoicon image
      var frbInfoiconTop = $('img.infoicon', this).offset().top;
      var frbInfoiconLeft = $('img.infoicon', this).offset().left;

      var bTopOffset = -90; //+ve -> down
      var bLeftOffset = -34; //+ve -> right
      //if ($(this).hasClass('anchor')) { bTopOffset += 0; bLeftOffset += 0;}

      var bWidth = 380;

      $(this).cluetip({
        sticky: true,
        attribute: 'rev',
        mouseOutClose: true,
        width: bWidth + 'px',
        cluezIndex: 10010,
        local: true,
        /* hideLocal: false, */
        cursor: 'pointer',
        dropShadow: false,
        positionBy: 'fixed',
        closePosition: 'title',
        closeText: 'Close {<span>x</span>}',
        topOffset:  (frbInfoiconTop - frbLinkTop) + bTopOffset,
        leftOffset: (frbInfoiconLeft - frbLinkLeft) + bLeftOffset,
        showTitle: true,
        fx: {
          open: 'show', // can be 'show' or 'slideDown' or 'fadeIn'
          openSpeed: 'fast'
        },
        hoverIntent: {
          sensitivity: 20, // number = sensitivity threshold (must be 1 or higher)
          interval: 100, // number = milliseconds for onMouseOver polling interval
          timeout: 500 // number = milliseconds delay before onMouseOut
        },
        onShow: function (ct, c) { // function to run just after clueTip is shown
          $('div#cluetip-outer').width(bWidth);                                                  
          var widthvar = $('div#cluetip table.af-popup').width() ;
          if (widthvar) $('div#cluetip-outer').width(widthvar + 3);
          var ctoff = $('div#cluetip').offset();
          var heightvar = $('div#cluetip table.af-popup').height();
          if (heightvar && ctoff.top) {
            $('div#cluetip').css('top',(ctoff.top - heightvar + 100)+'px');
          }
          var sbleft = $('div#sidebar-right').offset().left;
          if (ctoff.left > (sbleft - 315)) {
            $('div#cluetip').css('left',(ctoff.left - $('div#cluetip table.af-popup').width() + 81)+'px');
            $('div#cluetip').addClass('flip-horiz');
          }
          $('div.aflyout-product-info-cluetip').css('visibility','visible');
        //alert('ctoff.top = '+ctoff.top+"\n"+'heightvar = '+heightvar+"\n"+'$(\'div#cluetip\').css(\'top\') = '+$('div#cluetip').css('top')+"\n");
        }
      })
    });

  });
}


