package game
{
	import com.adobe.crypto.*;
	
	import flash.net.*;
	
	public class FbManager
	{
		protected static var mInstance:FbManager 	= null;
		protected var mfbId:String 				= '';
		protected var mfbName:String 				= '';
		
		static public function Create( fbId:String, fbName:String ):void
		{
			FbManager.Instance.Init( fbId, fbName );	
		}
		
		static public function get Instance():FbManager
		{
			if ( mInstance == null )
				mInstance = new FbManager();
			
			return mInstance;
		}
		
		public function FbManager()
		{
		}

		public function Init( fbId:String, fbName:String ):void
		{
			mfbId 	= fbId;
			mfbName = fbName;
		}
		
		public function get fbId():String
		{
			return mfbId;
		}
		
		public function get fbName():String
		{
			return mfbName;
		}
		
		public function genHash( gamenum:Number, val:String ):String
		{
			return MD5.hash( "joju_mo_" + mfbId + "_" + gamenum.toString() + "_" + val.toString() );
		}
		
		public function getUrl():String
		{
			return "http://spartan.no-ip.org/mo/index.php";
		}
		
		public function genUrl( exec:int, gamenum:Number, val:String ):String
		{
			return "s=1&exec=" + exec.toString() + "&fbid=" + mfbId + "&game=" + gamenum.toString() + "&val=" + val.toString() + "&hash=" + genHash( gamenum, val );
		}
		
		public function sendRequest( url:String ):void
		{
			var request:URLRequest = new URLRequest(url);
			request.method = URLRequestMethod.GET;
			
            try
			{
                sendToURL(request);
            }
            catch (e:Error)
			{
				trace( "error sending url" );
            }
		}
		
		public function sendHighScore( score:Number ):void
		{
			if ( mfbId != null )
				sendRequest( getUrl() + "?" + genUrl( 1, 999, score.toString() ) );
		}
		
	}
}