<?php
include("includes/site_info.php");
include_once __INCLUDE_CLASS_PATH."/class.Picture.php";
$picture = new Picture();

$option_manager = new OptionManager();
$currencysym = $option_manager->GetValue('currency_symbol');
$currency = $option_manager->GetValue('currency');
$currencypoint = $option_manager->GetValue('currency_point');

require 'includes/facebook.php';

$WUserID = $_SESSION['Sess_UserId'];
$WUsername = $_SESSION['Sess_UserName'];
$request=5;

if (isset($_GET['i']) && !empty($_GET['i'])) {
$_SESSION['inv_id'] = $_GET['i'];
}

if (isset($_GET['ref_id']) && !empty($_GET['ref_id'])) {
$_SESSION['ref_id'] = $_GET['ref_id'];
} 
if (isset($_REQUEST['VUserId'])) {
 $VUserID = $_REQUEST['VUserId'];
 $check_query=mysql_query("SELECT mem_userid FROM members LEFT JOIN adverts ON (mem_userid=adv_userid) where (mem_username='$VUserID' or  mem_profileid='$VUserID') AND mem_confirm='1' AND adv_approved='1'",$link);
 $check_count=mysql_num_rows($check_query);
 $check_array = mysql_fetch_object($check_query);
 if ($check_count > 0) { $VUserID = $check_array->mem_userid; }
 else { echo "<script>location.href='" . $CONST_LINK_ROOT . "/index.php'</script>"; die; }
} else {
$VUserID = $WUserID;
}
if ($VUserID == $WUserID && $WUserID!=0) {
echo "<script>location.href='" . $CONST_LINK_ROOT . "/home.php#ccover'</script>"; die; 
}
if ($VUserID != $WUserID && $WUserID!=0) {
    $query="SELECT * FROM encounters WHERE enc_userid = '$VUserID' AND enc_viewerid='$WUserID'";
    $result=mysql_query($query,$link) or die(mysql_error());
    $TOTAL = mysql_num_rows($result);
    if ($TOTAL == 0) {
        $tempdate=date("Y-m-d H:i:s");
        $result=mysql_query("INSERT INTO encounters (enc_userid, enc_viewerid, enc_viewdate,siteType) VALUES ('$VUserID','$WUserID','$tempdate','marketer')",$link) or die(mysql_error());
    } else {
        $tempdate=date("Y-m-d H:i:s");
        $result=mysql_query("UPDATE encounters SET enc_viewdate='$tempdate' WHERE enc_userid = '$VUserID' AND enc_viewerid='$WUserID' AND siteType='marketer'",$link) or die(mysql_error());
    }
	
	$retval=mysql_query("SELECT * FROM my_friends WHERE (uid='$WUserID' AND friend_uid='$VUserID') or (uid='$VUserID' AND friend_uid='$WUserID')",$link);
	$sql_array = mysql_fetch_object($retval);
	$result=mysql_num_rows($retval);

	if ($result == 0) {
	$request=1;
	} elseif ($sql_array->approved == 0) {
	$request=2;
	$retval=mysql_query("SELECT * FROM my_friends WHERE uid='$VUserID' AND friend_uid='$WUserID'",$link);
	$sql_array = mysql_fetch_object($retval);
	$result=mysql_num_rows($retval);
	if ($result == 0) {
	$request=2;
	} elseif ($sql_array->approved == 0) {
	$request=4;
	}else {
	$request=2;
	}
	} else {
	$request=0;
	}

}

$Vmember_query=mysql_query("SELECT *,(YEAR(CURDATE())-YEAR(adv_dob)) - (RIGHT(CURDATE(),5) < RIGHT(adv_dob,5)) AS age,DATE_FORMAT(mem_lastvisit, '%d-%b-%Y, %r') as mem_lastvisit, DATE_FORMAT(adv_dob, '%M %d') as adv_dob, DATE_FORMAT(mem_joindate, '%M %d, %Y') as mem_joindate FROM adverts LEFT JOIN members ON (adv_userid=mem_userid) LEFT JOIN Geo_Countries ON (adv_countryid = CountryId) WHERE adv_userid = '$VUserID' AND adv_approved='1'") or die(mysql_error());
$Vsql_array = mysql_fetch_object($Vmember_query);
$VTOTAL = mysql_num_rows($Vmember_query);

