function working(toggle) {
	if (toggle) { // enable working indicator
		Effect.Appear('remote-working-indicator');
	} else { // disable working indicator
		Effect.Fade('remote-working-indicator');
	}
};


var formFieldRotateLengths = new Array();
function formFieldRotate(id, target_id, length) {
	oldLen = formFieldRotateLengths[id];
	if (oldLen == null) oldLen = 0;
	newLen = $(id).getValue().length;
	formFieldRotateLengths[id] = newLen;
	if (newLen > oldLen && newLen >= length) $(target_id).focus();
};

var trustmeCountdowns = new Array();
function countdownAdd(name, total, intervalCallback, completionCallback) {
	// [ total, interval function pointer, complete function pointer, current counter]
	trustmeCountdowns[name] = [total, intervalCallback, completionCallback, total];
};
function countdownStart(name) {
	var nextIteration = function() { countdownIteration(name); };
	setTimeout(nextIteration, 1000);
};
function countdownIteration(name) {
	c = trustmeCountdowns[name];
	if (c==null) return;
	if (c[3] > 0) {
		c[3] -= 1;
		c[1](c[3]);
		var nextIteration = function() { countdownIteration(name); };
		setTimeout(nextIteration, 1000);
	} else {
		c[2]();
	}
};

document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body);

  if (container) {
    container.observe('click', function(e) {
      var el = e.element();
      if (el.match('#messages-panel .pagination a')) {
        new Ajax.Request(el.href, { method:'get', onComplete:function(request){working(false)}, onLoading:function(request){working(true)} });
        el.href = '#';
      };
    });
  };
});
