<?
  /* Main include info */
  ini_set('include_path',"/usr/web/sites/tuve.tv/include/");
  include("site.inc");

  if ($data[user] == "") {
    Header("Location: /index.php");
    exit(0);
    }

  /* Assign the page title */
  $data[tpl]->assign("PAGE_TITLE","Profile");

  /* Main defines */
  $data[tpl]->define(array(index=>"index/index.html"));
  $data[tpl]->define(array(body=>"/profile.html"));

  //DoSession();

  $query = "SELECT uid,username,caption,gender,bmonth,byear,city,state_prov,country,main_image,profile_image,registered,about_me FROM users WHERE username LIKE '$data[user]'";
  $res = mysql_query($query);


  if (mysql_numrows($res) == 0) {
    Header("Location: /people.php\n");
    exit;
    $data['tpl']->assign("USER","Unknown");
    }
  else {
    /* Set up age */
    $byear = mysql_result($res,0,'byear');
    $bmonth = mysql_result($res,0,'bmonth');

    $age = date("Y") - $byear;
   
    if ($bmonth > date("m"))
      $age--;
    $data['tpl']->assign("AGE",$age);

    /* Set up location */
    $location = mysql_result($res,0,'city') . ", ";
    $location .= mysql_result($res,0,'state_prov') . "<BR>" . mysql_result($res,0,'country');
    $data['tpl']->assign("LOCATION",$location);

    /* Set up username */
    $user = mysql_result($res,0,'username');
    $data['tpl']->assign("USER",$user);

    /* Set Caption */
    $data['tpl']->assign("CAPTION",mysql_result($res,0,'caption'));

    /* Set Gender */
    $gender = mysql_result($res,0,'gender');

    if ($gender == 1)
      $data['tpl']->assign("GENDER","Male");
    else if ($gender == 2)
      $data['tpl']->assign("GENDER","Female");
    else
      $data['tpl']->assign("GENDER","Undefined");

    /* Set Profile Image */
    $mid = mysql_result($res,0,'profile_image');
    if ($mid == 0)
      $data['tpl']->assign("PROFILE_IMAGE",mysql_result($res,0,'main_image'));
    else
      $data['tpl']->assign("PROFILE_IMAGE",$avatars[$mid]);

    /* Set Registered */
    $data['tpl']->assign("REGISTERED",date("M Y",mysql_result($res,0,'registered')));

    /* Set About Me */
    $data['tpl']->assign("ABOUT_ME",fixHTML(mysql_result($res,0,'about_me')));

    /* Channels */
    $uid = mysql_result($res,0,'uid');
  
    $data['tpl']->assign("PID",$uid);

    $query = "SELECT * FROM channels LEFT JOIN videos ON channels.nowplaying = videos.vid WHERE channels.oid = $uid ORDER BY channel";
    $result = mysql_query($query);

    for ($i=0;$i<mysql_numrows($result);$i++) {
      $channel = mysql_result($result,$i,'channel');
      $artist  = mysql_result($result,$i,'artist');
      $title = mysql_result($result,$i,'title');
      $thumb = mysql_result($result,$i,'thumb');
      $topic = mysql_result($result,$i,'topic');
      if ($thumb == "")
        $thumb = "UbixCube.gif";
      $chan2 = substr($channel,1,strlen($channel)-1);

      if (strlen($topic) > 65)
        $ebr = "<BR>";
      else
        $ebr = "";

      $rw .= "<TR><TD><A HREF=\"javascript:startTUve('$chan2')\"><IMG BORDER=\"0\" SRC=\"$thumb\" WIDTH=\"100\"></A></TD><TD>&nbsp;</TD><TH ALIGN=\"LEFT\">Channel:<BR>Topic:$ebr<BR>Playing:</TH><TD>&nbsp;</TD><TD><A HREF=\"javaqscript:startTUve('$chan2')\">$channel</A><BR>$topic<BR>$artist - $title</TD></TR>\n";
      $rw .= "<TR><TD COLSPAN=\"5\"><HR></TD></TR>\n";
      }
    $data['tpl']->assign("CHANNELS",$rw);
 
    if (mysql_numrows(mysql_query("SELECT userid FROM active WHERE uid = $uid")) == 1)
      $data['tpl']->assign("ONLINE","Currently Online");
    else
      $data['tpl']->assign("ONLINE","Currently Offline");

    $query = "SELECT vid,artist,title,thumb FROM videos WHERE oid = $uid ORDER BY vid DESC";
    $result = mysql_query($query);

    $rows = mysql_numrows($result);

    if ($rows > 16) {
      $rows = 16;
      $m = 1;
      }
    else
      $m = 0;

    for ($i = 0;$i < $rows;$i++) {
      $artist = mysql_result($result,$i,'artist');
      $title = mysql_result($result,$i,'title');
      $thumb = mysql_result($result,$i,'thumb');
      $vid   = mysql_result($result,$i,'vid');
      if ($media == "")
        $media = "<TR>";
      else if (($i % 4) == 0)
        $media .= "</TR><TR>";

      $media .= "<TD ALIGN=\"CENTER\"><A HREF=\"http://www.tuve.tv/watch/$vid\"><IMG SRC=\"$thumb\" WIDTH=\"100\"><BR>$artist - $title</A></TD>";
      }
   if ($m == 0)
     $media .= "</TR>";
   else
     $media .= "</TR><TR><TD COLSPAN=\"4\"><A HREF=\"\">See All</A></TD></TR>";

   $data['tpl']->assign("MEDIA",$media);

    $query = "SELECT bid,fid,main_image,profile_image,username FROM friends f INNER JOIN users u ON f.fid = u.uid WHERE f.oid = $uid AND approved = 1 ORDER BY fid DESC";
    $result = mysql_query($query);

    $rows = mysql_numrows($result);

    if ($rows > 16) {
      $rows = 16;
      $m = 1;
      }
    else
      $m = 0;

    for ($i = 0;$i < $rows;$i++) {
      /* Set Profile Image */
      $mid = mysql_result($result,$i,'profile_image');
      if ($mid == 0)
        $thumb = mysql_result($result,$i,'main_image');
      else
        $thumb = $avatars[$mid];

      $username = mysql_result($result,$i,'username');
      if ($buddies == "")
        $buddies = "<TR>";
      else if (($i % 4) == 0)
        $buddies .= "</TR><TR>";
      $buddies .= "<TD ALIGN=\"CENTER\"><A HREF=\"/$username\"><IMG SRC=\"$thumb-small.png\" BORDER=\"0\"><BR>$username</A></TD>\n";
      }
   if ($m == 0)
     $buddies .= "</TR>";
   else
     $buddies .= "</TR><TR><TD COLSPAN=\"4\"><A HREF=\"\">See All</A></TD></TR>";

   $data['tpl']->assign("BUDDIES",$buddies);

   $query = "SELECT fid,pdate,message,username,main_image,profile_image FROM comments c INNER JOIN users u ON c.fid = u.uid WHERE oid = $uid ORDER BY pdate DESC";
   $res = mysql_query($query);

   for ($i = 0;$i < mysql_numrows($res);$i++) {
     $user = mysql_result($res,$i,'username'); 
    /* Set Profile Image */
    $mid = mysql_result($res,$i,'profile_image');
    if ($mid == 0)
      $thumb = mysql_result($res,$i,'main_image');
    else
      $thumb = $avatars[$mid];

     $pdate = date("m/d/Y H:i:s",mysql_result($res,$i,'pdate'));
     $message = mysql_result($res,$i,'message');
     $comments .= "<TR><TD ALIGN=\"CENTER\"><A HREF=\"/$user\">$user</A></TD><TD>&nbsp;</TD><TD><B>$pdate</B></TD></TR><TR><TD><A HREF=\"/$user\"><IMG SRC=\"$thumb-small.png\"></A></TD><TD>&nbsp;</TD><TD VALIGN=\"TOP\">$message</TD></TR>";
     $comments .= "<TR><TD COLSPAN=\"3\">&nbsp;</TD></TR>\n";
     }

   $data['tpl']->assign("COMMENTS",$comments);
   }


  /* Parse and print */
  $data[tpl]->parse(BODY, array("body"));
  $data[tpl]->parse(INDEX, array("index"));
  $data[tpl]->FastPrint("INDEX");
?>
