HJ.MyResources= Class.create();

HJ.MyResources.prototype = {
   
   initialize: function() {
      this.targetContainerId = "content";
      this.myResourcesListTable = new Af.HtmlTable("myResourcesListTable", ",businessName");
      this.myResourcesListTable.listener = this;
      this.currentRow = -1;  
	  this.myResourceTitleContainer = document.getElementById("myResourceTitleContainer");	 	  
	  this.myResourceTitleContainer.onclick = this.loadMyResourcesTemplate.bind(this);
      
      this.url = basePageURL + "MyResources.htm";
      this.tl = null;
	  this.addNew = false;
   },
   
   cleanup: function() {
	  this.myResourcesListTable.cleanup();
   },
   
   
   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);
      }
      return false;
   },
   
   setDataList: function(dlist) {  
	   var newDlist = new Array();	   
	   for (Idx = 0; Idx < dlist.length ;Idx++ )  {
		   var obj = dlist[Idx];
		   obj["name"] = getFullName(obj);								  		
		   obj["bName"] = (obj["businessName"]);
		   newDlist.push(obj);
	   }
      this.dataTable = new Af.DataTable((newDlist));
   },
     
   paneStateChanged: function(pane) {
      this.loadMyResourcesTemplate();
   },
   
   loadMyResourcesTemplate: function () {

	//  HIDE CALENDAR'S HEADER
	  if(calenderTop == null){
		calenderTop = document.getElementById("calenderTop");
	  }
	  calenderTop.style.display = "none";

      if (this.tl == null) {
         this.tl = new Af.TemplateLoader(this.url, this.targetContainerId, "myResourceDiv");
         this.tl.listener = this;
         this.tl.loadTemplate();
      } else {
         this.tl.reAttachElement();
         this.myResourcesDetailsTable.setDataList(this.sortList(this.dataTable.getDataList()));
         app.setCurrentView(this);
      }
      return false;
   },
   
   templateLoaded: function() {
      this.myResourcesDetailsTable = new Af.HtmlTable("myResourcesDetailsTable", "bName,name,phone,published");
      this.myResourcesDetailsTable.listener = this;	 
      this.myResourcesDetailsTable.setDataList(this.sortList(this.dataTable.getDataList()));
      this.addResource= document.getElementById("addResource");
      this.addResource.onclick = this.doAddResource.bind(this);
      this.printResourcesList = document.getElementById("printResourcesList");
      this.printResourcesList.onclick = this.doPrintResourcesList.bind(this);
      this.editResource = 	 document.getElementById("editResource");
      this.editResource.onclick = this.doEditResource.bind(this);
      //app.setCurrentView(this);
      
   },
   
   doAddResource: function() {
	  this.currentRow = -1;
	  this.addNew = true;
      if (this.dialog == null) {
         this.dialog = new HJ.MyResourcesDetail("My Resources", this, null);
         this.dialog.width = "550px";		 
      }
      var obj = new Object();
      this.dialog.showHTMLTemplate(basePageURL + "MyResourcesDetail.htm", "content", "resourceDetail");
      this.dialog.centerDialog(150, 250);	 
      this.dialog.setObj(obj, true);
      return false;
   },
   
   doPrintResourcesList: function() {
   		showModalMessageDialog("Preparing ... please wait", 240, 40);
       var req = new Af.DataRequest(svcURL,this.printCompleted.bind(this), this.printFailed.bind(this), null, this.requestTimedout.bind(this));		   
		req.addService("WorkspaceService", "getPrintHtmlFromXML");
		req.addParameter("templateName", "myResource");
		req.addParameter("xlstFile","MyResourceList.xsl");
		ajaxEngine.processRequest(req);
		return false;
   },
   
   doEditResource: function() {
	    this.currentRow = this.myResourcesDetailsTable.selectedRowNumber;
      if (this.currentRow < 0 || this.currentRow >= this.dataTable.getDataList().length) {
         return false;
      }
      if (this.dialog == null) {
         this.dialog =  new HJ.MyResourcesDetail("My Resources", this, null);
         this.dialog.width = "550px";			 
      }
      
      this.dialog.showHTMLTemplate(basePageURL + "MyResourcesDetail.htm", "content", "resourceDetail");
	  this.dialog.centerDialog(250, 200);
      var selectedResource = this.dataTable.getDataList()[this.currentRow];	 
	  
      this.dialog.setObj(selectedResource);
      return false;
      
   },
   
   refresh: function() {
	  app.workspace["myResource"] = this.dataTable.getDataList();	  
      if (this.myResourcesListTable != null) {
        // this.myResourcesListTable.setDataList(this.dataTable.getDataList());
      }
      if (this.myResourcesDetailsTable != null) {
         this.myResourcesDetailsTable.setDataList(this.sortList(this.dataTable.getDataList()));
      }
	  this.loadMyResourcesTemplate();
   },	 	    

  printCompleted: function(response)	{
	hideModalMessageDialog();
     var p = new Af.PrintUtil();
     p.listener = this;
     p.showPrintView(response);
     return false;
  },
   
   printingDone:function() {	
   		this.viewSelected();
   		return false;
   },

   printFailed: function() {
     showMessageDialog("Printing is currently unavailable.Please try again.", "Printing Unavailable", 300, 100); 
   },
   
   requestTimedout: function() {
       showMessageDialog("Printing is currently unavailable.Please try again.", "Printing Unavailable", 300, 100); 
   },

	cellSelected: function(row, col) {
		this.myResourcesDetailsTable.normalBackcolor = "#eef7f6";
		this.myResourcesDetailsTable.selectRow(row, "#e0eeef");
		if(col == 0) {
		  this.doEditResource();
		} 	
   },

	sortList: function(l) {
      l.sort(sortBusinessName);
      return l;
   }

}

