// $Id: ajax_hide_status.js, v0.1 15/07/2009 $

Drupal.hideStatusAutoAttach = function() {
  var hs = [];
  $('span.status-inact, span.status-act').each(function () {
    // Read in the path to the PHP handler
    uri = $(this).attr('title');
    var online = $(this).html();
    $(this).html('');
    if (!hs[uri]) {
      hs[uri] = new Drupal.hideStatus(this, uri, null, online);
    }
  });
  $('span.status-act').each(function () {
  	$(this).attr('title', 'Appear Offline!');
  });
  $('span.status-inact').each(function () {
  	$(this).attr('title', 'Go Online!');
  });
}

/**
 * hideStatus object
 */
Drupal.hideStatus = function(elt, uri, id, online) {
  var db = this;
  this.elt = elt;
  this.online = online;
  this.offline = uri;
  this.id = $(elt).attr('id');
  this.uri = $(elt).is(".status-inact") == true ? uri : online;
  $(elt).click(function() {
    // Ajax GET request for status data
    $.ajax({
      type: "GET",
      url: db.uri,
      success: function (data) {
        if($(elt).is(".status-inact"))
        {
        	$('#' + db.id + '.status-inact')
    	      .removeClass('status-inact')
	          .addClass('status-act');
	        db.uri = db.online;
	        $('#' + db.id + '.status-act').attr('title', 'Appear Offline!');
        }
        else if($(elt).is(".status-act"))
        {
        	$('#' + db.id + '.status-act')
    	      .removeClass('status-act')
	          .addClass('status-inact');
	        db.uri = db.offline;
	        $('#' + db.id + '.status-inact').attr('title', 'Go Online!');
        }
        $('#hide_status_status').html(data);
        $.ajax({
		  type: "GET",
		  url: '/hide_status_update',
		  success: function (data) {
			$('#whos-online-content').html(data);
		  },
		  error: function (xmlhttp) {
			alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ db.uri);
		  }
		});
      },
      error: function (xmlhttp) {
        alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ db.uri);
      }
    });
  });
}

if (Drupal.jsEnabled) {
  $(document).ready(Drupal.hideStatusAutoAttach);
}
