
HJ.CommunityCalendar = Class.create();

HJ.CommunityCalendar.prototype = {

	initialize: function() {
		this.targetContainerId = "content";     
		this.url = basePageURL + "CommunityCalendar.htm";
		this.communityAddLink = document.getElementById("communityAddLink");		
		//this.communityAddLink.onclick = this.doLoadTemplate.bind(this);		
		this.dlist = null;
		this.resultList = null;
		this.defaultSearchValue = "";
	},
	
	cleanup: function() {
	    this.communityAddLink.onclick = null;
	    return false;
	},
	
	 viewSelected: function(invoker) {	      
		 this.doLoadTemplate();
	    document.getElementById(this.targetContainerId).style.backgroundColor = "white"; 
     	return false;
     },	
	
	doLoadTemplate: function() { 		
		if (this.load == null) {
         this.load = new Af.TemplateLoader(this.url, this.targetContainerId, "communityCalendarContent");
         this.load.listener = this;
         this.load.loadTemplate();
		} else {
		   this.load.reAttachElement();	
           this.doLoadCalCategoryInfo(); // refresh ...          		 

		   if(this.resultTitle != null){
			   this.resultTitle.style.display = "none";
		   }		   
		   if(this.searchResultsArea != null){
			   this.searchResultsArea.innerHTML = "";
		   }
		}
		return false;	 
	},
	templateLoaded: function () {
	    
	    var ec = new Af.ElementCollection(this.load.element);
	   // this.searchResultsTable = ec.getFirstElementById("searchResultsTable");
	    this.searchTrigger = ec.getFirstElementById("searchTrigger");
	    this.searchString = ec.getFirstElementById("searchString");
		this.resultTitle = document.getElementById("resultTitle");
	    this.defaultSearchValue = this.searchString.value;

		this.searchResultsArea = document.getElementById("searchResultsArea");
		this.showHowToEarnPnt = document.getElementById("showHowToEarnPnt");
		this.showHowToEarnPnt.onclick = this.doShowPoints.bind(this);

		/*
		this.tellAFriend	= document.getElementById("tellAFriend");	
		this.tellAFriend.onclick = this.doTellAFriend.bind(this);
		*/
	   // this.searchString.onchange = this.doSearch.bindAsEventListener(this);
		this.searchString.onfocus = this.focusOnTextBox.bindAsEventListener(this);
	    this.searchString.onblur = this.focusLostTextBox.bindAsEventListener(this);

		if (is_ie) {
			 this.textChanger = new Af.TextChanger(this.searchString);
		}
	    
	    this.searchTrigger.onclick = this.doSearch.bind(this);	    

		this.dontSeeLocalCal = document.getElementById("dontSeeLocalCal");
		this.createCommCalLink = document.getElementById("createCommCalLink");
		this.createCommCalLink.onclick = this.doCreateCommCal.bind(this);
	 
        this.loadMyProfile();
		this.doLoadCalCategoryInfo();
		return false;
	},
	
	doLoadCalCategoryInfo: function() {
		 var req = new Af.DataRequest(svcURL,
			this.requestInfoListCompleted.bind(this), requestFailedCommon.bind(this), null, requestTimedoutCommon.bind(this));
		 req.addService("PublicService", "getCalCategoryInfoList");
		 ajaxEngine.processRequest(req);
	},
	
	requestInfoListCompleted: function(response) {
         //debugA(response.responseText);
         var xds = new Af.XMLToDataSet(response.responseXML);
	     var list = xds.data["CategoryInfo"];
	     for (var i=0; i<list.length; i++) {
	        var c = list[i];
            if (c["name"] == "GENERAL" || c["name"] == "Adults/Gen Family") {
	           this.c1 = c;
	           this.categoryTable1 = new Af.HtmlColumnRepeater("categoryInfoListTable1","name,count",1);
             this.categoryTable1.setDataList(c["subCategory"]);
             this.categoryTable1.listener = this;
	        } else if (c["name"] == "Camps") {
	           this.c2 = c;
	           this.categoryTable2 = new Af.HtmlColumnRepeater("categoryInfoListTable2","name,count",1);
             this.categoryTable2.setDataList(c["subCategory"]);
             this.categoryTable2.listener = this;
	        } /*else if (c["name"] == "Young Kids") {
	           this.c3 = c;
	           this.categoryTable3 = new Af.HtmlColumnRepeater("categoryInfoListTable3","name,count",1);
			   this.categoryTable3.setDataList(c["subCategory"]);
			   this.categoryTable3.listener = this;
	       }*/
        }
				       
        if (this.selectedCatName != null){
            var s = this.selectedCatName;
            this.selectedCatName = null;
			var l = this.c2["subCategory"];
			for (var i=0; i<l.length; i++) {
				if (l[i]["name"] == s) {
					this._columnRepeaterRowSelected(l[i]);
					break;
				}
			}
        }
        return false;
    },
    
    columnRepeaterRowSelected: function(columnRepeater,  n) {	   
       var obj = columnRepeater.dlist[n];
       this._columnRepeaterRowSelected(obj);
       return false;
    },
    
    _columnRepeaterRowSelected: function(obj) {	   
       var req = new Af.DataRequest(svcURL,
			this.requestGetCommunityCalCompleted.bind(this), requestFailedCommon.bind(this), null, requestTimedoutCommon.bind(this));
	   req.addService("PublicService", "getCommunityCalendars");
	   req.addParameter("UUID", obj.UUID);
	   ajaxEngine.processRequest(req);
    },
    
    requestGetCommunityCalCompleted: function(response) {
       //debug(response.responseText);	   	   
		
		this.searchResultsArea.innerHTML = response.responseText;
		this.searchTemplateLoaded();
		this.resultTitle.innerHTML = this.catName;
		this.resultTitle.style.display = "block";
		return false;
    },

	searchTemplateLoaded: function() {					
		var addCalBtn = document.getElementsByName("addCalendar");
		this.schUUIDList = document.getElementsByName("schUUID");		
		var catNameList = document.getElementsByName("catNameList");
		var showOwnersInfo = document.getElementsByName("showOwnersInfo");
		var previewBtn = document.getElementsByName("preview");
		

	    if (addCalBtn != null) {
		   for(var bIdx = 0; bIdx < addCalBtn.length; bIdx++) {	
			 addCalBtn[bIdx].id = bIdx;
			 addCalBtn[bIdx].onclick = this.addCalendar.bindAsEventListener(this);
		   }
	    }
	    if (previewBtn != null) {
	    	for(var bIdx = 0; bIdx < previewBtn.length; bIdx++) {	
			 previewBtn[bIdx].id = bIdx;
			 previewBtn[bIdx].onclick = this.preview.bindAsEventListener(this);
		   }
	    }
		for(var idx = 0; idx < catNameList.length; idx++) {									
			var catName = document.getElementById(catNameList[idx].id + this.schUUIDList[idx].id);
			if(catName != null) {
				catName.innerHTML = this.getCategoryString(catNameList[idx].id); 
				this.catName = catName.innerHTML;
			}
		}
		
		for(var i=0; i<showOwnersInfo.length; i++){
			showOwnersInfo[i].onclick = this.doShowOwnersInfo.bindAsEventListener(this);
		}
		return false;
	},
    
    getCategoryString: function(category) {
      var o = findObjById(this.c1["subCategory"], category);
      if (o != null) {
         return this.c1["name"] + " : " + o["name"];
      }
      o = findObjById(this.c2["subCategory"], category);
      if (o != null) {
         return this.c2["name"] + " : " + o["name"];
      }
      /*
      o = findObjById(this.c3["subCategory"], category);
      if (o != null) {
         return this.c3["name"] + " : " + o["name"];
      }
      */
      return "---";
    },
    
    preview: function(evt) {
      if (this.schUUIDList == null) {
         return;
      }
      var e = evt.target ? evt.target : evt.srcElement;
      var n = parseInt(e.id);
      var schUUID = this.schUUIDList[n].id;
      url = window.location.toString()+"&svcName=WorkspaceService&methodName=getScheduleByUUID&UUID="+schUUID;
      window.open(url, "previewCommunityCalendarWindow", 'toolbar=no,menubar=no,copyhistory=no');
      return;
    },
    
    addCalendar: function(evt) {
      if (this.schUUIDList == null) {
         return;
      }
      var e = evt.target ? evt.target : evt.srcElement;
      var n = parseInt(e.id);
      var schUUID = this.schUUIDList[n].id;
      var req = new Af.DataRequest(svcURL,
			this.requestAddCommunityCalCompleted.bind(this), this.requestAddCommunityCalFailed.bind(this), null, requestTimedoutCommon.bind(this));
	  req.addService("WorkspaceService", "addCommunityCalendar");
	  req.addParameter("reference", schUUID);
	  ajaxEngine.processRequest(req);
      
    },

	doShowOwnersInfo: function(evt) {
		 var e = evt.target ? evt.target : evt.srcElement;		
		 if(e == null || e.id == null) {
			 return;
		 }
		 app.db.loadSeeingStarPubProfile(e.id);
		 return;
	},
    
    requestAddCommunityCalCompleted: function(response) {
       //debugA(response.responseText);
	   var xds = new Af.XMLToDataSet(response.responseXML);
	   var sch = xds.data
	   var dlist = app.schedule.communityScheduleTable.dlist;
	   if (dlist != null) {
		   dlist.push(sch);
		   app.schedule.communityCalendarListChanged();
		   var row = findObjIndex(dlist, sch);
		   
		   // FUNCTION CALLING TO SHOW SELECTED COMMUNITY CALENDAR 
		   app.schedule.showEventsInSchedule(row, 1, app.schedule.communityScheduleTable);
		   app.db.displayCalendarTab();
	   }
	   return false;
	},
	
	requestAddCommunityCalFailed: function(dataRequest, msg) {
      showMessageDialog(msg, "Attention!", 400, 150);
   },
   
   doSearch: function() {
         var s = this.searchString.value;
         s = trim(s);
         if (s == "") {
            return false;
         }
		 var req = new Af.DataRequest(svcURL,
			this.requestGetCommunityCalCompleted.bind(this), requestFailedCommon.bind(this), null, requestTimedoutCommon.bind(this));
		 req.addService("WorkspaceService", "searchCommunityCal");
		 req.addParameter("queryString", s);
		 ajaxEngine.processRequest(req);
		 return false;
   },
	
   focusOnTextBox: function(evt) {
      var s = this.searchString.value;
	  if(s == null){
		  s = "";
	  }
	  s = trim(s);
	  if(s == this.defaultSearchValue){
		  this.searchString.value = "";
	  }
	  this.searchString.focus();
	  return false;
   },
   
   focusLostTextBox: function(evt) {      	  
	  var s = this.searchString.value;
      if (s == null) {
        s = "";
      } else {
        s = trim(s);
      }
      if (s == this.defaultSearchValue || s == "") {
         this.searchString.value = this.defaultSearchValue;
         consumeEvent(evt);
      }else{
		   this.doSearch();
	  }
	  return false;
   },

   doShowPoints:function(){
		app.seeingStar.doLearnAboutPoints();
		return false;
   },

	doTellAFriend: function() {
         var tellAFriendPage = new HJ.TellAFriendPage();
         tellAFriendPage.init(this.targetContainerId);
		 return false;
  	},

    doCreateCommCal: function(ev) {
        app.mainTab = "ourCalendar";
        app.db.init();
        app.schedule.loadScheduleDetailTemplate1(ev);
        app.schedule.myScheduleDetail.schDetail.sharing.value="shared";
        app.schedule.myScheduleDetail.schDetail.sharingChanged();
        return false;
    },

	loadMyProfile: function() {
	   var req = new Af.DataRequest(svcURL,
       this.requestMyProfileLoadCompleted.bind(this), this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
	   req.addService("AccessControlSvc", "getMyProfile");
	   ajaxEngine.processRequest(req);
	},
	 
	 requestMyProfileLoadCompleted: function(response) {
	   //debugA(response.responseText);
		var xds = new Af.XMLToDataSet(response.responseXML);
        var userObj = xds.data;
        if (userObj.zip != null && userObj.zip.substring(0,2) == "94") {
		    this.dontSeeLocalCal.style.display = "none";
        } else {
		    this.dontSeeLocalCal.style.display = "block";
        }
        return false;
     },

	 requestFailedCommon: function(dataRequest, msg) {
        //no-op
    },

	 requestTimedoutCommon: function(dataRequest, msg) {
        //no-op
    }
}

