/*
PEN Javascript File // Event Calendar Teaser
AUTHOR: Jason Zajac & TJ Nicolaides
VERSION: 1.0
****************************/

$(document).ready(function() {	
	
	
	
	//Create date
	var dayOfMonth= [];
	var month= [];
	var gameDate= [];
	var teamCalendar = []
	var sportTeams=[];
	var eventLinks=[];
	var teamVenue=[]
	//Pattern to Determine if it is a Home game
	var venuePattr = /vs./gi;

	//Selects each Event Listing
	$("div.events.upcoming").each( function(calendar) {
		
		if ($("div.events.upcoming:eq(" + calendar + ") span.dim.date").length > 0) {
		teamCalendar[calendar] = $("div.events.upcoming:eq(" + calendar + ") span.dim.date").parent("td.dateColumn").siblings(".eventColumn").children(".eventName").html().slice(0, 3).toLowerCase();
		} 
		
		//Determines what team the calendar belongs to then it creates a <ul>
		teamEventCalendar(teamCalendar[calendar], calendar);
		
		//Creates List items for the next 5 days & Matches Game Dates
		for (i=0; i<5; i++) {
			//If i = 0 then it resets all important variables
			if (i == 0) {
				sportTeams = [];
				eventLinks = [];
				teamVenue=[]
				dateToday = new Date();
				dateToday.setDate(dateToday.getDate());
			} else if (i > 0) {
				//Advances date to next day
				dateToday.setDate(dateToday.getDate()+1);
			}
			
			dayOfMonth[i] = dateToday.getDate();
			month[i] = dateToday.getMonth()+1;
			gameDate[i] = month[i] + "/" + dayOfMonth[i];
			
			
			//Finds all list items that matches gameDate[i]
			$("div.events.upcoming:eq(" + calendar + ") span.dim.date:contains(" + gameDate[i] + ")").each(function(index) {
				//Determines if it gameDate[i] matches the current list item
				if (jQuery.trim($("div.events.upcoming:eq(" + calendar + ") span.dim.date:contains(" + gameDate[i] + ")").text()) == gameDate[i]) {
					//Stores whether it is home/away. If it finds "vs." within the string it stores it otherwise it is viewed as an away game.
					teamVenue[i] = $("div.events.upcoming:eq(" + calendar + ") span.dim.date:contains(" + gameDate[i] + ")").parent("td.dateColumn").siblings(".eventColumn").children(".eventName").text().toLowerCase().match(venuePattr);
					
					//Stores the opponent for the matched date
					sportTeams[i] = $("div.events.upcoming:eq(" + calendar + ") span.dim.date:contains(" + gameDate[i] + ")").parent("td.dateColumn").siblings(".eventColumn").children(".eventName").text().replaceAll("Eagles ", "").replaceAll("Flyers ", "").replaceAll("Sixers ", "").replaceAll("76ers ", "").replaceAll("Phillies ", "").replaceAll("@ ", "").replaceAll("at ", "").replaceAll("at", "").replaceAll("vs. ", "").replaceAll("@", "").replaceAll("vs.", "").replaceAll("vs ", "").replaceAll("vs", "").toLowerCase();
					
					if (sportTeams[i] == "49ers") {
						sportTeams[i] = "niners";
					}
					
					//Stores the link for the matched date
					eventLinks[i] = $("div.events.upcoming:eq(" + calendar + ") span.dim.date:contains(" + gameDate[i] + ")").parent("td.dateColumn").siblings(".eventColumn").children(".eventName").attr("href");

					//console.log(teamVenue[i]);
				} 

				
			});
			
			//If the teamCalendar is one of the 4 major teams then it creates the list items
			fiveDayCalendar(calendar, i, gameDate[i], teamCalendar[calendar], teamVenue[i], sportTeams[i], eventLinks[i]);
			
	}
		//If there are no list item (empty event calendar) then remove 5-day calendar + inserts "season's over" msg
			if (teamCalendar[calendar] === undefined) {
				$("div ul.calendar-" + calendar).remove();
				
				//if ($("div ul.calendar-" + calendar).text() == "0") {
				$("div.events.upcoming:eq(" + calendar + ")").prepend("<p class=\"empty-calendar-msg\">Seems like the season's over folks!</p>");
				//}
			}
	});						   
	
});

function teamEventCalendar(teamCalendar, calendar) {
	if (teamCalendar == "eag") {
		//console.log("eagles calendar");
		$("div.events.upcoming:eq(" + calendar + ")").prepend("<ul class=\"five-day-team-calendar eagles-calendar calendar-" + calendar + "\"></ul>");
	} else if (teamCalendar == "phi") {
		//console.log("phillies calendar");
		$("div.events.upcoming:eq(" + calendar + ")").prepend("<ul class=\"five-day-team-calendar phillies-calendar calendar-" + calendar + "\"></ul>");
	} else if (teamCalendar == "fly") {
		//console.log("flyers calendar");
		$("div.events.upcoming:eq(" + calendar + ")").prepend("<ul class=\"five-day-team-calendar flyers-calendar calendar-" + calendar + "\"></ul>");
	} else if (teamCalendar == "six") {
		//console.log("sixers calendar");
		$("div.events.upcoming:eq(" + calendar + ")").prepend("<ul class=\"five-day-team-calendar sixers-calendar calendar-" + calendar + "\"></ul>");
	} else if (teamCalendar == "76e") {
		//console.log("sixers calendar");
		$("div.events.upcoming:eq(" + calendar + ")").prepend("<ul class=\"five-day-team-calendar sixers-calendar calendar-" + calendar + "\"></ul>");
	} /*else if (teamCalendar === undefined) {
		$("div.events.upcoming:eq(" + calendar + ")").prepend("<ul class=\"five-day-team-calendar calendar-" + calendar + "\"></ul>");
	} else {
	
	}*/
}

function fiveDayCalendar(calendar, i, gameDate, teamCalendar, teamVenue, sportTeams, eventLinks) {
	if (teamCalendar == "eag" || teamCalendar == "phi" || teamCalendar == "fly" || teamCalendar == "six" || teamCalendar == "76e" /*|| teamCalendar === undefined*/) {
				//if there is no team (undefined), it creates a blank list item with only the date
				if (sportTeams === undefined) {
					$("div.events.upcoming:eq(" + calendar + ") ul" ).append("<li class=\"day-" + i + " " + sportTeams + "\"><span>" + gameDate + "</span></li>");
				} else { 
					//If the game is home, then it will display the event with a class of "home-game"
					if (teamVenue == "vs.") {
						$("div.events.upcoming:eq(" + calendar + ") ul" ).append("<li class=\"day-" + i + " " + sportTeams + "\"><a href=\"" + eventLinks + "\"><span class=\"home-game\">" + gameDate + "</span></a></li>");
					} 
					//For every event that isn't recognized as a home game, it will display the list item with a class of "away-game"
					else {
						$("div.events.upcoming:eq(" + calendar + ") ul" ).append("<li class=\"day-" + i + " " + sportTeams + "\"><a href=\"" + eventLinks + "\"><span class=\"away-game\">" + gameDate + "</span></a></li>");
					}
				}
			} 
}


// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
	strTarget, // The substring you want to replace
	strSubString // The string you want to replace in.
	){
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
	 
	// Keep looping while an instance of the target string
	// still exists in the string.
	while (intIndexOfMatch != -1){
	// Relace out the current instance.
	strText = strText.replace( strTarget, strSubString )
	 
	// Get the index of any next matching substring.
	intIndexOfMatch = strText.indexOf( strTarget );
	}
	 
	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return( strText );
}