	var fbcSession = "";
	fbcSession.uid = 0;
	var usersList;
	   
	
	function login(){		
		FB.Connect.requireSession();
		$('fbcLogin').addClass('hide');
		$('fbcLogout').removeClass('hide');
	}
	
	function logout(){
		FB.Connect.logoutAndRedirect(window.location.protocol + '//' + window.location.hostname + window.location.pathname);
	}
	
  	function initEvents(){
		//add event to every who's going and im going link
		
		
		/*$('connectButton').addEvent('click', function(){
			if($('connectButton').hasClass('fb_login_ready')){
				FB.Connect.logoutAndRedirect(window.location.protocol + '//' + window.location.hostname + window.location.pathname);
			}
		});*/
		
		//for every event, see who's going.
		jQuery.each($(".whosGoing"), function(){		
			//console.log(this);	
			eventId = this.id.split('_')[1];
			listEventUsers(eventId);
			
		});
		
		
		//add click event to imGoing links...
		
		jQuery.each($(".imGoing"), function(){
			$(this).bind('click', function(){
				//console.log('clicked');
				if(!$(this).hasClass('disabled')){
					eventId = this.id.split('_')[1];
					
					//send off ajax call that user is attending
					attendeeResponse(eventId, 1);
				}
			})
		});
		
		jQuery.each($(".imNotGoing"), function(){
				$(this).bind('click', function(){
				//console.log('clicked');
				eventId = this.id.split('_')[1];
				
				//send off ajax call that user is attending
				attendeeResponse(eventId, 0);			
			})
		});
		
/*		//handle the see more/see less toggle
		jQuery.each($(".viewAll"), function(){
			$(this).bind('click', function(){
				//console.log('clicked');
				eventId = this.id.split('_')[1];
				$('#whosGoingContainerOverflow_'+eventId).toggleClass('hide');
				if($('viewAll_'+eventId).get('text') == 'See All'){					
					$('#viewAll_'+eventId).text('See Less')
				}else{
					$('#viewAll_'+eventId).text('See All')
				}
			})
		});*/
	}
	
	
	function updateSessionState(session){
        fbcSession = session;
    }
	
	function drawWhosGoing(eventId,usersList){
		//console.log(usersList);
		//
		if(usersList.length){
			$('#whosGoingContainer_'+eventId).empty();
			//$('#whosGoingContainerOverflow_'+eventId).empty();
			for(i=0;i<usersList.length; i++){
				if(fbcSession.uid == usersList[i]){
					setImGoingButtonState(eventId,'disabled');
				}
				
				//the first 9 always get displayed, the rest go into overflow that is conditionally hidden
				/*if(i<9){*/
					whosGoingContainer = $('#whosGoingContainer_'+eventId);
			/*	}else{
					whosGoingContainer = $('#whosGoingContainerOverflow_'+eventId);					
				}*/
				drawProfilePic(usersList[i], whosGoingContainer);
				
			}
			
			//redraw the pics
			//jowen: experimental, prevents "FB.XFBML is not defined" error
			if(FB.XFBML){
				FB.XFBML.Host.parseDomTree(); 
			}
		}else{
			//if there are no users, put in some join text
			$('#whosGoingContainer_'+eventId).html("<p class='be_the_first>Be the first one to sign up.</p>");
		}
	}
	 
	//conditionally disable & show the imNotGoing link or enable and hide the imNotGoing link
	function setImGoingButtonState(eventId, state){
		if(state=='disabled'){
			$("#imGoing_"+eventId).addClass('disabled');
			$("#imNotGoing_"+eventId).removeClass('hide');
		}else{
			$("#imGoing_"+eventId).removeClass('disabled');
			$("#imNotGoing_"+eventId).addClass('hide');
		}
	}
	
	
	function drawProfilePic(userId, container){
		//console.log(userId);
		//<fb:profile-pic uid="24402817" size="thumb"></fb:profile-pic>		
		$(container).append("<fb:profile-pic uid='"+userId+"' size='square' facebook-logo='true'></fb:profile-pic>");

	}
	
	function attendeeResponse(eventId, isAttending){
		//require the user to login
		FB.Connect.requireSession();
		
		method = '';
		if(isAttending == 0){
			method='removeattendee';
		}else{
			method='addattendee';			
		}
		
		//wait until they are logged in to actually add them
		FB.Facebook.get_sessionWaitable().waitUntilReady(function(){			
			$.ajax({
			   type: "POST",
			   url: "/process.php",
			   data: 'method='+method+'&user_id='+this.fbcSession.uid+'&event_id='+eventId,
			   success: function(responseText){
					listEventUsersHandler(eventId, responseText);
				}
			 });
			if(isAttending){
				template_data = {"eventId":eventId, "eventName":getEventName(eventId)};
				
			//console.log(template_data);
				//add to users feed
				facebook_publish_feed_story('73392973116',template_data);
			}else{
				setImGoingButtonState(eventId,'enabled');
			}
		});	
		
		
	}
	
	function getEventName(eventId){
		//console.log("eventid=" + eventId);
		return eval("event_"+eventId+"_name");
	}
	
	function facebook_publish_feed_story(form_bundle_id, template_data) {
	  // Load the feed form
	  FB.ensureInit(function() {
	  	//console.log("FB.ensureInit");
         // FB.Connect.showFeedDialog(form_bundle_id, template_data);
		 // FB.Connect.showFeedDialog(form_bundle_id, template_data, FB.FeedStorySize.oneLine ,FB.RequireConnect.require ,null);
         // FB.Connect.showFeedDialog(form_bundle_id, template_data,null, null, null, FB.RequireConnect.promptConnect);
		    FB.Connect.showFeedDialog(form_bundle_id, template_data, null, null, null, FB.RequireConnect.promptConnect, showFeedDialogResult, "(comment on the event you just added)");
	  });
	}
	
	function showFeedDialogResult(){
		//console.log('done with dialog');
	}
	
	function listEventUsers(eventId){
		
		 $.ajax({
		   type: "POST",
		   url: "process.php",
		   data: 'method=getattendees&event_id='+eventId,
		   success: function(responseText){
				listEventUsersHandler(eventId, responseText);
		   }
		 });

	}
	
	function listEventUsersHandler(eventId, responseText){	
		eval("responseTextObj=" + responseText);	
		
		usersList = new Array();
		
		for(i=0; i<responseTextObj.data.length;i++){			
			usersList.push(responseTextObj.data[i].user_id);
		}
		
		//console.log(usersList);
		drawWhosGoing(eventId,usersList);
	}