<?php
	// NJS 2005-12-08: Grab document title on the fly if necessary.
	if ($title == '')
	{
		// NJS 2006-06-14: Added archive name as argument.
		$title = $sql->getEPrintTitle($GLOBALS["config_vars"]["archivename"],$_REQUEST["id"]);
		if ($title == '')
			$title = 'Eprint #' . sprintf("%08d", $_REQUEST["id"]) . ' (title is not available)';
	}
		
	// Create link to eprint.
	// NJS 2006-06-14: Added archive name as index.
	$title_link = '<a href="'. 
		$GLOBALS["config_vars"]["general"]["eprints_location"][$GLOBALS["config_vars"]["archivename"]] .
		'/';
	// NJS 2007-07-24: Long URLs no longer appear to work in EPrints 3,
	// so we need to check the version.
	if ( $GLOBALS["config_vars"]["general"]["eprints_version"] > 2 )
	{
		$title_link .= $_REQUEST["id"];
	}
	else
	{
		$title_link .= 'archive/' .
			sprintf("%08d", $_REQUEST["id"]);
	}
	$title_link .= '/" target="_blank">' .
		$title .
		'</a>';
	
?>

<h3><?php print $title_link; ?></h3>      

<?php
	/*
		NJS 2005-11-27: Note that we load the eprint-specific navigation
		bar manually rather than through the actions/includes array in
		inc.vars.es.php. This is (currently) the only place that it's
		used, and adding it to the array would require us to break this
		file into two parts.
	*/
	require("inc.html.navbar_eprint.es.php");

	$max_width = '400'; // Max pixel width of barcharts
?>

<table cellspacing='0' cellpadding='4'>
	
	<tr>
		<td colspan="7"><h4 style="margin-top: 0px;">Abstract views and full text downloads for <?php print $show_date; ?></h4></td>
	</tr>
	
<?php
	$count0 = 0;
	$count1 = 0;

	// NJS 2006-01-18: Calculate number of distinct countries for views/downloads.
	$count0c = 0;
	$count1c = 0;

	if (isset($type_count["0"]["count"])) {
		if ($type_count["0"]["view_type"] != "download") {
			$count0 = $type_count["0"]["count"];
			$count0c = $type_count["0"]["countries"];
		} else {
			$count1 = $type_count["0"]["count"];
			$count1c = $type_count["0"]["countries"];
		}
	}
	if (isset($type_count["1"]["count"])) {
		if ($type_count["1"]["view_type"] != "download") {
			$count0 = $type_count["1"]["count"];
			$count0c = $type_count["1"]["countries"];
		} else {
			$count1 = $type_count["1"]["count"];
			$count1c = $type_count["1"]["countries"];
		}
	}
	$max_count = max($count0, $count1, 1);
	
	if ($count0 + $count1 > 0)
	{
		print '<tr><td colspan="7"><span style="font-size:small;">The numbers in (parentheses) are the number of distinct countries that abstract views and full text downloads originated from.</span></td></tr>';
		print '<tr><th colspan="2" align="left" width="150" style="background-color:#cccccc;">&nbsp;</th>
		<th colspan="2" style="background-color:#66ddee;">Abstracts</th>
		<th colspan="2" style="background-color:#4477dd;color:#ffffff;">Full text</th>
		<th style="background-color:#cccccc;">&nbsp;</th></tr>';
	
		print '<tr><td colspan="2" align="left" style="background-color:#2ea495;color:#ffffff;font-size:small;"><b>Views</b></td><td align="right" style="font-size:small;">'.
			$count0.
			'</td><td align="right" style="font-size:small;border-right:1px solid #dddddd;">(' .
			$count0c.
			')</td><td align="right" style="font-size:small;">' .
			$count1 .
			'</td><td align="right" style="font-size:small;border-right:1px solid #dddddd;">(' .
			$count1c.
			')</td>';
	
		$cur_count = $count0;
		$col_width = (int) ($cur_count/$max_count * $max_width);
		if ( $cur_count > 0 ) $col_width = max($col_width,1);
		print '<td align="left"><img src="bars/hh.png" alt="abstracts" height="9" width="'. $col_width . '" /><br />';
	
		$cur_count = $count1;
		$col_width = (int) ($cur_count/$max_count * $max_width);
		if ( $cur_count > 0 ) $col_width = max($col_width,1);
		print '<img src="bars/hp.png" alt="downloads" height="9" width="'. $col_width . '" /></td></tr>';
	} else {
		print '<tr><td colspan="7" align="left" width="200"><strong style="color:#ff0000;">This document has not been viewed in this period.</strong></td></tr>';
	}
?>

	<tr>
		<td colspan="7"><h4>Downloads by country (derived from IP address of query) for <?php print $show_date; ?></h4></td>
	</tr>
	
