var calendarEl;
var calendar;
function filterCalendarSchedules(filterType){
	scheduleEventValue = $("#publicEventDropDown option:selected").val();
	teamValue = $("#publicTeamsDropDown option:selected").val();
	locationValue = $("#publicLocationsDropDown option:selected").val();
	gameSeasonId = $("#publicGameSeasonsDropDown option:selected").val();
	ageGoupDivisionId = $("#ageGroupDivisionDropDown option:selected").val();
	homeAwayValue = $("#homeAwayDropDown option:selected").val();

	var complex;
	var field;
	if(locationValue != null && locationValue != 0){
		if(locationValue.includes("complex")){
			complex = locationValue.split("_")[1]
		}else{
			field = locationValue.split("_")[1];
		}
	}

	complexId = parseInt(complex) || 0;
	locationValue = parseInt(field) || 0;

	//filterType: 1= team, 2= location, 3= event type, 4= game season drop down, 5= ageGroup/division dropdown
	//Set gamesesaon drop down to select season/league/tournment option
	if(filterType == 3 && scheduleEventValue != 1){
		gameSeasonId = 0;
		$("#publicGameSeasonsDropDown").val(gameSeasonId);

		homeAwayValue = 0;
		$("#homeAwayDropDown").val(homeAwayValue);
	}
	//Set event type drop down to select game option
	if(gameSeasonId > 0){
		scheduleEventValue = 1;
		$("#publicEventDropDown").val(scheduleEventValue);
	}

	//Set team drop down to 0 when select a ageGroup/Division dropdown
	if(filterType == 5 && teamValue > 0){
		teamValue = 0;
		$("#publicTeamsDropDown").val(0);
	}

	//Set ageGroup/Division drop down to 0 when select a team dropdown
	if(filterType == 1 && ageGoupDivisionId != 0){
		ageGoupDivisionId = '0';
		$("#ageGroupDivisionDropDown").val(0);
	}

	if(filterType == 6 && homeAwayValue > 0){
		scheduleEventValue = 1;
		$("#publicEventDropDown").val(scheduleEventValue);
	}

	if(complexId == undefined){
		complexId = 0;
	}

	if(scheduleEventValue != null && teamValue != null && locationValue != null && gameSeasonId != null && ageGoupDivisionId != null && homeAwayValue != null && complexId != null){
		changeCalendarSchedules(scheduleEventValue, teamValue, locationValue, gameSeasonId, ageGoupDivisionId, homeAwayValue, complexId);
		if(scheduleEventValue != 0 || teamValue != 0 || locationValue != 0 || gameSeasonId != 0 || ageGoupDivisionId != 0 || homeAwayValue != 0 || complexId != 0){
			$("#currentFilterIsApply").val(1);
		}else{
			$("#currentFilterIsApply").val(0);
		}
	}
}

//To load schedule events on calendar based on filters
function changeCalendarSchedules(eventType,team,location,gameSeasonId, ageGoupDivisionId, homeAwayValue, complexId){
	var serverURL = $("#serverURL").val();

	var domainServerURL = $("#domainServerURL").val();
	var scheduleCalUrl = domainServerURL+'/schedule/calendar/rest/events';
    var events = {
            url: scheduleCalUrl, // URL to fetch events
            method: 'POST', // HTTP method for the request
            extraParams: {
               teamId: team,
               locationId: location,
               eventType:eventType,
               gameSeasonId:gameSeasonId,
               ageGoupDivisionId: ageGoupDivisionId,
               homeAwayValue:homeAwayValue,
               complexId:complexId,
               _csrf: $("#_csrf").val()
            }
     }
    // Remove all events
    calendar.removeAllEvents();

    calendar.getEventSources().forEach(function(eventSource) {
        if (eventSource.url === scheduleCalUrl) {
            eventSource.remove();
        }
    });

    // Add new event source
    calendar.addEventSource(events);
    calendar.render();
 	//removing the previous loaded events and event source link
}

