/**
	 * Show the special page
	 *
	 * @param $par Mixed: parameter passed to the special page or null
	 */
	public function execute( $par ) {
		global $wgUser, $wgOut, $wgRequest, $wgScriptPath, $wgUploadPath;

		$output = '';

		/**
		 * Get query string variables
		 */
		$page = $wgRequest->getInt( 'page', 1 );
		$sport_id = $wgRequest->getVal( 'sport_id' );
		$team_id = $wgRequest->getVal( 'team_id' );

		/**
		 * Error message for teams/sports that do not exist (from URL)
		 */
		if( !$team_id && !$sport_id ) {
			$wgOut->setPageTitle( wfMsg( 'sportsteams-network-woops-title' ) );
			$out = '<div class="relationship-request-message">' .
				wfMsg( 'sportsteams-network-woops-text' ) . '</div>';
			$out .= '<div class="relationship-request-buttons">';
			$out .= '<input type="button" class="site-button" value="' .
				wfMsg( 'sportsteams-network-main-page' ) .
				"\" onclick=\"window.location='" .
				Title::newMainPage()->escapeFullURL() . "'\"/>";
			if ( $wgUser->isLoggedIn() ) {
				$out .= ' <input type="button" class="site-button" value="' .
					wfMsg( 'sportsteams-network-your-profile' ) .
					"\" onclick=\"window.location='" .
					Title::makeTitle( NS_USER, $wgUser->getName() )->escapeFullURL() . "'\"/>";
			}
		  	$out .= '</div>';
			$wgOut->addHTML( $out );
			return false;
		}

		// Add CSS
		if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
			$wgOut->addModuleStyles( 'ext.sportsTeams' );
		} else {
			$wgOut->addExtensionStyle( $wgScriptPath . '/extensions/SportsTeams/SportsTeams.css' );
		}

		$relationships = array();
		$friends = array();
		$foes = array();
		if( $wgUser->isLoggedIn() ) {
			$friends = $this->getRelationships( 1 );
			$foes = $this->getRelationships( 2 );
			$relationships = array_merge( $friends, $foes );
		}

		/**
		 * Set up config for page / default values
		 */
		$per_page = 50;
		$per_row = 2;

		if( $team_id ) {
			$team = SportsTeams::getTeam( $team_id );
			$this->network = $team['name'];
			$team_image = "<img src=\"{$wgUploadPath}/team_logos/" .
				SportsTeams::getTeamLogo( $team_id, 'l' ) .
				'" border="0" alt="' . wfMsg( 'sportsteams-network-alt-logo' ) . '" />';
		} else {
			$sport = SportsTeams::getSport( $sport_id );
			$this->network = $team['name'];
			$team_image = "<img src=\"{$wgUploadPath}/team_logos/" .
				SportsTeams::getSportLogo( $sport_id, 'l' ) .
				'" border="0" alt="' . wfMsg( 'sportsteams-network-alt-logo' ) . '" />';
		}
		$homepage_title = SpecialPage::getTitleFor( 'FanHome' );

		$total = SportsTeams::getUserCount( $sport_id, $team_id );

		/* Get all fans */
		$fans = SportsTeams::getUsersByFavorite(
			$sport_id, $team_id, $per_page, $page
		);

		$wgOut->setPageTitle( wfMsg( 'st-network-network-fans', $this->network ) );

		$output .= '<div class="friend-links">';
		$output .= "<a href=\"". $homepage_title->getFullURL(
			"sport_id={$sport_id}&team_id={$team_id}"
		) . '">' . wfMsg( 'sportsteams-network-back-to-network', $this->network ) . '</a>';
		$output .= '</div>';

		/* Show total fan count */
		$output .= '<div class="friend-message">' .
			wfMsgExt(
				'sportsteams-network-num-fans',
				'parsemag',
				$this->network,
				$total,
				SpecialPage::getTitleFor( 'InviteContacts' )->escapeFullURL()
			);
		$output .= '</div>';

		if( $fans ) {
			$x = 1;

			foreach ( $fans as $fan ) {
				$user = Title::makeTitle( NS_USER, $fan['user_name'] );
				$avatar = new wAvatar( $fan['user_id'], 'l' );
				$avatar_img = $avatar->getAvatarURL();

				$output .= "<div class=\"relationship-item\">
						<div class=\"relationship-image\"><a href=\"{$user->getFullURL()}\">{$avatar_img}</a></div>
						<div class=\"relationship-info\">
						<div class=\"relationship-name\">
							<a href=\"{$user->getFullURL()}\">{$fan['user_name']}</a>";

				$output .= '</div>
					<div class="relationship-actions">';
				if( in_array( $fan['user_id'], $friends ) ) {
					$output .= '	<span class="profile-on">' . wfMsg( 'sportsteams-your-friend' ) . '</span> ';
				}
				if( in_array( $fan['user_id'], $foes ) ) {
					$output .= '	<span class="profile-on">' . wfMsg( 'sportsteams-your-foe' ) . '</span> ';
				}
				if( $fan['user_name'] != $wgUser->getName() ) {
					if( !in_array( $fan['user_id'], $relationships ) ) {
						$ar = SpecialPage::getTitleFor( 'AddRelationship' );
						$output .= '<a href="' . $ar->escapeFullURL( "user={$fan['user_name']}&rel_type=1" ) . '">' .
							wfMsg( 'sportsteams-add-as-friend' ) . '</a> | ';
						$output .= '<a href="' . $ar->escapeFullURL( "user={$fan['user_name']}&rel_type=2" ) . '">' .
							wfMsg( 'sportsteams-add-as-foe' ) . '</a> | ';
					}
					$output .= '<a href="' . SpecialPage::getTitleFor( 'GiveGift' )->escapeFullURL( "user={$fan['user_name']}" ) . '">' .
						wfMsg( 'sportsteams-give_a_gift' ) . '</a> ';
					//$output .= "<p class=\"relationship-link\"><a href=\"index.php?title=Special:ChallengeUser&user={$fan['user_name']}\"><img src=\"images/common/challengeIcon.png\" border=\"0\" alt=\"issue challenge\"/> issue challenge</a></p>";
					$output .= '<div class="cleared"></div>';
				}
				$output .= '</div>';

				$output .= '<div class="cleared"></div></div>';

				$output .= '</div>';
				if( $x == count( $fans ) || $x != 1 && $x % $per_row == 0 ) {
					$output .= '<div class="cleared"></div>';
				}
				$x++;
			}
		}

		/**
		 * Build next/prev navigation
		 */
		$numofpages = $total / $per_page;

		if ( $numofpages > 1 ) {
			$output .= '<div class="page-nav">';
			if( $page > 1 ) {
				$output .= '<a href="' . $this->getTitle()->escapeFullURL(
					'page=' . ( $page - 1 ) . "&sport_id={$sport_id}&team_id={$team_id}"
				) . '">' . wfMsg( 'sportsteams-prev' ) . '</a> ';
			}

			if( ( $total % $per_page ) != 0 ) {
				$numofpages++;
			}
			if( $numofpages >= 9 ) {
				$numofpages = 9 + $page;
			}

			for( $i = 1; $i <= $numofpages; $i++ ) {
				if( $i == $page ) {
				    $output .= ( $i . ' ' );
				} else {
				    $output .= '<a href="' . $this->getTitle()->escapeFullURL(
						'page=' . ( $i ) . "&sport_id={$sport_id}&team_id={$team_id}"
					) . "\">$i</a> ";
				}
			}

			if( ( $total - ( $per_page * $page ) ) > 0 ) {
				$output .= ' <a href="' . $this->getTitle()->escapeFullURL(
					'page=' . ( $page + 1 ) . "&sport_id={$sport_id}&team_id={$team_id}"
				) . '">' . wfMsg( 'sportsteams-next' ) . '</a>';
			}
			$output .= '</div>';
		}

		$wgOut->addHTML( $output );
	}
	function displayForm( $id ) {
		global $wgUser, $wgRequest, $wgUploadPath;

		$form = '<div><b><a href="' . $this->getTitle()->escapeFullURL( 'sport_id=' . $wgRequest->getInt( 'sport_id' ) ) . '">' .
			wfMsg( 'sportsteams-team-manager-view-teams' ) . '</a></b></div><p>';

		if( $id ) {
			$team = SportsTeams::getTeam( $id );
		} else {
			$team = array( 'id' => '', 'name' => '' ); // prevent notices
		}

		// @todo FIXME: rename the form from gift to something else and update line 225 accordingly
		$form .= '<form action="" method="post" enctype="multipart/form-data" name="gift">';

		$form .= '<table border="0" cellpadding="5" cellspacing="0" width="500">';

		$form .= '

			<tr>
			<td width="200" class="view-form">' . wfMsg( 'sportsteams-team-manager-sport' ) . '</td>
			<td width="695">
				<select name="s_id">';
		$sports = SportsTeams::getSports();
		foreach( $sports as $sport ) {
			$selected = '';
			if (
				$wgRequest->getInt( 'sport_id' ) == $sport['id'] ||
				$sport['id'] == $team['sport_id']
			)
			{
				$selected = ' selected';
			}
			$form .= "<option{$selected} value=\"{$sport['id']}\">{$sport['name']}</option>";
		}
		$form .= '</select>

			</tr>
			<tr>
				<td width="200" class="view-form">' .
					wfMsg( 'sportsteams-team-manager-teamname' ) .
				'</td>
				<td width="695">
					<input type="text" size="45" class="createbox" name="team_name" value="' . $team['name'] . '" />
				</td>
			</tr>
			';

		if( $id ) {
			$team_image = "<img src=\"{$wgUploadPath}/team_logos/" .
				SportsTeams::getTeamLogo( $id, 'l' ) .
				'" border="0" alt="logo" />';
			$form .= '<tr>
					<td width="200" class="view-form" valign="top">' .
						wfMsg( 'sportsteams-team-manager-team' ) .
					'</td>
					<td width="695">' . $team_image . '
						<p>
						<a href="' . SpecialPage::getTitleFor( 'SportsTeamsManagerLogo' )->escapeFullURL( "id={$id}" ) . '">' .
							wfMsg( 'sportsteams-team-manager-add-replace-logo' ) .
						'</a>
					</td>
				</tr>';
			}

		if ( $id ) {
			$msg = wfMsg( 'sportsteams-team-manager-edit' );
		} else {
			$msg = wfMsg( 'sportsteams-team-manager-add-team' );
		}

		$form .= '<tr>
				<td colspan="2">
					<input type="hidden" name="id" value="' . $id . '" />
					<input type="button" class="site-button" value="' . $msg . '" size="20" onclick="document.gift.submit()" />
					<input type="button" class="site-button" value="' . wfMsg( 'cancel' ) . '" size="20" onclick="history.go(-1)" />
				</td>
			</tr>
		</table>

		</form>';
		return $form;
	}
	/**
	 * Get the full <img> tag for the given sport team's logo image.
	 *
	 * @param $sport_id Integer: sport ID number
	 * @param $team_id Integer: team ID number, 0 by default
	 * @param $size String: 's' for small, 'm' for medium, 'ml' for
	 *                      medium-large and 'l' for large
	 * @return String: full <img> tag
	 */
	static function getLogo( $sport_id, $team_id = 0, $size ) {
		global $wgUploadPath;

		if( $sport_id > 0 && $team_id == 0 ) {
			$logoTag = '<img src="' . $wgUploadPath . '/sport_logos/' .
				SportsTeams::getSportLogo( $sport_id, $size ) .
				'" border="0" alt="" />';
		} else {
			$logoTag = '<img src="' . $wgUploadPath . '/team_logos/' .
				SportsTeams::getTeamLogo( $team_id, $size ) .
				'" border="0" alt="" />';
		}

		return $logoTag;
	}
	/**
	 * Show the special page
	 *
	 * @param $par Mixed: parameter passed to the special page or null
	 */
	public function execute( $par ) {
		global $wgOut, $wgRequest, $wgScriptPath, $wgUploadPath;

		// Variables
		$output = '';
		$direction = $wgRequest->getVal( 'direction' );
		$type = $wgRequest->getVal( 'type' );
		$sport = $wgRequest->getInt( 'sport' );

		// Direction
		if ( $direction == 'worst' ) {
			$order = 'ASC';
			$adj = 'least';
		} else {
			$order = 'DESC';
			$adj = 'most';
		}

		// Type
		if ( $type == 'sport' ) {
			$type_title = 'sports';
		} else {
			$type_title = 'teams';
		}

		// Sport
		$where = array();
		if ( $sport ) {
			$where['team_sport_id'] = $sport;
		}

		// Set the page title
		// For grep: sportsteams-top-network-team-title-least-sports,
		// sportsteams-top-network-team-title-least-teams,
		// sportsteams-top-network-team-title-most-sports,
		// sportsteams-top-network-team-title-most-teams
		$wgOut->setPageTitle( wfMsg( 'sportsteams-top-network-team-title-' . $adj . '-' . $type_title ) );

		// Add CSS
		if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
			$wgOut->addModuleStyles( 'ext.sportsTeams' );
		} else {
			$wgOut->addExtensionStyle( $wgScriptPath . '/extensions/SportsTeams/SportsTeams.css' );
		}

		// Database handler
		$dbr = wfGetDB( DB_MASTER );

		// Teams
		$res = $dbr->select(
			array( 'sport_favorite', 'sport_team' ),
			array(
				'COUNT(sf_team_id) AS network_user_count',
				'sf_team_id', 'team_name', 'team_sport_id'
			),
			$where,
			__METHOD__,
			array(
				'GROUP BY' => 'team_id',
				'ORDER BY' => "network_user_count {$order}",
				'LIMIT' => 50
			),
			array(
				'sport_team' => array( 'INNER JOIN', 'sf_team_id = team_id' )
			)
		);

		// Sports
		$res_sport = $dbr->select(
			array( 'sport_favorite', 'sport' ),
			array(
				'COUNT(sf_sport_id) AS sport_count', 'sf_sport_id',
				'sport_name'
			),
			array(),
			__METHOD__,
			array(
				'GROUP BY' => 'sf_sport_id',
				'ORDER BY' => "sport_count {$order}",
				'LIMIT' => 50
			),
			array( 'sport' => array( 'INNER JOIN', 'sf_sport_id = sport_id' ) )
		);

		// Navigation
		$res_sport_nav = $dbr->select(
			array( 'sport', 'sport_team' ),
			array( 'sport_id', 'sport_name', 'team_sport_id' ),
			array(),
			__METHOD__,
			array(
				'GROUP BY' => 'sport_name',
				'ORDER BY' => 'sport_id'
			),
			array(
				'sport_team' => array( 'INNER JOIN', 'sport_id = team_sport_id' )
			)
		);

		// Navigation
		$output .= '<div class="top-networks-navigation">
			<h1>' . wfMsg( 'sportsteams-top-network-most-popular' ) . '</h1>';

		if ( !( $sport ) && !( $type ) && !( $direction ) ) {
			$output .= '<p><b>' . wfMsg( 'sportsteams-top-network-teams' ) . '</b></p>';
		} elseif ( !( $sport ) && !( $type ) && ( $direction == 'best' ) ) {
			$output .= '<p><b>' . wfMsg( 'sportsteams-top-network-teams' ) . '</b></p>';
		} else {
			$output .= '<p><a href="' . $this->getTitle()->escapeFullURL(
				array( 'direction' => 'best' )
			) . '">' . wfMsg( 'sportsteams-top-network-teams' ) . '</a></p>';
		}

		if ( !( $sport ) && ( $type == 'sport' ) && ( $direction == 'best' ) ) {
			$output .= '<p><b>' . wfMsg( 'sportsteams-top-network-sports' ) . '</b></p>';
		} else {
			$output .= '<p><a href="' . $this->getTitle()->escapeFullURL(
				array( 'type' => 'sport', 'direction' => 'best' )
			) . '">' . wfMsg( 'sportsteams-top-network-sports' ) . '</a></p>';
		}

		$output .= '<h1 style="margin-top:15px !important;">' .
			wfMsg( 'sportsteams-top-network-least-popular' ) . '</h1>';

		if ( !( $sport ) && !( $type ) && ( $direction == 'worst' ) ) {
			$output .= '<p><b>' . wfMsg( 'sportsteams-top-network-teams' ) . '</b></p>';
		} else {
			$output .= '<p><a href="' . $this->getTitle()->escapeFullURL(
				array( 'direction' => 'worst' )
			) . '">' . wfMsg( 'sportsteams-top-network-teams' ) . '</a></p>';
		}

		if ( !( $sport ) && ( $type == 'sport' ) && ( $direction == 'worst' ) ) {
			$output .= '<p><b>' . wfMsg( 'sportsteams-top-network-sports' ) . '</b></p>';
		} else {
			$output .= '<p><a href="' . $this->getTitle()->escapeFullURL(
				array( 'type' => 'sport', 'direction' => 'worst' )
			) . '">' . wfMsg( 'sportsteams-top-network-sports' ) . "</a></p>";
		}

		// for grep: sportsteams-top-network-most-pop-by-sport,
		// sportsteams-top-network-least-pop-by-sport
		$output .= '<h1 style="margin-top:15px !important;">' .
			wfMsg( 'sportsteams-top-network-' . strtolower( $adj ) . '-pop-by-sport' ) . '</h1>';

		foreach ( $res_sport_nav as $row_sport_nav ) {
			$sport_id = $row_sport_nav->sport_id;
			$sport_name = $row_sport_nav->sport_name;

			if ( $sport_id == $sport ) {
				$output .= "<p><b>{$sport_name}</b></p>";
				// For grep: sportsteams-top-network-least-team-title,
				// sportsteams-top-network-most-team-title
				$wgOut->setPageTitle(
					wfMsg( 'sportsteams-top-network-' . strtolower( $adj ) . '-team-title',
						$sport_name )
				);
			} else {
				$output .= '<p><a href="' . $this->getTitle()->escapeFullURL(
					array( 'direction' => $direction, 'sport' => $sport_id )
				) . '">' . $sport_name . '</a></p>';
			}
		}

		$output .= '</div>';

		// List Networks
		$output .= '<div class="top-networks">';

		// Set counter
		$x = 1;

		if ( $type == 'sport' ) {
			$fanHome = SpecialPage::getTitleFor( 'FanHome' );
			foreach ( $res_sport as $row_sport ) {
				// More variables
				$user_count = $row_sport->sport_count;
				$sport = $row_sport->sport_name;
				$sport_id = $row_sport->sf_sport_id;

				// Get team logo
				$sport_image = '<img src="' . $wgUploadPath . '/sport_logos/' .
					SportsTeams::getSportLogo( $sport_id, 's' ) .
					'" border="0" alt="logo" />';

				$output .= "<div class=\"network-row\">
					<span class=\"network-number\">{$x}.</span>
					<span class=\"network-team\">
						{$sport_image}
						<a href=\"" . $fanHome->escapeFullURL( array( 'sport_id' => $sport_id ) ) . "\">{$sport}</a>
					</span>
					<span class=\"network-count\">" .
						wfMsgExt(
							'sportsteams-count-fans',
							array( 'parse', 'parsemag' ),
							$user_count
						) .
					'</span>
					<div class="cleared"></div>
				</div>';
				$x++;
			}
		} else {
			foreach ( $res as $row ) {
				// More variables
				$user_count = $row->network_user_count;
				$team = $row->team_name;
				$team_id = $row->sf_team_id;
				$sport_id = $row->team_sport_id;

				// Get team logo
				$team_image = '<img src="' . $wgUploadPath . '/team_logos/' .
					SportsTeams::getTeamLogo( $team_id, 's' ) .
					'" border="0" alt="logo" />';

				$fanHome = SpecialPage::getTitleFor( 'FanHome' );
				$output .= "<div class=\"network-row\">
					<span class=\"network-number\">{$x}.</span>
					<span class=\"network-team\">
						{$team_image}
						<a href=\"" . $fanHome->escapeFullURL( array(
							'sport_id' => $sport_id,
							'team_id' => $team_id
						) ) . "\">{$team}</a>
					</span>
					<span class=\"network-count\">" .
						wfMsgExt(
							'sportsteams-count-fans',
							array( 'parsemag', 'parse' ),
							$user_count
						) .
					'</span>
					<div class="cleared"></div>
				</div>';
				$x++;
			}
		}

		$output .= '</div>
		<div class="cleared"></div>';

		$wgOut->addHTML( $output );
	}
	/**
	 * Ashish Datta
	 * GMaps code
	 * TODO:
	 * - The team images need to be cleaned up.
	 * - The team logos need some shadows.
	 * - The Google Maps Geocoder produces weird results sometimes:
	 * ie: New York, California geocodes to somewhere in CA instead of failing.
	 */
	function getMap() {
		global $wgUser, $wgOut, $wgRequest, $wgUploadPath;

		$sport_id = $wgRequest->getInt( 'sport_id' );
		$team_id = $wgRequest->getInt( 'team_id' );

		// maybe error check this to make sure the file exists...
		if( $team_id ) {
			$team_image = $wgUploadPath . '/teams_logos/' .
				SportsTeams::getTeamLogo( $team_id, 'l' );
		} else {
			$team_image = $wgUploadPath . '/sport_logos/' .
				SportsTeams::getSportLogo( $sport_id, 'l' );
		}

		$userIDs = array(); // stores the userIDs for this network
		$fanLocations = array(); // stores the locations on the map
		$fanStates = array(); // stores the states along with the fans from that state

		$markerCode = '';

		$output = '';

		$fans = SportsTeams::getUsersByFavorite( $sport_id, $team_id, 7, 0 );

		// go through all the fans for this network
		// grab their userIDs and save HTML for their mini-profiles
		foreach( $fans as $fan ) {
			$fanInfo = array();

			$user = Title::makeTitle( NS_USER, $fan['user_name'] );
			$avatar = new wAvatar( $fan['user_id'], 'l' );

			$out = "<p class=\"map-avatar-image\">
				<a href=\"{$user->getFullURL()}\">{$avatar->getAvatarURL()}</a></p>
				<p class=\"map-avatar-info\"> <a href=\"{$user->getFullURL()}\">{$fan['user_name']}</a>";

			$fanInfo['divHTML'] = $out;
			$fanInfo['URL'] = $user->getFullURL();
			$fanInfo['user_name'] = $fan['user_name'];

			$userIDs[$fan['user_id']] = $fanInfo;
		}

		// Get the location info about this network's fans
		$idList = implode( ',', array_keys( $userIDs ) );
		$idList = '(' . $idList . ')';

		// Get the info about the fans; only select fans that have country info
		$dbr = wfGetDB( DB_MASTER );
		$res = $dbr->select(
			'user_profile',
			array(
				'up_user_id', 'up_location_country', 'up_location_city',
				'up_location_state'
			),
			array(
				'up_user_id IN ' . $idList,
				'up_location_country IS NOT NULL'
			),
			__METHOD__
		);

		foreach( $res as $row ) {
			$topLoc = '';
			$loc = '';

			$userInfo = array();
			$userInfo['user_id'] = $row->up_user_id;
			$userInfo['user_name'] = $userIDs[$row->up_user_id]['user_name'];

			// case everything nicely
			$country = ucwords( strtolower( $row->up_location_country ) );
			$state = ucwords( strtolower( $row->up_location_state ) );
			$city = ucwords( strtolower( $row->up_location_city ) );

			// if the fan is in the US geocode by city, state
			if( $country == 'United States' ) {
				// if the user's profile doesn't have a city, only use a state
				if( strlen( $city ) > 0 && strlen( $state ) > 0 ) {
					$loc = $city . ', ' . $state;
					$topLoc = $state;
				} elseif( strlen( $state ) > 0 ) {
					$loc = $state;
					$topLoc = $state;
				} else {
					$loc = $country;
					$topLoc = $country;
				}
			} else { // if they are non-US then geocode by city, country
				if( strlen( $city ) > 0 && strlen( $country ) > 0 ) {
					$loc = $city . ', ' . $country;
					$topLoc = $country;
				} else {
					$loc = $country;
					$topLoc = $country;
				}
			}

			// build a hashtable using higher locations as keys and arrays of fans as objects
			if( !array_key_exists( $topLoc, $fanStates ) ) {
				$fanStates[$topLoc] = array();
				$fanStates[$topLoc][] = $userInfo;
			} else {
				$fanStates[$topLoc][] = $userInfo;
			}

			// htmlentities( $userIDs[$row->up_user_id]['divHTML'] )
			// JavaScript to place the marker
			//
			// @note Newlines and tab characters are trimmed from the HTML
			// since their presence would mess up the JS code
			$markerCode .= "geocoder.getLatLng( '" . $loc . "',
								function( point ) {
									if ( !point ) {
										geocoder.getLatLng( '" . $state . "',
											function( point ) {
												var nPoint = new GPoint( point.x + ( Math.random() * .12 ), point.y + ( Math.random() * .12 ) );
												var gMark = createMarker( nPoint, \"" .
													str_replace( array( "\n", "\t" ), '', addslashes( $userIDs[$row->up_user_id]['divHTML'] ) ) .
													'<br />' . $loc . "</p>\", '" .
													$userIDs[$row->up_user_id]['URL'] . "', map
												);
												mgr.addMarker( gMark, 6 );
											}
										);
									} else {
							";

			// this is the first fan at $loc
			if( !in_array( $loc, $fanLocations ) ) {
				$fanLocations[] = $loc;
			} else {
				// there is already a placemark at $loc so add some jitter
				$markerCode .= "var point = new GPoint( point.x + ( Math.random() * .1 ), point.y + ( Math.random() * .1 ) );";
			}

			$markerCode .= "var gMark = createMarker(point, \"" .
				str_replace( array( "\n", "\t" ), '', addslashes( $userIDs[$row->up_user_id]['divHTML'] ) ) .
				'<br />' . $loc . "</p>\", '" .
				$userIDs[$row->up_user_id]['URL'] . "', map);
							mgr.addMarker( gMark, 6 );
							}} );	";

		}

		// helper function to compare the $fanStates objects
		function cmpFanStates( $a, $b ) {
			if( $a['user_id'] < $b['user_id'] ) {
				return 1;
			} else {
				return -1;
			}
		}

		// at the state level markers include the 5 newest users
		foreach( $fanStates as $state => $users ) {
			usort( $users, 'cmpFanStates' );

			$userList = '';

			for( $i = 0; $i < ( count( $users ) < 5 ? count( $users ) : 5 ); $i++ ) {
				$userList .= $users[$i]['user_name'] . '<br />';
			}

			$markerCode .= "geocoder.getLatLng( '" . $state . "' ,
								function( point ) {
									if( point )
										mgr.addMarker(

									createTopMarker( point, '<div id=\"gMapStateInfo\" class=\"gMapStateInfo\"> <div class=\"fan-location-blurb-title\">" .
										wfMsg( 'sportsteams-network-newest', $state ) .
										"</div><div class=\"user-list\">" . $userList .
										"<div><div style=\"font-size:10px; color:#797979;\">" .
										wfMsg( 'sportsteams-network-clicktozoom' ) . "</div></div>', map ), 1, 5 );
								}	);";
		}

		// script
		$output .= "<script language=\"javascript\">var __team_image__ = \"{$team_image}\";


// loads everything onto the map
function loadMap() {
	if ( GBrowserIsCompatible() ) {
		var geocoder = new GClientGeocoder();
		var map = new GMap2( document.getElementById( 'gMap' ) );

		// make sure to clean things up
		window.onunload = GUnload;

		geocoder.setBaseCountryCode( 'US' );

		map.setCenter( new GLatLng( 37.0625, -95.677068 ), 3 );
		map.addControl( new GSmallZoomControl() );
		var mgr = new GMarkerManager( map );

		" . $markerCode . "

		mgr.refresh();
	}
}

</script>";

		return $output;
	}