if (empty ($_SESSION['ref_id'])) {
$_SESSION['ref_id'] = $Vsql_array->mem_referCode;
}

$active_query=mysql_query("SELECT postID, case when char_length(A.title) > 250 then concat(LEFT(A.title,250), '...') else A.title end as title, A.title as Alltitle, forTitle, price, IFNULL(repostDate,postDate) as postDate, location as Location, DATE_FORMAT(expDate, '%M %d, %Y') as expDate, why, description, IFNULL(clickTotal,'0') as clickTotal2, IFNULL(ImpTotal,'0') as ImpTotal2, mem_userid, mem_username, status, IF(expDate< now(),'Expired',NULL) AS expired, IFNULL(E.dataName,D.dataName) as Category, price2 FROM market_Post A LEFT JOIN market_category_for on (optionFor=forID) LEFT JOIN members USING (mem_userid) LEFT JOIN Geo_Countries C on (A.Country=C.CountryId) LEFT JOIN market_Post_click USING ( postID ) LEFT JOIN market_category_data D on (subCat=D.dataID) LEFT JOIN market_category_data E on (subCatsub=E.dataID) where mem_userid=$VUserID and approve=1 and expDate > now() order by expDate desc") or die(mysql_error());
$active_total = mysql_num_rows($active_query);

function ago($datefrom,$dateto=-1)
{
        // Defaults and assume if 0 is passed in that
        // its an error rather than the epoch
   
        if($datefrom==0) { return "A long time ago"; }
        if($dateto==-1) { $dateto = time(); }
       
        // Make the entered date into Unix timestamp from MySQL datetime field

        $datefrom = strtotime($datefrom);
   
        // Calculate the difference in seconds betweeen
        // the two timestamps

        $difference = $dateto - $datefrom;

        // Based on the interval, determine the
        // number of units between the two dates
        // From this point on, you would be hard
        // pushed telling the difference between
        // this function and DateDiff. If the $datediff
        // returned is 1, be sure to return the singular
        // of the unit, e.g. 'day' rather 'days'
   
        switch(true)
        {
            // If difference is less than 60 seconds,
            // seconds is a good interval of choice
            case(strtotime('-1 min', $dateto) < $datefrom):
                $datediff = $difference;
                $res = ($datediff==1) ? $datediff.' second ago' : $datediff.' seconds ago';
                break;
            // If difference is between 60 seconds and
            // 60 minutes, minutes is a good interval
            case(strtotime('-1 hour', $dateto) < $datefrom):
                $datediff = floor($difference / 60);
                $res = ($datediff==1) ? $datediff.' minute ago' : $datediff.' minutes ago';
                break;
            // If difference is between 1 hour and 24 hours
            // hours is a good interval
            case(strtotime('-1 day', $dateto) < $datefrom):
                $datediff = floor($difference / 60 / 60);
                $res = ($datediff==1) ? $datediff.' hour ago' : $datediff.' hours ago';
                break;
            // If difference is between 1 day and 7 days
            // days is a good interval               
            case(strtotime('-1 week', $dateto) < $datefrom):
                $day_difference = 1;
                while (strtotime('-'.$day_difference.' day', $dateto) >= $datefrom)
                {
                    $day_difference++;
                }
               
                $datediff = $day_difference;
                $res = ($datediff==1) ? 'yesterday' : $datediff.' days ago';
                break;
            // If difference is between 1 week and 30 days
            // weeks is a good interval           
            case(strtotime('-1 month', $dateto) < $datefrom):
                $week_difference = 1;
                while (strtotime('-'.$week_difference.' week', $dateto) >= $datefrom)
                {
                    $week_difference++;
                }
               
                $datediff = $week_difference;
                $res = ($datediff==1) ? 'last week' : $datediff.' weeks ago';
                break;           
            // If difference is between 30 days and 365 days
            // months is a good interval, again, the same thing
            // applies, if the 29th February happens to exist
            // between your 2 dates, the function will return
            // the 'incorrect' value for a day
            case(strtotime('-1 year', $dateto) < $datefrom):
                $months_difference = 1;
                while (strtotime('-'.$months_difference.' month', $dateto) >= $datefrom)
                {
                    $months_difference++;
                }
               
                $datediff = $months_difference;
                $res = ($datediff==1) ? $datediff.' month ago' : $datediff.' months ago';

                break;
            // If difference is greater than or equal to 365
            // days, return year. This will be incorrect if
            // for example, you call the function on the 28th April
            // 2008 passing in 29th April 2007. It will return
            // 1 year ago when in actual fact (yawn!) not quite
            // a year has gone by
            case(strtotime('-1 year', $dateto) >= $datefrom):
                $year_difference = 1;
                while (strtotime('-'.$year_difference.' year', $dateto) >= $datefrom)
                {
                    $year_difference++;
                }
               
                $datediff = $year_difference;
                $res = ($datediff==1) ? $datediff.' year ago' : $datediff.' years ago';
                break;
               
        }
        return $res;
	}

	$meta = "<title>Marketer - Wauhaha! Internet Routine Activity on Demand</title>\n";

