package com.guru.utils
{
	import flash.events.*;
	
	public class Delegate
	{
		private var mScope:Object = null;
		private var mFunction:Function = null;
		private var mParam:Array = null;
		
		/**
		 * init the delegate function using in event listener
		 * @param pScope the scope of the class
		 * @param pFunc the function name
		 * */
		public function Delegate(pScope:Object, pFunc:Function, ... args)
		{
			mScope = pScope;
			mFunction = pFunc;
			if ( args.length > 0 )
			{
				mParam = args.slice(0,args.length);
			}
			else
			{
				mParam = new Array();
			}
		}
		
		/**
		 * add the passing variables
		 * @param pValue the object of the variables
		 * */
		public function addParam(pValue:Object):void
		{
			mParam.push(pValue);
		}
		
		/**
		 * get the function to the event listener
		 * */
		public function getFunction(... args):Function
		{
			if ( args.length > 0 )
			{
				var tmpParam = args.slice(0,args.length);
				mParam = mParam.concat(tmpParam);
			}
			return execute;
		}
		
		/**
		 * this for the event listener use, user do not use
		 * */
		public function execute(pEvent = null):void
		{
			var param_array = mParam.concat();
			param_array.unshift(pEvent);
			
			mFunction.apply(mScope,param_array);
		}
	}
}