function getClubID(game,team) {
	var clubID = "";
	if (team == "home") {
		for (var i=0; i<2; i++) {
			if (game.teams[i].isHome == true) {
				clubID = game.teams[i].code;
			}
		}
	}
	if (team == "away") {
		for (var i=0; i<2; i++) {
			if (game.teams[i].isHome == false) {
				clubID = game.teams[i].code;
			}
		}
	}
	return clubID
}

function buildMediaLink(media,i) {

	if (i) { i = i; }
	else { i = 0; }
	
	var output = "";
	var realmedia = "";
	var windowsmedia = "";
	
	if (media.urls[i]) {
		if (media.urls[i].url.r_id) {
			realmedia = "r_id:\'" + media.urls[i].url.r_id + "\',r:\'" + media.urls[i].url.r + "\',";
		}
		if (media.urls[i].url.w_id) {
			windowsmedia = "w_id:\'" + media.urls[i].url.w_id + "\',w:\'" + media.urls[i].url.w + "\',";
		}
		
		output += "javascript:void(playMedia2({" + realmedia + windowsmedia + "pid:\'" + media.urls[i].url.pid + "\',gid:\'" + media.urls[i].url.gid + "\',cid:\'mlb\',fid:\'" + media.urls[i].url.fid + "\',v:\'" + media.urls[i].url.v + "\'}));";
	}
	
	return output;

}

function buildMLBHtml(game,team) {

	var clubID = getClubID(game,team);
	var callLetters = "";
	
	var html = "";
	
	// START VIDEO
	html += "<a href=\"/mediacenter/index.jsp?c_id=" + clubID +"\"><img src=\"http://www.mlb.com/images/news/bug_video.gif\" width=\"60\" height=\"13\" border=\"0\" alt=\"Video\" class=\"mar3tb\"></a><br />";

	// MLB.TV LINK
	if ((game.mlbtv) && (game.mlbtv.urls[0]) && (game.mlbtv.state=="video_archive")) {
		html += "<a href=\"";
		html += buildMediaLink(game.mlbtv);
		html += "\">MLB.TV</a>";
	}
	
	// CONDENSED GAME LINK
	if ((game.condensed_video) && (game.condensed_video.urls[0]) && ((game.condensed_video.state=="video_on") || (game.condensed_video.state=="video_archive"))) {
		html += "&nbsp;|&nbsp;<a href=\"";
		html += buildMediaLink(game.condensed_video);
		html += "\">Condensed Game</a>";
	}

	// EXTENDED HIGHLIGHTS
	if ((game.extended_highlights) && (game.extended_highlights.urls[0]) && ((game.extended_highlights.state=="video_on") || (game.extended_highlights.state=="video_archive"))) {
		html += "&nbsp;|&nbsp;<a href=\"";
		html += buildMediaLink(game.extended_highlights);
		html += "\">Highlight Reel</a>";
	}

	html += "<p />";
	
	// START AUDIO
	html += "<a href=\"/mediacenter/index.jsp?c_id=" + clubID +"\"><img src=\"http://www.mlb.com/images/news/bug_audio.gif\" width=\"67\" height=\"13\" border=\"0\" alt=\"Audio\" class=\"mar3tb\"></a><br />";

	// GAMEDAY AUDIO
	if ((team == "home") && (game.home_audio) && (game.home_audio.urls[0])) {
		html += "<img src=\"http://www.mlb.com/images/icons/audio_gold.gif\" width=\"15\" height=\"12\" alt=\"\" border=\"0\" align=\"absmiddle\">&nbsp;";
		html += "<a href=\"";
		html += buildMediaLink(game.home_audio);
		callLetters = game.home_audio.text;
		html += "\">" + callLetters + " - Gameday Audio</a><br />";
	} else if ((team == "away") && (game.away_audio) && (game.away_audio.urls[0])) {
		html += "<img src=\"http://www.mlb.com/images/icons/audio_gold.gif\" width=\"15\" height=\"12\" alt=\"\" border=\"0\" align=\"absmiddle\">&nbsp;";
		html += "<a href=\"";
		html += buildMediaLink(game.away_audio);
		callLetters = game.away_audio.text;
		html += "\">" + callLetters + " - Gameday Audio</a><br />";
	}	
    return html;
}

