HJ.GroupSelectorDialog = Class.create();

HJ.GroupSelectorDialog.prototype = Object.extend(new Af.HtmlDialog(), {

   initialize: function(title, handler, content) {
	    this._initializeDialog(title, handler, content);			
        this.groupListTable = null;	        
   },
	   
    setDataList: function(selectedGroups){	  
      this.dataTable = new Af.DataTable(this.handler.handler.groupList);       
      this.selectedGroupList = selectedGroups;      
      if(this.firstTime == false)
      this.updateView();	
   },
   
   updateView: function() {		  
	   debugA("firsttime:"+this.firstTime);		   
		//this will set data to the html tables 
	   var myGroupDataList = this.dataTable.getDataList();     
       if(myGroupDataList != null){
         for(idx=0;idx<myGroupDataList.length;idx++){
             // find idex of mylist Obj in selectedlist.			 
            var myListIndex = findObjIndex(this.selectedGroupList,myGroupDataList[idx]);			
            if(myListIndex > -1)
               myGroupDataList[idx]["isSelected"] = true;
            else myGroupDataList[idx]["isSelected"] = false;
         }
      }		  
	  if(this.groupListTable)
	   this.groupListTable.setDataList(myGroupDataList);
   }, 
  
   templateLoaded: function() {
     this.groupListTable = new Af.HtmlTable("groupListTable", "isSelected,name");
	 this.groupListTable.listener = this;
	 this.groupListTable.setDataList(this.handler.handler.groupList);
	 var ec = new Af.ElementCollection(this.element);
	 this.groupSelectionDone = ec.getFirstElementById("groupSelectionDone");
	 this.groupSelectionDone.onclick = this.selectionDone.bind(this);
	 this.armedButton = this.groupSelectionDone;
   },
   
   selectionDone: function() {	   
       this.selectedGroupList = new Array();
       var myGroupDataList = this.dataTable.getDataList();    	
       debugA('update selected list - groups');        
       for(idx=0;idx<myGroupDataList.length;idx++){		 
         if(myGroupDataList[idx]["isSelected"] == true)
		    this.selectedGroupList.push(myGroupDataList[idx]); 		 
       }						       
      debugA(this.selectedGroupList.length); 
	  if (this.handler != null && this.handler.selectionDone != null) {
         debugA("callback from groupSelectorDialog selector")
            this.handler.selectionDone(this);
           }
      this.hide();
   }
});