var hasSchedulesForCalender=0;
var scheduleEventValue = 0;
var teamValue = 0;
var locationValue = 0;
var gameSeasonId = 0;
var ageGoupDivisionId = 0;
var homeAwayValue = 0;
var complexId = 0;

function loadScheduleCalendar(){
	var eventEditable = false;
	var serverURL = $("#serverURL").val();
	var firstDayOfWeek = $("#firstDayOfWeek").val();
	var domainServerURL = $("#domainServerURL").val();
	var scheduleCalUrl = domainServerURL+'/schedule/calendar/rest/events';

	calendarEl = document.getElementById('calendar');
    calendar = new FullCalendar.Calendar(calendarEl, {
                    initialView: 'dayGridMonth',
                    timeFormat: 'h:mm A',
                    firstDay: firstDayOfWeek, // Set first day of the week
                    slotMinTime: '06:00:00', // Sets the earliest time displayed (6:00 AM)
                    slotMaxTime: '24:00:00', // Sets the latest time displayed (12:00 AM)
                    allDaySlot: true, // Enables displaying events that last all day (default behavior)
                    weekends: true, // Shows weekends in the calendar (default behavior)
                    navLinks:true,
                    stickyHeaderDates:true,
                    expandRows:true,
                    contentHeight: "auto",
                    headerToolbar: {
                        left: '',
                        center: 'title',
                        right: 'today,prev,dayGridMonth,timeGridWeek,timeGridDay,next' // User can switch between the two
                    },
                    views: {
                        dayGridMonth: {
                            columnHeaderFormat: 'dddd', // Uses full weekday names in month view headers
                        },
                        dayGridWeek: {
                            columnHeaderFormat: 'dddd M/D' // Uses weekday names and dates in week view headers
                        }
                    },
                    selectable: false, // Enables selecting dates to create new events if true
                    selectMirror: false, // Shows a visual indication while selecting dates
                    select: function(info) {
                        var viewStartDate = FullCalendar.DateEnv.prototype.subtract(info.view.currentStart, 'days', 1); // Last day of the previous month
                        var viewEndDate = info.view.currentEnd; // First day of the next month
                        var eventDate = info.start; // Selected start date
                        var startDate = FullCalendar.formatDate(info.start, { month: 'short', day: '2-digit', year: 'numeric' }); // Format as "MMM DD YYYY"

                        var today = FullCalendar.formatDate(new Date(), { year: 'numeric', month: '2-digit', day: '2-digit' }); // Today's date
                        var eventDayToCompare = FullCalendar.formatDate(info.start, { year: 'numeric', month: '2-digit', day: '2-digit' }); // Selected date
                    },
                    editable: eventEditable, // Enables dragging and resizing events if true
                    eventClick: function(info) { // Handle clicking on an existing event
                        // Your logic to handle editing existing events (e.g., open a modal)
                    },
                    events: {
                        url: scheduleCalUrl, // URL to fetch events
                        method: 'POST', // HTTP method for the request
                        extraParams: {
                           teamId: teamValue,
                           locationId: locationValue,
                           eventType:scheduleEventValue,
                           gameSeasonId:gameSeasonId,
                           ageGoupDivisionId: ageGoupDivisionId,
                           homeAwayValue:homeAwayValue,
                           complexId:complexId,
                           _csrf: $("#_csrf").val()
                        },
                        success: function(response) {
                                var events = response;
                                /*Object.keys(events).forEach(key => {
                                    event = events[key];
                                });*/
                        },
                        failure: function(error) {
                            console.error('Error fetching events:', error);
                        }
                    },
                    eventDidMount: function(info) {
                        var element = info.el;
                        var event = info.event;
                        var view = info.view;

                        var viewTitle = view.title; // Gets the calendar view title
                        var eventTitle = event.start; // Gets event start time in milliseconds
                        var eventTitle2 = moment(eventTitle).format('MMMM YYYY'); // Format the event into MMMM YYYY format
                           console.log(event);
                        // Now check the view type is month and event month and view month are same
                        if(eventTitle2 !== viewTitle && view.type === 'dayGridMonth') {
                            element.style.display = 'none'; // Hides the past and next months events.
                        }

                         if (view.type === 'dayGridMonth') {
                            $(".fc-prev-button").addClass("with-tooltip").attr("title","Previous Month");
                            $(".fc-next-button").addClass("with-tooltip").attr("title","Next Month");
                          } else if (view.type === 'timeGridWeek') {
                            $(".fc-prev-button").addClass("with-tooltip").attr("title","Previous Week");
                            $(".fc-next-button").addClass("with-tooltip").attr("title","Next Week");
                        } else if (view.type === 'timeGridDay') {
                            $(".fc-prev-button").addClass("with-tooltip").attr("title","Previous Day");
                            $(".fc-next-button").addClass("with-tooltip").attr("title","Next Day");
                        }

                        var name = '';
                        var start = event.extendedProps.eventStartTime;
                        var end = event.extendedProps.eventEndTime;
                        var location = event.extendedProps.location;
                        var isHomeGame = event.extendedProps.isHomeGame;
                        var dateFormat = event.start;
                        var eventDate = moment(dateFormat).format('ddd, MMM DD');
                        var oppositeTeamName = event.extendedProps.oppositeTeamName;
                        var locationAddress = event.extendedProps.locationAddress;
                        var additionalLocation = event.extendedProps.additionalLocation;
                        var moreInfo = event.extendedProps.moreInfo;
                        var arriveBeforeMinutes = event.extendedProps.arriveBeforeMinutes;
                        var eventScheduleName= event.extendedProps.eventScheduleName;
                        var eventTypeName = event.extendedProps.eventTypeName;
                        var teamName = "";
                        var eventTitle = event.title;
                        var eventStatus = event.extendedProps.eventStatus;
                        var allDayEvent = event.extendedProps.allDayEvent;
                        var eventId = event.extendedProps.eventId;
                        var teamName = event.extendedProps.teamName;
                        var teamColor = event.teamColor;
                        var teamTextColor = event.teamTextColor;
                        var oppositeTeamColor = event.extendedProps.oppositeTeamColor;
                        var oppositeTeamTextColor = event.extendedProps.oppositeTeamTextColor;
                        var teamId = event.teamId;
                        var eventName = event.extendedProps.eventName;
                        var gameResult = event.gameResult;
                        var oppositeTeamIsHome = event.extendedProps.oppositeTeamIsHomeGame;
                        if(eventTypeName == "Match"){
                            name = "<div class='align-center strong' style='white-space:pre-wrap;'><img src='/public/static/img/circle.png' style='width:10px;height:10px;vertical-align:middle;background-color:#0059a0;margin-bottom:2px;position:relative !important;' />&nbsp;"+eventName;
                            name = name +"</div>";
                        }else if(eventTypeName == "Practice Session"){
                            name = "<div class='align-center strong' style='white-space:pre-wrap;'><img src='/public/static/img/circle.png' style='width:10px;height:10px;vertical-align:middle;background-color:#99c624;margin-bottom:2px;position:relative !important;' />&nbsp;&nbsp;Practice";
                            name = name +"</div>";
                        }else if(event.eventTypeName == "Homework"){
                            name = "<div class='align-center strong' style='white-space:pre-wrap;'><img src='/public/static/img/circle.png' style='width:10px;height:10px;vertical-align:middle;background-color:#cccccc;margin-bottom:2px;position:relative !important;' />&nbsp;&nbsp;Homework";
                            name = name +"</div>";
                        }else if(event.eventTypeName == "Non Sport"){
                            name = "<div class='align-center strong' style='white-space:pre-wrap;'><img src='/public/static/img/circle.png' style='width:10px;height:10px;vertical-align:middle;background-color:#000000;margin-bottom:2px;position:relative !important;' />&nbsp;&nbsp;";
                            if(teamId != null && teamId != ''){
                                name = name+"Team";
                            }
                            name = name+"Event";
                            name = name +"</div>";
                        }
                        if(event.labelName != null && event.labelName != '' && event.labelName != undefined){
                            name = name+"<div class='align-center min-margin-top min-margin-bottom'>";
                            name = name+"<span style='padding:0px 5px;white-space: normal;word-break: break-word;background:"+event.labelBackGroundColor+";color:"+event.labelTextColor+";'>";
                            name = name+""+event.labelName;
                            name = name+"</span></div>";
                        }

                        var tooltipName = '';
                        if(eventTypeName == "Match"){
                            tooltipName = "<div class='align-center strong' style='white-space:pre-wrap; min-width:250px;'><img src='/public/static/img/circle.png' style='width:10px;height:10px;vertical-align:middle;background-color:#0059a0;margin-bottom:2px;position:relative !important;' />&nbsp;"+eventName;
                            tooltipName = tooltipName +"</div>";
                        }else if(eventTypeName == "Practice Session"){
                            tooltipName = "<div class='align-center strong' style='white-space:pre-wrap; min-width:250px;'><img src='/public/static/img/circle.png' style='width:10px;height:10px;vertical-align:middle;background-color:#99c624;margin-bottom:2px;position:relative !important;' />&nbsp;&nbsp;Practice";
                            tooltipName = tooltipName +"</div>";
                        }else if(event.eventTypeName == "Homework"){
                            tooltipName = "<div class='align-center strong' style='white-space:pre-wrap; min-width:250px;'><img src='/public/static/img/circle.png' style='width:10px;height:10px;vertical-align:middle;background-color:#cccccc;margin-bottom:2px;position:relative !important;' />&nbsp;&nbsp;Homework";
                            tooltipName = tooltipName +"</div>";
                        }else if(event.eventTypeName == "Non Sport"){
                            tooltipName = "<div class='align-center strong' style='white-space:pre-wrap; min-width:250px;'><img src='/public/static/img/circle.png' style='width:10px;height:10px;vertical-align:middle;background-color:#000000;margin-bottom:2px;position:relative !important;' />&nbsp;&nbsp;";
                            if(teamId != null && teamId != ''){
                                tooltipName = tooltipName+"Team";
                            }
                            tooltipName = tooltipName+"Event";
                            tooltipName = tooltipName +"</div>";
                        }

                        if(event.labelName != null && event.labelName != '' && event.labelName != undefined){
                            tooltipName = tooltipName+"<div class='align-center min-margin-top min-margin-bottom'>";
                            tooltipName = tooltipName+"<span style='padding:0px 5px;white-space: normal;word-break: break-word;background:"+event.labelBackGroundColor+";color:"+event.labelTextColor+";'>";
                            tooltipName = tooltipName+""+event.labelName;
                            tooltipName = tooltipName+"</span></div>";
                        }
                         var data  = tooltipName;
                         data = data +"<table style='margin-top:4px;'>";
                            if(teamName != null && teamName != ''){
                                data  = data +"<tr><td colspan='2' style='white-space:pre-wrap;min-width:150px;' class='strong'>";
                                if(isHomeGame == 1){
                                    data  = data +"<img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/home.png' class='width-14 relative small-margin-right' /><span class='small-padding-left small-padding-right' style='background:"+teamColor+" !important;color:"+teamTextColor+" !important;'>"+teamName+"</span>";
                                }else if(isHomeGame == 2){
                                    data  = data +"<img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/away.png' class='width-10 relative small-margin-right top2' /><span class='small-padding-left small-padding-right' style='background:"+teamColor+" !important;color:"+teamTextColor+" !important;'>"+teamName+"</span>";
                                }else if(isHomeGame == 3){
                                    data  = data +"<img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/neutral.png' class='width-12 relative small-margin-right top2' /><span class='small-padding-left small-padding-right' style='background:"+teamColor+" !important;color:"+teamTextColor+" !important;'>"+teamName+"</span>";
                                }else{
                                    data  = data +"<span class='small-padding-left small-padding-right' style='background:"+teamColor+" !important;color:"+teamTextColor+" !important;'>"+teamName+"</span>";
                                }
                                if(oppositeTeamName != null && oppositeTeamName != ''){
                                    /*data  = data +" Vs "+oppositeTeamName;*/
                                    if(oppositeTeamIsHome == 1){
                                        data  = data +" <span class='relative iconTop-1'><img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/schedules/verses.png' width='18'/></span> <img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/home.png' class='width-14 relative small-margin-right' /><span class='small-padding-left small-padding-right' style='background:"+oppositeTeamColor+" !important;color:"+oppositeTeamTextColor+" !important;'>"+oppositeTeamName+"</span>";
                                    }else if(oppositeTeamIsHome == 2){
                                        data  = data +" <span class='relative iconTop-1'><img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/schedules/verses.png' width='18'/></span> <img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/away.png' class='width-10 relative small-margin-right top2' /><span class='small-padding-left small-padding-right' style='background:"+oppositeTeamColor+" !important;color:"+oppositeTeamTextColor+" !important;'>"+oppositeTeamName+"</span>";
                                    }else if(oppositeTeamIsHome == 3){
                                        data  = data +" <span class='relative iconTop-1'><img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/schedules/verses.png' width='18'/></span> <img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/neutral.png' class='width-12 relative small-margin-right top2' /><span class='small-padding-left small-padding-right' style='background:"+oppositeTeamColor+" !important;color:"+oppositeTeamTextColor+" !important;'>"+oppositeTeamName+"</span>";
                                    }else{
                                        data  = data +" <span class='relative iconTop-1'><img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/schedules/verses.png' width='18'/></span> <span class='small-padding-left small-padding-right' style='background:"+oppositeTeamColor+" !important;color:"+oppositeTeamTextColor+" !important;'>"+oppositeTeamName+"</span>";
                                    }
                                }
                                data  = data +"</td></tr>";
                            }
                            if(eventTypeName == "Match"){
                                data  = data +"<tr><td colspan='2'><span class='icon-calendar blue relative disp-inline-block iconTop-1'></span>&nbsp;&nbsp;<span style='white-space:pre-wrap;min-width:150px;'>"+eventDate+" "+start;
                                if(end != null && end != ''){
                                    data  = data +" - "+end;
                                }
                                data  = data +"</span></td></tr>";
                                if(allDayEvent == 1){
                                    data  = data +"<tr><td colspan='2'>All Day Event</td></tr>";
                                }
                                if(location != null && location != ''){
                                    data  = data +"<tr><td colspan='2'><span class='icon-marker blue' style='position:relative !important;'></span>&nbsp;&nbsp;&nbsp;<span style='white-space:pre-wrap;min-width:150px;'>"+location+"</span></td></tr>";
                                }
                                if(locationAddress != null && locationAddress != ''){
                                    data  = data +"<tr><td colspan='2' style='white-space:pre-wrap;min-width:150px;'>"+locationAddress+"</td></tr>";
                                }
                                if(additionalLocation != null && additionalLocation != ''){
                                    data  = data +"<tr><td colspan='2' style='white-space:pre-wrap;min-width:150px;'>"+additionalLocation+"</td></tr>";
                                }
                                if(moreInfo != null && moreInfo != ''){
                                    data  = data +"<tr><td colspan='2'><span class='icon-page-list blue' style='position:relative !important;'></span>&nbsp;&nbsp;&nbsp;<div class='margin-left' style='margin-top:-16px;white-space:pre-wrap;min-width:150px;'>"+moreInfo+"</div></td></tr>";
                                }
                                if(arriveBeforeMinutes != null && arriveBeforeMinutes > 0){
                                    data  = data +"<tr><td colspan='2' style='white-space:pre-wrap;min-width:150px;'>Arrive "+arriveBeforeMinutes+" minutes before start time</td></tr>";
                                }
                            }else if(eventTypeName == "Practice Session"){
                                data  = data +"<tr><td colspan='2'><span class='icon-calendar green relative iconTop-3' style='vertical-align:middle;'></span>&nbsp;&nbsp;<span style='white-space:pre-wrap;min-width:150px;'>"+eventDate+" "+start;
                                if(end != null && end != ''){
                                    data  = data +" - "+end;
                                }
                                data  = data +"</span></td></tr>";
                                if(location != null && location != ''){
                                    data  = data +"<tr><td colspan='2'><span class='icon-marker green'></span>&nbsp;&nbsp;&nbsp;<span style='white-space:pre-wrap;min-width:150px;'>"+location+"</span></td></tr>";
                                }
                                if(locationAddress != null && locationAddress != ''){
                                    data  = data +"<tr><td colspan='2' style='white-space:pre-wrap;min-width:150px;'>"+locationAddress+"</td></tr>";
                                }
                                if(moreInfo != null && moreInfo != ''){
                                    data  = data +"<tr><td colspan='2'><span class='icon-page-list green' style='position:relative !important;'></span>&nbsp;&nbsp;&nbsp;<div class='margin-left' style='margin-top:-16px;white-space:pre-wrap;min-width:150px;'>"+moreInfo+"</div></td></tr>";
                                }
                            }else if(eventTypeName == "Homework"){
                                data  = data +"<tr><td colspan='2'><span class='icon-calendar grey'></span>&nbsp;&nbsp;<span style='white-space:pre-wrap;min-width:150px;'>"+eventDate+"</span></td></tr>";
                                data  = data +"<tr><td><img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/home-game.png' style='width:10px;height:10px;vertical-align:middle;' />&nbsp;&nbsp;<span style='white-space:pre-wrap;min-width:150px;'>"+eventScheduleName+"</span></td></tr></table>";
                            }else if(eventTypeName == "Non Sport"){
                                data = data +"<tr><td colspan='2' class='strong'>"+eventTitle+"</td></tr>";
                                data  = data +"<tr><td colspan='2'><span class='icon-calendar black relative iconTop-3' style='vertical-align:middle;'></span>&nbsp;&nbsp;<span style='white-space:pre-wrap;min-width:150px;'>"+eventDate+" "+start;
                                if(end != null && end != ''){
                                    data = data +" - "+end;
                                }
                                data = data +"</span></td></tr>";
                                if(allDayEvent == 1){
                                    data  = data +"<tr><td colspan='2'>All Day Event</td></tr>";
                                }
                                if(location != null && location != ''){
                                    data  = data +"<tr><td colspan='2'><span class='icon-marker black'></span>&nbsp;&nbsp;&nbsp;<span style='white-space:pre-wrap;min-width:150px;'>"+location+"</span></td></tr>";
                                }
                                if(locationAddress != null && locationAddress != ''){
                                    data  = data +"<tr><td colspan='2' style='white-space:pre-wrap;min-width:150px;'>"+locationAddress+"</td></tr>";
                                }
                                if(moreInfo != null && moreInfo != ''){
                                    data  = data +"<tr><td colspan='2'><span class='icon-page-list black' style='position:relative !important;'></span>&nbsp;&nbsp;&nbsp;<div class='margin-left' style='margin-top:-16px;white-space:pre-wrap;min-width:150px;'>"+moreInfo+"</div></td></tr>";
                                }
                            }
                            if(gameResult != null && gameResult != ""){
                                data  = data +"<tr><td colspan='2' style='white-space:pre-wrap;min-width:150px;'>";
                                data  = data +gameResult;
                                data  = data +"</td></tr>";
                            }
                            if(eventStatus !=null && eventStatus !=1){
                                //	If event cancelled then showing the status
                                data = data +"<tr><td><span class='red strong margin-bottom' style='font-size:10px'>CANCELLED</span></td></tr>";
                            }
                        data = data +"</table>";
                        var scheduleCalendarHeading  = '';
                        scheduleCalendarHeading = scheduleCalendarHeading +"<div class='small-margin-top small-margin-bottom tiny-padding-right'>";
                            if(eventTypeName == "Match"){
                                scheduleCalendarHeading = scheduleCalendarHeading+"<div class='blue'>"+name+"</div>";
                                if(teamName != null && teamName != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='blue strong space-prewrap small-margin-top'>";
                                    if(isHomeGame == 1){
                                        scheduleCalendarHeading  = scheduleCalendarHeading +"<img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/home.png' class='width-14 relative small-margin-right' />";
                                    }else if(isHomeGame == 2){
                                        scheduleCalendarHeading  = scheduleCalendarHeading +"<img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/away.png' class='width-10 relative small-margin-right top2' />";
                                    }else{
                                        scheduleCalendarHeading  = scheduleCalendarHeading +"<img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/neutral.png' class='width-12 relative small-margin-right top2' />";
                                    }
                                    scheduleCalendarHeading  = scheduleCalendarHeading +" <span class='small-padding-left small-padding-right' style='background:"+teamColor+" !important;color:"+teamTextColor+" !important;'>"+teamName+"</span>";
                                    if(oppositeTeamName != null && oppositeTeamName != ''){

                                        if(oppositeTeamColor != null && oppositeTeamColor != ''){
                                            oppositeTeamColor = oppositeTeamColor;
                                        }else{
                                            oppositeTeamColor = '';
                                        }
                                        if(oppositeTeamTextColor != null && oppositeTeamTextColor != ''){
                                            oppositeTeamTextColor = oppositeTeamTextColor;
                                        }else{
                                            oppositeTeamTextColor = '';
                                        }

                                        scheduleCalendarHeading  = scheduleCalendarHeading +" <span class=' relative iconTop-3'><img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/schedules/verses.png' width='18'/></span> ";
                                        if(oppositeTeamIsHome == 1){
                                            scheduleCalendarHeading  = scheduleCalendarHeading +" <img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/home.png' class='width-14 relative small-margin-right' />";
                                        }else if(oppositeTeamIsHome == 2){
                                            scheduleCalendarHeading  = scheduleCalendarHeading +" <img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/away.png' class='width-10 relative small-margin-right top2' />";
                                        }else if(oppositeTeamIsHome == 3){
                                            scheduleCalendarHeading  = scheduleCalendarHeading +" <img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/general/neutral.png' class='width-12 relative small-margin-right top2' />";
                                        }


                                        scheduleCalendarHeading  = scheduleCalendarHeading +" <span class='small-padding-left small-padding-right' style='background:"+oppositeTeamColor+" !important;color:"+oppositeTeamTextColor+" !important;'>"+oppositeTeamName+"</span>";


                                    }
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"</div>";
                                }
                                scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='blue space-prewrap'><span class='icon-calendar relative iconTop-1'></span>&nbsp;&nbsp;<span>"+eventDate+" "+start;
                                if(end != null && end != ''){
                                    scheduleCalendarHeading = scheduleCalendarHeading +" - "+end;
                                }
                                scheduleCalendarHeading= scheduleCalendarHeading+"</span></div>";
                                if(allDayEvent == 1){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='blue space-prewrap'>All Day Event</div>";
                                }
                                if(location != null && location != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='blue space-prewrap'><span class='icon-marker'></span>&nbsp;&nbsp;&nbsp;<span>"+location+"</span></div>";
                                }
                                if(locationAddress != null && locationAddress != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='blue space-prewrap'>"+locationAddress+"</div>";
                                }
                                if(additionalLocation != null && additionalLocation != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='blue space-prewrap'>"+additionalLocation+"</div>";
                                }
                                if(arriveBeforeMinutes != null && arriveBeforeMinutes > 0){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='blue space-prewrap'>Arrive "+arriveBeforeMinutes+" minutes before start time</div>";
                                }
                                if(gameResult != null && gameResult != ""){
                                    scheduleCalendarHeading  = scheduleCalendarHeading+"<div class='space-prewrap blue'>"+gameResult+"</div>";
                                }
                            }else if(eventTypeName == "Practice Session"){
                                scheduleCalendarHeading = scheduleCalendarHeading+"<div class='green space-prewrap'>"+name+"</div>";
                                if(teamName != null && teamName != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='green strong space-prewrap small-margin-top'><span class='small-padding-left small-padding-right' style='background:"+teamColor+" !important;color:"+teamTextColor+" !important;'>"+teamName;
                                    if(oppositeTeamName != null && oppositeTeamName != ''){
                                        scheduleCalendarHeading  = scheduleCalendarHeading +" <span class='relative' style='top:3px;'><img src='"+awsCloudUrl+"'/static/img/icons/schedules/verses.png' width='15'/></span> "+oppositeTeamName;
                                    }
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"</span></div>";
                                }
                                scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='green space-prewrap'><span class='icon-calendar green relative iconTop-1'></span>&nbsp;&nbsp;<span>"+eventDate+" "+start;
                                if(end != null && end != ''){
                                    scheduleCalendarHeading = scheduleCalendarHeading +" - "+end;
                                }
                                scheduleCalendarHeading= scheduleCalendarHeading+"</span></div>";
                                if(location != null && location != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='green space-prewrap'><span class='icon-marker green'></span>&nbsp;&nbsp;&nbsp;<span>"+location+"</span></div>";
                                }
                                if(locationAddress != null && locationAddress != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='green space-prewrap'>"+locationAddress+"</div>";
                                }
                            }else if(eventTypeName == "Homework"){
                                scheduleCalendarHeading = scheduleCalendarHeading+'<span class="strong">Homework</span>';
                                if(teamId != null && teamId != ''){
                                    scheduleCalendarHeading = scheduleCalendarHeading+'&nbsp;<img src="/public/static/img/team-icon.png" width="11" />';
                                }
                            }else if(eventTypeName == "Non Sport"){
                                scheduleCalendarHeading = scheduleCalendarHeading+"<div class='black space-prewrap'>"+name+"</div>";
                                scheduleCalendarHeading = scheduleCalendarHeading +"<div class='black strong space-prewrap'>"+eventTitle+"</div>";
                                if(teamName != null && teamName != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='black strong space-prewrap small-margin-top'><span class='small-padding-left small-padding-right' style='background:"+teamColor+" !important;color:"+teamTextColor+" !important;'>"+teamName;
                                    if(oppositeTeamName != null && oppositeTeamName != ''){
                                        scheduleCalendarHeading  = scheduleCalendarHeading +" <span class='relative' style='top:3px;'><img src='https://d351kgpk2ntpv6.cloudfront.net/static/img/icons/schedules/verses.png' width='15'/></span> "+oppositeTeamName;
                                    }
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"</span></div>";
                                }
                                scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='black space-prewrap'><span class='icon-calendar black relative iconTop-1'></span>&nbsp;&nbsp;<span>"+eventDate+" "+start;
                                if(end != null && end != ''){
                                    scheduleCalendarHeading = scheduleCalendarHeading +" - "+end;
                                }
                                scheduleCalendarHeading= scheduleCalendarHeading+"</span></div>";
                                if(allDayEvent == 1){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='black space-prewrap'>All Day Event</div>";
                                }
                                if(location != null && location != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='black space-prewrap'><span class='icon-marker black'></span>&nbsp;&nbsp;&nbsp;<span>"+location+"</span></div>";
                                }
                                if(locationAddress != null && locationAddress != ''){
                                    scheduleCalendarHeading  = scheduleCalendarHeading +"<div class='black space-prewrap'>"+locationAddress+"</div>";
                                }
                            }
                            if(eventStatus !=null && eventStatus !=1){
                                //	If event cancelled then showing the status
                                scheduleCalendarHeading = scheduleCalendarHeading +"<div><span class='red strong space-prewrap' style='font-size:10px'>CANCELLED</span></div>";
                            }
                            scheduleCalendarHeading = scheduleCalendarHeading +"</div>";
                            $(element).attr("title",data);
                            $(element).attr("href", null);
                            $(element).html("");
                            var newDiv = $("<div class='fc-content'></div>");
                            newDiv.attr("id", "calevent_"+eventId);
                            newDiv.html(scheduleCalendarHeading);
                            $(element).html(newDiv);
                            $(element).attr("data-tooltip-options",'{"content":"","classes":[]}');
                        // Additional rendering logic
                    },
                    eventResize: null, // Disable resizing events by default (optional)
                    dayMaxEvents: false // Display "more" link when too many events in a day
                });

                calendar.render();

}