HJ.Dashboard = Class.create();

HJ.Dashboard.prototype = {

	initialize: function() {
		this.url = basePageURL + "Dashboard.htm";
		this.targetContainerId = "MainArea";
		this.url1 = basePageURL + "CommunityResources.htm";
		this.data = null;
        this.previousIdleHandler = null;
		var destPage = HJ.getDestinationPage();
		if (destPage == null) {
			destPage = HJ.DESTINATION_PAGE_HOME;
		}

	    this.inviteFriendsRequest = HJ.requestQueryString.get("j_invite_friends", null);	
		this.inviteFriendsDialogDisplayed = false;
        this.workspace = null;
        this.loadWorkspaceCalled = false;
		
		app.setCurrentTabInYUIHistoryMgr(destPage, true);
		app.setCurrentTab(destPage);

        this.previousIdleHandler 
            = ajaxEngine.setIdleHandler(this.idleHandler.bind(this));

	// 2011-07-16 kee: delayed loading of friends and group until user clicked on friends tab
	this.myPeopleLoaded = false;
	},
	
	cleanup:function() {
	    this.logoutLink = null;
	    this.inputBoxSubmit  = null;
	},

	init: function () {
		if(this.tl == null) {
			showModalMessageDialog("Loading Your Jooners ... please wait", 300, 40);
			this.tl = new Af.TemplateLoader(this.url, this.targetContainerId, null);
			this.tl.listener = this;
			this.tl.loadTemplate();
		} else {
			this.tl.reAttachElement();
			this.tl.element.style.display = "block";
		}
	},
		
	//  Main horizontal navigation bar.  Dash board only control the last three tabs.
	//  Control given to homePageUI for the first two tabs.
	displayCurrentTab: function() {

	 if (this.workspace == null) {
		 this.waitForWorkspaceLoad(this.displayCurrentTab.bind(this), true);
	 }
	 
	 if (app.getCurrentTab()==HJ.DESTINATION_PAGE_OUR_CAL) {
			this.displayCalendarTab();
		}
		else if (app.getCurrentTab()==HJ.DESTINATION_PAGE_FRIENDS) {
			this.displayFriendsTab();
		}
		/*
		else if (app.getCurrentTab()==HJ.DESTINATION_PAGE_INAROUND){
			this.displayInAroundTab();
		}*/ else {
			app.homePageUI.loadNewHomePageTemplate();
		}
	},
	
	waitForWorkspaceLoad: function(methodToCallAfterWorkspaceLoad, firstTime) {
		if (this.workspace != null && app.schedule.scheduleList != null) {
			hideModalMessageDialog();			
			this.methodToCallAfterWorkspaceLoad();
			return false;
		}
		if (firstTime != null && firstTime == true) {
			this.methodToCallAfterWorkspaceLoad = methodToCallAfterWorkspaceLoad;
		}
		showModalMessageDialog("Loading ... please wait", 300, 40);
		
		setTimeout(this.waitForWorkspaceLoad.bind(this), 100);
	},
	
    templateLoaded: function() {	 
	    dndMgr.registerDropZone(new Af.Dropzone(this, $("MainArea")));	
	   
	    this.setHeaderLinks();

        // instantiate table components, associate with html elements  
        app.initialize2();
	

		// swap .png logo for .gif logo in IE6
		var browserName=navigator.appName;
		var browserVer=parseInt(navigator.appVersion);
		if (browserName=="Microsoft Internet Explorer" && browserVer<=6) {
			document.getElementById('joonersLogo').src = 'img/logo.gif';
		}



        // instantiate title panes and associate with html elements
        this.scribbleTitlePane = new Af.HtmlTitlePane2("scribbleTitlePane", "scribbleTitle", "openClose", "openClose", this);
        this.scheduleTitlePane = new Af.HtmlTitlePane2("scheduleTitleInternalPane", "scheduleTitle", "openClose", "openClose", this);
        this.myListsTitlePane = new Af.HtmlTitlePane2("myListsTable", "myListsTitle", "openClose", "openClose", this);
        this.peopleTitlePane = new Af.HtmlTitlePane2("peopleTitlePane", "peopleTitle", "openClose", "openClose", this);
        this.communityTitlePane = new Af.HtmlTitlePane2("communityTitlePane", "communityTitle", "openClose", "openClose", this);
        this.myScheduleTitlePane = new Af.HtmlTitlePane2("scheduleTable", "myScheduleTitle", "boxLink open", "boxLink closed", this);
        this.sharedScheduleTitlePane = new Af.HtmlTitlePane2("sharedScheduleTable", "sharedScheduleTitle", "boxLink open", "boxLink closed", this);
        this.invitationTitlePane = new Af.HtmlTitlePane2("invitationTable", "invitationTitle", "boxLink open", "boxLink closed", this);

        //SubTabs		   
        this.communityResourcesTitle = document.getElementById("communityResourcesTitle");
        this.communityResourcesTitle.onclick = this.displayCommunityResourcesSubTab.bind(this);
	  
        this.QATitle = document.getElementById("MHMTitle");
        this.QATitle.onclick = this.displayQASubTab.bind(this);	   

        this.communityCalenderTitle = document.getElementById("communityCalenderTitle");
        this.communityCalenderTitle.onclick = this.displayCommCalendarSubTab.bind(this);
 
        this.holidayTitle = document.getElementById("holidayTitle");
        this.holidayTitle.onclick 
        = this.displayHolidaySubTab.bind(this, true);
                  

        // Main Tab Navigation..	  
        this.homePage = document.getElementById("homePage");
        this.homePage.onclick = this.handleHomeTabClick.bind(this);
        /* 
           this.signups = document.getElementById("signups");
           this.signups.onclick = this.displaySignupsTab.bind(this);
        */
        this.friends = document.getElementById("friends");
        this.friends.onclick = this.handleFriendsTabClick.bind(this);

        this.ourCalendar =document.getElementById("ourCalendar");
        this.ourCalendar.onclick = this.handleCalendarTabClick.bind(this);
		
        /*
          this.inAround = document.getElementById("inAround");
          this.inAround.onclick = this.handleInAroundTabClick.bind(this);
        */	   
        //  DIVS to be shown and unshown..
        //this.cityTab = document.getElementById("cityTab");
        this.navBox = document.getElementById("tabs");
	   
        this.contentBox = document.getElementById("content");
	   
        this.calenderTop = document.getElementById("calenderTop");
	   
        this.planningDiv = document.getElementById("planningDiv");
        this.pageBottom = document.getElementById("pageBottom");	  
        this.commCalBox = document.getElementById("commCal");
        this.scheduleBox = document.getElementById("scheduleTitlePane");
        this.leftNavAd = document.getElementById("leftNavAd");
        this.listBox = document.getElementById("myListsTitlePane");
        this.peopleBox = document.getElementById("peopleBox");

        // prompts section on Our Calendar tab
        this.promptsDiv = document.getElementById("promptsDiv");
        this.promptCreateSignups = document.getElementById("createSignups");
        this.promptCreateSignups.onclick = this.displaySignupsTab.bind(this);
        this.promptWriteScribbles = document.getElementById("writeScribbles");
        this.promptWriteScribbles.onclick = this.displayFriendsTab.bind(this);
        this.promptSeeCommunityEvents = document.getElementById("seeCommunityEvents");
        this.promptSeeCommunityEvents.onclick = this.displayCommCalendarSubTab.bind(this);
        this.promptSeeCommunityResources = document.getElementById("seeCommunityResources");
        this.promptSeeCommunityResources.onclick = this.displayCommunityResourcesSubTab.bind(this);
        /*
          this.promptAskAQuestion = document.getElementById("askAQuestion");
          this.promptAskAQuestion.onclick = this.displayQASubTab.bind(this);
        */
        this.leftNavPlanningDiv = document.getElementById("leftNavPlanningDiv");
        this.subTabs = document.getElementById("tabsII");

        if (app.getCurrentTab() != HJ.DESTINATION_PAGE_HOME) {
            this.loadWorkspace();
            return;
        }
        app.homePageUI.loadNewHomePageTemplate();
        hideModalMessageDialog();
	},

	// Tab click handlers are needed to handle browser back button press
	
	handleHomeTabClick: function(event) {
		app.setCurrentTabInYUIHistoryMgr(HJ.DESTINATION_PAGE_HOME);
		app.homePageUI.loadNewHomePageTemplate();
		if (!event) var event = window.event;
		if (event) {
			YAHOO.util.Event.stopEvent(event);
		}
		if(app.footer == null || app.footer == 'undefined' )
		{
			app.footer = new HJ.Footer();
			app.footer.doLoadTemplate();
		}
		app.footer.show();
		return false;
	},

	handleFriendsTabClick: function(event) {
		app.setCurrentTabInYUIHistoryMgr(HJ.DESTINATION_PAGE_FRIENDS);
		this.displayFriendsTab();
		if (!event) var event = window.event;
		if (event) {
			YAHOO.util.Event.stopEvent(event);
		}
		if(app.footer == null || app.footer == 'undefined' )
		{
			app.footer = new HJ.Footer();
			app.footer.doLoadTemplate();
		}
		app.footer.show();
		return false;
	},

	handleCalendarTabClick: function(event) {
		app.setCurrentTabInYUIHistoryMgr(HJ.DESTINATION_PAGE_OUR_CAL);
		this.displayCalendarTab();
		if (!event) var event = window.event;
		if (event) {
			YAHOO.util.Event.stopEvent(event);
		}
		if(app.footer == null || app.footer == 'undefined' )
		{
			app.footer = new HJ.Footer();
			app.footer.doLoadTemplate();
		}
		app.footer.show();
		return false;
	},

	handleInAroundTabClick: function(event) {
		app.setCurrentTabInYUIHistoryMgr(HJ.DESTINATION_PAGE_INAROUND);
		this.displayInAroundTab();
		if (!event) var event = window.event;
		if (event) {
			YAHOO.util.Event.stopEvent(event);
		}
		if(app.footer == null || app.footer == 'undefined' )
		{
			app.footer = new HJ.Footer();
			app.footer.doLoadTemplate();
		}
		app.footer.show();

		return false;
	},
	
	viewSelected: function(htmlPane) {		
	   if (htmlPane.titleId == 'myListsTitle') {
           this.displayCalendarHeader(false);
		   app.myLists.viewSelected(this);
	   } else if (htmlPane.titleId == 'scheduleTitle' || htmlPane.titleId == 'myScheduleTitle' ||
				htmlPane.titleId == 'sharedScheduleTitle' || htmlPane.titleId == 'invitationTitle') {
           this.displayCalendarHeader(true);
		   app.schedule.viewSelected(this);
	   } else if (htmlPane.titleId == 'peopleTitle') {
           this.displayCalendarHeader(false);
		   app.myPeople.viewSelected(this);
	   } else if (htmlPane.titleId == 'communityTitle') {
           this.displayCalendarHeader(false);
		   app.communityCalendar.viewSelected(this);
	   } 
	   return false;
	},

	/**
     * @return true if the workspace has been loaded
     * false otherwise
     */
    loadWorkspace: function() {
        if (this.workspace != null) {
            return true;
        }

        // don't call getWorkspace twice
        if (this.loadWorkspaceCalled) {
            return false;
        }
        this.loadWorkspaceCalled = true;

        var req = new Af.DataRequest(
                svcURL,
                this.requestWorkspaceLoadCompleted.bind(this), 
                this.requestFailedCommon.bind(this), 
                null, 
                this.requestTimedoutCommon.bind(this));
        req.addService("WorkspaceService", "getWorkspaceSimple");
        ajaxEngine.processRequest(req);
	},
	
	requestWorkspaceLoadCompleted: function(response) {
	
	   //debugA(response.responseText);
	   var xds = new Af.XMLToDataSet(response.responseXML);
	   this.workspace = xds.data;
	   app.workspace = this.workspace;
	   this.completeWorkspace(this.workspace);	   
	   
	   app.schedule.setWorkspace(this.workspace);
	   app.invitation.setDataList(this.workspace["invitation"]);
	   app.myLists.setDataList(this.workspace["myList"]);
	   
	   // FUNCTION CALLED TO INITIALIZE CLASSES
	   app.initialize3();

	   //  Group Task
	   app.myGroups.setDataList(this.workspace["myGroup"], this.workspace["myPeople"]);
	   
	   //MyPeople
	   app.myPeople.setDataList(this.workspace["myPeople"], this.workspace["myGroup"]);

	   // MyLove Task
	   app.myLovedOne.setDataList(this.workspace["myLove"]);
		 //My Resources 
	   app.myResources.setDataList(this.workspace["myResource"]);	   


       if (app.getCurrentTab() != HJ.DESTINATION_PAGE_HOME) {
		hideModalMessageDialog();
		
		this.displayCurrentTab();
		document.getElementById("homeFooter").style.display = "block";
		
        
       }
	if(app.footer == null || app.footer == 'undefined' )
	{
		app.footer = new HJ.Footer();
		app.footer.doLoadTemplate();
	}
       
       if (login.showContactImporter != null && login.showContactImporter) {
           // toggle it off to avoid redisplay of contact importer in case of 
           // a browser refresh
           login.showContactImporter = false;  
           this.inviteFriendsDialogDisplayed = true;
           var importer = new HJ.ContactImporter();
           importer.showTemplate(true, false);
       }
  },
	
	
	requestFailedCommon: function(dataRequest, msg) {
	    hideDialogWin();
	    showMessageDialog("We are currently unable to create this display. Please try again.", "Excuse us!", 350, 100);
    },

    requestTimedoutCommon: function() {
        hideDialogWin();
        showMessageDialog("We are currently unable to create this display. Please try again.", "Excuse us!", 350, 100);
    },
    
    paneStateChanged: function(pane) {
		var openCloseImg = document.getElementById(pane.titleId+"Img");		
		if(!pane.open) {			
				if(openCloseImg != null) {
					openCloseImg.src = "img/plus.gif";				
				}
		}else {
			if(openCloseImg != null) {
				openCloseImg.src = "img/minus.gif";				
			}
		}		
		return;
        if (this.scribbleTitlePane == null) {
           return false;
        }
	    this.updateScribbleState();
        this.updateAnnounceState();
        this.updateCalendarState();
		return false;
    },
    
    updateScribbleState: function() {
        var e = document.getElementById("scribbleTitleContaner");
        if (this.scribbleTitlePane.open ) {
           if (this.miniCalTitlePane.open) {
              e.className = "box largeScrollBox";
           } else {
              e.className = "box largeScrollBox largeScrollBoxOpen";
           }
        } else {
           e.className = "box closed scribblesBoxClosed";
        }
    },
    
    updateAnnounceState: function() {
  
        var e = document.getElementById("announceId");
        var e1 = document.getElementById("googleBoxId");
        var e2 = document.getElementById("hjAnnouncementId");
        var e3 = document.getElementById("content");
        if (this.scribbleTitlePane.open ) {
           e.className = "googleAnnounce";
           e1.className = "googleBox";
           e2.className = "hjAnnouncement";
           e.insertBefore(e2,e1);
           e3.className = "contentScribble";
        } else {
           e.className = "noScribble";
           e1.className = "noScribble_googleBox";
           e2.className = "noScribble_hjAnnouncement";
           e.insertBefore(e1,e2);
           e3.className = "contentNoScribble";
        }
     
    },
    
    updateCalendarState: function() {
        var e = document.getElementById("scribbleTitleContaner");
        var e2 = document.getElementById("miniCalContaner");
        var e3 = document.getElementById("leftBar");
        if (this.scribbleTitlePane.open && this.miniCalTitlePane.open) {
           e2.className = "box miniCal"
           e.parentNode.insertBefore(e2, e3);
        } else {
           e2.className = "box soloCal";
           var e4 = document.getElementById("scheduleTitlePane");
           e3.insertBefore(e2, e4);
        }   
    },
	
	displaySignupsTab: function() {
		// this tab does not exist anymore. Signup templates have
		// moved to the home page.
		app.homePageUI.loadNewHomePageTemplate();
		return false;
	},

  	displayCalendarTab: function() {    
		 if (this.workspace == null) {
			 this.waitForWorkspaceLoad(this.displayCalendarTab.bind(this), true);
			 return false;
		 }
        if (HJ.getPostLoginAction() != null && 
            HJ.getPostLoginAction() == HJ.POST_LOGIN_ACTION_SHOW_MY_INFO &&
            !HJ.postLoginActionProcessed) {
            HJ.postLoginActionProcessed = true;
            this.displayMyInfo();
            return false;
        }
        
	    app.schedule.monthlyCalendarSelected();
	    app.schedule.viewSelected(this);
		app.setCurrentTab(HJ.DESTINATION_PAGE_OUR_CAL);	
		this.init();

		this.tl.element.style.display = "";
	    this.navBox.className = "calendar-page";
//	    this.cityTab.className = "";
	    this.listBox.style.display = "none";
	    this.peopleBox.style.display = "none";	
	    this.subTabs.style.display = "none"; 
		this.commCalBox.style.display = "block";
		this.scheduleBox.style.display = "block";
        this.displayCalendarHeader(true);
		this.leftNavPlanningDiv.style.display="block";
    	this.leftNavAd.style.display = "none";
    
		this.planningDiv.style.display="inline";
	    this.pageBottom.style.display="none";				
		this.promptsDiv.style.display = "block";
		this.contentBox.style.display = "block";

		return false;
  },

	displayInAroundTab: function() {
		return this.displayCalendarTab();
        this.init();
		app.setCurrentTab(HJ.DESTINATION_PAGE_INAROUND);

		this.tl.element.style.display = "";
		this.navBox.className = "city-page";
		//this.cityTab.className = "selected";
		this.promptsDiv.style.display = "none";
		this.displayHolidaySubTab();
		return false;
	},
	
	displayFriendsTab: function() {
		 if (this.workspace == null) {
			 this.waitForWorkspaceLoad(this.displayFriendsTab.bind(this), true);
			 return false;
		 }
        this.init();
		app.setCurrentTab(HJ.DESTINATION_PAGE_FRIENDS);
		if (!this.loadWorkspace()) {
            return false;
        }
        
		this.tl.element.style.display = "";
		app.myPeople.viewSelected();
	    //this.cityTab.className = "";
	    
	    this.navBox.className = "friends-page";
	    this.listBox.style.display = "none";
	    this.peopleBox.style.display = "block";	
	    this.subTabs.style.display = "none"; 
		this.commCalBox.style.display = "none";
		this.scheduleBox.style.display = "none";
        this.displayCalendarHeader(false);
		this.leftNavPlanningDiv.style.display="block";
    	this.leftNavAd.style.display = "none";
    
		this.planningDiv.style.display="inline";
	    this.pageBottom.style.display="none";	

	  	this.contentBox.style.backgroundColor = "#ffffff";
		this.promptsDiv.style.display = "none";
	    //this.contentBox.style.paddingTop = "10px";
	    
	    if (this.inviteFriendsRequest != null 
            && !this.inviteFriendsDialogDisplayed) {

			this.inviteFriends();
	    	this.inviteFriendsDialogDisplayed = true;
	    }
	    return false;
	},
	
	displayCommunityResourcesSubTab: function() {
        this.displayCalendarTab(); 
 	    app.communityCalendar.viewSelected(this);	  
        
	    this.subTabs.style.display = "block"; 
		this.planningDiv.style.display="none";
		this.leftNavPlanningDiv.style.display="block";
        this.leftNavAd.style.display = "block";
        this.commCalBox.style.display = "none";
        this.pageBottom.style.display="inline";	  
		this.promptsDiv.style.display = "none";
		app.resource.loadCommunityResourcesTemplate();

		return false;
	},

	displayQASubTab: function() {
        this.displayCalendarTab();

	    this.subTabs.style.display = "block"; 
		this.promptsDiv.style.display = "none";
		this.planningDiv.style.display="none";
		this.leftNavPlanningDiv.style.display="block";
	  	this.leftNavAd.style.display = "block";
	  	this.commCalBox.style.display = "none";
    	this.pageBottom.style.display="inline";
		
		app.question.loadQATemplate();

		return false;
	},

    displayMyInfo: function() {	
		if (app.myInfo == null) {
			hideModalMessageDialog();
			showModalMessageDialog("Processing ... please wait", 300, 40);
			window.setTimeout(this.displayMyInfo.bind(this), 3000);
			return false; // noop
		}
        // invocation from any tab will revert focus to Our Calendar tab
        this.displayCalendarTab(); 

		this.leftNavPlanningDiv.style.display="none";
		this.leftNavAd.style.display = "block";
    	this.planningDiv.style.display="none";
	    this.pageBottom.style.display="inline";		
		this.promptsDiv.style.display = "none";
		app.myInfo.loadMyInfoTemplate();
		return false;
	},

	displayCommCalendarSubTab: function() {
        this.displayCalendarTab(); 
        
	    app.communityCalendar.viewSelected(this);	  

	    this.listBox.style.display = "none";
	    this.peopleBox.style.display = "none";	
	    this.subTabs.style.display = "block"; 
		this.commCalBox.style.display = "block";
		this.scheduleBox.style.display = "none";	    	    
        this.displayCalendarHeader(false);
		this.leftNavPlanningDiv.style.display="block";
		this.leftNavAd.style.display = "block";
		
    	this.planningDiv.style.display="inline";
		this.pageBottom.style.display="none";		
		
		return false;
	},
	
  	displayHolidaySubTab: function(logIt) {
	    //app.holiday.viewSelected(this);	  
        this.displayCalendarTab(); 
    
	    this.listBox.style.display = "none";
	    this.peopleBox.style.display = "none";	
	    this.subTabs.style.display = "block"; 
		this.commCalBox.style.display = "none";
		this.scheduleBox.style.display = "none";
        this.displayCalendarHeader(false);
		this.leftNavPlanningDiv.style.display="block";
    	this.leftNavAd.style.display = "block";
		this.promptsDiv.style.display = "none";
    
		this.planningDiv.style.display="inline";
	    this.pageBottom.style.display="none";				
		
		if (logIt) {
			addLog("CampTab_Clicked", "Dashboard");
		}
		
		app.summerCamps.viewSelected(this);
		return false;
  },
	
	completeWorkspace: function(ws) {
	
	   if (ws["scribble"] == null) {
	      createOneToManyAssoc(ws, "scribble", "Scribble");
	   } else {
	      ws["scribble"].sort(seqComparator);
	   }
	   
	   if (ws["schedule"] == null) {
	      createOneToManyAssoc(ws, "schedule", "Schedule");
	   }
	   if (ws["sharedSchedule"] == null) {
	      createOneToManyAssoc(ws, "sharedSchedule", "Schedule");
	   }
	   if (ws["communitySchedule"] == null) {
	      createOneToManyAssoc(ws, "communitySchedule", "Schedule");
	   }
	  
	   if (ws["invitation"] == null) {
	      createOneToManyAssoc(ws, "invitation", "Event");
	   }
	   
	   if (ws["notes"] == null) {
	      createOneToManyAssoc(ws, "notes", "Note");
	   }
	   
	   if (ws["myList"] == null) {
	      createOneToManyAssoc(ws, "myList", "MyList");
	   }
	 
	   if (ws["myGroup"] == null) {
	      createOneToManyAssoc(ws, "myGroup", "Group");
	   }
	   
	   if (ws["myPeople"] == null) {	      
		  createOneToManyAssoc(ws, "myPeople", "Person");
	   }
	   
	   var l = ws["myPeople"];
	   
	   for (var i=0; i<l.length; i++) {
	      var o = l[i];
	      var fn = o["fName"];
	      var ln = o["lName"];
	      if (fn == null) {
	        fn = "";
	      }
	      if (ln == null) {
	        ln = "";
	      }
	      o["name"] = fn + " " + ln;
	   }

	   if (ws["myLove"] == null) {	      
		  createOneToManyAssoc(ws, "myLove", "MyLovedOne");
	   }

	   if (ws["myResource"] == null) {	      
		  createOneToManyAssoc(ws, "myResource", "Resource");
	   }
	   
	   this.drefAssociations(ws, "myPeople", "myGroup", "group");
	   this.drefAssociations(ws, "myGroup", "myPeople", "people");
	   this.drefAssociations(ws, "myLove", "myPeople", "contact1");
	   this.drefAssociations(ws, "myLove", "myPeople", "contact2");
	   this.drefAssociations(ws, "myLove", "myPeople", "friendContact1");
	   this.drefAssociations(ws, "myLove", "myPeople", "friendContact2");
	   this.drefAssociations(ws, "myLove", "myResource", "doctor1");
	   this.drefAssociations(ws, "myLove", "myResource", "doctor2");
	   this.drefAssociations(ws, "myLove", "myResource", "insuranceCarrier");
	   
	   this.drefAssociations(ws, "schedule", "myPeople", "people");
	   this.drefAssociations(ws, "schedule", "myPeople", "people2");
	   this.drefAssociations(ws, "schedule", "myGroup", "group");
	   this.drefAssociations(ws, "schedule", "myGroup", "group2");
	  
	   var currentDate = new Date();
	   var l = ws["schedule"];
	   var invitations = ws["invitation"];	  
	   for (var i=0; i<l.length; i++) {
	      var sch = l[i];
	      var l2 = sch["event"];
	      if (l2 == null) {
	         continue;
	      }	      
	      for (var j=0; j<l2.length; j++) {
	         var e = l2[j];
	         if (e["inviteStatus"] == "invited") {
				var tempDt = new Date(parseInt(e["endDT"]));
				if(currentDate.getTime() <= tempDt.getTime()) {
					invitations.push(e);
				}
	            var dt = new Date(parseInt(e["startDT"]));
	            var s = (dt.getMonth() + 1) + "/" + dt.getDate();
	            e["dateSmallStr"] = s;
	            e["rsvpSummary"] = "";
	         }
	      }
	   }	 
	   invitations = invitations.sort(sortByStartTime);
	   
	   for (var i=0; i<l.length; i++) {
	      var sch = l[i];
	      var l2 = sch["event"];
	      if (l2 == null) {
	         continue;
	      }	      
	      for (var j=0; j<l2.length; j++) {
	         var e = l2[j];
	         app.updateEventReferences(sch, e);
	      }
	   }	 

	},
	
	
	drefAssociations: function(ws, target, src, targetRel) {
	   l1 = ws[target];
	   l2 = ws[src];
	  
       
       for (var i=0; i<l1.length; i++) {
          var l3 = l1[i][targetRel];
          if (l3 == null) {
             continue;
          }
          var l = new Array();
		  l.__parent = l3.__parent; 
		  l.__className = l3.__className;
          
          for (j=0; j<l3.length; j++) {
             var idx = findObjIndex(l2, l3[j]);
             if (idx > -1) {
                l.push(l2[idx]);
             }
          }
           l1[i][targetRel] = l;
       }

	},
	
	processDragBegin: function(draggable) {
    },
      
      
    processDragged: function(draggable, e) {
         
    },
    
    processDragComplete: function(draggable) {
    },

	changeScreenName: function() {
		if (this.userScreenName != null) {
	     this.userScreenName.innerHTML = "Hi " + login.userFullName + "!";
	    }
	},

	doChangePassword: function() {
		if(this.pwdDialog == null) {  
			this.pwdDialog = new HJ.ChangePassword("Password", this, null);				
			this.pwdDialog.width = "370px";	
		}
		this.pwdDialog.showHTMLTemplate(basePageURL + "ChangePassword.htm", "content", "changePwdContent");
		this.pwdDialog.centerDialog(400, 280);
		
		return false;		
	},

	doShowContactUs: function(){
		if(this.hp == null){
			this.hp = new HJ.Hompage();
		}
		this.hp.loadContactUsTemplate();
	},

	doShowSendStories: function() {
		if(this.hp == null){
			this.hp = new HJ.Hompage();
		}
		this.hp.loadSendStoriesTemplate();
	},

	doShowHelp:function(){
		if(this.hp == null){
			this.hp = new HJ.Hompage();
		}
		this.hp.loadHelpTemplate();		
	},	

	doShowFeedBack:function(){
		if(this.hp == null){
			this.hp = new HJ.Hompage();
		}
		this.hp.loadFeedBackTemplate();		
	},

	setHeaderLinks: function() {
	   this.userScreenName = document.getElementById("userId");
	   if (this.userScreenName != null) {
	     this.userScreenName.innerHTML = "Welcome <strong>" + login.userFullName + "</strong>!";
	    }
	   
	   this.logoutLink = document.getElementById("logoutLink");
	   if(this.logoutLink != null){		  
		   this.logoutLink.onclick = login.logout.bind(login);
	   }	

	   this.myInfo = document.getElementById("myInfoLink");
	   this.myInfo.onclick = this.displayMyInfo.bind(this);
	},

    doTellAFriend: function() {
        var tellAFriendDialog = new HJ.TellAFriendDialog();
        tellAFriendDialog.showTemplate();
    },

    refreshMyPeople: function() {
        var req = new Af.DataRequest(svcURL, 
                                     this.peopleRefreshCompleted.bind(this),
                                     requestFailedCommon.bind(this), null,
                                     requestTimedoutCommon.bind(this));
        req.addService("WorkspaceService", "getMyPeople");
        ajaxEngine.processRequest(req);
    },

    peopleRefreshCompleted: function(response) {
        var xds = new Af.XMLToDataSet(response.responseXML);
        app.workspace["myPeople"] = xds.data["Person"];
        if (app.workspace["myPeople"] == null) {
            createOneToManyAssoc(app.workspace, "myPeople", "Person");
        }
        app.myPeople.setDataList(app.workspace["myPeople"], 
                                 app.workspace["myGroup"]);       
        app.myGroups.refresh();
	/* 2011-07-16 kee: delayed loading of friends list 
	 * upon switching friends tab
	 */
        if (app.getCurrentTab() == HJ.DESTINATION_PAGE_FRIENDS) {
           app.myPeople.refresh();
        }
	this.myPeopleLoaded = true;
	hideModalMessageDialog();			
        
    },

    refreshMyGroups: function() {
        var req = new Af.DataRequest(svcURL, 
                                     this.groupsRefreshCompleted.bind(this),
                                     requestFailedCommon.bind(this), null,
                                     requestTimedoutCommon.bind(this));
        req.addService("WorkspaceService", "getMyGroups");
        ajaxEngine.processRequest(req);
    },

    groupsRefreshCompleted: function(response) {
        var xds = new Af.XMLToDataSet(response.responseXML);
        app.workspace["myGroup"] = xds.data["Group"];
        if (app.workspace["myGroup"] == null) {
            createOneToManyAssoc(app.workspace, "myGroup", "Group");
        }

        // Copy the myPeople data (name, email, etc) to "myGroup" 
        // and vice-versa. 
        // XXX: 
        // - It doesn't seem like we need to do this for other datatypes that 
        //   have group associations because refreshMyGroups is only called 
        //   after group import.  
        // - dereferencing the group and people references seems like it might 
        //   be a poor choice, but that's how it's been working. 
        this.drefAssociations(app.workspace, "myPeople", "myGroup", "group");
        this.drefAssociations(app.workspace, "myGroup", "myPeople", "people");

        app.myGroups.setDataList(this.workspace["myGroup"], 
                                 this.workspace["myPeople"]);
        app.myPeople.setDataList(app.workspace["myPeople"], 
                                 app.workspace["myGroup"]);
        if (app.getCurrentTab() == HJ.DESTINATION_PAGE_FRIENDS) {
            app.myGroups.refresh();
        }
    }, 

    displayCalendarHeader: function(display) {
        // XXX: calenderTop is a global var in Dashboard
        if (this.calenderTop == null) {
			this.calenderTop = document.getElementById("calenderTop");
        }
        if (this.calenderTop == null) {
            // XXX: Can be null on first load.  Probaby a bug.
            return;
        }
        if (display) {
            this.calenderTop.style.display = "block";
        } else {
            this.calenderTop.style.display = "none";
        }
    }, 

    idleHandler: function(engine) {
        // only really loads the workspace once
        this.loadWorkspace();
        if (this.previousIdleHandler != null) {
            this.previousIdleHandler(engine);
        }
    },
    loadMyPeople: function() {

		if(!this.myPeopleLoaded)
		{
			//showModalMessageDialog("Loading Your Contacts... please wait", 300, 40);
			this.refreshMyPeople();
			this.refreshMyGroups();
		}
    }
	
}

function seqComparator(o1, o2) {
   var s1 = parseInt(o1["seq"]);
   var s2 = parseInt(o2["seq"]);
   return s1 - s2;
}