<?php
	// NJS 2007-02-07: Rewritten to match similar code in
	// inc.html.cumulative_usage_country.php.
	if (!isset($GLOBALS["db_values"]["0"]["country_downloads"])) {
		print '<tr><td colspan="7" align="left" width="200" style="color:#ff0000;"><b>No statistics available.</b></td></tr>';
	}
	else {
		$max_count = max( getMaxCount( 'country' ), 1 );
		
		print '<tr><td colspan="7"><span style="font-size:small;">Click on a country name or flag to view details of the eprints that have been downloaded from that country.</span></td></tr>
		<tr><th colspan="2" style="background-color:#cccccc;" width="150">Country</th>
		<th colspan="2" style="background-color:#66ddee;">Abstracts</th>
		<th colspan="2" style="background-color:#4477dd;color:#ffffff;">Full text</th>
		<th style="background-color:#cccccc;">&nbsp;</th></tr>';
		
		generateCountryTable( 'country', $max_count, $max_width );
	}
?>

	<tr>
		<td colspan="7"><h4>Downloads by automated indexers and crawlers for <?php print $show_date; ?></h4></td>
	</tr>
	
<?php
	if (!isset($GLOBALS["db_values"]["0"]["country_downloads"])) {
		print '<tr><td colspan="7" align="left" width="200" style="color:#ff0000;"><b>No statistics available.</b></td></tr>';
	}
	else {
		$max_count = max( getMaxCount( 'bot' ), 1 );
		
		print '<tr><td colspan="7"><span style="font-size:small;">Click on a search engine name or icon to view details of the eprints that have been indexed by that engine.</span></td></tr>
		<tr><th colspan="2" style="background-color:#cccccc;" width="150">Country</th>
		<th colspan="2" style="background-color:#66ddee;">Abstracts</th>
		<th colspan="2" style="background-color:#4477dd;color:#ffffff;">Full text</th>
		<th style="background-color:#cccccc;">&nbsp;</th></tr>';
		
		generateCountryTable( 'bot', $max_count, $max_width );
	}
?>

	<tr>
		<td colspan="7"><h4>History of views for this eprint</h4></td>
	</tr>

<?php
	if (count($month_tally) > 0)
	{
		$max_count = 1;
		for ($rs=0;$rs<count($month_tally);$rs++) {
			if($max_count < $month_tally[$rs]["abstracts"])
				$max_count = $month_tally[$rs]["abstracts"];
			if($max_count < $month_tally[$rs]["downloads"])
				$max_count = $month_tally[$rs]["downloads"];
		}
		
		print '<tr><td colspan="7"><span style="font-size:small;">Click on a month to see document downloads for that month.<br />
		The numbers in (parentheses) are the number of distinct countries or search engines that abstract views and full text downloads originated from.</span></td></tr>';
		print '<tr><th colspan="2" style="background-color:#cccccc;" width="150">Period</th><th colspan="2" style="background-color:#66ddee;">Abstracts</th><th colspan="2" style="background-color:#4477dd;color:#ffffff;">Full text</th><th style="background-color:#cccccc;">&nbsp;</th></tr>';
		for ($rs=0;$rs<count($month_tally);$rs++)
		{

			$str_posn = strpos('JanFebMarAprMayJunJulAugSepOctNovDec',$month_tally[$rs]["month"]);
			if ($str_posn === false) $str_posn = 0;
			$month_number = (int) ($str_posn / 3) + 1;

			
			// NJS 2005-12-15: subtly highlight every alternate row
			print '<tr';
			if ($rs % 2) print ' style="background-color:#EDF3FE;"';
			// NJS 2006-06-14: Only generate a link if there are actually
			// some downloads.
			print '><td colspan="2" style="font-size:small;border-right:1px solid #dddddd;">';
			if ( $month_tally[$rs]["downloads"] > 0 )
				print '<a href="'.
					$_SERVER['PHP_SELF'].
					'?action=show_detail_eprint;id=' .
					$_REQUEST["id"] .
					';year=' .
					(int) $month_tally[$rs]["year"] .
					';month='.
					$month_number .
					'">';
			print (int) $month_tally[$rs]["year"] .
				' '.
				$month_tally[$rs]["month"];
			if ( $month_tally[$rs]["downloads"] > 0 )
				print '</a>';
			print '</td>';
			print '<td align="right" style="font-size:small;">'.
				$month_tally[$rs]["abstracts"].
				'</td><td align="right" style="font-size:small;border-right:1px solid #dddddd;">('.
				$month_tally[$rs]["acountries"].
				')</td>'; 
			print '<td align="right" style="font-size:small;">'.
				$month_tally[$rs]["downloads"].
				'</td><td align="right" style="font-size:small;border-right:1px solid #dddddd;">('.
				$month_tally[$rs]["dcountries"].
				')</td>'; 
			// abstract views bar
			$cur_count = $month_tally[$rs]["abstracts"];
			$col_width = (int) ($cur_count/$max_count * $max_width);
			if ( $cur_count > 0 ) $col_width = max($col_width,1);
			print '<td align="left"><img src="bars/hh.png" alt="views" height="9" width="'. 
				$col_width . 
				'" /><br />';
			// download views bar
			$cur_count = $month_tally[$rs]["downloads"];
			$col_width = (int) ($cur_count/$max_count * $max_width);
			if ( $cur_count > 0 ) $col_width = max($col_width,1);
			print '<img src="bars/hp.png" alt="views" height="9" width="'. 
				$col_width . 
				'" /></td></tr>';
		}
	} else {
		print '<tr><td colspan="7" align="left" width="200" style="color:#ff0000;"><b>No statistics available.</b></td></tr>';
	}
?>

</table>
