<?php
/**
* @version 1.0.0
* @package RSEvents! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

	jimport('joomla.utilities.simplexml');
	
	global $mainframe;
	
	//Get rid of the trailing slash 
	if(!empty($this->data->ExtraValue))
	{
		$this->data->ExtraValue = trim($this->data->ExtraValue);
		$data = explode('/',$this->data->ExtraValue);
		$latest = strlen($this->data->ExtraValue)-1;
		if($this->data->ExtraValue[$latest] == '/') $this->data->ExtraValue = substr($this->data->ExtraValue,0,$latest);
	}
	
	//get the photoset id
	$photosetid = end(explode('/',$this->data->ExtraValue));
	//get the username
	$usernameid = @$data[4];
	//get the api key
	$api_key = $this->rseventsConfig['flikr.key'];
	
	
	$rows = 4; $columns = 3;
	
	$photos = ($rows * $columns);
	
	if (empty($_REQUEST['page'])) 
	{
		$pagenum = 1;
	} else 
	{
		$pagenum = JRequest::getVar('page','','request');
	}

	$xml = new JSimpleXML();
	$file = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=".$api_key."&photoset_id=".$photosetid."&per_page=".$photos."&page=".$pagenum;
	
	//Try getting the xml
	$xml_string = rseventsHelper::fopen($file);
	$xml->loadString($xml_string);
	
	//Get the total number of photos
	$total = @$xml->document->photoset[0]->attributes('total');
	
	if (($total % $photos) == 0) {
	
		$pages = $total / $photos;
	} else 
	{
		$pages = ($total / $photos) - (($total % $photos)/$photos) + 1;
	}
?>


<ul class="rs_navigator">
<?php if (($pagenum-1) > 0) {?>
<li><a href="<?php echo JURI::root();?>index.php?option=com_rsevents&view=events&layout=album&cid=<?php echo $this->data->IdExtra;?>&page=<?php echo $pagenum-1;?>">&lt;&lt;<?php echo JText::_('RSE_PREV'); ?></a></li>
<?php
} else { ?>
<li>&lt;&lt;<?php echo JText::_('RSE_PREV'); ?></li>
<?php }
 for ($k = 1; $k<=$pages; $k++) { ?>
<li><a href="<?php echo JURI::root();?>index.php?option=com_rsevents&view=events&layout=album&cid=<?php echo $this->data->IdExtra;?>&page=<?php echo $k;?>"><?php echo $k?></a></li>
<?php } ?>
<?php if (($pagenum+1) < ($pages+1)) {?>
<li><a href="<?php echo JURI::root();?>index.php?option=com_rsevents&view=events&layout=album&cid=<?php echo $this->data->IdExtra;?>&page=<?php echo $pagenum+1;?>"><?php echo JText::_('RSE_NEXT_BTN'); ?>&gt;&gt;</a></li>
<?php
} else { ?>
<li><?php echo JText::_('RSE_NEXT_BTN'); ?>&gt;&gt;</li>
<?php } ?>
</ul>
<span class="rsevents-clear"></span>
<?php 
	//what am i seeing 
	if (($pagenum * $photos) > $total) 
	$to = $total % $photos;
	else 
	$to = $photos;  
?>


<?php 
for($l = 0; $l<$rows; $l++) 
{
	$to = ($columns+($l*$columns)); 
	for ($k = ($l*$columns); $k<$to; $k++) 
	{
		$photo = @$xml->document->photoset[0]->photo[$k];
		$id = is_object($photo) ? $photo->attributes('id') : null;
		
		if (!empty($id))
		{
			echo '<ul class="rs_navigator"><li><a href="http://www.flickr.com/photos/'.$usernameid.'/'.$photo->attributes('id').'" target="_blank">
				  <img src="http://farm'.$photo->attributes('farm').'.static.flickr.com/'.$photo->attributes('server').'/'.$photo->attributes('id').'_'.$photo->attributes('secret').'_m.jpg" height="173"/>
				</a></li></ul>';
		}
	}
}
?>
