package modules.chat.blocks.help
{
	import flash.display.*;
	import flash.events.*;
	import modules.chat.connect.BxConnect;
	import modules.chat.blocks.BxBlock;
	import utils.*;
	
	/**
	* Dispatched when the topic is being selected.
	* The topic ID is passed as <code>data</code> value for event.
	*/
	[Event(name="open", type="utils.BxXmlEvent")]
	
	/**
	* Help block.
	*/
	dynamic public class BxHelp extends BxBlock
	{
		private const iPadding:Number = 2;
		private var
			bLoaded:Boolean = false,
			iPaneY:Number,
			aTopics:BxList,
			mcHelpPane:Object;
		/**
		* @private
		*/
		function BxHelp()
		{
			sCaptionKey = "blockHelp";
			iPaneY = this.mcPane.y;
			bMaximized = true;
			aTopics = new BxList();
			aTopics.setHolder(this.mcPane, (this.mcHeader.width - this.mcPane.x * 2), (this.mcBack.height - (this.mcPane.y - this.mcHeader.height) * 2), 0);
			aTopics.padding = iPadding;
			aTopics.left = iPadding;
			parent.parent.addEventListener(BxConnect.EVENT_CONNECTED, onConnected);
		}
		
		private function onConnected(event:Event):void
		{
			mcHelpPane = root.mcChat.addPane(new BxHelpPane());
		}
		/**
		* Help pane object in chat area
		*/
		public function get pane():Object {return mcHelpPane;}
		/**
		* @private
		*/
		override public function set maximized(bMax:Boolean):void
		{
			this.mcPane.y = bMax ? iPaneY : 0;
			this.mcPane.scaleY = bMax ? 1 : 0;
			this.mcPane.visible = bMax;
			super.maximized = bMax;
		}
		/**
		* @private
		*/
		override public function set visible(b:Boolean):void
		{
			if(b && !bLoaded) loadHelp();
			super.visible = b;
		}
		
		private function loadHelp():void
		{
			root.oXml.returnXml(root.oXml.getXmlUrl("help"), this, "onLoadHelp", 3);
			root.mcMessage.init(root.oLanguage.getString("msgLoading"));
		}
		
		/**
		* @private
		*/
		public function onLoadHelp(event:Event):void
		{
			if(bLoaded) return;
			var aData:Object = event.data[0][0];
			for(var i:Number=0; i<aData.length; i++)
			{
				var mcTopic:Object = aTopics.add(new BxTopic(aData[i]));
				mcTopic.addEventListener(Event.OPEN, onTopicClick);
			}

			bLoaded = true;
			mcHelpPane.init(aData);
			root.mcMessage.visible = false;
		}
		
		private function onTopicClick(event:Event):void
		{
			dispatchEvent(new BxXmlEvent(Event.OPEN, event.data));
		}
		/**
		* @private
		*/
		override public function set width(iWidth:Number):void
		{
			var iShiftX:Number = iWidth - this.mcHeader.width;
			this.mcPane.setSize(this.mcPane.width + iShiftX);
			
			for(var i:Number=0; i<aTopics.length; i++)
				aTopics.elements[i].width += iShiftX;
				
			super.width = iWidth;
		}
		/**
		* @private
		*/
		override public function set height(iHeight:Number):void
		{
			super.height = iHeight;
			this.mcPane.setSize(-1, (this.mcBack.height - (this.mcPane.y - this.mcHeader.height) * 2));
		}
	}
}