<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
		 xmlns:s="library://ns.adobe.com/flex/spark" 
		 xmlns:mx="library://ns.adobe.com/flex/mx"
		 creationComplete="creationCompleteHandler(event)"
		 width="100%" height="23" xmlns:ui="com.xt.hb.views.ui.*" xmlns:ui1="com.xt.hb.ui.*" >
	<fx:Declarations>
		<mx:DateFormatter id="dateInputFormator" formatString="YYYY-MM-DD JJ:NN" />
	</fx:Declarations>
	<s:HGroup id="dt" gap="0" verticalAlign="middle" width="100%" height="100%">
		<ui1:QTextInput id="returnTime" width="100%" editable="{initeditable}" text="{initvalue}" skinClass="com.skins.tdt.DateTimeInputSkin"/>
		<s:Button width="22" height="22" click="open(event)" icon="@Embed(source='mx/controls/DateChooser.png')"/>
		<s:Spacer width="5"/>
		<s:Button width="22" height="22" click="button1_clickHandler(event)" icon="@Embed(source='flexResources/yjpt/list/iconpng.png')" toolTip="清空"/>
	</s:HGroup>
	<fx:Script>
		<![CDATA[
			import mx.binding.utils.BindingUtils;
			import mx.containers.HBox;
			import mx.controls.Button;
			import mx.controls.Label;
			import mx.controls.NumericStepper;
			import mx.controls.Text;
			import mx.controls.TileList;
			import mx.core.IFlexDisplayObject;
			import mx.events.CloseEvent;
			import mx.events.FlexEvent;
			import mx.events.ListEvent;
			import mx.events.NumericStepperEvent;
			import mx.managers.PopUpManager;
			
			[Bindable]
			private var initvalue: String;
			[Bindable]
			private var initeditable:Boolean;
//			[Bindable]
//			public var dt_percentWidth:Number;
			
			public var resultTime:Text;
			public var hasTime:Boolean=false;
			
			private var m_width:Number = 168;
			
			private var pop:Object;
//			private var isOpen:Boolean = false;
			private var nowTime:Date;
			
			protected function creationCompleteHandler(event:FlexEvent):void
			{
//				BindingUtils.bindProperty(this.dt,"percentWidth",this,"dt_percentWidth");
			}
			
//			override public function set width(value:Number):void{
//				dt.width = value;
//			}
		
//			override public function set percentWidth(value:Number):void{
//				dt_percentWidth = value;
//			}
		
			private function open(event:MouseEvent):void{
				resultTime = new Text();
//				if(!isOpen){
				if(pop==null||!systemManager.contains(pop as DisplayObject)){
					if(nowTime==null){
						nowTime=new Date;
					}
					pop = PopUpManager.createPopUp(this, mx.containers.TitleWindow, false);
					pop.showCloseButton = true;
					pop.addEventListener(CloseEvent.CLOSE,closeHandler);
					pop.addEventListener(MouseEvent.ROLL_OUT,closeHandler);
					pop.move(event.stageX+event.target.width-event.localX-175,event.stageY-event.localY+25);
					//pop.title = nowTime.fullYear.toString() + " 年 " + (nowTime.month+1) + " 月";
					pop.title = "时间选择";
					pop.setStyle("fontSize",12);
					
					
					var yearChangeHandler:Function = function(evt:NumericStepperEvent):void{
						nowTime.setFullYear(evt.value,nowTime.month,nowTime.date);
						if(systemManager.contains(pop as DisplayObject)){
							PopUpManager.removePopUp(pop as IFlexDisplayObject);
							open(event);
						}
//						isOpen = false;
					}
					
					var monthChangeHandler:Function = function(evt:NumericStepperEvent):void{
						nowTime.setFullYear(nowTime.fullYear,evt.value-1,nowTime.date);
						if(systemManager.contains(pop as DisplayObject)){
							PopUpManager.removePopUp(pop as IFlexDisplayObject);
							open(event);
						}
//						isOpen = false;
					}
					
					var minuteChangeHandler:Function = function(evt:NumericStepperEvent):void{
						nowTime.setHours(nowTime.hours,evt.value,nowTime.seconds,nowTime.milliseconds);
					}
					
					var hourChangeHandler:Function = function(evt:NumericStepperEvent):void{
						nowTime.setHours(evt.value,nowTime.minutes,nowTime.seconds,nowTime.milliseconds);
					}
					
					var testHandler:Function = function(event:ListEvent):void{
						nowTime.date = new Number(event.target.selectedItems[0]);
						resultTime.text = nowTime.fullYear + "-" + (nowTime.month+1) + "-" 
							+ event.target.selectedItems;
						if(hasTime)
							resultTime.text = resultTime.text
							+ " " +hour.value
							+ ":" + minute.value + ":" +nowTime.seconds;
//						returnTime.text =  resultTime.text;
						returnTime.text = dateInputFormator.format(resultTime.text);
					}
					
					
					var hBoxTop:HBox = new HBox();
					hBoxTop.width = m_width;
					var year:NumericStepper = new NumericStepper();
					year.setStyle("fontSize",10);
					year.stepSize = 1;
					year.minimum = 1900;
					year.maximum = 2100;
					year.width = 60;
					year.height = 20;
					year.value = nowTime.fullYear;
					year.addEventListener(NumericStepperEvent.CHANGE,yearChangeHandler);
					
					var y_label:Label = new Label();
					y_label.text = "年";
					y_label.setStyle("textAlign","center");
					y_label.width = 15;
					
					var month:NumericStepper = new NumericStepper();
					month.setStyle("fontSize",10);
					month.stepSize = 1;
					month.minimum = 0;
					month.maximum = 13;
					month.width = 50;
					month.height = 20;
					month.value = nowTime.month+1;
					month.addEventListener(NumericStepperEvent.CHANGE,monthChangeHandler);
					
					var m_label:Label = new Label();
					m_label.text = "月";
					m_label.setStyle("textAlign","center");
					m_label.width = 15;
					
					hBoxTop.addChild(year);
					hBoxTop.addChild(y_label);
					hBoxTop.addChild(month);
					hBoxTop.addChild(m_label);
					
					pop.addChild(hBoxTop);
					
					var hBox:HBox = new HBox();
					hBox.width = m_width;
					hBox.setStyle("horizontalGap",0);
					var weekName:Array = new Array("日","一","二","三","四","五","六");
					for(var index:Number=0; index<weekName.length; index++){
						var lab:Label = new Label();
						lab.text = weekName[index];
						lab.width = 24;
						lab.setStyle("textAlign","center");
						lab.setStyle("fontSize",12);
						if(weekName[index]=="日"){
							lab.setStyle("color","red");
						}
						hBox.addChild(lab);
					}
					pop.addChild(hBox);
					
					var tmpDate:Date = new Date(nowTime.fullYear,nowTime.month+1,0);
					var sumDay:Number = tmpDate.date ;
					
					var firstDay:Date = new Date(nowTime.fullYear,nowTime.month,1);
					var wday:Number = firstDay.day ;
					
					var todayNum:Number = nowTime.date;
					
					var rowNum:uint = Math.ceil((sumDay+wday)/7);
					//Alert.show(Math.round((sumDay+day)/7).toString());
					
					var tileList:TileList = new TileList();
					tileList.setStyle("fontSize",10);
					tileList.columnCount=7;
					tileList.rowCount=rowNum;
					tileList.width = 7*25;
					tileList.height = rowNum * 25;
					
					var myDP:Array = new Array();
					for(var i:Number=0; i<sumDay; i++){
						myDP.push((i+1).toString());
					}
					for(i=0; i<wday; i++){
						myDP.unshift(null);
					}
					tileList.dataProvider = myDP;
					tileList.selectedIndex = wday+todayNum-1; 
					tileList.addEventListener(ListEvent.ITEM_CLICK,testHandler);
					pop.addChild(tileList);
					
					if(hasTime)
					{
						var hBoxBottom:HBox = new HBox();
						hBoxBottom.width = m_width;
						hBoxBottom.setStyle("horizontalAlign","right");
						
						var hour:NumericStepper = new NumericStepper();
						hour.setStyle("fontSize",10);
						hour.stepSize = 1;
						hour.minimum = 0;
						hour.maximum = 23;
						hour.width = 50;
						hour.height = 20;
						hour.value = new Date().hours;
						hour.setStyle("paddingRight",0);
						hour.addEventListener(NumericStepperEvent.CHANGE,hourChangeHandler);
						var h_label:Label = new Label();
						h_label.text = "时";
						h_label.setStyle("textAlign","center");
						h_label.width = 15;
						
						var minute:NumericStepper = new NumericStepper();
						minute.setStyle("fontSize",10);
						minute.stepSize = 1;
						minute.minimum = 0;
						minute.maximum = 59;
						minute.width = 50;
						minute.height = 20;
						minute.value = new Date().minutes;
						minute.addEventListener(NumericStepperEvent.CHANGE,minuteChangeHandler);
						var mi_label:Label = new Label();
						mi_label.text = "分";
						mi_label.setStyle("textAlign","center");
						mi_label.width = 15;
						
						//					var subBtn:Button = new Button();
						//					subBtn.width = 40;
						//					subBtn.label = "确定";
						hBoxBottom.addChild(hour);
						hBoxBottom.addChild(h_label);
						hBoxBottom.addChild(minute);
						hBoxBottom.addChild(mi_label);
						//					hBoxBottom.addChild(subBtn);
						pop.addChild(hBoxBottom);
					}
					
//					isOpen = true;
				}else{
					if(systemManager.contains(pop as DisplayObject)){
						PopUpManager.removePopUp(pop as IFlexDisplayObject);
					}
//					isOpen = false;
				}
			}
			private function closeHandler(event:Event):void{
				if(systemManager.contains(pop as DisplayObject)){
					PopUpManager.removePopUp(event.target as IFlexDisplayObject);
				}
//				isOpen = false;
			}
			
			public function set text(str:String):void{
				initvalue = str ;
			}
			public function get text():String{
				return returnTime.text ;
			}
			
			public function set dateTime(str:Date):void{
				if(str==null)
				{
					nowTime = new Date();
					returnTime.text = "";
					return;
				}
				nowTime = str;
				var strDate:String = nowTime.fullYear + "-" + (nowTime.month+1) + "-" + nowTime.date ;
				if(hasTime)
					strDate = strDate
					+ " " + nowTime.hours + ":" + nowTime.minutes;
//				returnTime.text = strDate;
				returnTime.text = dateInputFormator.format(strDate);
				
			}
			public function get dateTime():Date{
				if(returnTime.text!=""&&returnTime.text!=null&&returnTime.text.length>0)
					return nowTime ;
				return null;
			}
			
			public function set editable(str:Boolean):void{
				initeditable = str;
			}
			public function get editable(): Boolean{
				return returnTime.editable;
			}
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				returnTime.text="";
			}
			
		]]>
	</fx:Script>
</s:Group>
