HJ.PeopleGroupSelector2 = Class.create();

HJ.PeopleGroupSelector2.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;
         
      },
      
      setDataList: function(myGroups,myPeople,selectedGroups,selectedPeople){
         this.myGroupDataList = myGroups;		
         this.myPeopleDataList = new Array();
		  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]);
		   }
		 }
         this.selectedGroupList = selectedGroups;
         this.selectedPeopleList = selectedPeople;
	     
		 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();
         
      },
      
      
      updateView: function(){
         
         if(this.myGroupDataList == null && this.myPeopleDataList == null) {
            return;
         }
         if (this.myGroupDataList != null) {
            for(idx=0; idx < this.myGroupDataList.length; idx++){
               
               var myListIndex = findObjIndex(this.selectedGroupList, this.myGroupDataList[idx]);
               
               
               if(myListIndex > -1) {
                  this.myGroupDataList[idx]["isSelected"] = this.selectedGroupList[myListIndex]["isSelected"];
                  this.myGroupDataList[idx]["isCellSelected"] = this.selectedGroupList[myListIndex]["isCellSelected"];
               } else {
                  this.myGroupDataList[idx]["isSelected"] = false;
                  this.myGroupDataList[idx]["isCellSelected"] = false;
               }
            }
            
            if(this.groupTable) {
               this.groupTable.setDataList(this.myGroupDataList);
            }
         }
         
         if (this.myPeopleDataList != null) {
            for(idx=0;idx<this.myPeopleDataList.length;idx++){
               
               var myListIndex = findObjIndex(this.selectedPeopleList,this.myPeopleDataList[idx]);
               if(myListIndex > -1) {
                  this.myPeopleDataList[idx]["isSelected"] = this.selectedPeopleList[myListIndex]["isSelected"];
                  this.myPeopleDataList[idx]["isCellSelected"] = this.selectedPeopleList[myListIndex]["isCellSelected"];
               } else {
                  this.myPeopleDataList[idx]["isSelected"] = false;
                  this.myPeopleDataList[idx]["isCellSelected"] = false;
               }
            }
            
            if(this.peopleTable) {
               this.peopleTable.setDataList(this.myPeopleDataList);
            }
         }
		
		 var pl = this.myPeopleDataList;
		 var gl = this.myGroupDataList;

		 if( this.groupSelectionDiv2 != null && gl != null && gl.length >= 7){			
			 this.groupSelectionDiv2.style.overflow = "auto";
			 this.groupSelectionDiv2.style.height = "130px";				
		 }		 
		 if(this.peopleSelectionDiv2 != null && pl != null && pl.length >= 7){			 
			this.peopleSelectionDiv2.style.overflow = "auto";
			this.peopleSelectionDiv2.style.height = "130px";		
		 }
		 if(this.peopleSelector2 != null && ((gl.length  + pl.length) >= 14)) {			
			  if (is_ie) {
				 this.peopleSelector2.style.height = "375px";	
			  }else{
				  this.peopleSelector2.style.height = "365px";	
			  }
		 }

      },
      
      
      templateLoaded: function() {
         var ec = new Af.ElementCollection(this.element);
         
		 this.groupSelectionDiv2 = ec.getFirstElementById("groupSelectionDiv2") ;
		 this.peopleSelectionDiv2 = ec.getFirstElementById("peopleSelectionDiv2") ;
		 this.peopleSelector2 = ec.getFirstElementById("peopleSelector2");


         if (ec.getFirstElementById("groupTable2") != null) {
            this.groupTable = new Af.HtmlTable("groupTable2", "isCellSelected,isSelected,name");			
			this.groupTable.normalBackcolor = "#f6f6f6";
         }
         if (ec.getFirstElementById("peopleTable2") != null) {
            this.peopleTable = new Af.HtmlTable("peopleTable2", "isCellSelected,isSelected,name");			
			this.peopleTable.normalBackcolor = "#f6f6f6";
         }
         
         this.btnDone = ec.getFirstElementById("btnDone");
         this.armedButton = this.btnDone;
         this.btnDone.onclick = this.selectionDone.bind(this);
         
         this.updateView();
         
      },
    
      selectionDone: function (){
         
         // in order to retain the passed reference, these are not newed but emptied
         this.selectedGroupList.length = 0
         this.selectedPeopleList.length = 0;
         
         if (this.myGroupDataList != null) {
            for(idx=0; idx<this.myGroupDataList.length; idx++){
               if(this.myGroupDataList[idx]["isSelected"] == true || this.myGroupDataList[idx]["isCellSelected"] == true) {
                  this.selectedGroupList.push(this.myGroupDataList[idx]);
               }
            }
         }
         
         if (this.myPeopleDataList != null) {
            for(idx=0; idx<this.myPeopleDataList.length; idx++){
               if(this.myPeopleDataList[idx]["isSelected"] == true || this.myPeopleDataList[idx]["isCellSelected"] == true) {
                  this.selectedPeopleList.push(this.myPeopleDataList[idx]);
               }
            }
         }
         
         if (this.handler != null && this.handler.selectionDone != null) {
            this.handler.selectionDone(this);
         }
         
         this.hide();
         
      }
      
});

var pgSelector2 = null;
function getPeopleGroupSelector2(handler) {
   if (pgSelector2 == null) {
      pgSelector2 = new HJ.PeopleGroupSelector2("Choose Groups &amp; People", handler, null);
      //pgSelector2.width = "285px";
   } else {
      pgSelector2.handler = handler;
   }
   pgSelector2.showHTMLTemplate(basePageURL + "PeopleGroupSelector2.html", "MainArea", "peopleSelector2");
   pgSelector2.centerDialog(400, 300);

   return pgSelector2;
   
}