include("includes/header.php");
?>
<?php if (isset($WUserID)) { ?>
	<div id="top_panel" class="submenu">
		<ul id="submenu">
			<li><a href="<?=$CONST_LINK_ROOT?>/home.php#mcover" id="tab_maincover" rel="sub_link">Cover</a></li>
			<li><a href="<?=$CONST_LINK_ROOT?>/home.php#mcategories" id="tab_maincategories" rel="sub_link">Categories</a></li>
			<li><a href="<?=$CONST_LINK_ROOT?>/home.php#msearch" id="tab_mainsearch" rel="sub_link">Search</a></li>
		</ul>
	</div>
	<?php } ?>
<script type="text/javascript" src="http://cdn.socialtwist.com/2010091044975-2/script.js"></script>
<script type="text/javascript">
jQuery.noConflict();
		
function hidePost(itemID) {

	jQuery("#"+itemID).fadeOut(500);
}
		
  </script>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="5" class="white_back_t_xb">
				<tr>
					<td><div id="showContent"><?php if($active_total>0) {?>
<div id="search_result" class="result_box padding_5" rel="hotPostLoad">
	<?php
					$i=0;
					while ($sql = mysql_fetch_object($active_query) ) {
					$i++;
					$encode = mb_detect_encoding(strip_tags(html_entity_decode($sql->description, ENT_QUOTES, "UTF-8")));
					if($encode=="UTF-8") { $desc = str_replace("&", "&amp;", mb_substr(strip_tags(html_entity_decode($sql->description, ENT_QUOTES, "UTF-8")), 0, 190, "UTF-8")); if (strlen (strip_tags(html_entity_decode($sql->description, ENT_QUOTES, "UTF-8")))>190) $desc.="...";
					} elseif ($encode=="ASCII") { $desc = str_replace("&", "&amp;", substr(strip_tags(html_entity_decode($sql->description, ENT_QUOTES, "UTF-8")), 0, 250)); if (strlen (strip_tags(html_entity_decode($sql->description, ENT_QUOTES, "UTF-8")))>250) $desc.="...";
					} else { $desc = str_replace("&", "&amp;", substr(strip_tags(html_entity_decode($sql->description, ENT_QUOTES, "UTF-8")), 0, 250)); if (strlen (strip_tags(html_entity_decode($sql->description, ENT_QUOTES, "UTF-8")))>250) $desc.="...";
					} 
					
					$photo_query=mysql_query("SELECT photoMed FROM market_PostPhoto where mem_userid=$sql->mem_userid and postID=$sql->postID order by photoID asc limit 1") or die(mysql_error());
					$photoTotal = mysql_num_rows($photo_query);
					if($photoTotal>0) {
					$sqlPhoto = mysql_fetch_object($photo_query);
					if(!empty($sqlPhoto->photoMed)) $url = $sqlPhoto->photoMed; 
					else $url = "images/noimage.jpg";
					}
					else $url = "images/noimage.jpg";

						$comment_query = mysql_query("SELECT COUNT(comment_id) AS CommentTotal FROM market_Post_comment where postID=$sql->postID") or die(mysql_error());
						$comment = mysql_fetch_object($comment_query);
						
if ($WUserID==$sql->mem_userid) {
$pic_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$sql->mem_userid' and pic_default='Y'") or die(mysql_error()); 
} else {
$pic_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$sql->mem_userid' and pic_default='Y' and pic_private='N'") or die(mysql_error()); 
}
$pics_count = mysql_num_rows($pic_query);
$aPicture = mysql_fetch_object($pic_query);
if ($pics_count > 0) {
	$medium = $aPicture->pic_picture."_ps.".$aPicture->ext;
	$male1_picture1 = "$SITE_MAIN_WWW_ROOT/members/$sql->mem_username/picture/$medium";
             }else{
$male1_picture1=$CONST_LINK_ROOT."/".$picture->GetProfilePicture($sql->mem_userid);
}

$price = number_format(ceil($sql->price/$currencypoint),0);
$price2 = number_format(ceil($sql->price2/$currencypoint),0);
						?>
	<div class="content4 border-1px-light<?php if(!empty($sql->status) || !empty($sql->expired)) echo " redlight_row_back";?>" id="<?=$i?>_post<?=$sql->postID?>">
<div class="delete"><a href="javascript:;" onclick="javascript:hidePost('<?=$i?>_post<?=$sql->postID?>');">x</a></div>
<div class="share"><fb:like href="<?=$CONST_LINK_ROOT."/MKT".$sql->postID?>" send="true" layout="box_count" show_faces="false"></fb:like><a class="email" href="javascript:;" onclick="STTAFFUNC.cw(this, {id:'2010091044975-2', link: '<?=$CONST_LINK_ROOT."/MKT".$sql->postID?>', title: '<?php echo ucwords($sql->title); if(!empty($price)) echo " for ".$price;?>' }); return false;">Email</a></div>
		<div class="content5">
		<div class="image_w100h1003"><a href="<?=$CONST_LINK_ROOT."/MKT".$sql->postID?>" onclick="javascript: jQuery.facebox({ajax:'<?=$CONST_LINK_ROOT?>/mshowlistPop.php?tab=categories&postID=<?=$sql->postID?>&uID=<?=$sql->mem_userid?>'}, '<?=$sql->forTitle?>', 'hotpost'); return false;"><img src="<?=$url?>" width="170" height="170" border="0" /></a></div>
		<span class="rightContent5"><strong>
		<?=$sql->forTitle?>
		:</strong> <a href="<?=$CONST_LINK_ROOT."/MKT".$sql->postID?>" onclick="javascript: jQuery.facebox({ajax:'<?=$CONST_LINK_ROOT?>/mshowlistPop.php?tab=categories&postID=<?=$sql->postID?>&uID=<?=$sql->mem_userid?>'}, '<?=$sql->forTitle?>', 'hotpost'); return false;" class="blue_b"> <?php echo ucwords($sql->title); if(!empty($price)) echo " for <img src=\"images/iconPA_o.gif\" border=\"0\" align=\"absmiddle\" />".$price;?></a> <br />
		<span class="rightContent6">
		<?php /*if(!empty($sql->why)) { echo ucwords($sql->why); } else*/ echo ucfirst($desc);?>
		</span> </span><span class="lightgrey_10">
		<span class='grey_10'><?=$sql->Category?></span><br />
		<?=ucwords($sql->Location)?><br />
		<?=ago($sql->postDate)?>
		</span>
		<div class="clear"><img src="images/spacer.gif" height="5" /></div>
		<span class="grey_10"> <span class="orangeBackp3">Clicks: <strong>
		<?=$sql->clickTotal2?>
		</strong></span> <span class="orangeBackp3">Impressions: <strong>
		<?=$sql->ImpTotal2?>
		</strong></span> <span class="orangeBackp3">Comments: <strong>
		<?=$comment->CommentTotal?>
		</strong></span> <span class="orangeBackp3">Expiration: <strong>
		<?=$sql->expDate?>
		</strong></span> <span class="orangeBackp3">Promo Link: <strong>
		<?=$CONST_LINK_ROOT."/MKT".$sql->postID?>
		</strong></span> </span> 
		</div>
		</div>
	<?php 
	} ?>
<div class="clear"><img src="images/spacer.gif" /></div>
</div>
<?php
						} else {
						 print(NO_RECORDS_SECTION_NAME."!"); 
						 }?>
</div></td>
				</tr>
			</table></td>
	</tr>
</table>
<?php include("includes/footer.php"); ?>
