package com.guru.project.brother.vote.voteStep 
{
	import com.guru.call.FunctionCall;
	import com.guru.call.ICall;
	import com.guru.conn.ConnPath;
	import com.guru.display.button.LabelButton;
	import com.guru.display.DisplayAlign;
	import com.guru.display.externalLoader.ExternalLoader;
	import com.guru.display.Text;
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Eddy Lee
	 */
	public class VoteItem
	{
		private var mTimeline_mc:MovieClip;
		
		private var mMask_mc:MovieClip;
		private var mVoteGot:Text;
		private var mCaption:Text;
		private var mName:Text;
		private var mId:Text;
		private var mVoteBtn:LabelButton;
		private var mImageLoader:ExternalLoader;
		private var mXML:XML;
		private var mPressCall:ICall;
		
		public function VoteItem(pTimeline_mc:MovieClip)
		{
			mTimeline_mc = pTimeline_mc;
			
			mMask_mc = mTimeline_mc.mask_mc;
			mVoteGot = new Text(mTimeline_mc.voteGot_mc);
			mCaption = new Text(mTimeline_mc.caption_mc);
			mName = new Text(mTimeline_mc.name_mc);
			mId = new Text(mTimeline_mc.id_mc);
			mVoteBtn = new LabelButton(mTimeline_mc.voteBtn_mc);
			mImageLoader = new ExternalLoader(mTimeline_mc.imageLoader_mc, DisplayAlign.CENTER);
			
			mVoteBtn.setPressCall(new FunctionCall(this, onVoteBtnPress));
		}
		
		public function select():void
		{
			mVoteBtn.select();
		}
		
		public function setXML(pXML:XML):void
		{
			mXML = pXML;
		}
		
		public function refresh():void
		{
			mId.setText(mXML..pid[0]);
			mVoteGot.setText(mXML..voteGot[0]);
			mCaption.setText(mXML..caption[0]);
			mName.setText(mXML..name[0]);
			mImageLoader.getTimeline().scaleX = 1;
			mImageLoader.getTimeline().scaleY = 1;
			mImageLoader.load(ConnPath.getImagePrefix() + mXML..image[0], false, new FunctionCall(this, onImageLoadDone), new FunctionCall(this, onImageLoadFail), new FunctionCall(this, onImageFadeInDone));
		}
		
		public function setPressCall(pCall:ICall):void
		{
			mPressCall = pCall;
		}
		
		public function getXML():XML
		{
			return mXML;
		}
		
		public function getTimeline():MovieClip
		{
			return mTimeline_mc;
		}
		
		public function enable(pEnable_bol:Boolean):void
		{
			mVoteBtn.enabled = pEnable_bol;
		}
		
		public function unloadImage():void
		{
			mImageLoader.unload();
		}
		
		public function reset():void
		{
			mVoteBtn.reset();
		}
		
		public function dispose():void
		{
			mVoteGot.dispose();
			mCaption.dispose();
			mName.dispose();
			mId.dispose();
			mVoteBtn.dispose();
			mImageLoader.dispose();
		}
		
		private function onVoteBtnPress():void
		{
			if(mPressCall)
				mPressCall.execute();
		}
		
		private function onImageFadeInDone():void
		{
			
		}
		
		private function onImageLoadDone():void
		{
			if (mImageLoader.getTimeline().width > mMask_mc.width || mImageLoader.getTimeline().height > mMask_mc.height)
			{
				var widthRatio_num:Number = mImageLoader.getTimeline().width / mMask_mc.width;
				var heightRatio_num:Number = mImageLoader.getTimeline().height / mMask_mc.height;
				
				if (widthRatio_num > heightRatio_num)
				{
					mImageLoader.getTimeline().width /= widthRatio_num;
					mImageLoader.getTimeline().height /= widthRatio_num;
				}
				else
				{
					mImageLoader.getTimeline().width /= heightRatio_num;
					mImageLoader.getTimeline().height /= heightRatio_num;
				}
			}
			
			mImageLoader.fadeIn();
		}
		
		private function onImageLoadFail():void
		{
			
		}
		
	}

}