HJ.PeopleScheduleSelector = Class.create();

HJ.PeopleScheduleSelector.prototype = Object.extend(new Af.HtmlDialog(), {
      
      initialize: function(title, handler, content) {
         this._initializeDialog(title, handler, content);
         this.myPeopleDataList = null;
         this.myScheduleDataList = null;
         this.selectedScheduleList = null;
         this.selectedPeopleList = null;
         this.scheduleTable = null;
         this.peopleTable = null;
		 this.singleSelectionMode = true;
		 this.tableNormalBackcolor = "#ffffff";
         
      },
      
      setDataList: function(mySchedules,myPeople,selectedUUID){
         this.myScheduleDataList = mySchedules;
         this.myPeopleDataList = new Array();
         this.selectedUUID = selectedUUID;

		  for (Idx1 = 0; Idx1 < myPeople.length ;Idx1++ ) {			 
			 if(myPeople[Idx1]["email"] != null || myPeople[Idx1]["email"] != undefined) {				 				
				  myPeople[Idx1]["name"] = getFullName(myPeople[Idx1]);
				  this.myPeopleDataList.push(myPeople[Idx1]);				  
			  }
		 }
		 
		 if(this.myPeopleDataList != null && this.myPeopleDataList.length > 0){
			 this.myPeopleDataList.sort(sortByLastName);
		 }
        
         this.updateView();
         
      },
      
    
      updateView: function(){
         if((this.myScheduleDataList == null && this.myPeopleDataList == null) ||
         (this.scheduleTable == null && this.peopleTable == null)) {
            return;
         }
         
         this.peopleTable.setDataList(this.myPeopleDataList);
         this.scheduleTable.setDataList(this.myScheduleDataList);

		 var pl = this.myPeopleDataList;
		 var sl = this.myScheduleDataList;

		 if( this.schSelectionDiv != null && sl != null && sl.length > 7){			
			 this.schSelectionDiv.style.overflow = "auto";
			 this.schSelectionDiv.style.height = "130px";				
		 }		 
		 if(this.peopleSelectionDiv != null && pl != null && pl.length > 7){			 
			this.peopleSelectionDiv.style.overflow = "auto";
			this.peopleSelectionDiv.style.height = "130px";		
		 }
		 if(this.peopleScheduleSelector != null && ((sl.length  + pl.length) > 14)) {			
			 this.peopleScheduleSelector.style.height = "330px";		
		 }
        
      },
      
      
      templateLoaded: function() {
         this.element.style.zIndex = "500001";
         var ec = new Af.ElementCollection(this.element);

		 this.schSelectionDiv = ec.getFirstElementById("schSelectionDiv") ;
		 this.peopleSelectionDiv = ec.getFirstElementById("peopleSelectionDiv") ;
		 this.peopleScheduleSelector = ec.getFirstElementById("peopleScheduleSelector");

         
         this.scheduleTable = new Af.HtmlTable("scheduleTable", "name", ec);
         this.scheduleTable.listener = this;
         this.scheduleTable.normalBackcolor = this.tableNormalBackcolor;
   
         
         
			var s;
			if ( this.singleSelectionMode) {
			   s = "name";
			} else {
			   s = "isSelected,name";
			}
			this.peopleTable = new Af.HtmlTable("peopleTable", s, ec);
			this.peopleTable.listener = this;
			this.peopleTable.normalBackcolor = this.tableNormalBackcolor;
        
		  
         this.btnDone = ec.getFirstElementById("btnDone");
         this.armedButton = this.btnDone;
         this.btnDone.onclick = this.selectionDone.bind(this);
         this.updateView();
      },
      
      selectionDone: function (){
         var r = this.scheduleTable.selectedRowNumber;
         var obj = null;
         if (r < 0) {
            r = this.peopleTable.selectedRowNumber
            if (r >= 0) {
               obj = this.myPeopleDataList[r];
            }
         } else {
             obj = this.myScheduleDataList[r];
         }
         if (this.handler != null && this.handler.selectionDone != null) {
            this.handler.selectionDone(this, obj);
         }
         this.hide();
         
      },
      
      cellSelected: function(row, col, reselected, htmlTable) {	
		 htmlTable.normalBackcolor = "#f6f6f6";
         if (htmlTable == this.peopleTable) {
            this.scheduleTable.unselectRow(this.scheduleTable.selectedRowNumber);
         }
         if (htmlTable == this.scheduleTable) {
            this.peopleTable.unselectRow(this.peopleTable.selectedRowNumber);
         }
         htmlTable.selectRow(row);
      }
});

var psSelector = null;
function getPoepleScheduleSelector(handler) {
   if (psSelector == null) {
      psSelector = new HJ.PeopleScheduleSelector("Choose from My Calendars or My People", handler, null);
      //psSelector.width = "290px";
   } else {
      psSelector.handler = handler;
   }
   psSelector.showHTMLTemplate(basePageURL + "PeopleScheduleSelector.htm", "content", "peopleScheduleSelector");
   psSelector.centerDialog(400, 300);
   return psSelector;
   
}

