/*
// +------------------------------------------------------------+
// | Copyright (c) 2008 Mark Yasuda @ Manha                     |
// +------------------------------------------------------------+
// | Author  : Mark Yasuda <mark.yasuda@manha.com>              |
// | Version : 0.0.1                                            |
// | Created : 16/10/2008                                       |
// +------------------------------------------------------------+
// | Version Information                                        |
// |                                                            |
// |  0.0.1 - jQuery pulsate indefinitely function.             |
// +------------------------------------------------------------+
// | Known Bugs And Issues                                      |
// |                                                            |
// |  Requires a global value for each timer resource.          |
// |                                                            |
// | To Do:                                                     |
// |                                                            |
// |  Integrate global timer in pulsate function.               |
// +------------------------------------------------------------+
*/

var pulsateTimer = null ;

(function($) {

  $.fn.pulsate = function(options) {
    var version = '0.0.1';

    // options
    var opts = $.extend({}, $.fn.pulsate.defaults, options);

    return this.each(function() {
      $this = $(this);
      $this.timerID = null;
      $this.running = false;

      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

      $this.duration = o.duration;
      $this.waitTime = o.waitTime;
      $this.targetOpacity = o.targetOpacity;

      $.fn.pulsate.startPulse($this);
    });
  };

  $.fn.pulsate.startPulse = function(el) {
    $.fn.pulsate.stopPulse(el);
    $.fn.pulsate.holdPulse(el);
  }
  $.fn.pulsate.stopPulse = function(el) {
    clearInterval(pulsateTimer);
    el.running = false ;
  }
  $.fn.pulsate.holdPulse = function(el) {
    el.running = true ;
    pulsateTimer = setInterval(function(){$.fn.pulsate.pulse(el);},el.waitTime);
  }
  $.fn.pulsate.pulse = function(el) {
    el.animate({opacity:el.targetOpacity},el.duration,function(){
      el.animate({opacity:1},el.duration);
    });
  };

  // plugin defaults
  $.fn.pulsate.defaults = {
    duration: 500,
    waitTime: 1000,
    targetOpacity: 0.5
  };

})(jQuery);