package com.guru.facebookApplicationBase 
{
	import com.adobe.images.JPGEncoder;
	import com.guru.call.FunctionCall;
	import com.guru.conn.ConnPath;
	import com.guru.facebookApplicationBase.message.Message;
	import com.guru.panel.Panel;
	import com.guru.panel.StatusBar;
	import com.hurlant.util.Base64;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.external.ExternalInterface;
	import flash.net.FileReference;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	/**
	 * ...
	 * @author Eddy Lee
	 */
	public class CommonFunction
	{
		
		public function CommonFunction() 
		{
			
		}
		
		public static function convertBmpDataIntoByteArrayString(pBmpData:BitmapData):String
		{
			var encoder:JPGEncoder = new JPGEncoder(80);
			var byteArray:ByteArray = encoder.encode(pBmpData);
			var byteArrayAsString:String = Base64.encodeByteArray(byteArray);
			return byteArrayAsString;
		}
		
		public static function gotoURL(pUrl_str:String):void
		{
			navigateToURL(new URLRequest(pUrl_str), "_blank");
		}
		
		public static function gotoFanPage():void
		{
			gotoURL("http://www.facebook.com");
		}
		
		public static function downloadFile(pPath_str:String):void
		{
			var request:URLRequest = new URLRequest(pPath_str);
			var localRef:FileReference = new FileReference();
			localRef.addEventListener(IOErrorEvent.IO_ERROR, onDownloadFileErrorHandler);
			localRef.addEventListener(ProgressEvent.PROGRESS, onDownloadFileProgressHandler);
			localRef.addEventListener(Event.COMPLETE, onDownloadFileCompleteHandler);
			localRef.addEventListener(Event.CANCEL, onDownloadFileCancelHandler);
			Panel.getProgressBar().showPanel();
			Panel.getProgressBar().setHideDoneCall(new FunctionCall(CommonFunction, CommonFunction.onDownloadFileCancelHandler, null, localRef));
			try
			{
				localRef.download( request );
			}catch (error:Error)
			{
				Logger.eddy("Unable to download file: " + pPath_str);
			}
		}
		
		public static function inviteFriends():void
		{
			ExternalInterface.call("showInviteBox");
			//navigateToURL(new URLRequest(ConnPath.getAppPrefix() + "invite.php"), "_top");
		}
		
		public static function getRandomNumber(pRange_num:Number):Number
		{
			return Math.floor(Math.random() * 10000) % pRange_num;
		}
		
		public static function drawRectangle(pStartX:Number, pStartY:Number, pWidth:Number, pHeight:Number, pColor:uint = 0xFFFF0000, pAlpha:Number = 1):MovieClip
		{
			var shape_mc:MovieClip = new MovieClip();
			
			var tmpShape:Shape = new Shape();
			tmpShape.graphics.beginFill(pColor, pAlpha);
			tmpShape.graphics.lineStyle(0, uint(0x000000), 0);
			tmpShape.graphics.moveTo(pStartX, pStartY);
			tmpShape.graphics.lineTo(pStartX + pWidth, pStartY);
			tmpShape.graphics.lineTo(pStartX + pWidth, pStartY + pHeight);
			tmpShape.graphics.lineTo(pStartX, pStartY + pHeight);
			tmpShape.graphics.lineTo(pStartX, pStartY);
			tmpShape.graphics.endFill();
			shape_mc.addChild(tmpShape);
			
			return shape_mc;
		}
		
		private static function onDownloadFileCancelHandler(e:Event, pLocalRef:FileReference = null):void
		{
			Logger.eddy("DOWNLOAD CANCELLED!");
			if(pLocalRef != null)
				pLocalRef.cancel();
			Panel.getProgressBar().setHideDoneCall(null);
			Panel.getProgressBar().hidePanel();
		}
		
		private static function onDownloadFileErrorHandler(e:IOErrorEvent):void
		{
			Logger.eddy(e);
			Panel.getProgressBar().setHideDoneCall(null);
			Panel.showStatusBar(StatusBar.STATUS, Message.DOWNLOAD_ERROR, new FunctionCall(Panel.getProgressBar(), Panel.getProgressBar().hidePanel));
		}
		
		private static function onDownloadFileCompleteHandler(e:Event):void
		{
			Logger.eddy(e);
			Panel.getProgressBar().setHideDoneCall(null);
			Panel.getProgressBar().hidePanel(new FunctionCall(Panel, Panel.showPanel, StatusBar.STATUS, Message.DOWNLOAD_SUCCESS));
		}
		
		private static function onDownloadFileProgressHandler(e:ProgressEvent):void
		{
			Logger.eddy(e);
			Panel.getProgressBar().updateProgress(Math.floor(e.bytesLoaded / e.bytesTotal));
		}
	}

}