package com.guru.section 
{
	import com.guru.call.FunctionCall;
	import com.guru.call.ICall;
	import com.guru.display.LabelMovieClip;
	import com.guru.panel.Panel;
	import com.guru.project.whisper.book.Book;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Eddy Lee
	 */
	public class SwfSection extends SectionBase
	{
		private var mPath_str:String;
		private var mLoader:Loader;
		private var mContainer_mc:MovieClip;
		private var mLoaderControl:LabelMovieClip;
		private var mLoaderInfo:LoaderInfo;
		private var mLoading_mc:MovieClip;
		private var mURLRequest:URLRequest;
		private var mLoadDoneCall:ICall;
		private var mSectionLoadingBar:SectionLoadingBar;
		
		public function SwfSection(pIndex_str:String, pPath_str:String, pContainer_mc:MovieClip, pLoading_mc:MovieClip)
		{
			super(pIndex_str, null);
			mContainer_mc = pContainer_mc;
			mPath_str = pPath_str;
			mLoading_mc = pLoading_mc;
		}
		
		public function loadSwf(pDoneCall:ICall = null):void
		{
			mLoadDoneCall = pDoneCall;
			mSectionLoadingBar = new SectionLoadingBar(mLoading_mc);
			mSectionLoadingBar.show(new FunctionCall(this, doLoadSwf));
		}
		
		override public function dispose():void 
		{
			mContainer_mc.removeChild(mLoader);
			mLoader.unload();
			mLoader = null;
			mSectionLoadingBar.dispose();
			mSectionLoadingBar = null;
			super.dispose();
		}
		
		public function getPath():String
		{
			return mPath_str;
		}
		
		private function doLoadSwf():void
		{
			mLoader = new Loader();
			mLoaderInfo = mLoader.contentLoaderInfo;
			mURLRequest = new URLRequest(mPath_str);
			mLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			mLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			mLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
			
			mLoader.load(mURLRequest);
			
			Book.getInstance().loopFlip();
		}
		
		private function onLoadError(e:IOErrorEvent):void
		{
			Logger.eddy("onLoadError: " + e);
		}
		
		private function onLoadComplete(e:Event):void
		{
			//Book.getInstance().flip(new FunctionCall(mSectionLoadingBar, mSectionLoadingBar.hide, new FunctionCall(this, onLoadComplete2)));
			Book.getInstance().flip(new FunctionCall(Book.getInstance(), Book.getInstance().show, new FunctionCall(mSectionLoadingBar, mSectionLoadingBar.hide, new FunctionCall(this, onLoadComplete2))));
			//mSectionLoadingBar.hide(new FunctionCall(this, onLoadComplete2));
		}
		
		private function onLoadComplete2():void
		{
			mContainer_mc.addChild(mLoader);
			mTimeline_mc = MovieClip(mLoader.content);
			
			if (mLoadDoneCall != null)
			{
				mLoadDoneCall.execute();
				mLoadDoneCall = null;
			}
		}
		
		private function onLoadProgress(e:ProgressEvent):void
		{
			mSectionLoadingBar.updateProgress(e.bytesLoaded/e.bytesTotal);
		}
		
	}

}