// $Id:$
(function ($) {
Drupal.behaviors.qtip = {
  attach: function(context) {
		$.each(Drupal.settings.block_refresh.settings, function(element, settings) {
		  
      //Get argument from referring page and append to end of load request
      args = '';
      $.each(Drupal.settings.block_refresh.args, function(index, arg) {
        args += '/'+arg;
      });
      query = Drupal.settings.block_refresh.query;
     	if (settings['auto']) {
				setInterval(function() {
					$('#' + element + ' .content').load(Drupal.settings.basePath + 'block_refresh/' + settings['block']['block'] + '/' + settings['block']['delta'] + args + query);
				}, settings['timer'] * 1000); // We need to multiply by 1000 because the admin enters a number in seconds,  but the setInterval() function expects milliseconds
			}
			if (settings['manual']) {
			  if (settings['processed'] != 1) {
			    settings['processed'] = 1;
			    refresh_link = '<div class="block-refresh-button">' + Drupal.t('Refresh') + '</div>';
  				var selector = element.replace(/_/g, '-');
  			  // We'll attach the refresh link to the header if it exists...
  				if ($('#' + selector + ' h2').length) {
  					// note: for some reason I couldn't get $(this) to work, I don't know why
  					$('#' + selector + ' h2').before(refresh_link);
  				}
  				// ...otherwise we will attach it to the content
  				else {
  					$('#' + selector + ' .content').before(refresh_link);
  				}
  				$('#' + selector + ' .block-refresh-button').click(function() {
    				$(this).addClass('block-refresh-button-throbbing');
    				var path = Drupal.settings.basePath + 'block_refresh/' + settings['block']['block'] + '/' + settings['block']['delta'] + args + query;
    				$('#' + selector + ' .content').load(path, '', function() {
    					$('.block-refresh-button').removeClass('block-refresh-button-throbbing');
    				});
    			});
			  }
			}
		});
  }
};
})(jQuery);;

