//----------------------------------------------------------------------------------
var opponent = new Array();
var opponent_locations = new Array();
var events = new Array();
var opponent_list = new Array();
//----------------------------------------------------------------------------------
function show_opponent(url)
{
	var html_body = document.getElementById('rt-mainbody');

	event_id = get_opponent_id(url);

	var title = '<span>Athletic</span> Opponent';
	var content = '<div style="width: 100%; text-align: center; margin-top: 30px; margin-bottom: 20px;" id="opponent_loading"><img src="http://www.tkalions.net/images/loading.gif"/><br/><b>Percolating Opponent...</b></div>';

	html_body.innerHTML = build_opponent_section(content, title);

	get_opponent(event_id);

	wait();
}
//----------------------------------------------------------------------------------
function build_opponent_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_events_title">';
	html += title + '				</h1>\
								</div>\
							</div>\
						</div>\
						<div class="clear">\
						</div>\
						<div class="module-inner">\
							<div class="module-inner2">\
								<div id="sportsdb_opponent">';
	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_opponent(id)
{
	var e = document.createElement('script');
	e.src = 'http://www.tkalions.net/scripts/sportsdb_opponents/get_opponent.php?id=' + id;
	e.type = 'text/javascript';
	document.getElementById('sportsdb_opponent').appendChild(e);
}
//----------------------------------------------------------------------------------
function get_opponent_id(url)
{
	for(i = url.length; i >= 0; i--)
	{
		if(url.substr(i, 1) == '=')
		{
			if(url.substr(i - 10, 10) == 'opponentid') return url.substr(i + 1, url.length);
			else return '';
		}
	}
}
//----------------------------------------------------------------------------------
function wait()
{
	if(opponent.length <= 0) setTimeout(wait, 0);
	else
	{
		//document.getElementById('opponent_loading').innerHTML = '';
		display_opponent();
	}
}
//----------------------------------------------------------------------------------
function display_opponent()
{

	//http://tkalions.net/index.php?option=com_content&view=article&id=140&opponentid=35

	var html = '';

	/*
		OPPONENT
		0 = school name

		LOCATIONS
		0 = location
		1 = location id
		2 = city
		3 = state
		4 = zip
		5 = street
		6 = site link
		7 = driving directions
		8 = phone number

		EVENTS
		0 = team name
		1 = time
		2 = advantage
		3 = date
		4 = game result
		5 = opposing score
		6 = our score
		7 = team id
	*/

	html += '<div style="margin-top: 20px;"></div>';
	html += '<div style="font-size: 16px; font-weight: normal; margin-bottom: 10px; float: left;"><b style="margin-right: 10px;">' + opponent[0] + '</b></div>';
	html += '<div style="float: right;">Jump To: ';
	html += generate_opponents_select();
	html += '</div>';
	html += '<div style="clear: both;"></div><hr style="margin-top: 20px;">';

	if(opponent_locations.length >= 1)
	{
		html += 'Please select a location from below to load the directions to it:<br/>';
		html += generate_location_select();
	}
	else
	{
		html += 'There are no locations in the database for this opponent yet.';
	}

	html += '<hr>';

	//SHOW ALL events against this opponent
	if(events.length >= 1)
	{
		html += '<h4>Events against ' + opponent[0] + ':</h4>';
		for(i = 0; i < events.length; i++)
		{
			if(i % 2 == 0)
			{
				html += '<div style="width: 500px; border: 1px solid #BFBFBF; background-color: #f3efe4; position: relative; color: black;">';
				html += '<div style="height: 20px; width: 495px; background-color: #f3efe4; font-weight: normal; padding-left: 5px;">' + events[i][3] + '</div>';
			}
			else
			{
				html += '<div style="width: 500px; border: 1px solid #BFBFBF; background-color: #fdfaf2; position: relative; color: black;">';
				html += '<div style="height: 20px; width: 495px; background-color: #fdfaf2; font-weight: normal; padding-left: 5px;">' + events[i][3] + '</div>';
			}
			html += '<b style="padding-left: 5px; font-weight: bolder; cursor: pointer; color: #095075; text-decoration: underline;" onMouseOver="highlight(this);" onMouseOut="clear_highlight(this);" onClick="load_team(' + events[i][7] + ');">' + events[i][0] + '</b> vs. <b>' + opponent[0] + '</b> (' + events[i][2] + ') ' + events[i][1];

			if(events[i][4] != 'Pending')
			{
				html += '<div style="position: absolute; right: 5px; top: 20px; color: #AB0C14; font-weight: bolder;">' + events[i][4] + ' ';
				if(events[i][5] > events[i][6]) html += events[i][5] + ' - ' + events[i][6];
				else html += events[i][6] + ' - ' + events[i][5];
				html += '</div>';
			}

			html += '</div>';
		}
	}
	else
	{
		html += 'No games were found against this opponent.';
	}

	html += '<div style="height: 1px; width: 100%; margin-top: 20px;"></div>';

	document.getElementById('sportsdb_opponent').innerHTML = html;

	if(document.getElementById('opponent_location')) generate_directions(document.getElementById('opponent_location').value);
}
//----------------------------------------------------------------------------------
function load_team(id)
{
	var url = 'http://tkalions.net/index.php?option=com_content&view=article&id=139&teamid=' + id + '&sportsdbdisplay=schedule';
	location.href = url;
}
//----------------------------------------------------------------------------------
function generate_location_select()
{
	var html = '';
	html += '<select id="opponent_location" style="cursor: pointer;" onChange="generate_directions(parseInt(this.value));">';

	for(i = 0; i < opponent_locations.length; i++)
	{
		html += '<option value="' + i + '">' + opponent_locations[i][0] + '</option>';
	}

	html += '</select><br/><div id="opponent_directions"></div>';

	return html;
}
//----------------------------------------------------------------------------------
function generate_opponents_select()
{
	var html = '';

	if(opponent_list.length == 0) return '';

	html += '<select style="cursor: pointer;" onChange="load_opponent(this);">';

	for(i = 0; i < opponent_list.length; i++)
	{
		if(opponent_list[i][1] == opponent[0]) html += '<option selected="true" value="' + opponent_list[i][0] + '">' + opponent_list[i][1] + '</option>';
		else html += '<option value="' + opponent_list[i][0] + '">' + opponent_list[i][1] + '</option>';
	}

	html += '</select>';

	return html;
}
//----------------------------------------------------------------------------------
function load_opponent(e)
{
	var id = e.value;
	document.getElementById('sportsdb_opponent').innerHTML = '';
	opponent = new Array;
	opponent_locations = new Array();
	events = new Array();
	opponent_list = new Array();
	
	show_opponent('http://tkalions.net/index.php?option=com_content&view=article&id=140&opponentid=' + id);
}
//----------------------------------------------------------------------------------
function generate_directions(id)
{

	/*
		LOCATIONS
		0 = location
		1 = location id
		2 = city
		3 = state
		4 = zip
		5 = street
		6 = site link
		7 = driving directions
		8 = phone number
	*/

	var html = '';
	if(opponent_locations[id][2] != '' && opponent_locations[id][3] != '' && opponent_locations[id][4] != '' && opponent_locations[id][5] != '')
	{
		html += '<h4 style="margin-bottom: 10px;">Directions to ' + opponent_locations[id][0] + ':</h4>';
		html += opponent_locations[id][5] + '<br/>';
		html += opponent_locations[id][2] + ', ' + opponent_locations[id][3] + ' ' + opponent_locations[id][4] + '<br/>';
		html += opponent_locations[id][8] + '<br/>';
		if(opponent_locations[id][6] != '') html += '<a href="' + opponent_locations[id][6] + '">' + opponent_locations[id][6] + '</a>';
		html += '<div style="height: 1px; width: 100%; margin-top: 20px;"></div>';

		if(opponent_locations[id][7] != '')  html += opponent_locations[id][7] + '<br/>';

		html += '<div style="height: 1px; width: 100%; margin-top: 20px;"></div>';
		html += '<div id="opponent_google_map" style="height: 400px; width: 600px;"></div>';

		html += '<div style="height: 1px; width: 100%; margin-top: 20px;"></div>';

		document.getElementById("opponent_directions").innerHTML = html;
		generate_google_map(id);

	}
	else 
	{
		html += 'The directions for this locations are missing or still need to be configured.<br/>Please check back at a later time.';
		document.getElementById("opponent_directions").innerHTML = html;
	}
}
//----------------------------------------------------------------------------------
function generate_google_map(id)
{
	// GOOGLE MAPS API = ABQIAAAAOB8o7R9R1bmu248ItjsAkRRDVLM84YTHMfN88-uxgvJSzbpt7RQ8BMZqKwCyaST22t6Sfx0uvwjXSg
	var map = null;
	var geocoder = null;
	var address = opponent_locations[id][5] + ', ' + opponent_locations[id][2] + ', ' + opponent_locations[id][3] + ', ' + opponent_locations[id][4];

	var html = '<b>' + opponent_locations[id][0] + '</b><br/>' + opponent_locations[id][5] + '<br/>' + opponent_locations[id][2] + ', ' + opponent_locations[id][3] + ', ' + opponent_locations[id][4] + '<br/>' + opponent_locations[id][8];

	if(GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("opponent_google_map"));
		geocoder = new GClientGeocoder();

		if(geocoder)
		{
			geocoder.getLatLng(address, function(point)
			{
				if(!point)
				{
					//address wasnt found
					document.getElementById('opponent_google_map').innerHTML = '';
				}
				else
				{
					map.setCenter(point, 15);
					var marker = new GMarker(point, {draggable: true});
					map.addOverlay(marker);
					GEvent.addListener(marker, "dragend", function(){ marker.openInfoWindowHtml(html); });
					GEvent.addListener(marker, "click", function(){ marker.openInfoWindowHtml(html); });
					GEvent.trigger(marker, "click");
				}
			});
		}

		map.setUIToDefault();
	}
}
//----------------------------------------------------------------------------------
function highlight(e)
{
	e.style.color = '#AB0C14';
}
//----------------------------------------------------------------------------------
function clear_highlight(e)
{
	e.style.color = '#095075';
}
//----------------------------------------------------------------------------------
