jQuery.Toggle = function (element, state, options)
{
	$('#' + element).click(function(event) { 
	    $.ajax({
	        url: getOption('url'),
	        type: 'GET',
	        data: 'state=' +  ((state == 1) ? 0 : 1),
	        complete: function(xhr) {
	    		update(getHeaderJSON(xhr));
	    	}    	    	
	    });

	});
	
	function update(json)
	{
  	  state = json.state;
	  
  	  if (state == 1)
	  {
  		$('#' + element).removeClass('off');
  		$('#' + element).addClass('on');
  		$('#' + element).html(getOption('on', 'on'));
	    $('#' + element + '_indicator').html(getOption('on_indicator', ''));
	  }
	  else if (state == -1)	  
	  {
		document.location = json['url'];
	  }
	  else
	  {
		$('#' + element).removeClass('on');
		$('#' + element).addClass('off');
		$('#' + element).html(getOption('off', 'off'));
	    $('#' + element + '_indicator').html(getOption('off_indicator', ''));
	  }
	}
	
	function getHeaderJSON(xhr) {
	  var json;
	  try { json = xhr.getResponseHeader('X-Json') }
	  catch(e) {}

	  if (json) {
	    var data = eval('(' + json + ')'); // or JSON.parse or whatever you like
	    return data
	  }
	 }
	
	function getOption(name, defaultValue)
	{
	  return eval('options.' + name) ? eval('(options.' + name + ')') : defaultValue;
	}
	
}
