﻿// Initialise bubble place holders until they are overwritten.
if (OddBubble == null) {
	OddBubble = {};
	OddBubble.OnMouseOverOdds = function() { }
	OddBubble.StartLoading = function() { }
}

// FUNCTIONS

// Overwrite original update function so that we can fit our data into the structure we have.
DataManager.Update = function() {
	if (DataManager.DataExists()) {
		var content = DataManager.GetData();

		for (var r = 0; r < content.length; r++) {
			var row = DataManager.LOCAL.Cells[r];
			var data = content[r];

			var c = 0;
			var column = 0;

			for (var item in data) {

				// If this item starts with an underscore, then it is an odd.
				if (item.indexOf("_") == 0) {
					var cell = row[c];
					var text = (new Number(data[item])).round(2); // The odd text within the cell.

					// Add to the number of columns that have already been displayed.
					column += 1;
					var isOdds = (column > 2 ? true : false);

					// Reset.
					cell.target = "";
					cell.href = "Javascript:void(0);";
					cell.setStyle("cursor", "default");

					// If not numeric, don't format.
					if (data[item].length == 0 || isNaN(text)) {
						text = data[item];
					}

					// If this odd has a dollar return value, add that to the string.
					if (data["RETURN" + item] != null && data["RETURN" + item].length > 0) {
						text += " ($" + (new Number(data["RETURN" + item])).round(2) + ")";
					}

					// If no text, show blank.
					if (text.length == 0) {
						text = "&nbsp;";
						isOdds = false;
					}

					var comparisonLink = Utils.WebsiteURL + "odds/odds-comparison.aspx?eventid=" + data['EventID'];

					// This is the event title, link to odds comparison.
					if ($type(text) == "string" && text.indexOf(" v ") > -1) {
						text = "<a href=\"" + comparisonLink + "\" class=\"oddsSummaryEventLink\">" + text + "</a>";
						cell.setStyle("cursor", "auto");
					}

					// Set the cell text.
					cell.innerHTML = text;

					if (isOdds) {
						var details = DataManager.GetDetails(data["OddID"]);

						// If there are details associated with these odds.
						if (details) {
							var odds = data[item];

							var id = '';
							var sportBook = '';
							var oddURL = '';
							var oddUpdate = '';
							var comments = '';

							if (data["RETURN" + item] != null && data["RETURN" + item].length > 0) {
								odds += " ($" + data["RETURN" + item] + ")";
							}

							// Money Line
							if (column == 3 || column == 4 || column == 5) {
								odds += " <span class=\"betType\">";
								odds += "- Money Line";

								// Home
								if (column == 3) {
									odds += " (Home)";

									id = details["HomeMoneyLineOddID"];
									sportsBook = details["HomeMoneyLineSportsBook"];
									oddURL = details["HomeMoneyLineURL"];
									comments = details["HomeMoneyLineComments"];
									oddUpdate = details[""];

									// Away
								} else if (column == 4) {
									odds += " (Away)";

									id = details["AwayMoneyLineOddID"];
									sportsBook = details["AwayMoneyLineSportsBook"];
									oddURL = details["AwayMoneyLineURL"];
									comments = details["AwayMoneyLineComments"];
									oddUpdate = details[""];

									// Draw
								} else if (column == 5) {
									odds += " (Draw)";

									id = details["DrawMoneyLineOddID"];
									sportsBook = details["DrawMoneyLineSportsBook"];
									oddURL = details["DrawMoneyLineURL"];
									comments = details["DrawMoneyLineComments"];
									oddUpdate = details[""];
								}

								odds += "</span>";

								// Points Spread
							} else if (column == 6 || column == 7) {
								odds += " <span class=\"betType\">";
								odds += "- Spread";

								// Home
								if (column == 6) {
									odds += " (Home)";

									id = details["HomeSpreadOddID"];
									sportsBook = details["HomeSpreadSportsBook"];
									oddURL = details["HomeSpreadURL"];
									comments = details["HomeSpreadComments"];
									oddUpdate = details[""];

									// Away
								} else if (column == 7) {
									odds += " (Away)";

									id = details["AwaySpreadOddID"];
									sportsBook = details["AwaySpreadSportsBook"];
									oddURL = details["AwaySpreadURL"];
									comments = details["AwaySpreadComments"];
									oddUpdate = details[""];
								}

								odds += "</span>";

								// Total Points
							} else if (column == 8 || column == 9) {
								odds += " <span class=\"betType\">";
								odds += "- Total Points";

								// Over
								if (column == 8) {
									odds += " (Over)";

									id = details["TotalOverOddID"];
									sportsBook = details["TotalOverSportsBook"];
									oddURL = details["TotalOverURL"];
									comments = details["TotalOverComments"];
									oddUpdate = details[""];

									// Under
								} else if (column == 9) {
									odds += " (Under)";

									id = details["TotalUnderOddID"];
									sportsBook = details["TotalUnderSportsBook"];
									oddURL = details["TotalUnderURL"];
									comments = details["TotalUnderComments"];
									oddUpdate = details[""];
								}

								odds += "</span>";
							}

							cell.dataID = id;
							cell.data = {
								'SportsBook': sportsBook,
								'CompetitorName': details["EventName"],
								'Odds': odds,
								'OddURL': oddURL,
								'OddUpdate': oddUpdate,
								'Comments': comments
							}

							if (oddURL.length > 0) {
								cell.target = "_blank";
								cell.href = oddURL;
								cell.setStyle("cursor", "pointer");
							}

							cell.addEvent("mouseover", function(e) { OddBubble.OnMouseOverOdds(e, this); });
						}
					}

					// If this is the last item of data, grab the button and link it.
					if (row.length - 2 == c) {
						cell = row[c + 1];
						cell.href = comparisonLink;
					}

					c++;
				}
			}
		}
	}
}