function buildMLBPSHtml(game,team) {

	var clubID = getClubID(game,team);
	var callLetters = "";
	
	var html = "";
	
	// START VIDEO
	html += "<a href=\"/mediacenter/index.jsp?c_id=" + clubID +"\"><img src=\"http://www.mlb.com/images/news/bug_video.gif\" width=\"60\" height=\"13\" border=\"0\" alt=\"Video\" class=\"mar3tb\"></a><br />";

	// MLB.TV LINK
	if ((game.mlbtv) && (game.mlbtv.urls[0]) && (game.mlbtv.state=="video_archive")) {
		html += "<a href=\"";
		html += buildMediaLink(game.mlbtv);
		html += "\">MLB.TV</a>";
	}
	
	// CONDENSED GAME LINK
	if ((game.condensed_video) && (game.condensed_video.urls[0]) && ((game.condensed_video.state=="video_on") || (game.condensed_video.state=="video_archive"))) {
		html += "&nbsp;|&nbsp;<a href=\"";
		html += buildMediaLink(game.condensed_video);
		html += "\">Condensed Game</a>";
	}

	// EXTENDED HIGHLIGHTS
	if ((game.extended_highlights) && (game.extended_highlights.urls[0]) && ((game.extended_highlights.state=="video_on") || (game.extended_highlights.state=="video_archive"))) {
		html += "&nbsp;|&nbsp;<a href=\"";
		html += buildMediaLink(game.extended_highlights);
		html += "\">Highlight Reel</a>";
	}


	html += "<br />"

	// TOP PLAYS
	if ((game.top_play_index) && (game.top_play_index[0]) && (game.top_play_index[0].urls[0])) {
		
		for (var i=0; i<game.top_play_index.length; i++) {
			if (game.top_play_index[i].team_code == clubID) {
				html += "<img src=\"http://www.mlb.com/images/icons/video.gif\" width=\"15\" height=\"12\" alt=\"\" border=\"0\" align=\"absmiddle\">&nbsp;" + game.top_play_index[i].text + "&nbsp;<a href=\"";
				html += buildMediaLink(game.top_play_index[i]);
				html += "\">350K</a><br />";
			}
		}
		
	}
	html += "<p />";
	
	// START AUDIO
	html += "<a href=\"/mediacenter/index.jsp?c_id=" + clubID +"\"><img src=\"http://www.mlb.com/images/news/bug_audio.gif\" width=\"67\" height=\"13\" border=\"0\" alt=\"Audio\" class=\"mar3tb\"></a><br />";

	// GAMEDAY AUDIO
	if ((team == "home") && (game.home_audio) && (game.home_audio.urls[0])) {
		html += "<img src=\"http://www.mlb.com/images/icons/audio_gold.gif\" width=\"15\" height=\"12\" alt=\"\" border=\"0\" align=\"absmiddle\">&nbsp;";
		html += "<a href=\"";
		html += buildMediaLink(game.home_audio);
		callLetters = game.home_audio.text;
		html += "\">" + callLetters + " - Gameday Audio</a><br />";
	} else if ((team == "away") && (game.away_audio) && (game.away_audio.urls[0])) {
		html += "<img src=\"http://www.mlb.com/images/icons/audio_gold.gif\" width=\"15\" height=\"12\" alt=\"\" border=\"0\" align=\"absmiddle\">&nbsp;";
		html += "<a href=\"";
		html += buildMediaLink(game.away_audio);
		callLetters = game.away_audio.text;
		html += "\">" + callLetters + " - Gameday Audio</a><br />";
	}	
    return html;
}

function buildClubAHtml(game,team) {

	var clubID = getClubID(game,team);
	var callLetters = "";
	
	var html = "<a href=\"/mediacenter/index.jsp?c_id=" + clubID +"\"><img src=\"http://www.mlb.com/images/news/bug_audio.gif\" alt=\"Audio\" border=\"0\" class=\"mar3tb\"></a><br />";
					
	// GAMEDAY AUDIO
	if ((team == "home") && (game.home_audio) && (game.home_audio.urls[0])) {
		html += "<img src=\"http://www.mlb.com/images/icons/audio_gold.gif\" width=\"15\" height=\"12\" alt=\"\" border=\"0\" align=\"absmiddle\">&nbsp;";
		html += "<a href=\"";
		html += buildMediaLink(game.home_audio);
		callLetters = game.home_audio.text;
		html += "\">" + callLetters + " - Gameday Audio</a><br />";
	} else if ((team == "away") && (game.away_audio) && (game.away_audio.urls[0])) {
		html += "<img src=\"http://www.mlb.com/images/icons/audio_gold.gif\" width=\"15\" height=\"12\" alt=\"\" border=\"0\" align=\"absmiddle\">&nbsp;";
		html += "<a href=\"";
		html += buildMediaLink(game.away_audio);
		callLetters = game.away_audio.text;
		html += "\">" + callLetters + " - Gameday Audio</a><br />";
	}	

    return html;
}

