package com.ederdiaz.utils
{
	import flash.ui.MouseCursor;
	import flash.ui.MouseCursorData;
	import flash.geom.Point;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;

	/**
	 * @author Eder
	 */
	public class GrabCursor
	{
		private var isOpen:Boolean = true;
		private var isHidden:Boolean = true;

		public function GrabCursor()
		{
			//function that initially takes the graphics for the cursor
			setupBitmapData();
		}

		private function setupBitmapData():void
		{
			//we create instances of the hands bitmapData
			var openedBmd:BitmapData = new HandOpened();
			var closedBmd:BitmapData = new HandClosed();

			//we must create the mouse cursor data first
			var cursorDataOpen:MouseCursorData = new MouseCursorData();
			//we have to center the bitmap so it corresponds to the mouse position
			cursorDataOpen.hotSpot = new Point(9, 9);
			//each cursor data must have a vector of bitmapdata (in case you need an animated one)
			var bitmapDatasOpen:Vector.<BitmapData> = new Vector.<BitmapData>(1, true);
			//here we just assign 1 so it won't be animated
			bitmapDatasOpen[0] = openedBmd;
			cursorDataOpen.data = bitmapDatasOpen;
			//now we register it with an id
			Mouse.registerCursor("open_grab_cursor", cursorDataOpen);

			
			//we repeat the process for closed hand cursor
			var cursorDataClosed:MouseCursorData = new MouseCursorData();
			cursorDataClosed.hotSpot = new Point(9, 9);
			var bitmapDatasClosed:Vector.<BitmapData> = new Vector.<BitmapData>(1, true);
			bitmapDatasClosed[0] = closedBmd;
			cursorDataClosed.data = bitmapDatasClosed;
			Mouse.registerCursor("closed_grab_cursor", cursorDataClosed);
		}

		public function show():void
		{
			if (!isHidden)
				return;
			if (isOpen)
				Mouse.cursor = "open_grab_cursor";
			else
				Mouse.cursor = "closed_grab_cursor";
			isHidden = false;
		}

		public function hide():void
		{
			if (isHidden)
				return;
			Mouse.cursor = MouseCursor.AUTO;
			isHidden = true;
		}

		public function open(e:MouseEvent = null):void
		{
			isOpen = true;
			if (!isHidden)
				Mouse.cursor = "open_grab_cursor";
		}

		public function close(e:MouseEvent = null):void
		{
			isOpen = false;
			if (!isHidden)
				Mouse.cursor = "closed_grab_cursor";
		}
	}
}
