<?PHP 

include("config.php");
require_once("rating/classes/include.all.php");

$uidhere = intval(mysql_escape_string($_GET[uid]));
$results = mysql_query("SELECT lat, lng, onetype, twotype, threetype, oneunit, twounit, threeunit, results, uid, name, nametag, type, indivfee, teamfee, swim, bike, bike_surface, draft_legal, kids_race, run, state, statetag, city, address, phone, email, courseinfo, moreinfo, directions, register, website, valid, submitted_by, UNIX_TIMESTAMP(date) as foo, registration FROM gforum_Triathlons WHERE uid='".$uidhere."'");
$row = mysql_fetch_array($results);
if (!$row){header('Location:https://slowtwitch.com/calendar/?error=invalid_id');}

if($row[valid] == 0) {
  if(!is_admin($user)){
    header('Location:https://slowtwitch.com/calendar/');
    exit();
  }
}

if($row[type] == 1){
   $typename = 'Triathlon';
}else if($row[type] == 2){
   $typename = 'Duathlon';
}else if($row[type] == 3){
   $typename = 'Other';
}else if($row[type] == 4){
   $typename = 'Swim-Run';
}

// set the page title
$pagetitle = $row[name];

// set meta tags
$meta_keywords = "triathlons, ".$row[name].", ".$row[state]." triathlon";
$meta_description = "Considering racing at the ".$row[name]."? Here is all of the information including date, time, location, and course information.";

if($_POST['addcomment'] == 1){
$init = '';
$errmsg = NULL;
$content1 = strip_tags(substr($_POST[content], 0, 4096));
$rating = $_POST[rating];
if($_POST[rating] == NULL){ $rating = 6; }
$uid = $row[uid];
if((trim($_POST[content]) == NULL)){ $errmsg = 'Please make a comment.'; }
if(!$errmsg){
$content = nl2br($content1);
mysql_query("INSERT INTO comments(rating, name, content, uid, date) VALUES($rating, \"$_POST[postusername]\", \"$content\", $uid, NOW())");
} 

}

// TODO: SLOWTWITCH EDIT
//$results = mysql_query("SELECT uid, rating, name, content, uid, UNIX_TIMESTAMP(date) as foo FROM comments WHERE uid=$row[uid] ORDER BY date DESC");
//$numcomments = mysql_num_rows($results);

//Compute average rating
//$test = mysql_query("SELECT rating FROM comments WHERE uid=$row[uid]");
//$num = mysql_num_rows($test);
if($num > 0){
  $total = 0;
  while($testrow = mysql_fetch_array($test)){
    if($testrow[rating] < 6){ $total = $total + $testrow[rating]; }
    if($testrow[rating] == 6){ $num = $num - 1; }
  }
  $avg = $total / $num;
}else{
  //No ratings
  $avg = 0;
}
?>

<? include("include_common_head.php"); ?>

