- (void)accessibilitySetup
{
	[[NSNotificationCenter defaultCenter] addObserver: self 
											 selector: @selector( _accessibilityNoteMovement: ) 
												 name: FSSelectionDidMoveStartPositionNotification
											   object: self];
	[[NSNotificationCenter defaultCenter] addObserver: self 
											 selector: @selector( _accessibilityNoteMovement: ) 
												 name: FSSelectionDidMoveEndPositionNotification
											   object: self];
	[[NSNotificationCenter defaultCenter] addObserver: self 
											 selector: @selector( _accessibilityNoteMovement: ) 
												 name: FSSelectionDidMovePositionNotification
											   object: self];
}

- (void)accessibilityTeardown
{
	[[NSNotificationCenter defaultCenter] removeObserver: self
													name: FSSelectionDidMoveStartPositionNotification
												  object: self];
	[[NSNotificationCenter defaultCenter] removeObserver: self
													name: FSSelectionDidMoveEndPositionNotification
												  object: self];
	[[NSNotificationCenter defaultCenter] removeObserver: self
													name: FSSelectionDidMovePositionNotification
												  object: self];
}

#pragma mark -

- (NSArray *)accessibilityAttributeNames
{
    static NSArray *attributes = nil;
    if( attributes == nil ) {
		attributes = [[NSArray alloc] initWithObjects:
			NSAccessibilityRoleAttribute,
			NSAccessibilityRoleDescriptionAttribute,
			NSAccessibilityFocusedAttribute,
			NSAccessibilityParentAttribute,
			NSAccessibilityWindowAttribute,
			NSAccessibilityTopLevelUIElementAttribute,
			NSAccessibilityPositionAttribute,
			NSAccessibilitySizeAttribute,
			NSAccessibilityDescriptionAttribute,
			NSAccessibilityValueAttribute,
			NSAccessibilityEnabledAttribute,
			NSAccessibilityChildrenAttribute,
			NSAccessibilityMinValueAttribute,
			NSAccessibilityMaxValueAttribute,
			nil];
    }
    return attributes;
}

- (NSRect)_accessibilityRect
{
	NSRect rect = [self bounds];
	rect = [mContainer convertRect: rect toView: nil];
	
	NSPoint p1 = rect.origin;
	NSPoint p2 = NSMakePoint( NSMaxX( rect ), NSMaxY( rect ) );
	
	p1 = [[mContainer window] convertBaseToScreen: p1];
	p2 = [[mContainer window] convertBaseToScreen: p2];
	
	rect = NSMakeRect( p1.x, p1.y, p2.x - p1.x, p2.y - p1.y );
	
	return rect;
}

- (NSValue *)_accessibilityPosition
{
	return [NSValue valueWithPoint: [self _accessibilityRect].origin];
}

- (NSValue *)_accessibilitySize
{
	return [NSValue valueWithSize: [self _accessibilityRect].size];
}

- (void)_accessibilityNoteMovement: (NSNotification *)note
{
	NSAccessibilityPostNotification( self, NSAccessibilityResizedNotification );
	NSAccessibilityPostNotification( self, NSAccessibilityMovedNotification );
	NSAccessibilityPostNotification( self, NSAccessibilityValueChangedNotification );
	
	[self accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityFocusedAttribute];
	NSAccessibilityPostNotification( self, NSAccessibilityFocusedUIElementChangedNotification );
	[self accessibilitySetValue: [NSNumber numberWithBool: NO] forAttribute: NSAccessibilityFocusedAttribute];
	NSAccessibilityPostNotification( self, NSAccessibilityFocusedUIElementChangedNotification );
		
	NSLog(@"%@ %@", [NSApp accessibilityAttributeValue: NSAccessibilityFocusedUIElementAttribute], self);
}


- (NSString *)_accessibilityRoleDescription
{
	if( [self isPlayhead] )
		return NSLocalizedString( @"Playhead", @"Accessibility: playhead role description" );
	else if( [self isPoint] )
		return NSLocalizedString( @"Split", @"Accessibility: split role description" );
	else
		return NSLocalizedString( @"Selection", @"Accessibility: selection role description" );
}

