HJ.PeopleGroupSelector = Class.create();

HJ.PeopleGroupSelector.prototype = Object.extend(new Af.HtmlDialog(), {
      
      initialize: function(title, handler, content) {
         this._initializeDialog(title, handler, content);
         this.myPeopleDataList = null;
         this.myGroupDataList = null;
         this.selectedGroupList = null;
         this.selectedPeopleList = null;
         this.groupTable = null;
         this.peopleTable = null;
		 this.singleSelectionMode = false;
		 this.tableNormalBackcolor = "#f6f6f6";	
		 this.displayTextMessage = "";
      },
      
      setDataList: function(myGroups,myPeople,selectedGroups,selectedPeople, disableSelected){		
         this.disableSelected = disableSelected;
         this.myGroupDataList = myGroups;
         this.myPeopleDataList = new Array();;
         this.selectedGroupList = selectedGroups;
         this.selectedPeopleList = selectedPeople;			
		 for (Idx1 = 0; Idx1 < myPeople.length ;Idx1++ ) {
			 if(myPeople[Idx1]["email"] != null || myPeople[Idx1]["email"] != undefined) {
				  if(" "!=getFullName(myPeople[Idx1])){
					myPeople[Idx1]["name"] = getFullName(myPeople[Idx1]);
				  }else{
					myPeople[Idx1]["name"] = myPeople[Idx1]["email"]; 
				  }
				  this.myPeopleDataList.push(myPeople[Idx1]);				  
			  } 
		 }
		 if(this.myGroupDataList != null && this.myGroupDataList.length > 0){
			 this.myGroupDataList.sort(sortGroups);
		 }
		 if(this.myPeopleDataList != null && this.myPeopleDataList.length > 0){
			 this.myPeopleDataList.sort(sortByLastName);
		 }
	     
         this.updateView();
         
      },
      
      setGroupDataList: function(myGroups, selectedGroups, disableSelected){
         this.disableSelected = disableSelected;
         this.myGroupDataList = myGroups;
         this.myPeopleDataList = null;
         this.selectedGroupList = selectedGroups;
         this.selectedPeopleList = null;

		 if(this.myGroupDataList != null && this.myGroupDataList.length > 0){
			 this.myGroupDataList.sort(sortGroups);
		 }
		 

         this.updateView();
      },
	  
	  setPeopleDataList: function(myPeoples, selectedPeoples, disableSelected){
	     this.disableSelected = disableSelected;
         this.myPeopleDataList = new Array();         
         this.selectedPeopleList = selectedPeoples;          
		 for (Idx2 = 0; Idx2 < myPeoples.length ;Idx2++ ) {
			 if(myPeoples[Idx2]["email"] != null || myPeoples[Idx2]["email"] != undefined) {
				  if(" "!=getFullName(myPeoples[Idx2])){
					myPeoples[Idx2]["name"] = getFullName(myPeoples[Idx2]);
				  }else{
					myPeoples[Idx2]["name"] = myPeoples[Idx2]["email"];
				  }
				  
				  this.myPeopleDataList.push(myPeoples[Idx2]);				  
			  } 
		 }		 
		 if(this.myPeopleDataList != null && this.myPeopleDataList.length > 0){
			 this.myPeopleDataList.sort(sortByLastName);
		 }
         this.updateView();
      },
      
      updateView: function(){
         if(this.myGroupDataList == null && this.myPeopleDataList == null) {
            return;
         }
         if (this.myGroupDataList != null) {
			 for(idx=0; idx < this.myGroupDataList.length; idx++){
				// find index of mylist Obj in selectedlist.
				var myListIndex = findObjIndex(this.selectedGroupList, this.myGroupDataList[idx]);

				if(myListIndex != null && myListIndex > -1) {
				   this.myGroupDataList[idx]["isSelected"] = true;
				} else {
				   this.myGroupDataList[idx]["isSelected"] = false;
				}
			 }
			 if(this.groupTable) {
				this.groupTable.setDataList(this.myGroupDataList);
				if (this.disableSelected) {
					this.groupTable.disableCheckedColumn(0);
				}
			 }
         }
         
         if (this.myPeopleDataList != null) {
			 for(idx=0;idx<this.myPeopleDataList.length;idx++){
				// find index of mylist Obj in selectedlist.
				var myListIndex = findObjIndex(this.selectedPeopleList,this.myPeopleDataList[idx]);
				if(myListIndex != null && myListIndex > -1) {
				   this.myPeopleDataList[idx]["isSelected"] = true;
				} else {
				   this.myPeopleDataList[idx]["isSelected"] = false;
				}
			 }
			 if(this.peopleTable) {
				this.peopleTable.setDataList(this.myPeopleDataList);
				if (this.disableSelected) {
					this.peopleTable.disableCheckedColumn(0);
				}
			 }
         }
		 var p = this.peopleSelectionDiv;
		 var pl = this.myPeopleDataList;
		 var g = this.groupSelectionDiv; 
		 var gl = this.myGroupDataList;
		 
		 if( p != null && pl != null && pl.length >= 7){			
			 this.peopleSelectionDiv.style.overflow = "auto";
			 this.peopleSelectionDiv.style.height = "130px";				
		 }		 
		 if(g != null && gl != null && gl.length >= 7){			 
			this.groupSelectionDiv.style.overflow = "auto";
			this.groupSelectionDiv.style.height = "130px";		
		 }
		 var numEntries = 0;
		 if (pl != null) {
		 	numEntries = pl.length;
		 }
		 if (gl != null) {
		 	numEntries += gl.lengh;
		 }
		 if(this.peopleSelector != null && (numEntries >= 14)) {			
			  if (is_ie) {
				 this.peopleSelector.style.height = "350px";		
			  }else{
				  this.peopleSelector.style.height = "340px";
			  }
		 }
		 this.setDisplayMessage();

      },
	
	  setDisplayMessage: function() {
		  if(this.messageTemplate != null && this.displayTextMessage != "") {
			  this.messageTemplate.style.display = "block";			  
			  this.messageTemplate.innerHTML = this.displayTextMessage;
		  }												  
	  },      
      
      templateLoaded: function() {		
		 var ec = new Af.ElementCollection(this.element);

		 this.peopleSelectionDiv = ec.getFirstElementById("peopleSelectionDiv") ;
		 this.groupSelectionDiv = ec.getFirstElementById("groupSelectionDiv") ;
		 this.peopleSelector = ec.getFirstElementById("peopleSelector") ;
		 this.messageTemplate = ec.getFirstElementById("messageTemplate") ;
		 
		 

         if (ec.getFirstElementById("groupTable") != null) {
            this.groupTable = new Af.HtmlTable("groupTable", "isSelected,name");
            this.groupTable.normalBackcolor = this.tableNormalBackcolor;
         }
         
         if (ec.getFirstElementById("peopleTable") != null) {
            var s;
            if ( this.singleSelectionMode) {
               s = "name";
            } else {
               s = "isSelected,name";
            }
            this.peopleTable = new Af.HtmlTable("peopleTable", s);
			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();
		 this.setDisplayMessage();
		 this.element.style.zIndex = "500001";
      },
      
      selectionDone: function (){
        
         if (this.myGroupDataList != null) {
             this.selectedGroupList.length = 0;
			 for(idx=0; idx<this.myGroupDataList.length; idx++){
				if(this.myGroupDataList[idx]["isSelected"] == true) {
				   this.selectedGroupList.push(this.myGroupDataList[idx]);
				}
			 }
         }
		 if (this.singleSelectionMode)	{	 
			 if (this.myPeopleDataList != null) {
				  this.selectedPeopleList.length = 0;
				  this.selectedPeopleList.push(this.myPeopleDataList[this.peopleTable.selectedRowNumber]);			 
			 }
		 } else if (this.myPeopleDataList != null) {
             this.selectedPeopleList.length = 0;
			 for(idx=0; idx<this.myPeopleDataList.length; idx++){
				if(this.myPeopleDataList[idx]["isSelected"] == true) {
				   this.selectedPeopleList.push(this.myPeopleDataList[idx]);
				}
			 }
         }
                  
         if (this.handler != null && this.handler.selectionDone != null) {
            this.handler.selectionDone(this);
         }
         this.hide();
         
      }
});

var pgSelector = null;
function getPoepleGroupSelector(handler) {
   if (pgSelector == null) {
      pgSelector = new HJ.PeopleGroupSelector("Choose Groups &amp; People", handler, null);
      //pgSelector.width = "285px";
   } else {
      pgSelector.handler = handler;
   }
   pgSelector.showHTMLTemplate(basePageURL + "PeopleGroupSelector.htm", "MainArea", "peopleSelector");
   pgSelector.centerDialog(400, 300);
   return pgSelector;
   
}

var gSelector = null;

function getGroupSelector(handler) {
   if (gSelector == null) {
      gSelector = new HJ.PeopleGroupSelector("Select a Group", handler, null);
      //gSelector.width = "285px";
   } else {
      gSelector.handler = handler;
   }
   gSelector.showHTMLTemplate(basePageURL + "GroupSelector.htm", "MainArea", "groupListBox");
   gSelector.centerDialog(400, 300);
   return gSelector;
   
}

var pSelector = null;

function getPeopleSelector(handler) {	
   if (pSelector == null) {
      pSelector = new HJ.PeopleGroupSelector("Select persons", handler, null);
      //pSelector.width = "285px";
   } else {
      pSelector.handler = handler;
   }
   pSelector.showHTMLTemplate(basePageURL + "PeopleSelector.htm", "MainArea", "peopleListBox");
   pSelector.centerDialog(400, 300);
   return pSelector;
   
}

var pSingleSelector = null;

function getPeopleSingleSelector(handler) {	
   if (pSingleSelector == null) {
      pSingleSelector = new HJ.PeopleGroupSelector("Select a person", handler, null);
      //pSingleSelector.width = "285px";
	  pSingleSelector.singleSelectionMode = true;
   } else {
      pSingleSelector.handler = handler;
   }
   pSingleSelector.showHTMLTemplate(basePageURL + "PeopleSelector.htm", "MainArea", "peopleListBox");
   pSingleSelector.centerDialog(400, 300);
   return pSingleSelector;
   
}

