<?php
///////////////////// ADD BOOKMARK
if (isset($_SESSION['id']) and isset($_GET['id']) and $_GET['mes']=='add') {
	$userId = $_SESSION['id'];
	$pageId = $_GET['id'];
	$res = mysql_query ("INSERT INTO user_page (UserId, PageId) VALUES ('$userId', '$pageId') ");
	//echo $userId . " | " . $pageId . " | " . $_GET['mes'];
	$note = 'Page bookmark added.';
	
	// INSER NOTE
	$query = "INSERT INTO note (PageId, Content) VALUES ('$pageId', '$note') ";
	mysql_query ($query);
	
	// INSERT USER_PAGE_NOTE
	$noteId = mysql_insert_id();
	$query = "INSERT INTO user_note (UserId, NoteId) VALUES ('$userId', '$noteId') ";
	mysql_query ($query);
	
	
}

///////////////////// REMOVE BOOKMARK
if (isset($_SESSION['id']) and isset($_GET['id']) and $_GET['mes']=='rem') {
	$userId = $_SESSION['id'];
	$pageId = $_GET['id'];
	$res = mysql_query ("DELETE FROM user_page WHERE Userid='$userId' AND PageId='$pageId' ");
	//echo $userId . " | " . $pageId . " | " . $_GET['mes'];
	$note = 'Page bookmark removed.';
	
	// INSER NOTE
	$query = "INSERT INTO note (PageId, Content) VALUES ('$pageId', '$note') ";
	mysql_query ($query);
	
	// INSERT USER_PAGE_NOTE
	$noteId = mysql_insert_id();
	$query = "INSERT INTO user_note (UserId, NoteId) VALUES ('$userId', '$noteId') ";
	mysql_query ($query);

}
?>

<script type='text/javascript'>
      google.load('visualization', '1', {'packages':['annotatedtimeline']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('date', 'Date');
        data.addColumn('number', 'Total Fans');
        data.addColumn('string', 'title1');
        data.addColumn('string', 'text1');
        data.addColumn('number', 'Daily Fans');
        data.addColumn('string', 'title2');
        data.addColumn('string', 'text2');
       
		data.addRows([
			<?php 
			$res = mysql_query ("SELECT DailyFans, TotalFans, Date FROM report WHERE PageId = $id ORDER BY Date ASC ") or die ("error");
			$nr = mysql_num_rows ($res);
			while ($nr > 0) {
				$row = mysql_fetch_array ($res);
				extract ($row);
				$date= explode (" ", $Date);
				$yyyymmdd = explode ("-", $date[0]);
				$yyyymmdd[1]--;
				$yyyymmdd[2]--;
				echo "[new Date(".$yyyymmdd[0].", ".$yyyymmdd[1]." ,".$yyyymmdd[2]."), ".$TotalFans.", undefined, undefined, ".$DailyFans.",undefined, undefined]";
				$nr--;
				if ($nr !=0) {
					echo ", ";
				}
			}
			?>
			
       ]);

        var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
        chart.draw(data, {colors: ['#009900','#990000'], thickness: 2, scaleType:'allfixed', fill:10, legendPosition: 'newRow', wmode:'transparent'});
      }

</script>

    
 <div id="content">


<div class="huge_box">

<?php include ("components/page_tabs.php"); ?>

<div class="huge_box_header">
<div class="title">Page Statistics</div>
<div class="nums">Total</div>
<div class="nums">Monthly</div>
<div class="nums">Weekly</div>
<div class="nums">Daily</div>
</div>
<?php

switch ($orby) {
	case 'day_asc': {
		$ordby = ' r.DailyFans ASC ';
		break;
	} case 'day_desc': {
		$ordby = ' r.DailyFans DESC ';
		break;
	} case 'month_asc': {
		$ordby = ' r.MonthlyFans ASC ';
		break;
	} case 'month_desc': {
		$ordby = ' r.MonthlyFans DESC ';
		break;
	} case 'week_asc': {
		$ordby = ' r.WeeklyFans ASC ';
		break;
	} case 'week_desc': {
		$ordby = ' r.WeeklyFans DESC ';
		break;
	} case 'total_asc': {
		$ordby = ' r.TotalFans ASC ';
		break;
	} default: {
		$orby = 'total_desc';
		$ordby = ' r.TotalFans DESC ';
		break;
	} 
}


$res = mysql_query ("SELECT p.Id, p.Link, p.Title, p.Image, p.Category, r.Date, r.DailyFans, r.WeeklyFans, r.MonthlyFans, r.TotalFans, r.PageId FROM page AS p JOIN report AS r ON p.Id = r.PageId WHERE ".$viewPrivatePages." p.Id = $id AND DATE(r.Date) = (SELECT MAX(DATE(Date)) FROM report) ") or die ("error");

if (mysql_num_rows ($res) != 1 ) {
?>
<script type="text/javascript">
window.location = '?page=pages';
</script>
<?php	
}
$row = mysql_fetch_array ($res);
extract ($row);


?>
<div class="huge_box_content">
<div class="huge_box_item">
	
    <div class="avatar"><img src="<?php echo $Image; ?>" alt="<?php echo $Title; ?>" width="50" height="50" /></div>
	<div class="description">
	<div class="title"><?php echo $Title; ?></div>
	<div class="category"><?php echo $Category; ?></div>
	</div>
	
    <div class="fans">
    <div class="total"><?php echo number_format($TotalFans, 0, ',', '.'); ?></div>
    </div>
	
    <div class="fans">
	<?php if ($MonthlyFans < 0 ) { ?><div class="negative"><?php } else { ?><div class="positive">+<?php } ?>
	<?php echo number_format($MonthlyFans, 0, ',', '.'); ?></div>
	</div>
    
	<div class="fans">
	<?php if ($WeeklyFans < 0 ) { ?><div class="negative"><?php } else { ?><div class="positive">+<?php } ?>
	<?php echo number_format($WeeklyFans, 0, ',', '.'); ?></div>
	</div>
	
    <div class="fans">
    <?php if ($DailyFans < 0 ) { ?><div class="negative"><?php } else { ?><div class="positive">+<?php } ?>
	<?php echo number_format($DailyFans, 0, ',', '.'); ?></div>
	</div>
	
    </div>
   


<div id="chart_div" style="width:614; height:320;"></div>

</div>

<?php
include ("components/notes_page.php");

?>
</div>

<?php
include ("components/page_menu.php");
include ("components/my_pages.php");

//include ("components/ads.php"); 

?>


</div>