package com.guru.framePulse {

	import com.guru.call.*;
	import com.guru.event.EventCall;
	import com.guru.event.SimpleEventDispatcher;
	import com.guru.utils.ArrayUtil;
	import com.guru.utils.MovieClipUtil;
	import flash.events.*;
	import flash.display.*;
	
	public class FramePulse {
		
		private static var sInstance:FramePulse = null;
		private var mEventDispatcher:SimpleEventDispatcher = null;
		private var _mc:MovieClip;
		
		private static function getInstance():FramePulse
		{
			if(sInstance == null)
			{
				sInstance = new FramePulse();
			}
			return sInstance;
		}
		
		/**
		 * add a listener listen to enterframe
		 * @param	pType_str	FramePulseEvent.ENTER_FRAME
		 * @param	pListener	a eventcall
		 */
		public static function addEventListener(pType_str:String, pListener:EventCall):void
		{
			getInstance().doAddEventListener(pType_str, pListener);
		}
		
		/**
		 * remove a listener listen to enterframe
		 * @param	pType_str	FramePulseEvent.ENTER_FRAME
		 * @param	pListener	a eventcall
		 */
		public static function removeEventListener(pType_str:String, pListener:EventCall):void
		{
			getInstance().doRemoveEventListener(pType_str, pListener);
		}
		
		/**
		 * remove all listener
		 * @param	pOjbect	the scope
		 */
		public static function removeListener(pOjbect):void
		{
			getInstance().doRemoveListener(pOjbect);
		}
		
		private function doAddEventListener(pType_str:String, pListener:EventCall):void
		{
			mEventDispatcher.addEventListener(pType_str, pListener);
		}
		
		private function doRemoveEventListener(pType_str:String, pListener:EventCall):void
		{
			mEventDispatcher.removeEventListener(pType_str, pListener);
		}
		
		private function onEnterFrame(pEvent:Event):void
		{
			mEventDispatcher.dispatchEvent(new FramePulseEvent(FramePulseEvent.ENTER_FRAME));
		}
		
		private function doRemoveListener(pOjbect):void
		{
			mEventDispatcher.removeListener(pOjbect);
		}
		
		function FramePulse()
		{
			mEventDispatcher = new SimpleEventDispatcher();
			_mc = new MovieClip();
			_mc.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
	}
}