var HPScoreboard = {

	isInit: true,

	load : function(data) {

		var games = data.games;

		for (var i=0, num_games = games.length; i < num_games; i++) {

			var game = games[i];

			if (game.game_status < 2) { continue; }

			if (HPScoreboard.isInit) { 
				var $tab = $$('#hp-scoreboard .tabModule')[0];
				if ($tab) { $tab.tabModule.onNavClick(1); } 
			}
			
			var $hp_scoreboard_game = $("hp-scoreboard-game-" + game.game_id);

			// Set game status class for game
			$hp_scoreboard_game.removeClassName("pre");
			$hp_scoreboard_game.removeClassName("live");
			$hp_scoreboard_game.removeClassName("final");

			if (game.game_status == 2) {
				$hp_scoreboard_game.addClassName("live");
			} else if (game.game_status == 3) {
				$hp_scoreboard_game.addClassName("final");
			} else {
				$hp_scoreboard_game.addClassName("pre");
			}


			// Set visiting score if score span exists, if not, create span, then update
			$visiting_score = $hp_scoreboard_game.down("." + game.visiting_team.teamcode + " .score");
			if (!$visiting_score && game.game_status > 1) { 
				$visiting_team = $hp_scoreboard_game.down("." + game.visiting_team.teamcode);	

				$visiting_score = new Element("span").addClassName("score");
				$visiting_team.insert({ bottom : $visiting_score});
			}

			if (game.game_status > 1) {
				$visiting_score.update((game.visiting_team.score ? game.visiting_team.score : 0));

				if (game.visiting_team.score > game.home_team.score) {
					$visiting_score.addClassName("winning");
				} else {
					$visiting_score.removeClassName("winning");
				}
			}


			// Set home score if score span exists, if not, create span, then update
			$home_score = $hp_scoreboard_game.down("." + game.home_team.teamcode + " .score");
			if (!$home_score && game.game_status > 1) { 
				$home_team = $hp_scoreboard_game.down("." + game.home_team.teamcode);	

				$home_score = new Element("span").addClassName("score");
				$home_team.insert({ top : $home_score});
			}

			if (game.game_status > 1) {
				$home_score.update((game.home_team.score ? game.home_team.score : 0));

				if (game.home_team.score > game.visiting_team.score) {

					$home_score.addClassName("winning");
				} else {
					$home_score.removeClassName("winning");
				}
			}


			// Set scoring state
			if (game.game_status > 1) { 
				$hp_scoreboard_game.down(".state").innerHTML = "|";
			} else {
				$hp_scoreboard_game.down(".state").innerHTML = "@";
			}


			// Set links
			$hp_scoreboard_game.down(".teams").siblings().each(function(n, i) { n.remove(); });
			
			if (game.game_status == 2 && game.national_broadcaster) {
				$hp_scoreboard_game.insert({"bottom" : ["<a href='", game.national_broadcaster[2], "' class='broadcaster ", game.national_broadcaster[0], "'>", game.national_broadcaster[1], "</a>"].join("") });
			} else {
				$hp_scoreboard_game.insert({"bottom" : ["<a href='", game.boxscore_link, "' class='box'>Boxscore</a>"].join("") });
			}
			
			if (game.game_status == 3 && game.highlight_link) {
				$hp_scoreboard_game.insert({"bottom" : ["<a href='", game.highlight_link, "' class='highlights'>Highlights</a>"].join("") });
			}

			$hp_scoreboard_game = null;
			$visiting_score = null;
			$visiting_team = null;
			$home_score = null;
			$home_team = null;
		}
		
		HPScoreboard.isInit = false;
		
	}
}



$(document).observe("dom:loaded", (function() {

		var interval = null;

		function cleanUpData() {
			var headTag = $$('head')[0];
			var dataScript = $$('head script[rel="hp-scoreboard-data"]')[0];

			if (dataScript) {
				headTag.removeChild(dataScript);	
			}
			
			try {
				for (var prop in dataScript) {
					delete dataScript[prop];
				}
			} catch(e) { }

			dataScript = null;
			headTag = null;
		}

		function loadTodaysGames() {
		
			if ($("scoreboard_hp_today_games")) {
				var timestamp = (new Date).getTime();

				var dataScript = new Element("script", {"type" : "text/javascript", "src" : "/data/10s/json/dleaguecom/homepage/noseason/hp_scoreboard_today_jsonp.js?ts=" + timestamp, "rel" : "hp-scoreboard-data"});

				// onload callback works in Mozilla
				dataScript.onload = function(e) { cleanUpData(); interval = setTimeout(loadTodaysGames, 15000); };
				dataScript.onerror = function(e) { cleanUpData(); interval = setTimeout(loadTodaysGames, 5000); };

				// onreadystatechange callback works in IE
				dataScript.onreadystatechange = function(e) { if (this.readyState == 'complete' || this.readyState == 'loaded') { cleanUpData(); interval = setTimeout(loadTodaysGames, 15000); } }.bind(dataScript);

				var headTag = $$('head')[0];



				headTag.appendChild(dataScript);


				dataScript = null;
				headTag = null;
			}
		}

		return loadTodaysGames;
})());
