(function( $ ){

	$.fn.ajaxReadMarker = function(options) {
	
		return this.each(function() {
		
			// default settings
			var settings = {
				'prefix' : 'read',
				'url' : location.href
			};
			
			// If options exist, lets merge them
			// with our default settings
			if (options) { 
				$.extend(settings, options);
			}

			var $this = $(this);
			
			$this.click(function() {
				var re = new RegExp(settings.prefix + '-(\\d+)');
				var m = re.exec($this.attr('id'));

				if (m != null) {
					$this.addClass('loading')
						.attr('title', 'Daten werden verarbeitet!');
				
					$.getJSON(settings.url, 
						{ id: m[1], read: $this.hasClass('unread') }, 
						function(data, textStatus, xhr) {
							$this.removeClass('loading');
							if (data.success) {
								if ($this.hasClass('unread')) {
									$this.removeClass('unread')
										.addClass('read')
										.attr('title', 'Bereits gelesen!');
								} else {
									$this.removeClass('read')
										.addClass('unread')
										.attr('title', 'Noch nicht gelesen!');
								}
							}
						} 
					);
				}

				return false;
			});
			
		});

	
	};
	
})( jQuery );