function buildClubVHtml(game,team) {
	
	var clubID = getClubID(game,team);
	var html = "<a href=\"/mediacenter/index.jsp?c_id=" + clubID +"\"><img src=\"http://www.mlb.com/images/news/bug_video.gif\" alt=\"video\" border=\"0\" class=\"mar3tb\"></a><br />";
	
	// MLB.TV LINK
	if ((game.mlbtv) && (game.mlbtv.urls[0]) && (game.mlbtv.state=="video_archive")) {
		html += "<a href=\"";
		html += buildMediaLink(game.mlbtv);
		html += "\">MLB.TV</a>";
	}
	
	// CONDENSED GAME LINK
	if ((game.condensed_video) && (game.condensed_video.urls[0]) && ((game.condensed_video.state=="video_on") || (game.condensed_video.state=="video_archive"))) {
		html += "&nbsp;|&nbsp;<a href=\"";
		html += buildMediaLink(game.condensed_video);
		html += "\">Condensed Game</a>";
	}

	// EXTENDED HIGHLIGHTS
	if ((game.extended_highlights) && (game.extended_highlights.urls[0]) && ((game.extended_highlights.state=="video_on") || (game.extended_highlights.state=="video_archive"))) {
		html += "&nbsp;|&nbsp;<a href=\"";
		html += buildMediaLink(game.extended_highlights);
		html += "\">Highlight Reel</a>";
	}
	
	html += "<br />"

	// TOP PLAYS
	if ((game.top_play_index) && (game.top_play_index[0]) && (game.top_play_index[0].urls[0])) {
		
		for (var i=0; i<game.top_play_index.length; i++) {
			if (game.top_play_index[i].team_code == clubID) {
				html += "Top Play:&nbsp;<a href=\"";
				html += buildMediaLink(game.top_play_index[i]);
				html += "\">350K</a>";
				break;
			}
		}
		
	}

    return html;
}

function buildClubPSVHtml(game,team) {
	
	var clubID = getClubID(game,team);
	var html = "<a href=\"/mediacenter/index.jsp?c_id=" + clubID +"\"><img src=\"http://www.mlb.com/images/news/bug_video.gif\" alt=\"video\" border=\"0\" class=\"mar3tb\"></a><br />";
	
	// MLB.TV LINK
	if ((game.mlbtv) && (game.mlbtv.urls[0]) && (game.mlbtv.state=="video_archive")) {
		html += "<a href=\"";
		html += buildMediaLink(game.mlbtv);
		html += "\">MLB.TV</a>";
	}
	
	// CONDENSED GAME LINK
	if ((game.condensed_video) && (game.condensed_video.urls[0]) && ((game.condensed_video.state=="video_on") || (game.condensed_video.state=="video_archive"))) {
		html += "&nbsp;|&nbsp;<a href=\"";
		html += buildMediaLink(game.condensed_video);
		html += "\">Condensed Game</a>";
	}

	// EXTENDED HIGHLIGHTS
	if ((game.extended_highlights) && (game.extended_highlights.urls[0]) && ((game.extended_highlights.state=="video_on") || (game.extended_highlights.state=="video_archive"))) {
		html += "&nbsp;|&nbsp;<a href=\"";
		html += buildMediaLink(game.extended_highlights);
		html += "\">Highlight Reel</a>";
	}
	
	html += "<br />"
	/*
	// TOP PLAYS
	if ((game.top_play_index) && (game.top_play_index[0]) && (game.top_play_index[0].urls[0])) {
		
		for (var i=0; i<game.top_play_index.length; i++) {
			if (game.top_play_index[i].team_code == clubID) {
				html += "<img src=\"http://www.mlb.com/images/icons/video.gif\" width=\"15\" height=\"12\" alt=\"\" border=\"0\" align=\"absmiddle\">&nbsp;" + game.top_play_index[i].text + "&nbsp;<a href=\"";
				html += buildMediaLink(game.top_play_index[i]);
				html += "\">350K</a><br />";
			}
		}
		
	}
	*/

    return html;
}


function buildWrapAV(data, game_id, team, view) {
	var html = "";
	
	// cycle through the games in the Gamesbydate file
    for (var i=0; i<data.length; i++) {
	
		// look for the matching game_id
		if (game_id == (data[i].gameid)) {
			var g = i;
			
			if (view == "club-video") {
			
				html += buildClubVHtml(data[g],team);
				
			} else if (view == "club-audio") {
			
				html += buildClubAHtml(data[g],team);
				
			} else if (view == "mlb") {
			
				html += buildMLBHtml(data[g],team);
				
			} else {
			
				html += "";
				
			}		
		}
    }
    return html;
}


function buildPSWrapAV(data, game_id, team, view) {
	var html = "";
	
	// cycle through the games in the Gamesbydate file
    for (var i=0; i<data.length; i++) {
	
		// look for the matching game_id
		if (game_id == (data[i].gameid)) {
			var g = i;
			
			if (view == "club-video") {
			
				html += buildClubPSVHtml(data[g],team);
				
			} else if (view == "club-audio") {
			
				html += buildClubAHtml(data[g],team);
				
			} else if (view == "mlb") {
			
				html += buildMLBPSHtml(data[g],team);
				
			} else {
			
				html += "";
				
			}		
		}
    }
    return html;
}

