HJ.MyLists = Class.create();

HJ.MyLists.prototype = {
   
	   initialize: function() {
		 
		  this.myListsTable = new Af.HtmlTable("myListsTable", "name,count");
		  this.myListsTable.dndMgr2 = app.dndMgrMyLists;
		  this.myListsTable.draggableAttr = "name";
		  this.myListsTable.listener = this;
		  this.currentRow = -1;
		  this.url = basePageURL + "MyListsDetailView.html";
		  this.targetContainerId = "content";
		  this.tl = null;

		  this.addNewMyList = document.getElementById("addNewMyListLink");
		  this.addNewMyList.onclick = this.doAddNewMyList.bindAsEventListener(this); 
		  
		  this.delMyListLink = document.getElementById("delMyListLink");
		  this.delMyListLink.onclick = this.doDelMyList.bindAsEventListener(this);
	   },
	   
	   cleanup: function() {
	      this.delMyListLink.onclick = null;
	      this.addNewMyList.onclick = null;
	      this.myListsTable.cleanup();
	      
	   },
	   
	   setDataList: function(dlist) {
	     
		  this.myListsTable.setDataList(dlist);
		  this.dataTable = new Af.DataTable(dlist);		
	   },

	   refreshListDetailView: function(obj){
		   if(this.myOneList != null){
			   this.myOneList.listNameElement.innerHTML = obj.name;
		   }
		   return;
	   },
	   
	   viewSelected: function(invoker) {
		  //  HIDE CALENDAR'S HEADER
		  if(calenderTop == null){
			calenderTop = document.getElementById("calenderTop");
		  }
		  calenderTop.style.display = "none";

	      if (this.tl == null) {
	         var row = this.dataTable.getDataList().length > 0 ? 0 : -1;
	         this.cellSelected(row,0,false);
	      } else {
	         this.tl.reAttachElement();
			 app.setCurrentView(this.myOneList);
	      }
	   },
	   
	   cellSelected: function(row, col, reselected) {
		 // refreshWidget();   
		 //  HIDE CALENDAR'S HEADER
		  if(calenderTop == null){
			calenderTop = document.getElementById("calenderTop");
		  }
		  calenderTop.style.display = "none";

		  this.currentRow = row;
		  if (!reselected) {
		     this.myListsTable.selectRow(row);
			 this.loadMyListsTemplate();
		  } else  if (this.tl != null) {
			 this.tl.reAttachElement();
			 app.setCurrentView(this.myOneList);
		  }
		  return false;
	   },
	   
	   loadMyListsTemplate: function () {
		  if (this.tl == null) {
			 this.tl = new Af.TemplateLoader(this.url, this.targetContainerId, "MyListDetails");
			 this.tl.listener = this;
			 this.tl.loadTemplate();
		  } else {
			 this.tl.reAttachElement();
			 if (this.myOneList != null && this.currentRow >=0 && this.currentRow < this.dataTable.getDataList().length) {
				this.myOneList.setMyOneList(this.dataTable.getDataList()[this.currentRow]);
			 }
			 app.setCurrentView(this.myOneList);
		  }
	   },
	   
	   templateLoaded: function() {
		  this.myOneList = new HJ.MyOneList(this.tl.element);
		  if (this.currentRow >=0 && this.currentRow < this.dataTable.getDataList().length) {
			 this.myOneList.setMyOneList(this.dataTable.getDataList()[this.currentRow]);
		  }
		  app.setCurrentView(this.myOneList);
	   },

	   doAddNewMyList: function(ev) {
	        consumeEvent(ev);
			if(this.dialog == null) {  
				this.dialog = new HJ.AddMyListDialog("Add New MyList", this, null);
				this.dialog.left = 200;
				this.dialog.top = 60;
			}
			this.dialog.showHTMLTemplate(basePageURL + "AddMyList.html", "content", "newMyList");
			this.dialog.centerDialog(400, 280);
			this.dialog.setDataObject(null, "Type the name of a new MyList");
			return false;				
	   },
	   
	   doDelMyList: function() {
	       if (this.currentRow == -1) {
			   showMessageDialog("Please select the item you would like to delete, then click Delete icon.", "Select Item", 350, 100);
			  return false;
		   }
		   showConfirmDialog("Are you sure you want to delete this list?", "Are You Sure?", 300, 100, this.doDelMyList2.bind(this));
	   },
	   
	   doDelMyList2: function() {
		  hideDialogWin();
		  var obj = this.dataTable.deleteObjectByIndex(this.currentRow);
		  this.myListsTable.setDataList(this.dataTable.getDataList());
		  this.deleteMyListObj(obj);
		  var n = this.dataTable.getDataList().length;
		  if (n >= 0) {
		     if (this.currentRow >= n) {
		        this.currentRow--;
		     }
		     this.myListsTable.setSelectedRow(this.currentRow);
		     this.myOneList.setMyOneList(this.dataTable.getDataList()[this.currentRow]);			
		  } else {
		     this.currentRow = -1;
		     this.myOneList.setMyOneList(new Object());
		  }
	   },
	   
	   deleteMyListObj: function(obj) {
		  if (this.currentRow == -1) {
			 return;
		  }
		  var req = new Af.DataRequest(svcURL,this.requestMyListDeletedCompleted.bind(this), 
				requestFailedCommon, null, requestTimedoutCommon);
		  req.addService("WorkspaceService", "deleteMyList");
		  req.addParameter("UUID", obj.UUID);
		  ajaxEngine.processRequest(req);
	   },
	   
	   requestMyListDeletedCompleted: function(response) {
	      
	   }
}

