/*
 * jQuery loadInterval / killInterval 1.05
 * Copyright 2010
 *
 * Original Authors : Nicholas Ortenzio & Jason Duncan
 * Created : 3/11/2010
 * Last modified : 4/24/2010
 *
 * loadInterval makes a jQuery.load call at a specified interval. 
 * forceInterval makes an immediate ajax request (requested by Wayne)
 * killInterval clears the interval
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */


(function($){

  $.fn.loadInterval = function(options) {

    // Set defaults
    var defaults = $.extend({
      seconds: 60,
      url: null,
      stopOnError: true,
      onSuccess: $.noop,
      onError: $.noop
    }, options);

    // Definitely need a URL
    if (defaults.url==null) return;

    var init = function(){
      // Alias self
      var $this = $(this);

      // Create Interval
      var ajaxInterval = setInterval(function(){

        // Check to see if there is already an interval AJAX request pending
        // Otherwise, keep track of the request in progress
        if ($this.data('loadIntervalActive')) { return; } 
        else { $this.data('loadIntervalActive', true); }

        // jQuery load AJAX request
        $this.load(defaults.url, function(response, status, xhr) {
          // Clear the tracker 
          $this.data('loadIntervalActive', false);

          // Check for error in response status and execute
          // the appropriate callback. if there was an error
          // check to see if we should stop making requests
          if (status == "error") {
            defaults.onError(response, status, xhr, defaults);
            if (defaults.stopOnError) { clearInterval(ajaxInterval); }
          } else {
			$this.html(response);
            defaults.onSuccess(response, status, xhr, defaults);
          }
        });

      }, defaults.seconds*1000);

      // Store the interval
      $this.data('intervalDefaults', defaults);
      $this.data('intervalHandle', ajaxInterval);
    }

    // For each object
    return this.each(init);

  };

	// Method for forcing a load
	$.fn.forceInterval = function() {
		return this.each(function() {
			var $this = $(this);

			// Get interval defaults
			var defaults = $this.data('intervalDefaults');
			if (!defaults) return;

			// Make ajax call right away
			$this.load(defaults.url, function(response, status, xhr) {
				if (status == "error") { defaults.onError(response, status, xhr, defaults); } 
				else { defaults.onSuccess(response, status, xhr, defaults); }
			});

		});
	}

	// Method for killing a loadInterval  
	$.fn.killInterval = function() {
		return this.each(function() {
			var $this = $(this);

			// Get Interval
			var ajaxInterval = $this.data('intervalHandle');
			if (!ajaxInterval) return;

			// Clear interval
			clearInterval(ajaxInterval)

		});
	}

})(jQuery);