(function ($) {
  $.fn.bubble = function () {  
    // options
    var options = {
    	'top': 0,
        'left': 10,
        'time': 250,
        'hideDelay': false,
    	'hideDelayTimer': null
	}
    var trigger = $('a', this);
    var popup = $('.popup', this).css('opacity', 0);
    
    for(var i = 0;i<this.length;i++){
    	$.bubbleOverOut([trigger.get(i), popup.get(i)], options);
    }
    
    if (jQuery.browser.msie) {
	  // fix css background pngs in all ie versions
	  jQuery(this).find("*").each(function(){
	    var bgIMG = jQuery(this).css('background-image');
	    if(bgIMG.indexOf(".png")!=-1){
	      var iebg = bgIMG.split('url("')[1].split('")')[0];
	      jQuery(this).css('background-image', 'none');
	     
	      jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
	    }
	  });
	}    
    
    };
    

	
	$.bubbleOverOut = function(elements, options){
		var hideDelayTimer = options.hideDelayTimer;
		var hideDelay = options.hideDelay;
	 	var beingShown = false;
      	var shown = false;
	//alert(elements);
    // set the mouseover and mouseout on both element
    $(elements).mouseover(function () {
    
     
      
      // stops the hide event if we move from the trigger to the popup element
      if (options.hideDelayTimer) clearTimeout(options.hideDelayTimer);
      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        
        $(elements[1]).css({
          top: -45,
          left: -280,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + options.top + 'px',
          left: '-=' + options.left + 'px',
          opacity: 1
        }, options.time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        $(elements[1]).animate({
          top: '-=' + options.top + 'px',
          left: '-=' + options.left + 'px',
          opacity: 0
        }, options.time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          $(elements[1]).css({
          	display: 'none'
          });
        });
      }, hideDelay);
    });

  };
})(jQuery);