 import mx.resources.ResourceManager;
 import mx.resources.IResourceManager;
 import spark.components.DataGrid;
-import spark.components.Grid;  
+import spark.components.Grid;
 import spark.components.supportClasses.GridSelectionMode;
 import spark.components.supportClasses.CellPosition;
 import spark.events.GridEvent;
 import spark.events.GridCaretEvent;
 import spark.events.GridSelectionEvent;
+import spark.events.GridSelectionEventKind;
 import spark.skins.spark.DefaultGridItemRenderer;
 
 use namespace mx_internal;


internal class ItemAccInfo
{
	import mx.core.UIComponent;
	import spark.components.DataGrid;
	import spark.components.Grid;  
	import spark.components.supportClasses.GridSelectionMode;
	import spark.components.supportClasses.CellPosition;

	/**
	 *  Constructor.
	 */
	public function ItemAccInfo()
	{
		super();
	}

	/**
	 *
	 *  @param master The UIComponent instance that this AccImpl instance
	 *  is making accessible.
	 *  @param childID The childID of the item of interest.
	 */
	public function setup(master:UIComponent, childID:uint):void
	{
		this.master = master;
		this.childID = childID;
		dataGrid = DataGrid(master);
		if (dataGrid.columns)
			columnCount = dataGrid.columns.length;
		else
			columnCount = 0;
		if (dataGrid.dataProvider)
			rowCount = dataGrid.dataProvider.length;
		else
			rowCount = 0;
		itemIndex = childID - 1;
		if (dataGrid.columnHeaderBar)
		{
			// There are column headers, and their childIDs come first.
			itemIndex -= columnCount;
			headerCount = columnCount;
		}
		else
		{
			headerCount = 0;
		}
		var mode:String = dataGrid.selectionMode;
		isCellMode = (
			mode == GridSelectionMode.SINGLE_CELL
			|| mode == GridSelectionMode.MULTIPLE_CELLS
		);
		maxChildID = 0;
		// Account for headers.
		maxChildID += headerCount;
		// Then for cells or rows as appropriate.
        if (isCellMode)
			maxChildID += rowCount * columnCount;
        else
			maxChildID += rowCount;
		isColumnHeader = false;
		isInvalid = false;
		if (childIDOutOfBounds(childID))
		{
			isInvalid = true;
			columnIndex = -1;
			rowIndex = -1;
			itemIndex = -1;
		}
		else if (itemIndex < 0)
		{
			// This childID refers to a header, not a data row or cell.
			isColumnHeader = true;
			columnIndex = itemIndex + columnCount;
			rowIndex = -1;
			itemIndex = -1;
		}
		else if (isCellMode)
		{
			rowIndex = Math.floor(itemIndex / columnCount);
			columnIndex = itemIndex % columnCount;
		}
		else
		{
			rowIndex = itemIndex;
			// Using 0 here so, for example, getItemRendererAt() calls still work for a row.
			columnIndex = 0;
		}
	}

	public var master:UIComponent;
	public var childID:uint;
	public var dataGrid:DataGrid;
	public var columnCount:int;
	public var headerCount:int;
	public var rowCount:int;
	public var maxChildID:int;
	public var itemIndex:int;
	public var isCellMode:Boolean;
	public var isInvalid:Boolean;
	public var isColumnHeader:Boolean;
	public var columnIndex:int;
	public var rowIndex:int;

	public function childIDFromRowAndColumn(rowIndex:int, columnIndex:int):uint
	{
		var childID:int = headerCount + 1;
		if (isCellMode)
			childID += rowIndex * columnCount + columnIndex;
		else
			childID += rowIndex;
		return uint(childID);
	}

    private function childIDOutOfBounds(childID: int): Boolean
    {
		if (int(childID) <= 0)
			return true;
        if (!dataGrid.dataProvider || !dataGrid.columns)
            return true
		if (childID > maxChildID)
			return true;
		return false;
    }

}

/**
 *  DataGridAccImpl is a subclass of AccessibilityImplementation
 *  which implements accessibility for the DataGrid class.
 *
 *  @langversion 3.0
 *  @playerversion Flash 10
 *  @playerversion AIR 1.5