- (NSString *)_accessibilityDescription
{
	NSString *fmt = ([self isPoint]
					 ? @"%f"
					 : NSLocalizedString( @"From %f to %f", @"Accessibility: marker position description" ));
	
	return [NSString stringWithFormat: fmt, [self startPos], [self endPos]];
}

- (id)_accessibilityValue
{
	return [NSNumber numberWithDouble: [self position]];
}

- (void)_accessibilitySetFocus
{
	[mContainer selectSelection: self];
	[[mContainer window] makeFirstResponder: mContainer];
}

- (id)accessibilityAttributeValue: (NSString *)attribute
{
	NSLog(@"%s %@", _cmd, attribute);
    if( [attribute isEqualToString: NSAccessibilityRoleAttribute] )
		return NSAccessibilitySliderRole;
	else if( [attribute isEqualToString: NSAccessibilityRoleDescriptionAttribute] )
		return [self _accessibilityRoleDescription];
    else if( [attribute isEqualToString: NSAccessibilityFocusedAttribute] )
		return [NSNumber numberWithBool: [[NSApp accessibilityAttributeValue: NSAccessibilityFocusedUIElementAttribute] isEqual: self]];
    else if( [attribute isEqualToString: NSAccessibilityParentAttribute] )
		return NSAccessibilityUnignoredAncestor( mContainer );
    else if( [attribute isEqualToString: NSAccessibilityWindowAttribute] )
		return [mContainer accessibilityAttributeValue: NSAccessibilityWindowAttribute];
    else if( [attribute isEqualToString: NSAccessibilityTopLevelUIElementAttribute] )
		return [mContainer accessibilityAttributeValue: NSAccessibilityTopLevelUIElementAttribute];
    else if( [attribute isEqualToString: NSAccessibilityPositionAttribute] )
		return [self _accessibilityPosition];
    else if( [attribute isEqualToString: NSAccessibilitySizeAttribute] )
		return [self _accessibilitySize];
	else if( [attribute isEqualToString: NSAccessibilityDescriptionAttribute] )
		return [self _accessibilityDescription];
	else if( [attribute isEqualToString: NSAccessibilityValueAttribute] )
		return [self _accessibilityValue];
	else if( [attribute isEqualToString: NSAccessibilityEnabledAttribute] )
		return [NSNumber numberWithBool: YES];
	else if( [attribute isEqualToString: NSAccessibilityChildrenAttribute] )
		return [NSArray array];
	else if( [attribute isEqualToString: NSAccessibilityMinValueAttribute] )
		return [NSNumber numberWithDouble: 1.0];
	else if( [attribute isEqualToString: NSAccessibilityMaxValueAttribute] )
		return [NSNumber numberWithDouble: 0.0];				
    else
		return nil;
}

- (BOOL)accessibilityIsAttributeSettable: (NSString *)attribute
{
    if( [attribute isEqualToString:NSAccessibilityFocusedAttribute] )
		return YES;
    else
		return NO;
}

- (void)accessibilitySetValue: (id)value forAttribute: (NSString *)attribute
{
    if( [attribute isEqualToString:NSAccessibilityFocusedAttribute] )
		[self _accessibilitySetFocus];
}

- (NSArray *)accessibilityActionNames
{
    return [NSArray arrayWithObjects:
		NSAccessibilityIncrementButtonAttribute,
		NSAccessibilityDecrementButtonAttribute,
		nil];
}

- (NSString *)accessibilityActionDescription: (NSString *)action
{
    return NSAccessibilityActionDescription( action );
}

- (void)accessibilityPerformAction: (NSString *)action
{
	NSLog( @"%s %@", __func__, action );
}

- (BOOL)accessibilityIsIgnored
{
    return NO;
}

- (id)accessibilityHitTest: (NSPoint)point
{
	return self;
}

- (id)accessibilityFocusedUIElement
{
	return self;
}

