<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"
		 backgroundColor="white"
		 backgroundAlpha="0"
		 fontSize="12"
		 paddingTop="2" horizontalAlign="center">
	<mx:Script>
		<![CDATA[
			import com.xt.hb.utils.ConstUtil;
			
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.controls.DataGrid;
			import mx.controls.dataGridClasses.DataGridColumn;
			import mx.events.ItemClickEvent;
			import com.xt.hb.events.PageEvent;
			
			
			
			
			//首页图片不可用标志
			[Bindable]
			[Embed(source='/flexResources/Perpage/firstPage.png')]
			private var _icon_first_disabled:Class;
			private var firstPage:uint;
			//上一页图片不可用标志             
			[Bindable]
			[Embed(source='/flexResources/Perpage/prePage.png')]
			private var _icon_previous_disabled:Class;
			private var prePage:uint;
			//下一页图片不可用标志       
			[Bindable]
			[Embed(source='/flexResources/Perpage/nextPage.png')]
			private var _icon_next_disabled:Class;

			//末页图片不可用标志 
			[Bindable]
			[Embed(source='/flexResources/Perpage/lastPage.png')]
			private var _icon_last_disabled:Class;


			[Embed(source='/flexResources/Perpage/firstEnable.png')]
			private var _icon_first_enable:Class;

			[Embed(source='/flexResources/Perpage/previousEnable.png')]
			private var _icon_previous_enable:Class;

			[Embed(source='/flexResources/Perpage/nextEnable.png')]
			private var _icon_next_enable:Class;

			[Embed(source='/flexResources/Perpage/lastEnable.png')]
			private var _icon_last_enable:Class;

			private var nextPage:uint;
			private var lastPage:uint;

			//页码条数据,绑定   
			[Bindable]
			private var nav:ArrayCollection=new ArrayCollection();

			//默认起始页码,第1页   
			private var currentPageIndex:uint=0;

			//是否已初始化   
			private var isInit:Boolean=true;

			//总页数   
			private var _totalPages:uint=0;

			//是否重绘页码条，当使用服务端分页时使用   
			private var isCreateNavBar:Boolean=true;

			//显示到Grid的数据   
			[Bindable]
			public var viewData:ArrayCollection=new ArrayCollection();

			//所有的数据   
			public var orgData:ArrayCollection=new ArrayCollection();

			//每页记录数下拉框   
			public var pageSizeDropDownListData:ArrayCollection=new ArrayCollection();

			// 每页记录数 
			[Bindable]
			private var _pageSize:uint = ConstUtil.DATAGRIDCTRL_DATA_NUMBER;

			// 页码条上显示页码的个数   
			public var navSize:uint=1;

			//记录总数，调用服务端发页时使用   
			public var totalRecord:int=0;

			//分页函数   
			public var pagingFunction:Function=null;
			
			//是否为真分页
			public var isServerSidePage:Boolean = false;

			//分页条对应的Grid  
			[Bindable]
			public var dataGrid:DataGrid=null;
			//过滤的字段数组
			[Bindable]
			public var filterAttr:Array=[];
			//跳转的页码  
			private var sliderPager:Array=[];
			[Bindable]
			private var _currentPage:int=0;

			public function set currentPage(value:int):void
			{
				this._currentPage=value;
			}

			public function set totalPages(value:int):void
			{
				this._totalPages=value;
			}
			
			/**
			 * add
			 * */
			private function sendEvent():void
			{
				if(isServerSidePage) {
					this.dispatchEvent(new PageEvent(PageEvent.PAGECHANGE,_currentPage));
				}
			}
			
			public function dataBind(isServerSide:Boolean=false, bindType:int=0):void
			{
				isServerSidePage = isServerSide;
				currentPageIndex=0;
				if (isInit)
				{
					if (pageSizeDropDownListData == null)
					{
						pageSizeDropDownListData=new ArrayCollection();
						pageSizeDropDownListData.addItem({label: 5, data: 5});
						pageSizeDropDownListData.addItem({label: 10, data: 10});
						pageSizeDropDownListData.addItem({label: 20, data: 20});
						pageSizeDropDownListData.addItem({label: 30, data: 30});
					}
					isInit=false;
				}

				refreshDataProvider(currentPageIndex, isCreateNavBar, _pageSize, isServerSide, bindType);
				sliderPager=new Array();
				for (var j:int=0; j < _totalPages; j++)
				{
					sliderPager.push(j + 1);
				}
				cmb_popPageSize.dataProvider=sliderPager;
				if (cmb_popPageSize.dropdown)
				{
					cmb_popPageSize.dropdown.dataProvider=sliderPager;
				}
				if(isInit)
					initIcon();
			}

			public function clear():void
			{
				totalPagesLabel.text="0/0页 共0条";
				cmb_popPageSize.dataProvider=null;
			}

			/**
			 * 构建页码条
			 * pages:总页数
			 * pageIndex:当前页（注意，从0开始）
			 *
			 */
			private function createNavBar(pageIndex:uint=0):void
			{
				nav.removeAll();
				if (pageIndex >= 1)
				{
					firstPage=0;
					this.btn_firstNav.enabled=true;
					var intLFive:int=pageIndex - navSize; // calculate start of last 5;   
					prePage=intLFive;
					this.btn_preNav.enabled=true;
				}
				else
				{
					btn_firstNav.enabled=false;
					btn_preNav.enabled=false;
				}
				//页码条           
				for (var x:uint=0; x < navSize; x++)
				{
					var pg:uint=x + pageIndex;
					nav.addItem({label: pg + 1, data: pg});
					var pgg:uint=pg + 1;
					if (pgg >= _totalPages)
					{
						//搜索到最后一个页码，停止添加到navbar   
						x=navSize;
					}
				}
				//计算最后一组页码条中第一个页码的页码编号   
				var lastpage:Number=0;
				for (var y:uint=navSize; y <= _totalPages - 1; y=y + navSize)
				{
					//lets calculate the lastpage button   
					if (y + 5 > navSize)
					{
						lastpage=y;
					}
				}
				//向后图标   
				if (pg < _totalPages - 1)
				{
					nextPage=pg + 1;
					this.btn_nextNav.enabled=true;
					lastPage=lastpage;
					this.btn_lastNav.enabled=true;
				}
				else
				{
					this.btn_nextNav.enabled=false;
					this.btn_lastNav.enabled=false;
				}
			}

			/**
			 * 页码按钮按下(页码条点击)
			 */
			private function navigatePage(event:ItemClickEvent):void
			{
				refreshDataProvider(event.item.data, false);
			}

			/**
			 * 页码按钮按下,first,pre,next,last
			 */
			private function navigateButtonClick(pageString:String):void
			{
				var pageIndex:uint=0;
				switch (pageString)
				{
					case "firstPage":
					{
						pageIndex=firstPage;
						this.btn_firstNav.setStyle("icon", _icon_first_disabled);
						this.btn_preNav.setStyle("icon", _icon_previous_disabled);
						if (_totalPages > 1)
						{
							this.btn_lastNav.setStyle("icon", _icon_last_enable);
							this.btn_nextNav.setStyle("icon", _icon_next_enable);
						}
						//SiteContainer.dispatchEvent(new AppEvent("slider_reGetData"));
						break;
					}
					case "prePage":
					{
						_currentPage=this._currentPage - 1;
						if (_currentPage == 1)
						{
							this.btn_firstNav.setStyle("icon", _icon_first_disabled);
							this.btn_preNav.setStyle("icon", _icon_previous_disabled);
						}
						if (_currentPage == this._totalPages - 1)
						{
							this.btn_lastNav.setStyle("icon", _icon_last_enable);
							this.btn_nextNav.setStyle("icon", _icon_next_enable);
						}
						pageIndex=prePage;
						//SiteContainer.dispatchEvent(new AppEvent("slider_reGetData"));
						break;
					}
					case "nextPage":
					{
						_currentPage=_currentPage + 1;
						if (_currentPage == _totalPages)
						{
							this.btn_lastNav.setStyle("icon", _icon_last_disabled);
							this.btn_nextNav.setStyle("icon", _icon_next_disabled);
							this.btn_firstNav.setStyle("icon", _icon_first_enable);
							this.btn_preNav.setStyle("icon", _icon_previous_enable);
						}
						else
						{
							this.btn_lastNav.setStyle("icon", _icon_last_enable);
							this.btn_nextNav.setStyle("icon", _icon_next_enable);
							this.btn_firstNav.setStyle("icon", _icon_first_enable);
							this.btn_preNav.setStyle("icon", _icon_previous_enable);
						}
						pageIndex=nextPage;
						//	SiteContainer.dispatchEvent(new AppEvent("slider_reGetData"));
						break;
					}

					default: //尾页
					{
						_currentPage=_totalPages;
						this.btn_lastNav.setStyle("icon", _icon_last_disabled);
						this.btn_nextNav.setStyle("icon", _icon_next_disabled);
						if (_totalPages > 1)
						{
							this.btn_firstNav.setStyle("icon", _icon_first_enable);
							this.btn_preNav.setStyle("icon", _icon_previous_enable);
						}
						pageIndex=lastPage;
							//SiteContainer.dispatchEvent(new AppEvent("slider_reGetData"));
					}
				}
				if(!isServerSidePage)
				{
					refreshDataProvider(pageIndex);
				}else
				{
					refreshDataProvider(pageIndex,true,_pageSize,isServerSidePage);
					sendEvent();
				}

			}

			/**
			 * 更新数据源,更新表格显示数据
			 */
			private function refreshDataProvider(pageIndex:uint, isCreateNavBar:Boolean=true, _pageSize:uint=0, resultReturn:Boolean=false, bindType:int=0):void
			{
				var totalCount:String="";
				if (dataGrid == null)
					return;
				currentPageIndex=pageIndex;
				if (_pageSize == 0)
				{
					_pageSize=this._pageSize;
				}
				else
				{
					this._pageSize=_pageSize;
					if (!resultReturn && _pageSize > 0 && orgData != null)

						_totalPages=Math.ceil(orgData.length / _pageSize);

				}
				if (!resultReturn)
				{
					if (this.pagingFunction != null)
					{
						pagingFunction(pageIndex, _pageSize);
						this.isCreateNavBar=isCreateNavBar;
					}
					else if (orgData != null)
					{
						viewData=new ArrayCollection(orgData.source.slice((pageIndex * _pageSize), (pageIndex * _pageSize) + _pageSize));
						dataGrid.dataProvider=viewData;
						filterAttributes(dataGrid);
						if (this._totalPages > 0)
						{
							_currentPage=pageIndex + 1;
						}
						else
							_currentPage=0;
						totalCount=orgData.length.toString() + '条';
					}
				}
				else
				{
					dataGrid.dataProvider=orgData;
					filterAttributes(dataGrid);
					_currentPage=pageIndex + 1;
					_totalPages=Math.ceil(totalRecord / _pageSize);
					totalCount=totalRecord.toString() + '条';
				}
				if (this._totalPages > 0)
					_currentPage=pageIndex + 1;
				else
					_currentPage=0;
				totalPagesLabel.text=_currentPage + '/' + _totalPages + "页 共" + totalCount;
				this.cmb_popPageSize.selectedItem=_currentPage;
				if (isCreateNavBar)
					createNavBar(pageIndex);

			}

			/**
			*  增加字段过滤不显示
			*/
			private function filterAttributes(dataGrids:DataGrid):void
			{
				var columns:Array=dataGrids.columns;
				for (var m:int=0; m < filterAttr.length; m++)
				{
					for (var j:int=0; j < columns.length; j++)
					{
						var dtColumn:String=columns[j].dataField;
						var column:DataGridColumn=columns[j] as DataGridColumn;
						if (dtColumn == filterAttr[m]) //隐藏不必要显示的字段
						{
							column.visible=false;
							break;
						}
					}
				}
			}

			/**
			 * 每页记录数变更（下拉框选择）
			 */
			private function pageSizeSelectChange():void
			{
				var tmpPage:int=new int(cmb_popPageSize.selectedItem);
				var pageIndex:int=tmpPage - 1;
				if (tmpPage > this._totalPages)
				{
					Alert.yesLabel="确定";
					Alert.show("页码超出范围", "提示", Alert.YES, this);
					return;
				}
				this._currentPage=tmpPage;
				if (_currentPage > 1)
				{
					this.btn_firstNav.setStyle("icon", _icon_first_enable);
					this.btn_preNav.setStyle("icon", _icon_previous_enable);
				}
				if (_currentPage < _totalPages)
				{
					this.btn_lastNav.setStyle("icon", _icon_last_enable);
					this.btn_nextNav.setStyle("icon", _icon_next_enable);
				}
				if (pageIndex >= 0 && pageIndex < _totalPages && pageIndex != currentPageIndex)
				{
					if(!isServerSidePage)
					{
						refreshDataProvider(pageIndex);
					}
					else
					{
						refreshDataProvider(pageIndex,true,_pageSize,isServerSidePage);
					}
				}
				if (tmpPage == this._totalPages)
				{
					this.btn_lastNav.setStyle("icon", _icon_last_disabled);
					this.btn_nextNav.setStyle("icon", _icon_next_disabled);
					if (_totalPages > 1)
					{
						this.btn_firstNav.setStyle("icon", _icon_first_enable);
						this.btn_preNav.setStyle("icon", _icon_previous_enable);
					}

				}
				if (tmpPage == 1)
				{
					this.btn_firstNav.setStyle("icon", _icon_first_disabled);
					this.btn_preNav.setStyle("icon", _icon_previous_disabled);
					if (_totalPages > 1)
					{
						this.btn_lastNav.setStyle("icon", _icon_last_enable);
						this.btn_nextNav.setStyle("icon", _icon_next_enable);
					}
					else
					{
						this.btn_lastNav.setStyle("icon", _icon_last_disabled);
						this.btn_nextNav.setStyle("icon", _icon_next_disabled);
					}
				}
				if(isServerSidePage)
					sendEvent();
				//SiteContainer.dispatchEvent(new AppEvent("slider_reGetData"));
			}

			private function initIcon():void
			{
				if (this._totalPages > 0)
				{
					if (this._totalPages == 1)
					{
						this.btn_lastNav.setStyle("icon", _icon_last_disabled);
						this.btn_nextNav.setStyle("icon", _icon_next_disabled);
					}
					else
					{
						this.btn_lastNav.setStyle("icon", _icon_last_enable);
						this.btn_nextNav.setStyle("icon", _icon_next_enable);
					}
				}
				if (this.orgData.length == 0)
				{
					totalPagesLabel.text="0/0页 共0条";
					cmb_popPageSize.dataProvider=null;
				}
			}
		]]>
	</mx:Script>

	<mx:Label id="totalPagesLabel"
			  text="0/0页 共0条"
			  paddingTop="0"/>
	<mx:Button id="btn_firstNav" buttonMode="true" useHandCursor="true"
			   icon="{_icon_first_disabled}"
			   width="18"
			   height="18"
			   click=" navigateButtonClick('firstPage');"
			   enabled="{this._currentPage!=1}"
			   cornerRadius="10"/>
	<mx:Button id="btn_preNav" buttonMode="true" useHandCursor="true"
			   icon="{_icon_previous_disabled}"
			   width="18"
			   height="18"
			   click="navigateButtonClick('prePage');"
			   enabled="{this._currentPage>1}"
			   cornerRadius="10"/>
	<mx:Label text="转至:"
			  width="36"
			  paddingTop="0"/>
	<mx:ComboBox id="cmb_popPageSize"
				 keyDown="pageSizeSelectChange()"
				 width="55"
				 editable="false"
				 paddingLeft="0"
				 fontWeight="normal"
				 enter="pageSizeSelectChange()"
				 change="pageSizeSelectChange()"
				 close="pageSizeSelectChange()"
				 height="21"
				 />
	<mx:Label text="页"
			  width="20"
			  paddingTop="0"/>
	<mx:Button id="btn_nextNav" buttonMode="true" useHandCursor="true"
			   icon="{_icon_next_disabled}"
			   width="18"
			   height="18"
			   click="navigateButtonClick('nextPage');"
			   enabled="{_totalPages &gt; _currentPage}"
			   cornerRadius="10"/>
	<mx:Button id="btn_lastNav" buttonMode="true" useHandCursor="true"
			   icon="{_icon_last_disabled}"
			   width="18"
			   height="18"
			   click="navigateButtonClick('lastPage');"
			   enabled="{_currentPage!=this._totalPages}"
			   cornerRadius="10"/>

</mx:HBox>