<body class="listings">
  <? include($common_path . "/ads/ad_wallpaper.html"); ?>

  <div class="container">
    <? include($common_path . "/templates/include_header.php"); ?>
    <div class="main">
      <div class="contentwrapper clearfix">
        <? include("include_breadcrumb.php"); ?>

        <section class="section listings section-has-widgets section-static remove-sidebar">
          <div class="sidebar-b">
            <? $selected_item = strtolower($row[state]); ?>
            <? include("include_sidebar.php"); ?>
          </div>
          <div class="content content-has-widgets">
            <div class="grid">

              <div class="clearfix">
                <h1 class="float-left">Races</h1>
                <? if ($user) { ?>
                <form method="post" action="wiki_edit.php?retailer_id=<? echo $row[id] ?>">
                  <input type="submit" value="Edit (wiki)" class="nobutton btn btn-white float-right">
                </form>
                <? } ?>
                <a href="" class="btn btn-white float-right">Back</a>
                <hr class="line" />
                <? include("../runshops/include_region_list.php"); ?>
                <div class="details grid">
                  <div class="col-9-12">
                    <h2><? echo stripslashes(stripslashes($row[name])) ; echo " "; ?></h2>
                  </div>
                  <div class="col-3-12 right">
                    <? if(is_logged_in($user)) { ?><a href="#comment">Comment</a><? }?>
                  </div>
                  <div class="clearfix"></div>
                  <div class="col-1-1">
                    Email: <a href="mailto:<? echo $row[email]; ?>"><? echo stripslashes(htmlspecialchars($row[email])); ?></a><br />
                    <span class="briefy-b">Website: <a href="<? echo $row[website]; ?>" target="_blank"><? echo stripslashes(htmlspecialchars($row[website])); ?></a></span>
                  </div>
                  <div class="clearfix"></div>
                  <? if($_GET[review_add] == 'ok') { ?>
                  <p class="success"><strong>Commenter:</strong> your comments have been submitted and will be validated within 24 hours.</p>
                  <? } ?>

                  <? if($_GET[wiki_change] == 'success') { ?>
                  <p class="success"><strong>Editor:</strong> your changes have been submitted and will be validated within 24 hours.</p>
                  <? } ?>

                  <? $wikiFetchSQL = "SELECT edit_id FROM gforum_TriathlonsEdits WHERE race_uid_fk = '".intval(mysql_escape_string($_GET[uid]))."'";
                  $boolEdits = mysql_num_rows(mysql_query($wikiFetchSQL));
                  if($boolEdits !== 0 && $_GET[wiki_change] != 'success') { ?>
                  <p class="error"><strong>Group Edits:</strong> there are group edits waiting to be validated.</p>
                  <? } ?>
                  <div class="col-1-1">
                    <? include("include_buttons.php"); ?><br />
                    <? include('include_share.php'); ?>
                  </div>

                  <div class="col-1-1">
                    <?
                    if(file_exists("/srv/www/htdocs/trifind/racepics/$row[pic1].img")){
                      print "<img alt=\"$row[name]\" border=\"1\" bordercolor=\"#000\" src=\"phpthumb/phpThumb.php?src=../racepics/$row[pic1].img&w=170&h=150\" />";
                      print "&nbsp;&nbsp;&nbsp;&nbsp;";
                    } 

                    if(file_exists("/srv/www/htdocs/trifind/racepics/$row[pic2].img")){ 
                      print "<img alt=\"$row[name]\" border=\"1\" bordercolor=\"#000\" src=\"phpthumb/phpThumb.php?src=../racepics/$row[pic2].img&w=170&h=150\" />";
                      print "&nbsp;&nbsp;&nbsp;&nbsp;";
                    }

                    if(file_exists("/srv/www/htdocs/trifind/racepics/$row[pic3].img")){
                      print "<img alt=\"$row[name]\" border=\"1\" bordercolor=\"#000\" src=\"phpthumb/phpThumb.php?src=../racepics/$row[pic3].img&w=170&h=150\" />";
                      print "<br /><br />";
                    }
                    ?>
                  </div>
                  <div class="col-1-1 detail-block">
                    <h3>Race Information</h3>
                    <div class="highlight-block">
                      <strong>Date:</strong> <? echo date("F j, Y", $row[foo]); ?><br />
                      <strong>Race Type:</strong> <? echo $typename; ?><br />
                      <? //Determine bike surface
                      if ($row[bike_surface] == 1) { $bike_surface = "Offroad"; } 
                      if ($row[bike_surface] == 2) { $bike_surface = "Gravel"; } 
                      else { $bike_surface = "Road"; }
                      ?>
                      <strong>Bike Surface:</strong> <? echo $bike_surface; ?><br />
                      <? //Determine draft rules
                      if ($row[draft_legal] == 1)
                      { $draft_legal = "Draft legal"; } 
                      else
                      { $draft_legal = "Non-drafting"; }
                      ?>
                      <strong>Drafting rules:?</strong> <? echo $draft_legal; ?><br />

                      <? //Determine kids race

                      if ($row[kids_race] == 1)
                      { $kids_race = "This *IS* a kids' race"; } 
                      else
                      { $kids_race = "*NOT* a kids' race"; }
                      ?>
                      <strong>Kids Race?</strong> <? echo $kids_race; ?><br />

                      <? //Determine units
                      if($row[oneunit] == 1){ $oneunit = "mile"; }
                      if($row[oneunit] == 2){ $oneunit = "meter"; }
                      if($row[oneunit] == 3){ $oneunit = "yard"; }
                      if($row[oneunit] == 4){ $oneunit = "kilometers"; }

                      if($row[twounit] == 1){ $twounit = "mile"; }
                      if($row[twounit] == 2){ $twounit = "meter"; }
                      if($row[twounit] == 3){ $twounit = "yard"; }
                      if($row[twounit] == 4){ $twounit = "kilometers"; }

                      if($row[threeunit] == 1){ $threeunit = "mile"; }
                      if($row[threeunit] == 2){ $threeunit = "meter"; }
                      if($row[threeunit] == 3){ $threeunit = "yard"; }
                      if($row[threeunit] == 4){ $threeunit = "kilometers"; }

                      if($row[onetype] == 1){ $onetype = "swim"; }
                      if($row[onetype] == 2){ $onetype = "bike"; }
                      if($row[onetype] == 3){ $onetype = "run"; }
                      if($row[onetype] == 4){ $onetype = "paddle"; }
                      if($row[onetype] == 5){ $onetype = "ski"; }

                      if($row[twotype] == 1){ $twotype = "swim"; }
                      if($row[twotype] == 2){ $twotype = "bike"; }
                      if($row[twotype] == 3){ $twotype = "run"; }
                      if($row[twotype] == 4){ $twotype = "paddle"; }
                      if($row[twotype] == 5){ $twotype = "ski"; }

                      if($row[threetype] == 1){ $threetype = "swim"; }
                      if($row[threetype] == 2){ $threetype = "bike"; }
                      if($row[threetype] == 3){ $threetype = "run"; }
                      if($row[threetype] == 4){ $threetype = "paddle"; }
                      if($row[threetype] == 5){ $threetype = "ski"; }
                      ?>
                      <strong>Course:</strong> <? echo "$row[swim] $oneunit $onetype, $row[bike] $twounit $twotype, $row[run] $threeunit $threetype"; ?><br />
                      <strong>Start:</strong> <? echo date("g:i A", $row[foo]); ?><br />
                      <strong>Location:</strong> <? echo "$row[city], $row[state]"; ?><br />
                      <strong>Address:</strong> <? echo stripslashes(stripslashes($row[address])); ?><br />
                      <strong>Contact:</strong> <? echo "$row[phone] &nbsp;&nbsp; | &nbsp;&nbsp; <a href='mailto:$row[email]'>$row[email]</a>"; ?><br />
                      <strong>Individual Price:</strong> <? echo $row[indivfee]; ?><br />
                      <strong>Team Price:</strong> <? echo $row[teamfee]; ?>
                    </div>
                    
                    <div class="race-info hide">
                        <strong>Date:</strong> <? echo date("F j, Y", $row[foo]); ?><br />
                        <strong>Course:</strong> <? echo "$row[swim] $oneunit $onetype, $row[bike] $twounit $twotype, $row[run] $threeunit $threetype"; ?><br />
                        <strong>Location:</strong> <? echo "$row[city], $row[state]"; ?><br />
                        <strong>Address:</strong> <? echo stripslashes(stripslashes($row[address])); ?><br />
                    </div>
                  </div>
                  <div class="col-1-1 detail-block">
                    <h3>General Information</h3>
                    <div class="highlight-block">
                      <? //Determine registration fees
                      if ($row[registration] == 1)
                      { $registration = "Yes, all registration/convenience fees are included in price or disclosed up front."; } 
                      else
                      { $registration = "Not specified whether registration/convenience fees are added."; }
                      ?>
                      <strong>Registration fees included or disclosed up front?:</strong> <? echo $registration; ?><br />
                      <strong>Course Info:</strong> <? echo nl2br(stripslashes(stripslashes($row[courseinfo]))); echo "<br />"; ?><br />
                      <strong>More:</strong> <? echo nl2br(stripslashes(stripslashes($row[moreinfo]))); echo "<br />"; ?><br />
                      <strong>Directions:</strong> <? echo nl2br(stripslashes(stripslashes($row[directions]))); echo "<br />"; ?><br />
                      <? if(strlen($row[register]) > 0){ 
                      $substring = "http";
                      if (!is_int(strpos($row[register], $substring))) {
                        $row[register] = "http://".$row[register];
                      }
                      ?>
                      <strong>Register:</strong> <? echo "<a target=\"_blank\" href=\"$row[register]\">Click here</a>"; ?>
                      <? } ?>

                    </div>
                  </div>
                  <? if ($row[lng] && $row[lat]) { ?>
                  <div class="col-1-1">
                    <div id="map" class="map map-single" data-name="single" data-category="race" data-lat="<? echo $row[lat] ?>" data-lng="<? echo $row[lng] ?>">
                    </div>

                    <div class="map-description">
                      <small>
                        Map location is based off of a geocode of the addressed entered using Google's Google Maps API. If your address does not geocode properly, you can either update the address (we will attempt to re-geocode after any updates to your entry) <strong>or</strong> you can use Google's geocoding tool found here: <a href="http://gmaps-samples.googlecode.com/svn/trunk/geocoder/singlegeocode.html">http://gmaps-samples.googlecode.com/svn/trunk/geocoder/singlegeocode.html</a> to obtain a lat/long for your location and enter that. To use the utility, type in an address that is close to your location and then drag-and-drop the marker to obtain the desired lat/long. If you choose to enter a lat/long, you <strong>must</strong> check the box on the edit page that says, "override geocode with entered lat/long."
                      </small>
                    </div>
                  </div>
                  <? } ?>
                  <div class="col-1-1 detail-block">
                    <h3>Who was in this race?</h3>
                    <div class="highlight-block">
                      <a name="racers"></a>
                      <? 
                      $testresults = mysql_query("SELECT racetags FROM gforum_Triathlons WHERE uid=$row[uid]");
                      $testrow = mysql_fetch_array($testresults);
                      $tags = explode(",", $testrow[racetags]);
                      $numcount = count($tags) - 1;
                      if($numcount == 0){ $phrase = "No users have tagged this race yet."; }
                      else if($numcount == 1){ $phrase = "There is 1 user who has tagged this race:"; } 
                      else{ $phrase = "There are $numcount users who have tagged this race: "; }
                      ?>
                      <? echo $phrase; ?>
                      <?
                      //List users
                      $i = 0;
                      foreach($tags as $value){
                      if($i > 0){
                      $tempresults = mysql_query("SELECT user_username FROM ".$prefix."User WHERE user_id='$value' and user_show_racetags=1") or die (mysql_error());
                      $temprow = mysql_fetch_array($tempresults);
                      if($i > 1){ echo ", "; }
                      // TODO: SLOWTWITCH EDIT
                      //echo "<a href=\"profiles/$temprow[user_username].html\">$temprow[user_username]</a>";
                      // replace the link above with the link below for integration into gforum
                      echo "<a href=\"http://forum.slowtwitch.com/gforum.cgi?username=$temprow[user_username]&session=".$_SESSION['session_id']."&from=racecalendar\">$temprow[user_username]</a>";
                      // TODO: SLOWTWITCH EDIT END
                      }
                      $i++;
                      }
                      ?>
                    </div>
                  </div>
                  <div class="col-1-1 detail-block">
                    <a name="editors"></a>
                    <? 
                    $testresults = mysql_query("SELECT * FROM gforum_TriathlonsEditors WHERE race_uid_fk=$row[uid] ORDER BY edit_timestamp DESC");
                    $numcount = mysql_num_rows($testresults);
                    if($numcount == 0){ $phrase = "No updates have been made this race yet."; }
                    else if($numcount == 1){ $phrase = "There has been 1 update to this race:"; } 
                    else{ $phrase = "There have been $numcount updates to this race: "; }
                    ?>
                    <h3><? echo $phrase; ?></h3>
                    <div class="highlight-block">
                    <?
                    //List users
                    $i = 0;
                    while($row=mysql_fetch_array($testresults)){
                    if($i >= 0){
                    $tempresults = mysql_query("SELECT user_username FROM ".$prefix."User WHERE user_id='$row[user_id_fk]'") or die (mysql_error());
                    $temprow = mysql_fetch_array($tempresults);
                    if($i >= 1){ echo "<br />"; }
                      echo "<a href=\"http://forum.slowtwitch.com/gforum.cgi?username=$temprow[user_username]&session=".$_SESSION['session_id']."&from=racecalendar\">$temprow[user_username]</a> at ".date("F j, Y g:i A", $row[edit_timestamp]);
                    }
                    $i++;
                    }
                    if($numcount <= 0){ echo "No users"; }
                    ?>
                    </div>
                  </div>
                </div>

                <div class="content-divider-bottom"></div>
                <? include ('comments_show.php'); ?>

              </div><!-- end col-2/3 -->


            </div><!-- end grid -->
          </div><!-- end content -->
        </section>

      </div><!-- end contentwrapper -->
    </div> <!-- end main -->

  <? include($common_path . "/templates/include_footer.php") ?>
  </div> <!-- container  -->
</body>
<? include($common_path . "/templates/include_global_js.php") ?>
</html>
