<?php
$ajaxurl = 'http://www.theartoutlet.com/facebook/paintings/';

require_once 'appinclude.php';
require_once 'dbappinclude.php';

/*echo "<pre>App : ";
	print_r($_POST);
echo "</pre>";*/

//$fb_sig_user=$_POST['fb_sig_user']; //gets the user who is using the app
$is_tab = isset($_POST['fb_sig_in_profile_tab']);

if($is_tab)
{
	$fb_sig_user=$_POST['fb_sig_profile_user']; //gets the user who is using the app
	$fb_sig_pages_id = $_POST['fb_sig_page_id'];
}
else
{
	$fb_sig_user=$_POST['fb_sig_user']; //gets the user who is using the app
}

if($_GET['fb_page_id'] != ""){
	$_SESSION['fb_page_id'] = $_GET['fb_page_id']; 
	$page_id= $_SESSION['fb_page_id'];
	$page_info = $facebook->api_client->fql_query("SELECT has_added_app,name FROM page WHERE page_id = $page_id");
	for ($i=0; $i < count($page_info); $i++) {
	$record = $page_info[$i];
	$has_added_app=$record['has_added_app'];
	$admin = $facebook->api_client->pages_isAdmin("$page_id");
	}
}
else if($is_tab) 
{
	//$page_id= '6665652845';
	$page_id= $_SESSION['fb_page_id'];
	//$page_info = $facebook->api_client->fql_query("SELECT has_added_app,name FROM page WHERE page_id = $page_id");
	/*for ($i=0; $i < count($page_info); $i++) {
	$record = $page_info[$i];
	$has_added_app=$record['has_added_app'];
	$admin = $facebook->api_client->pages_isAdmin("$page_id");
	}*/
}
/*echo "Page id is : " . $fb_sig_pages_id;
echo "<br>User is : " . $fb_sig_user;*/

if($fb_sig_user==$fb_sig_pages_id)
{	
	include_once('browse.php');
	exit();
}

