//----------------------------------------------------------------------------------
var team = new Array();
var team_url = '';
var events = new Array();
var roster = new Array();
var coaches = new Array();
var teams = new Array();
//----------------------------------------------------------------------------------
function show_team(url)
{
	team_url = url;
	var html_body = document.getElementById('rt-mainbody');

	var action = get_action();
	var team_id = parseInt(get_team_id());

	var title = '<span>TKA</span> Teams';
	var content = '<div style="width: 100%; text-align: center; margin-top: 30px; margin-bottom: 20px;" id="team_loading"><img src="http://www.tkalions.net/images/loading.gif"/><br/><b>Percolating Team...</b></div>';

	// http://tkalions.net/index.php?option=com_content&view=article&id=139&teamid=3&sportsdbdisplay=schedule

	html_body.innerHTML = build_team_section(content, title);

	get_team_info(team_id);

	wait(action);
}
//----------------------------------------------------------------------------------
function get_team_info(id)
{
	if(isNaN(id)) return;

	var e = document.createElement('script');
	e.src = 'http://www.tkalions.net/scripts/sportsdb_teams/get_team.php?id=' + id;
	e.type = 'text/javascript';
	document.getElementById('sportsdb_teams').appendChild(e);
}
//----------------------------------------------------------------------------------
function get_action()
{
	for(i = team_url.length; i >= 0; i--)
	{
		if(team_url.substr(i, 1) == '=')
		{
			if(team_url.substr(i - 15, 15) == 'sportsdbdisplay') 
			{
				var temp = team_url.substr(i + 1, team_url.length);
				team_url = team_url.substr(0, i - 16);
				return temp;
			}
		}
	}

	return '';
}
//----------------------------------------------------------------------------------
function build_team_section(content, title)
{
	var html = ' \
		<div class="rt-joomla">\
			<div class="module-content">\
				<div class="module-tm">\
					<div class="module-tl">\
						<div class="module-tr"></div>\
					</div>\
				</div>\
				<div class="module-l">\
					<div class="module-r">\
						<div class="rt-headline">\
							<div class="module-title">\
								<div class="module-title2">\
									<h1 class="title" style="visibility: visible;" id="sportsdb_team_title">';
	html += title + '				</h1>\
								</div>\
							</div>\
						</div>\
						<div class="clear">\
						</div>\
						<div class="module-inner">\
							<div class="module-inner2">\
								<div id="sportsdb_teams">';
	html += content + '			</div>\
							</div>\
						</div>\
					</div>\
				</div>\
				<div class="module-bm">\
					<div class="module-bl">\
						<div class="module-br"></div>\
					</div>\
				</div>\
			</div>\
		</div>';

	return html;
}
//----------------------------------------------------------------------------------
function get_team_id()
{
	for(i = team_url.length; i >= 0; i--)
	{
		if(team_url.substr(i, 1) == '=')
		{
			if(team_url.substr(i - 6, 6) == 'teamid') return team_url.substr(i + 1, team_url.length);
			else return '';
		}
	}
}
//----------------------------------------------------------------------------------
function wait(action)
{
	if(team.length == 0) setTimeout(function(){wait(action)}, 0);
	else
	{
		document.getElementById('team_loading').innerHTML = '';

		var html = '<div style="text-align: center; width: 100%; margin-bottom: 20px; margin-top: 10px;">';
		html += '<b style="text-decoration: underline; border-right: 1px solid black; padding-right: 10px; cursor: pointer;" onMouseOver="highlight(this);" onMouseOut="clear_highlight(this);" onClick="clear_vars(); show_team(\'' + (team_url + '&sportsdbdisplay=schedule') + '\');">Schedule</b>'
		html += '<b style="text-decoration: underline; border-right: 1px solid black; padding-right: 10px; cursor: pointer; padding-left: 10px;" onMouseOver="highlight(this);" onMouseOut="clear_highlight(this);" onClick="clear_vars(); show_team(\'' + (team_url + '&sportsdbdisplay=roster') + '\');">Roster</b>'
		html += '<b style="text-decoration: underline; cursor: pointer; padding-left: 10px;" onMouseOver="highlight(this);" onMouseOut="clear_highlight(this);" onClick="clear_vars(); show_team(\'' + (team_url + '&sportsdbdisplay=coaches') + '\');">Coach Info</b>'
		html += '</div>';
		html += '<div style="text-align: center;">Jump to: <select style="cursor: pointer;" id="team_select" onChange="show_new_team(this);">';

		html += generate_team_select();

		html += '</select></div>';
		document.getElementById('sportsdb_teams').innerHTML = html;
		if(action == 'schedule') display_schedule();
		else if(action == 'roster') display_roster();
		else if(action == 'coaches') display_coaches();
	}
}
//----------------------------------------------------------------------------------
function show_new_team(e)
{
	clear_vars();
	var id = e.value;
	var url = 'http://tkalions.net/index.php?option=com_content&view=article&id=139&teamid=' + id + '&sportsdbdisplay=schedule';
	show_team(url);
}
//----------------------------------------------------------------------------------
function generate_team_select()
{
	html = '';
	for(i = 0; i < teams.length; i++)
	{
		if(teams[i][0] == team[0]) html += '<option selected="true" value="' + teams[i][1] + '">' + teams[i][0] + '</option>';
		else html += '<option value="' + teams[i][1] + '">' + teams[i][0] + '</option>';
	}
	return html;
}
//----------------------------------------------------------------------------------
function clear_vars()
{
	team = new Array();
	events = new Array();
	roster = new Array();
	coaches = new Array();
	teams = new Array();
}
//----------------------------------------------------------------------------------
function display_coaches()
{
	var html = '';

	html += '<hr>';

	html += '<h4>Coaches for ' + team[0] + ':</h4>';

	if(coaches.length >= 1)
	{
		/*
			0 = first
			1 = last
			2 = bio
		*/

		for(i = 0; i < coaches.length; i++)
		{
			html += '<b style="color: #AB0C14;">' + coaches[i][0] + ' ' + coaches[i][1] + '</b><br/><br/>' + coaches[i][2] + '<br/><br/>';
		}
	}
	else
	{
		html += 'No coaches found for this team.';
	}

	document.getElementById('sportsdb_teams').innerHTML += html;
}
//----------------------------------------------------------------------------------
function display_roster()
{
	var html = '';

	html += '<hr>';

	html += '<h4>Roster for ' + team[0] + ':</h4>';

	if(roster.length >= 1)
	{
		/*
			0 = first name
			1 = last name
			2 = grade
			3 = position
			4 = number
		*/

		html += '<div style="margin-left: auto; margin-right: auto; width: 300px; border: 1px solid #BFBFBF; padding: 10px; margin-bottom: 20px;">';
		for(i = 0; i < roster.length; i++)
		{
			//HOTFIX APPLIED
			if(parseInt(roster[i][4], 10) === 0 || roster[i][4].replace(/^\s+|\s+$/g,"") === ''){
				if(roster[i][3] === '0' || roster[i][3].replace(/^\s+|\s+$/g,"") === ''){
					html += '<div style="display: inline; float: left;"><b style="color: #AB0C14;">' + roster[i][1] + ', ' + roster[i][0] + '</b></div><div style="float: right; display: inline;">' + roster[i][2] + '</div><div style="clear: both;"></div>';
				}
				else{
					html += '<div style="display: inline; float: left;"><b style="color: #AB0C14;">' + roster[i][1] + ', ' + roster[i][0] + '</b> (' + roster[i][3] + ')</div><div style="float: right; display: inline;">' + roster[i][2] + '</div><div style="clear: both;"></div>';
				}
			}
			else{
				if(roster[i][3] === '0' || roster[i][3].replace(/^\s+|\s+$/g,"") === ''){
					html += '<div style="display: inline; float: left;">#' + roster[i][4] + ' <b style="color: #AB0C14;">' + roster[i][1] + ', ' + roster[i][0] + '</b></div><div style="float: right; display: inline;">' + roster[i][2] + '</div><div style="clear: both;"></div>';
				}
				else{
					html += '<div style="display: inline; float: left;">#' + roster[i][4] + ' <b style="color: #AB0C14;">' + roster[i][1] + ', ' + roster[i][0] + '</b> (' + roster[i][3] + ')</div><div style="float: right; display: inline;">' + roster[i][2] + '</div><div style="clear: both;"></div>';
				}
			}
		}
		html += '</div>';

	}
	else
	{
		html += 'No players found for this team.';
	}

	document.getElementById('sportsdb_teams').innerHTML += html;	
}
//----------------------------------------------------------------------------------
function display_schedule()
{	
	var html = '';

	html += '<hr>';

	html += '<h4>Schedule for ' + team[0] + ':</h4>';

	if(events.length >= 1)
	{
		/*
			0 = date
			1 = time
			2 = advantage
			3 = opponent
			4 = location
			5 = game result
			6 = our score
			7 = opposing score
			8 = rescheduled
			9 = rescheduled date
			10 = cancelled
			11 = opponent id
		*/

		for(i = 0; i < events.length; i++)
		{
			if(i % 2 == 0)
			{
				html += '<div style="width: 550px; border: 1px solid #BFBFBF; background-color: #f3efe4; position: relative; color: black;">';
				html += '<div style="height: 20px; width: 545px; background-color: #f3efe4; color: black; font-weight: normal; padding-left: 5px;">' + events[i][0] + '</div>';
			}
			else 
			{
				html += '<div style="width: 550px; border: 1px solid #BFBFBF; background-color: #fdfaf2; position: relative; color: black;">';
				html += '<div style="height: 20px; width: 545px; background-color: #fdfaf2; color: black; font-weight: normal; padding-left: 5px;">' + events[i][0] + '</div>';
			}
			html += '<div style="padding-left: 5px; display: inline;">vs.</div> <b style="color: #095075; cursor: pointer; text-decoration: underline;" onMouseOver="highlight(this);" onMouseOut="clear_highlight(this);" onClick="load_opponent(' + events[i][11] + ')">' + events[i][3] + '</b> @ ' + events[i][4] + ' (' + events[i][2] + ') ' + events[i][1] + '<br/>';
			if(events[i][5] != 'Pending') 
			{
				html += '<div style="position: absolute; right: 5px; top: 20px; color: #AB0C14; font-weight: bolder;">' + events[i][5] + ' ';
				html += events[i][6] + '-' + events[i][7];
				html += '</div>';
			}
			if(events[i][10] == 'Y') html += '<div style="padding-left: 5px; display: inline; color: #AB0C14; font-weight: bolder;">Game Cancelled</div>';
			else if(events[i][8] == 'Y') html += '<div style="padding-left: 5px; display: inline; color: #AB0C14; font-weight: bolder;">Rescheduled to: ' + events[i][9] + '</div>';
			html += '</div>';
		}
	}
	else
	{
		html += 'No events found for this team.';
	}

	html += '<div style="width: 100%; margin-top: 10px;"></div>';

	document.getElementById('sportsdb_teams').innerHTML += html;
}
//----------------------------------------------------------------------------------
function load_opponent(id)
{
	location.href = 'http://tkalions.net/index.php?option=com_content&view=article&id=140&opponentid=' + id;
}
//----------------------------------------------------------------------------------
function highlight(e)
{
	e.style.color = '#AB0C14';
}
//----------------------------------------------------------------------------------
function clear_highlight(e)
{
	e.style.color = '#095075';
}
//----------------------------------------------------------------------------------
