function eventsModule( communityID, numEventPosts )
{
        if( get_cookie( 'lang' ) == 'ar' )
        {
                $('#eventslContainer').addClass('ar');
                $('#comevents').html('أحداث المجتمع');
        } else  {
                        $('#eventsContainer').removeClass('ar');
                        if( get_cookie( 'currentCommunityID' ) == 33 )
                        {
                                $('#comevents').html('Community Events').addClass( 'burj-titles' );
                        } else  {
                                        $('#comevents').html('Community Events');
                                }
                }

        // AJAX CALL TO PERL HANDLER TO RETURN WALL XML FOR HIS COMMUNITY
	var params = "func=events&communityID=" + communityID + "&numEventPosts=" + numEventPosts;
	$.ajax(
        {
                type: "POST",
                async: true,
                url: "/cgi-bin/mycom.cgi",
                data: params,
                dataType: "xml",
		success: function(data)
		{
			$(data).find('RESULTS').each( function()
			{
				var result = $(this).find('RESULT').text();
				var errorMsg = $(this).find('ERROR').text();
				var html = "";
				if (result == "TRUE")
				{
					$(this).find('EVENT').each(function(index)
					{
						//var section = $(this).find('SECTION').text();
						var recordid = $(this).find('RECORDID').text();
						var title = $(this).find('TITLE').text();
						var venue = $(this).find('VENUE').text();
						var start = $(this).find('START').text();
						var end = $(this).find('END').text();
						var filename = $(this).find('FILENAME').text();
										
						html += "<div class='eventItem'><span class='eventImage'>"; 
						html += "<img src='/attachments/events-" + filename + "-sml.jpg'></span>";
						if( title.length > 26 )
						{
							title = title.substr(0,22);
							title = title + " ...";
						}
						html += "<h4>" + title + "</h4>";
						html += "<p>" + venue + "</p>";
						var newStartTimeDate = decodetimestamp( start );
						var newEndTimeDate = decodetimestamp( end );
						var startSplit = newStartTimeDate.split(" ");
						var newStart = startSplit[0] + " " + startSplit[1];
						var endSplit = newEndTimeDate.split(" ");
						var newEnd = endSplit[0] + " " + endSplit[1] + " " + endSplit[2];
						var newStartEnd = newStart + " - " + newEnd;
						html += "<p><span class='date'>" + newStartEnd + "</span></p></div>";
					});
				} else  if( errorMsg == "No Results" )
					{
						// RETURN A NICE MESSAGE IF NO EVENTS
						if( get_cookie('lang') == "AR" )
						{
							var html = "<p class='empty' dir='rtl'>لا توجد أحداث لهذا المجتمع -- أن يكون أول لإضافة واحد عن طريق تسجيل الدخول والاتصال مع غيرهم من الناس الذين يعيشون في مجتمعك</p>";
						} else	{
								var html = "<p class='empty'>There are no events for this community - Be the first to add one by logging in and connect with other people who live in your community.</p>";
							}
					} // END: IF RESULT == TRUE

				// RETURN DATA TO DIV
				$('#eventItems').html( html );

				if( get_cookie( 'lang' ) == 'ar' )
				{
					$('#eventsContainer').addClass('ar');
				} else	{
						$('#eventsContainer').removeClass('ar');   // added this condition to add the 'ar' class to the event container
					}
			}); // END .EACH
		} // END: SUCCESS
	}); // END: AJAX

        //start: EVENT ENPAND FUNCTION CALL
        $('#eventExt .expand').click(function()
        {
                moreevents();
                return false;
        });
        //end: EVENT EXPAND FUNCTION
}

function eventsModuleHome( userID, communityID, numEvents )
{
        // AJAX CALL TO PERL HANDLER TO RETURN WALL XML FOR HIS COMMUNITY
        var params = "func=events&userID=" + userID + "&communityID=" + communityID + "&numEvents=" + numEvents;
		
		$.ajax(
        {
                type: "POST",
                async: true,
                url: "/cgi-bin/funcHandler.cgi",
                data: params,
                dataType: "xml",
				success: function(data)
				{
										
					$(data).find('RESULTS').each( function()
					{
						var result = $(this).find('RESULT').text();
						var errorMsg = $(this).find('ERROR').text();
						var html = "";
						if (result == "TRUE")
						{
								
								$(this).find('EVENTS').each(function(index)
								{
										var section = $(this).find('SECTION').text();
										var eventID = $(this).find('EVENTID').text();
										var title = $(this).find('TITLE').text();
										var comID = $(this).find('COMMUNITYID').text();
										var comName = $(this).find('COMMUNITYNAME').text();
										var start = $(this).find('START').text();
										var end = $(this).find('END').text();
											switch( section )                        
										{
												case "PARENT":                                        
												html += "<div class='eventItem'><span class='eventImage'>"; 
												html += "<img src='/events/" + eventID + "/photos/eventpic-sml.jpg' width='62' height='46'></span>";
												if( title.length > 21 )
														{
																title = title.substr(0,19);
																title = title + " ...";
														}
												html += "<h4>" + title + "</h4>";
												html += "<p><a href='/community/" + getCommunityUrl( comID )  + "'>" + comName + "</a></p>";
												var newStartTimeDate = decodetimestamp( start );
												var newEndTimeDate = decodetimestamp( end );
												var startSplit = newStartTimeDate.split(" ");
												var newStart = startSplit[0] + " " + startSplit[1];
												var endSplit = newEndTimeDate.split(" ");
												var newEnd = endSplit[0] + " " + endSplit[1] + " " + endSplit[2];
												var newStartEnd = newStart + " - " + newEnd;
												html += "<p><span class='date'>" + newStartEnd + "</span></p></div>";
												break;
												
												case "RSVP":
												break;
										}
									
								});
								
								
						} else  if( errorMsg == "no events" )
								{
										// RETURN A NICE MESSAGE IF NO EVENTS
										if( get_cookie('lang') == "AR" )
										{
												var html = "<p class='empty' dir='rtl'>لا توجد أحداث لهذا المجتمع -- أن يكون أول لإضافة واحد عن طريق تسجيل الدخول والاتصال مع غيرهم من الناس الذين يعيشون في مجتمعك</p>";
										} else	{
														var html = "<p class='empty'>There are no events for this community - Be the first to add one by logging in and connect with other people who live in your community.</p>";
												}
								}
								// RETURN DATA TO DIV
									$('#events').html( html );

								if( get_cookie( 'lang' ) == 'ar' )
								{
										$('#eventsContainer').addClass('ar');
								} else	{
												$('#eventsContainer').removeClass('ar');   // added this condition to add the 'ar' class to the event container
										}
						
					});
				}
		});
	
	
	// start: EXPAND EVENT
        $('#eventExt .expand').click(function()
        {
		window.location = "/community/oceana";
        });
        // end: EXPAND EVENT
}

function moreevents()
{
        currentNumEventPosts += 3;
        eventsModule( get_cookie( 'currentCommunityID' ), currentNumEventPosts );
}