echo "<fb:title>Browse for Art...</fb:title>";
echo "<style type=\"text/css\"> .full-column { padding-left: 5px; padding-right: 5px; }
 .left_tabs{padding:0px !important;}
 .left-column { float: left; width: 24%; margin: 0px; }
 .left-column-content { border: 1px solid lightgrey; margin-top: 0px; margin-bottom: 10px; padding: 0px 0px 0px 0px; }
 .left-column-bottom { border: 1px solid lightgrey; margin-top:10px; margin-bottom:10px; padding: 0px 0px 0px 0px; }
 .right-column { float: right; width: 76%; margin: 0px; }
 .right-column-content { border: 1px solid lightgrey; padding-right: 0px; padding-left: 0px; padding-top: 0px; margin-bottom: 10px; }
 .box_head { border-top: 1px solid #3b5998; border-bottom: 1px solid #cccccc; padding: 2px 10px;margin-top: 13px; background: #d8dfea none repeat scroll 0% 50%; color: #3b5998; font-weight: bold; }
 .box-content { border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-bottom: 1px solid #cccccc; padding: 5px; background: white none repeat scroll 0% 0%; }
 .image_hover:hover { border: 1px solid lightgrey; background-color: lightyellow; }
 .delete_link {font-size:10px; text-decoration: underline; color:#ae0011;}
 .delete_link:hover {font-size:10px; text-decoration: none; color:#ae0011;}
/* ul.list_artist {list-style-image: url(	'http://lib.store.yahoo.net/lib/overstockart/subnav-blt.gif');}*/
 li a.menu_artist { text-decoration: none; font-weight:bold; margin-left:10px;}
 li a.menu_artist:hover { background-color: lightyellow; text-decoration: underline;  }
 li.menu_artist_on { font-weight:bold; background-color: transparent; color:#AE0011; margin-left:10px;}
 .comment_submit{background-color:#3b59a4; color:#FFFFFF; border:none; font-size:11px; padding:7px; margin-top:3px; margin-left:600px}
</style>";

if($is_tab)
{
	echo "<a href=\"http://apps.facebook.com/artoftheday/\" class=\"comment_submit\">Add My Art Gallery App >></a>";
}

$str_artist = stripslashes($artist);

if($op=='delete') //DELETE ART
{
$queryD = "DELETE FROM `gallery` WHERE `facebook_id` = '$fb_sig_user' AND `art_id` = '$artID';"; //
$result1 = mysql_query($queryD);
}


// Query for record set
$query1 = "SELECT DISTINCT art_id FROM `gallery` WHERE facebook_id = '$fb_sig_user' ORDER BY time"; //
$result1 = mysql_query($query1);
  
if ($myrow = mysql_fetch_array($result1)) {
$nume=mysql_num_rows($result1);
}

if(!isset($start)) { // This variable is set to zero for the first page
$start = 0;
}

$eu = ($start - 0);
$limit = 6; // No of records to be shown per page.
$back = $eu - $limit;
$next = $eu + $limit;

if (isset($_GET['sortby']))
$sortwith = $_GET['sortby']; // this be the real magic
else if (isset($sortby)) $sortwith = $sortby;
else $sortwith = "time";

if(!$is_tab)
{
echo "<div style=\"padding: 10px;\"><fb:tabs>  <fb:tab_item href=\"index.php?fb_page_id=$page_id\" title=\"Random Art\" />  <fb:tab_item href=\"browse.php?fb_page_id=$page_id\" title=\"Browse for Art\" /><fb:tab_item href=\"gallery.php?fb_page_id=$page_id\" title=\"My Gallery\" /><fb:tab_item href=\"invite.php\" title=\"Invite Friends\" /></fb:tabs>";
}

echo "<div class=\"box_head\"><table align ='center'><tr>";
if ($nume == 0)
  {
 echo "<td width=\"540\">&nbsp;</td>";
  }
else
{
	echo "<td align='left' width='10%'>";
	if($back >=0) 
	{
	echo "<a href=\"gallery.php?start=$back&sortby=$sortwith&fb_page_id=$page_id\">&laquo; PREV</a>";
	}
	else
	{
	echo "&laquo; PREV";
	}
	
	echo "</td><td align=center width='60%'><center>";
	$i=0;
	$k=1;
	for($i=0; $i<$nume; $i=$i+$limit){
	if($i <> $eu){
	echo " <a href=\"gallery.php?start=$i&sortby=$sortwith&fb_page_id=$page_id\">$k</a> ";
	}
	else { 
	echo "<font color='#ae0011'>$k</font>";
	$current = $k;
	} 
	/// Current page is not displayed as link and given font color red
	$k=$k+1;
	}
	
	echo "</center></td><td align='center' width='10%'>";
	if($nume > $next) {
	echo "<a href=\"gallery.php?start=$next&sortby=$sortwith&fb_page_id=$page_id\">NEXT &raquo;</a>";
	}
	else
	{
	echo "NEXT &raquo;";
	}
	echo "</td>";
}
//echo "<td align='right' width='20%'>Displaying $limit of $nume </td>";
echo "</tr></table></div>";

//categories
// Query for record set
$query2 = "SELECT DISTINCT SubDeptName1 FROM `framed-feed` WHERE SubDeptName1 <> '' ORDER BY SubDeptName1 ASC"; //
$result2 = mysql_query($query2);
echo "<div class=\"left-column\"><div class=\"left-column-content\"><div class=\"box_head\">Choose an Artist...</div><ul class=\"list_artist\">";
while ($row = mysql_fetch_array($result2))
{
	/*if ($row[0] == $str_artist)
	{
		echo "<li class=\"menu_artist_on\">" . $row[0] . "</li>";
	}
	else
		echo "<li><a class=\"menu_artist\" href=\"browse.php?artist=$row[0]&fb_page_id=" . $page_id . "\">" . $row[0] . "</a></li>";*/
	$artist_check = str_replace(" ","+",$row[0]);
	if ($artist_check == str_replace(" ","+",$artist))
	{
		echo "<li class=\"menu_artist_on\">" . $row[0] . "</li>";
	}
	else
		echo "<li><a class=\"menu_artist\" href=\"browse.php?artist=$artist_check&fb_page_id=$page_id\">" . $row[0] . "</a></li>";
}
echo "</ul></div>";


// Get list of friends who have this app installed... 
$rs = $facebook->api_client->fql_query("SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = $user)"); 
// Build a list of friends
echo "<div class=\"left-column-content\"><div class=\"box_head\">Your Friends...</div>";
if ($rs) 
{ 
	for ( $i = 0; $i < count($rs); $i++ ) 
	{ 
	$friend_id = $rs[$i]["uid"];
	echo "<fb:profile-pic uid=\"$friend_id\" linked=\"true\"  size=\"square\" />";
	} 
} 
else 
{
echo "<p style=\"color:#ae0011;font-weight:bold;text-align:center;\">Sorry... none of your friends have this app...</p>";
}
echo "<br><center><a href=\"invite.php\">Invite Your Friends...</a></center><br></div>";

//logo
echo "<div class=\"left-column-bottom\"><a href=\"http://www.overstockart.com/index.html?utm_source=facebook&utm_medium=app&utm_campaign=app1\"><img src=\"http://site.overstockart.com/images/osa.gif\" border=\"0\" width=\"175\" height=\"50\"></a></div>";

echo "</div>";

$html = "<div class=\"right-column\"><div class=\"right-column-content\"><div class=\"box_head\">Paintings in Your Art Collection - Page $current </div><table align=\"center\"><tbody>";
$count = 3;
$i = 0;
$html .= "<tr width=540 >";

//QUERY FOR ALL THE ART in the gallery
$query = "SELECT DISTINCT art_id FROM `gallery` WHERE facebook_id = '$fb_sig_user' ORDER BY $sortwith DESC LIMIT $eu, $limit";
//echo $query;
//FIND THE THUMBNAIL OF ART
$path_thumb = "http://site.overstockart.com/sorter/shadow/";

$result = mysql_query($query);

if ($nume == 0)
  {
 $html .= "<td width=\"540\" style=\"color:#ae0011;font-weight:bold;text-align:center;\">Sorry... You don't have any art in your gallery...</td>";
  }

while ($row_id = mysql_fetch_array($result))
{
$art_id = $row_id['art_id'];
//QUERY FOR ALL THE ART ON THE PAGE
$query2 = "SELECT * FROM `framed-feed` WHERE YahooID = '$art_id'";
$result2 = mysql_query($query2);
while ($row = mysql_fetch_array($result2))
{
$imageCaption = $row['Caption'];
$imagePartId = $row['CatalogSK'];
$part_id = substr($imagePartId,0,strpos($imagePartId,'-'));
$artist = $row['SubDeptName1'];
$path_to_thumb = $path_thumb . $part_id. '.jpg';
$path_to_thumb_gif = $path_thumb . $part_id . '.gif';
if(url_exists($path_to_thumb))
{
// file exists
}
else if(url_exists($path_to_thumb_gif))
{
//gif file exists...
$path_to_thumb = $path_to_thumb_gif;
}
else
{
// file doesn't exists
continue;
}
$imageFile = $row['Filename'];

//STRIPS THE FRAME INFO
$art_name = substr($imageCaption,0,strrpos($imageCaption,'with'));

$imageID = $row['YahooID'];
$html .= "<td class=\"image_hover\" width=\"180\"><center>";
$html .= "<a href=\"index.php?image_id=$imageID&fb_page_id=$page_id\">";
$html .= "<img src=\"" . $path_to_thumb . "\"></a></center><br>";
if(!$is_tab) {
	$html .= "<center><a href=\"index.php?image_id=$imageID&fb_page_id=$page_id\" >" . $art_name . "</a><br><a class=\"delete_link\" href=\"gallery.php?artID=$imageID&fb_page_id=$page_id&op=delete&start=$start\" >(-) remove</a></center>";
}	
else
{
	$html .= "<center><a href=\"index.php?image_id=$imageID&fb_page_id=$page_id\" >" . $art_name . "</a><br></center>";
}
$html .= "<br><center><fb:share-button class=\"meta\">
<meta name=\"title\" content=\"" . $art_name . "\" />
<meta name=\"description\" content=\"Check out this painting: " . $art_name . " by " . $artist . " one of my all time favorites!\" />
<link rel=\"image_src\" href=\"" . $path_to_thumb . "\" />
<link rel=\"target_url\" href=\"http://www.overstockart.com/" . $imageID .".html?utm_source=facebook&utm_medium=app&utm_term=$imageID&utm_campaign=app2\" />
</fb:share-button></center><br>";
//$html .= "<center><a href=\"index.php?image_id=$imageID&fb_page_id=$page_id\" >" . $art_name . "</a><br><a class=\"delete_link\" href=\"gallery.php?artID=$imageID&fb_page_id=$page_id&op=delete&start=$start\" >(-) remove</a></center>";
$html .= "</td>";

	$i++;
	 if ($i == $count)
		{
		 $html .= "</tr><tr>";
		 $i = 0;
		}
}
}
$html .= "</tbody></table></div>";

//THE CURRENT ART ON YOUR WALL...
$html .= "<div class=\"right-column-content\"><div class=\"box_head\">Currently on Your Profile Wall... </div><table align=\"center\"><tbody>";
$count = 3;
$i = 0;
$html .= "<tr width=540 >";

//QUERY FOR ALL THE ART in the gallery
$query = "SELECT art_id FROM `facebook` WHERE facebook_id = '$fb_sig_user' ORDER BY time DESC LIMIT 1";
//echo $query;
//FIND THE THUMBNAIL OF ART
$path_thumb = "http://site.overstockart.com/sorter/shadow/";

$result = mysql_query($query);
$on_wall=mysql_num_rows($result);
if ($on_wall == 0)
  {
 $html .= "<td width=\"540\" style=\"color:#ae0011;font-weight:bold;text-align:center;\">Sorry... You don't have any art on your profile...</td>";
  }
while ($row_id = mysql_fetch_array($result))
{
$art_id = $row_id['art_id'];
//QUERY FOR ALL THE ART ON THE PAGE
$query2 = "SELECT * FROM `framed-feed` WHERE YahooID = '$art_id'";
$result2 = mysql_query($query2);
while ($row = mysql_fetch_array($result2))
{
$imageCaption = $row['Caption'];
$imagePartId = $row['CatalogSK'];
$artist = $row['SubDeptName1'];
$part_id = substr($imagePartId,0,strpos($imagePartId,'-'));
$path_to_thumb = $path_thumb . $part_id . '.jpg';

if(url_exists($path_to_thumb))
{
// file exists
} else {
// file doesn't exists
continue;
}
$imageFile = $row['Filename'];

//STRIPS THE FRAME INFO
$art_name = substr($imageCaption,0,strrpos($imageCaption,'with'));

$imageID = $row['YahooID'];
$html .= "<td class=\"image_hover\" width=\"180\"><center>";
$html .= "<a href=\"index.php?image_id=$imageID&fb_page_id=$page_id\">";
$html .= "<img src=\"" . $path_to_thumb . "\"></a></center><br>";
$html .= "<center><a href=\"index.php?image_id=$imageID&fb_page_id=$page_id\" >" . $art_name . "</a></center>";
$html .= "<br><center><fb:share-button class=\"meta\">
<meta name=\"title\" content=\"" . $art_name . "\" />
<meta name=\"description\" content=\"Check out this painting: " . $art_name . " by " . $artist . " one of my all time favorites!\" />
<link rel=\"image_src\" href=\"" . $path_to_thumb . "\" />
<link rel=\"target_url\" href=\"http://www.overstockart.com/" . $imageID .".html?utm_source=facebook&utm_medium=app&utm_term=$imageID&utm_campaign=app2\" />
</fb:share-button></center><br>";
$html .= "</td>";

	$i++;
	 if ($i == $count)
		{
		 $html .= "</tr><tr>";
		 $i = 0;
		}
}
}
$html .= "</tbody></table></div>";

/*$html .= "<div><img src=\"http://www.theartoutlet.com/facebook/paintings/images/bookmark_tip.png\" border=\"0\"></div></div></div>";*/
$html .= "</div></div>";
print "$html";

//close database
mysql_close($conn);
?>


<script type="text/javascript"><!--
/* This is the call...
onclick=\"do_ajax('delayedpicker','$ajaxurl . index.php?image_id=$imageID');return false;\"
*/
//FBJS AJAX, using FBML (modified from example on wiki):
function do_ajax(div,val) {
	var ajax = new Ajax();
	/*var callback = <?=$ajaxurl?> + val;*/
	ajax.responseType = Ajax.FBML;
	ajax.ondone = function(data) {
			document.getElementById(div).setInnerFBML(data);
	}
	ajax.requireLogin = 1;
	ajax.post(val);
}
//-->
</script>