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

		if ( $wgUser->isLoggedIn() ) {
			$this->friends = $this->getRelationships( 1 );
			$this->foes = $this->getRelationships( 2 );
			$this->relationships = array_merge( $this->friends, $this->foes );
		} else {
			// Prevent fatals (+1 notice) for anonymous users
			$this->friends = $this->foes = $this->relationships =
				$fan_info = '';
		}

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

		// If there's neither a sports ID nor a team ID, show an error message.
		// @todo FIXME: I don't like this; we should be showing a listing of
		// all networks or something instead of basically telling the user to
		// go away.
		if( !$sport_id && !$team_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 true;
		}

		$this->network_count = SportsTeams::getUserCount( $sport_id, $team_id );
		$this->friends_network_count = SportsTeams::getFriendsCountInFavorite(
			$wgUser->getId(),
			$sport_id,
			$team_id
		);

		if( $team_id ) {
			$team = SportsTeams::getTeam( $team_id );
			$this->network = $team['name'];
		} else {
			$sport = SportsTeams::getSport( $sport_id );
			$this->network = $sport['name'];
		}

		$team_image = SportsTeams::getLogo( $sport_id, $team_id, 'l' );

		$homepage_title = Title::makeTitle( NS_MAIN, $this->network );
		$view_fans_title = SpecialPage::getTitleFor( 'ViewFans' );
		$join_fans_title = SpecialPage::getTitleFor( 'AddFan' );
		$leave_fans_title = SpecialPage::getTitleFor( 'RemoveFan' );

		// Set the page title
		$wgOut->setPageTitle( wfMsg( 'sportsteams-network-fan-network', $this->network ) );

		// Add CSS & JS
		if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
			$wgOut->addModuleStyles( 'ext.sportsTeams' );
			$wgOut->addModuleScripts( 'ext.sportsTeams.fanHome' );
		} else {
			$wgOut->addExtensionStyle( $wgScriptPath . '/extensions/SportsTeams/SportsTeams.css' );
			$wgOut->addScriptFile( $wgScriptPath . '/extensions/SportsTeams/fanhome.js' );
		}

		// Ashish Datta
		// Add the script for the maps and set the onload() handler
		// DONT FORGET TO CHANGE KEY WHEN YOU CHANGE DOMAINS
		// @note As of 12 August 2011, http://code.google.com/apis/maps/documentation/javascript/v2/
		// states that the version 2 of Google Maps API has been deprecated
		$wgOut->addScript( "<script src=\"http://maps.google.com/maps?file=api&amp;v=2.x&amp;key={$wgSportsTeamsGoogleAPIKey}\" type=\"text/javascript\"></script>" );
		$wgOut->addScript( $this->getMap() );
		// this originally used setOnloadHandler; addOnloadHook() won't work
		$wgOut->addScript( '<script>jQuery( document ).ready( function() { loadMap(); } );</script>' );

		if( SportsTeams::isFan( $wgUser->getID(), $sport_id, $team_id ) ) {
			$fan_info = '<p><span class="profile-on">' .
				wfMsg( 'sportsteams-network-you-are-fan' ) . '</span></p>';
			$fan_info .= '<p><span><a href="' . $leave_fans_title->getFullURL(
				"sport_id={$sport_id}&team_id={$team_id}"
			) . '" style="text-decoration:none;">' .
				wfMsg( 'sportsteams-network-leave-network' ) .
			'</a></span></p>';
		} elseif ( $wgUser->isLoggedIn() ) {
			$fan_info = '<p><span class="profile-on"><a href="' .
				$join_fans_title->getFullURL(
					"sport_id={$sport_id}&team_id={$team_id}"
				) . '" style="text-decoration: none;">' .
				wfMsg( 'sportsteams-network-join-network' ) . '</a></span></p>';
		}

		$output = '';

		$output .= '<div class="fan-top">';

		$output .= '<div class="fan-top-left">';
		$output .= '<h1>' . wfMsg( 'sportsteams-network-info' ) . '</h1>';
		$output .= '<div class="network-info-left">';
		$output .= $team_image;
		$output .= '<p>' . wfMsg( 'sportsteams-network-logo' ) . '</p>';
		$output .= '</div>';
		$output .= '<div class="network-info-right">';
		$output .= '<p>' . wfMsg( 'sportsteams-network-fans-col' ) . ' <a href="' .
			$view_fans_title->getFullURL(
				array(
					'sport_id' => $sport_id,
					'team_id' => $team_id
				)
			) . "\">{$this->network_count}</a></p>";
		if( $wgUser->isLoggedIn() ) {
			$output .= '<p>' . wfMsg( 'sportsteams-network-friends-col' ) ." {$this->friends_network_count}</p>";
		}
		$output .= $fan_info;
		$output .= '</div>';
		$output .= '<div class="cleared"></div>';
		$output .= '</div>';
		$this_count = count( SportsTeams::getUsersByFavorite( $sport_id, $team_id, 7, 0 ) );
		$output .= '<div class="fan-top-right">';
		$output .= '<h1>' . wfMsg( 'sportsteams-network-fans', $this->network ) . '</h1>';
		$output .= '<p style="margin:-8px 0px 0px 0px; color:#797979;">' .
			wfMsgExt(
				'sportsteams-network-fan-display',
				'parsemag',
				$this_count,
				$view_fans_title->getFullURL( array(
					'sport_id' => $sport_id, 'team_id' => $team_id
				) ),
				$this->network_count
			) . '</p>';
		$output .= $this->getFans();
		$output .= '</div>';

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

		$output .= '<div class="fan-left">';

		// Latest Network User Updates
		$updates_show = 25;
		$s = new UserStatus();
		$output .= '<div class="network-updates">';
		$output .= '<h1 class="network-page-title">' .
			wfMsg( 'sportsteams-network-latest-thoughts' ) . '</h1>';
		$output .= '<div style="margin-bottom:10px;">
			<a href="' . SportsTeams::getFanUpdatesURL( $sport_id, $team_id ) . '">' .
				wfMsg( 'sportsteams-network-all-thoughts' ) . '</a>
		</div>';
		// Registered users (whether they're members of the network or not) can
		// post new status updates on the network's page from the network's
		// page
		if( $wgUser->isLoggedIn() ) {
			$output .= "\n<script type=\"text/javascript\">
				var __sport_id__ = {$sport_id};
				var __team_id__ = {$team_id};
				var __updates_show__ = {$updates_show};
				var __user_status_link__ = '" . SpecialPage::getTitleFor( 'UserStatus' )->getFullURL() . "';</script>\n";
			$output .= "<div class=\"user-status-form\">
				<span class=\"user-name-top\">{$wgUser->getName()}</span> <input type=\"text\" name=\"user_status_text\" id=\"user_status_text\" size=\"40\" onkeypress=\"detEnter(event)\" maxlength=\"150\" />
				<input type=\"button\" value=\"" . wfMsg( 'sportsteams-add-button' ) . '" class="site-button" onclick="add_status()" />
			</div>';
		}
		$output .= '<div id="network-updates">';
		$output .= $s->displayStatusMessages(
			0, $sport_id, $team_id, $updates_show, 1/*$page*/
		);
		$output .= '</div>';

		$output .= '</div></div>';

		$output .= '<div class="fan-right">';

		// Network location map
		$output .= '<div class="fan-map">';
		$output .= '<h1 class="network-page-title">' .
			wfMsg( 'sportsteams-network-fan-locations' ) . '</h1>';
		$output .= '<div class="gMap" id="gMap"></div>
			<div class="gMapInfo" id="gMapInfo"></div>';
		$output .= '</div>';

		// Top network fans
		$output .= '<div class="top-fans">';
		$output .= '<h1 class="network-page-title">' .
			wfMsg( 'sportsteams-network-top-fans' ) . '</h1>';
		$tfr = SpecialPage::getTitleFor( 'TopUsersRecent' );
		$output .= "<p class=\"fan-network-sub-text\">
				<a href=\"" . $tfr->escapeFullURL( 'period=weekly' ) . '">' .
					wfMsg( 'sportsteams-network-top-fans-week' ) .
				"</a> -
				<a href=\"{$view_fans_title->getFullURL( array( 'sport_id' => $sport_id, 'team_id' => $team_id ))}\">" .
					wfMsg( 'sportsteams-network-complete-list' ) . '</a>
			</p>';
		$output .= $this->getTopFans();
		$output .= '</div>';

		$output .= '<div class="network-articles">';
		$output .= '<h1 class="network-page-title">' .
			wfMsg( 'sportsteams-network-articles', $this->network ) . '</h1>';
		$output .= '<p class="fan-network-sub-text">';
		if ( class_exists( 'BlogPage' ) ) { // @todo CHECKME: is there any point in this check?
			$createBlogPage = SpecialPage::getTitleFor( 'CreateBlogPage' );
			$output .= '<a href="' . $createBlogPage->escapeFullURL() . '">' .
				wfMsg( 'sportsteams-network-write-article' ) . '</a> - ';
		}
		$output .= "<a href=\"{$homepage_title->getFullURL()}\">" . wfMsg( 'sportsteams-network-main-page' ) . '</a>
			</p>';
		$output .= $this->getArticles();
		$output .= '</div>';

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

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

		$messages_show = 25;
		$updates_show = 25; // just an arbitrary value to stop PHP from complaining on 12 August 2011 --ashley
		$output = '';
		$sport_id = $wgRequest->getInt( 'sport_id' );
		$team_id = $wgRequest->getInt( 'team_id' );
		$page = $wgRequest->getInt( 'page', 1 );

		if ( $team_id ) {
			$team = SportsTeams::getTeam( $team_id );
			$network_name = $team['name'];
		} elseif ( $sport_id ) {
			$sport = SportsTeams::getSport( $sport_id );
			$network_name = $sport['name'];
		} else {
			// No sports ID nor team ID...bail out or we'll get a database
			// error...
			$wgOut->setPageTitle( wfMsg( 'userstatus-woops' ) );
			$out = '<div class="relationship-request-message">' .
				wfMsg( 'userstatus-invalid-link' ) . '</div>';
			$out .= '<div class="relationship-request-buttons">';
			$out .= '<input type="button" class="site-button" value="' .
				wfMsg( 'mainpage' ) .
				"\" onclick=\"window.location='" .
				Title::newMainPage()->escapeFullURL() . "'\"/>";
			/* removed because I was too lazy to port the error message over :P
			if ( $wgUser->isLoggedIn() ) {
				$out .= ' <input type="button" class="site-button" value="' .
					wfMsg( 'st_network_your_profile' ) .
					"\" onclick=\"window.location='" .
					Title::makeTitle( NS_USER, $wgUser->getName() )->escapeFullURL() . "'\"/>";
			}
			*/
			$out .= '</div>';
			$wgOut->addHTML( $out );
			return true;
		}

		$wgOut->setPageTitle( wfMsg( 'userstatus-network-thoughts', $network_name ) );

		/**
		 * Config for the page
		 */
		$per_page = $messages_show;

		$s = new UserStatus();
		$total = $s->getNetworkUpdatesCount( $sport_id, $team_id );
		$messages = $s->getStatusMessages(
			0,
			$sport_id,
			$team_id,
			$messages_show,
			$page
		);

		$output .= '<div class="gift-links">';
		$output .= '<a href="' .
			SportsTeams::getNetworkURL( $sport_id, $team_id ) . '">' .
				wfMsg( 'userstatus-back-to-network' ) . '</a>';
		$output .= '</div>';

		if( $page == 1 ) {
			$start = 1;
		} else {
			$start = ( $page - 1 ) * $per_page + 1;
		}
		$end = $start + ( count( $messages ) ) - 1;

		if( $total ) {
			$output .= '<div class="user-page-message-top">
			<span class="user-page-message-count" style="font-size: 11px; color: #666666;">' .
				wfMsgExt( 'userstatus-showing-thoughts', 'parsemag', $start, $end, $total ) .
			'</span>
		</div>';
		}

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

		if( $numofpages > 1 ) {
			$output .= '<div class="page-nav">';
			if( $page > 1 ) {
				$output .= '<a href="' .
					SportsTeams::getFanUpdatesURL( $sport_id, $team_id ) .
					'&page=' . ( $page - 1 ) . '">' . wfMsg( 'userstatus-prev' ) .
					'</a> ';
			}

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

			for( $i = 1; $i <= $numofpages; $i++ ) {
				if( $i == $page ) {
					$output .= ( $i . ' ');
				} else {
					$output .= '<a href="' .
						SportsTeams::getFanUpdatesURL( $sport_id, $team_id ) .
						"&page=$i\">$i</a> ";
				}
			}

			if( ( $total - ( $per_page * $page ) ) > 0 ) {
				$output .= ' <a href="' .
					SportsTeams::getFanUpdatesURL( $sport_id, $team_id ) .
					'&page=' . ( $page + 1 ) . '">' . wfMsg( 'userstatus-next' ) .
					'</a>';
			}
			$output .= '</div><p>';
		}

		// Add CSS & JS
		if ( defined( 'MW_SUPPORTS_RESOURCE_MODULES' ) ) {
			$wgOut->addModuleStyles( 'ext.userStatus' );
			$wgOut->addModuleScripts( 'ext.userStatus' );
		} else {
			$wgOut->addExtensionStyle( $wgScriptPath . '/extensions/UserStatus/UserStatus.css' );
			$wgOut->addScriptFile( $wgScriptPath . '/extensions/UserStatus/UserStatus.js' );
		}

		// Registered users who are not blocked can add status updates when the
		// database is not locked
		if( $wgUser->isLoggedIn() && !$wgUser->isBlocked() && !wfReadOnly() ) {
			$output .= "<script>
				var __sport_id__ = {$sport_id};
				var __team_id__ = {$team_id};
				var __updates_show__ = \"{$updates_show}\";
				var __redirect_url__ = \"" . str_replace( '&amp;', '&', SportsTeams::getFanUpdatesURL( $sport_id, $team_id ) ) . "\";
			</script>";

			$output .= "<div class=\"user-status-form\">
			<span class=\"user-name-top\">{$wgUser->getName()}</span> <input type=\"text\" name=\"user_status_text\" id=\"user_status_text\" size=\"40\"/>
			<input type=\"button\" value=\"" . wfMsg( 'userstatus-btn-add' ) . '" class="site-button" onclick="add_status()" />
			</div>';
		}

		$output .= '<div class="user-status-container">';
		if( $messages ) {
			foreach ( $messages as $message ) {
				$user = Title::makeTitle( NS_USER, $message['user_name'] );
				$avatar = new wAvatar( $message['user_id'], 'm' );

				$messages_link = '<a href="' .
					UserStatus::getUserUpdatesURL( $message['user_name'] ) . '">' .
					wfMsg( 'userstatus-view-all-updates', $message['user_name'] ) .
					'</a>';
				$delete_link = '';
				// Allow the owner of the status update and privileged users to
				// delete it
				if(
					$wgUser->getName() == $message['user_name'] ||
					$wgUser->isAllowed( 'delete-status-updates' )
				)
				{
					$delete_link = "<span class=\"user-board-red\">
							<a href=\"javascript:void(0);\" onclick=\"javascript:delete_message({$message['id']})\">" .
						wfMsg( 'userstatus-delete' ) . '</a>
					</span>';
				}

				$message_text = preg_replace_callback(
					'/(<a[^>]*>)(.*?)(<\/a>)/i',
					array( 'UserStatus', 'cutLinkText' ),
					$message['text']
				);

				$output .= "<div class=\"user-status-row\">
					<a href=\"{$user->getFullURL()}\">{$avatar->getAvatarURL()}</a>
					<a href=\"{$user->getFullURL()}\"><b>{$message['user_name']}</b></a> {$message_text}
					<span class=\"user-status-date\">" .
						wfMsg( 'userstatus-ago', UserStatus::getTimeAgo( $message['timestamp'] ) ) .
					'</span>
				</div>';
			}
		} else {
			$output .= '<p>' . wfMsg( 'userstatus-no-updates' ) . '</p>';
		}

		$output .= '</div>';

		$wgOut->addHTML( $output );
	}