package com.guru.section 
{
	import com.guru.call.FunctionCall;
	import com.guru.call.ICall;
	import com.guru.display.LabelMovieClip;
	import com.guru.component.panel.Panel;
	import com.guru.display.progressBar.GuruProgressBar;
	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 mProgressBar:GuruProgressBar;
		
		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;
			
			mLoading_mc.visible = false;
		}
		
		public function loadSwf(pDoneCall:ICall = null):void
		{
			mLoadDoneCall = pDoneCall;
			mProgressBar = new GuruProgressBar(mLoading_mc, "sectionLoading");
			mProgressBar.showPanel(new FunctionCall(this, doLoadSwf));
		}
		
		override public function dispose():void 
		{
			mContainer_mc.removeChild(mLoader);
			mLoader.unload();
			mLoader = null;
			mProgressBar.dispose();
			mProgressBar = 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);
		}
		
		private function onLoadError(e:IOErrorEvent):void
		{
			Logger.eddy("onLoadError: " + e);
		}
		
		private function onLoadComplete(e:Event):void
		{
			mTimeline_mc = MovieClip(mLoader.content);
			mTimeline_mc.gotoAndStop(1);
			
			mProgressBar.hidePanel(new FunctionCall(this, onLoadComplete2));
		}
		
		private function onLoadComplete2():void
		{
			mContainer_mc.addChild(mLoader);
			
			if (mLoadDoneCall != null)
			{
				mLoadDoneCall.execute();
				mLoadDoneCall = null;
			}
		}
		
		private function onLoadProgress(e:ProgressEvent):void
		{
			mProgressBar.updateProgress(e.bytesLoaded, e.bytesTotal);
		}
		
	}

}