function wfUserProfileFavoriteTeams( $user_profile ) {
	global $wgUser, $wgOut, $wgScriptPath, $wgUploadPath;

	$output = '';
	$user_id = $user_profile->user_id;

	// Add JS
	if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
		$wgOut->addModuleScripts( 'ext.sportsTeams.userProfile' );
	} else {
		$wgOut->addScriptFile( $wgScriptPath . '/extensions/SportsTeams/SportsTeamsUserProfile.js' );
	}

	$add_networks_title = SpecialPage::getTitleFor( 'UpdateFavoriteTeams' );

	$favs = SportsTeams::getUserFavorites( $user_id );

	if ( $favs ) {
		$output .= '<div class="user-section-heading">
			<div class="user-section-title">' .
				wfMsg( 'sportsteams-profile-networks' ) .
			'</div>
			<div class="user-section-actions">
				<div class="action-right">';
		if ( $user_profile->isOwner() ) {
			$output .= '<a href="' . $add_networks_title->escapeFullURL() . '">' .
				wfMsg( 'sportsteams-profile-add-network' ) . '</a>';
		}
		$output .= '</div>
				<div class="cleared"></div>
			</div>
		</div>
		<div class="network-container">';

		foreach( $favs as $fav ) {
			$homepage_title = SpecialPage::getTitleFor( 'FanHome' );

			$status_link = '';
			if ( $wgUser->getId() == $user_id ) {
				$onclick = "SportsTeamsUserProfile.showMessageBox({$fav['order']},{$fav['sport_id']},{$fav['team_id']})";
				$status_link = ' <span class="status-message-add"> - <a href="javascript:void(0);" onclick="' .
					$onclick . '" rel="nofollow">' .
					wfMsg( 'sportsteams-profile-add-thought' ) . '</a></span>';
			}

			$network_update_message = '';

			// Originally the following two lines of code were not present and
			// thus $user_updates was always undefined
			$s = new UserStatus();
			$user_updates = $s->getStatusMessages(
				$user_id, $fav['sport_id'], $fav['team_id'], 1, 1
			);

			// Added empty() check
			if ( !empty( $user_updates[$fav['sport_id'] . '-' . $fav['team_id']] ) ) {
				$network_update_message = $user_updates[$fav['sport_id'] . '-' . $fav['team_id']];
			}

			if ( $fav['team_name'] ) {
				$display_name = $fav['team_name'];
				$logo = "<img src=\"{$wgUploadPath}/team_logos/" .
					SportsTeams::getTeamLogo( $fav['team_id'], 's' ) .
					'" border="0" alt="" />';
			} else {
				$display_name = $fav['sport_name'];
				$logo = "<img src=\"{$wgUploadPath}/sport_logos/" .
					SportsTeams::getSportLogo( $fav['sport_id'], 's' ) .
					'" border="0" alt="" />';
			}

			$output .= "<div class=\"network\">
				{$logo}
				<a href=\"" . $homepage_title->escapeFullURL(
					'sport_id=' . $fav['sport_id'] . '&team_id=' . $fav['team_id']
				) . "\" rel=\"nofollow\">{$display_name}</a>
				{$status_link}
			</div>

			<div class=\"status-update-box\" id=\"status-update-box-{$fav['order']}\" style=\"display:none\"></div>";
		}

		$output .= '<div class="cleared"></div>
		</div>';
	} elseif ( $user_profile->isOwner() ) {
		$output .= '<div class="user-section-heading">
			<div class="user-section-title">' .
				wfMsg( 'sportsteams-profile-networks' ) .
			'</div>
			<div class="user-section-actions">
				<div class="action-right">
					<a href="' . $add_networks_title->escapeFullURL() . '">' .
						wfMsg( 'sportsteams-profile-add-network' ) . '</a>
				</div>
				<div class="cleared"></div>
			</div>
		</div>
		<div class="no-info-container">' .
			wfMsg( 'sportsteams-profile-no-networks' ) .
		'</div>';
	}

	$wgOut->addHTML( $output );
	return true;
}
	/**
	 * Displays the main upload form, optionally with a highlighted
	 * error message up at the top.
	 *
	 * @param string $msg as HTML
	 * @access private
	 */
	function mainUploadForm( $msg = '' ) {
		global $wgOut, $wgUser, $wgUploadDirectory, $wgRequest;
		global $wgUseCopyrightUpload, $wgUploadPath;

		$cols = intval( $wgUser->getOption( 'cols' ) );
		$ew = $wgUser->getOption( 'editwidth' );
		if ( $ew ) {
			$ew = ' style="width: 100%"';
		} else {
			$ew = '';
		}

		if ( $msg != '' ) {
			$sub = wfMsg( 'uploaderror' );
			$wgOut->addHTML( "<h2>{$sub}</h2>\n" .
				"<h4 class='error'>{$msg}</h4>\n" );
		}

		$sk = $wgUser->getSkin();

		$sourcefilename = wfMsg( 'sourcefilename' );
		$destfilename = wfMsg( 'destfilename' );

		$fd = wfMsg( 'filedesc' );
		$ulb = wfMsg( 'uploadbtn' );

		$iw = wfMsg( 'ignorewarning' );

		$titleObj = SpecialPage::getTitleFor( 'Upload' );
		$action = $titleObj->escapeLocalURL();

		$encDestFile = htmlspecialchars( $this->mDestFile );
		$source = null;

		if ( $wgUseCopyrightUpload ) {
			$source = "
	<td align='right' nowrap='nowrap'>" . wfMsg( 'filestatus' ) . ":</td>
	<td><input tabindex='3' type='text' name=\"wpUploadCopyStatus\" value=\"" .
	htmlspecialchars( $this->mUploadCopyStatus ). "\" size='40' /></td>
	</tr><tr>
	<td align='right'>". wfMsg( 'filesource' ) . ":</td>
	<td><input tabindex='4' type='text' name='wpUploadSource' value=\"" .
	htmlspecialchars( $this->mUploadSource ). "\" style='width:100px' /></td>
	";
		}

		$watchChecked = $wgUser->getOption( 'watchdefault' )
			? 'checked="checked"'
			: '';

		$team_logo = SportsTeams::getTeamLogo( $this->team_id, 'l' );
		if( $team_logo != '' ) {
			$output = '<table>
				<tr>
					<td style="color: #666666; font-weight: 800">' .
						wfMsg( 'sportsteams-logo-current-image' ) .
					'</td>
				</tr>
				<tr>
					<td>
						<img src="' . $wgUploadPath . '/team_logos/' . $team_logo . '" border="0" alt="no logo" />
					</td>
				</tr>
			</table>
			<br />';
		}
		$wgOut->addHTML( $output );

		$wgOut->addHTML( "
	<form id='upload' method='post' enctype='multipart/form-data' action=\"\">
	<table border='0'><tr>

	<td style='color:#666666;font-weight:800'>" . wfMsg( 'sportsteams-logo-image-instructions' ) . "<br />
	<input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' style='width:100px' />
	</td></tr><tr>
	{$source}
	</tr>
	<tr><td>
	<input tabindex='5' type='submit' name='wpUpload' value=\"{$ulb}\" />
	</td></tr></table></form>\n" );
	}