<?php
/**
* @id $Id$
* @author  Joomla Bamboo
* @package  prettyBox
* @copyright Copyright (C) 2006 - 2010 Joomla Bamboo. http://www.joomlabamboo.com  All rights reserved.
* @license  GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
*/
// no direct access
defined('_JEXEC') or die('Restricted access');
require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');
$helper = JPATH_SITE.DS.'media'.DS.'zengridframework'.DS.'helpers'.DS.'image.php';
if (!(JFile::exists($helper))){
	$helper = JPATH_SITE.DS.'media'.DS.'plg_jblibrary'.DS.'helpers'.DS.'image.php';
}
$framework = JPATH_SITE.DS.'plugins'.DS.'system'.DS.'zengridframework'.DS.'zengrid.php';
if (!(JFile::exists($framework))){
	$framework = false;
}
if (JFile::exists($helper)){ 
	require_once($helper); 
} else {

	echo '<div style="font-size:12px;font-family: helvetica neue, arial, sans serif;width:600px;margin:0 auto;background: #f9f9f9;border:1px solid #ddd ;margin-top:100px;padding:40px"><h3>Ooops. It looks like JbLibrary plugin or the Zen Grid Framework plugin is not installed!</h3> <br />Please install it and ensure that you have enabled the plugin by navigating to extensions > plugin manager. <br /><br />JB Library is a free Joomla extension that you can download directly from the <a href="http://www.joomlabamboo.com/joomla-extensions/jb-library-plugin-a-free-joomla-jquery-plugin">Joomla Bamboo website</a>.</div>';

}
class modPrettyBoxHelper
{
	function getList($params, $id)
	{
		// Import libraries
		jimport('joomla.filesystem.file');
		jimport('joomla.filesystem.folder');	
		$directory = $params->get('directory', '/images/stories');
		//Remove Slashes from directory
		$directory = ltrim($directory,'/');
		$directory = rtrim($directory,'/');			
		$subfolderDisplay = $params->get('subfolderDisplay', 0);
		$output = $params->get('output', 3); // Selects the type of layout for the gallery
		$link = $params->get('link',1);
		$details = $params->get('details', '1');
		$rightMargin =  str_replace('px', '', $params->get('right_margin','0'));
		// PrettyPhoto Settings
		$prettyPhoto = $params->get('prettyPhoto','1');
		$padding = str_replace('px', '', $params->get('padding','40'));
		$resize = $params->get('resize','true');
		$theme = $params->get('theme','dark_rounded');
		$prettyBoxTitle = $params->get('prettyBoxTitle','false');
		$opacity = $params->get('opacity','0.6');
		$prettyBoxSpeed = $params->get('prettyBoxSpeed','normal');
		// Image Size and container, remove px if user entered
		$option = $params->get('option', 'crop');
		$img_width = str_replace('px', '', $params->get('img_width','150'));
		$img_height = str_replace('px', '', $params->get('img_height','100'));
		$height = str_replace('px', '', $params->get('height','200'));		
		// list of filetypes you want to show
		$allowed_types = '\.png$|\.gif$|\.jpg$|\.$';
		// list of filetypes you want to exclude
		$exclude = array('.svn', 'CVS','.DS_Store','__MACOSX');
		if ((strpos(JPATH_ROOT, '/'))===FALSE){//windows
			$directory = str_replace('/', '\\', $directory);
			$path = JPATH_ROOT.'\\'.$directory;
		} else {//linux
			$directory = str_replace('\\', '/', $directory);
			$path = JPATH_ROOT.'/'.$directory;
		}
		//get list of images from dir
		$images = JFolder::files($path, $allowed_types, false, true, $exclude);
		//we create the array
		$items = array();
		//create an array of items for template
		foreach ($images as $image)
		{
			//windows or linux, find local 
			$local_image = str_replace('\\', '/', $image);
			$pos = strpos($local_image, '/images');
			$local_image = substr_replace($local_image, '', 0, $pos);
			// remove file path
			$file = JFile::getName($image);
			// remove file extension
			$name = JFile::stripExt($file);
			// remove root path & File name
			$names = explode('-', $name);
			// Creates new variables from the file name
			switch ($details){
				case "1":
					$title = (!empty($names[0]))? $names[0] : '';
					$description = "";
					$date = '';
					$author = '';
					$articleid = '';
					$itemid = '';					
				break;
				case "2":
					$title = (!empty($names[0]))? $names[0] : '';
					$description = (!empty($names[1]))? $names[1] : '';
					$date = '';
					$author = '';
					$articleid = '';
					$itemid = '';					
				break;
				case "3":
					$title = (!empty($names[0]))? $names[0] : '';
					$description = (!empty($names[1]))? $names[1] : '';
					$date = (!empty($names[2]))? $names[2] : '';
					$author = '';
					$articleid = '';
					$itemid = '';					
				break;
				case "4":
					$title = (!empty($names[0]))? $names[0] : '';
					$description = (!empty($names[1]))? $names[1] : '';
					$date = (!empty($names[2]))? $names[2] : '';
					$author  = (!empty($names[3]))? $names[3] : '';
					$articleid = '';
					$itemid = '';					
				break;
				case "5":
					$title = (!empty($names[0]))? $names[0] : '';
					$description = (!empty($names[1]))? $names[1] : '';
					$date = (!empty($names[2]))? $names[2] : '';
					$author  = (!empty($names[3]))? $names[3] : '';
					$articleid  = (!empty($names[4]))? $names[4] : '';
					$itemid = '';				
				break;
				case "6":
					$title = (!empty($names[0]))? $names[0] : '';
					$description = (!empty($names[1]))? $names[1] : '';
					$date = (!empty($names[2]))? $names[2] : '';
					$author  = (!empty($names[3]))? $names[3] : '';
					$articleid  = (!empty($names[4]))? $names[4] : '';
					$itemid = (!empty($names[5]))? $names[5] : '';
				break;
				default:
					$title = (!empty($names[0]))? $names[0] : '';
					$description = (!empty($names[1]))? $names[1] : '';
					$date = '';
					$author = '';
					$articleid = '';
					$itemid = '';				
				break;			
			}
			// Link Behaviour
			switch ($link){
				// No Link
				case "0":
				$lightbox = '';
				$openlink = '';
				$closelink = '';
				break;
				// Pretty Photo
				case  "1":
				$lightbox = 'rel="prettyPhoto'.$id.'[gallery]"';
				$openlink ='<a href="'.JURI::base(true).$local_image.'" '.$lightbox.' title="'.$title.' ">';
				$closelink = '</a>';
				break;
				// Same Window
				case  "2":
				$lightbox = '';
				$openlink ='<a href="'.JURI::base(true).$local_image.'">';
				$closelink = '</a>';
				break;
				// New Window
				case "3":
				$lightbox = '';
				$openlink ='<a href="'.JURI::base(true).$local_image.'" target="_blank">';
				$closelink = '</a>';
				break;
				// Content Item
				case "4":
				if ($articleid > 0) {
					$lightbox = '';
					$openlink ='<a href="index.php?option=com_content&amp;view=article&id='.$articleid.'&amp;Itemid='.$itemid.'">';
					$closelink = '</a>';
				} else {
					$lightbox = '';
					$openlink = '';
					$closelink = '';
				}
				break;
				// Blog Category
				case "5":
				if ($articleid > 0) {
					$lightbox = '';
					$openlink ='<a href="index.php?option=com_content&view=category&layout=blog&id='.$articleid.'&amp;Itemid='.$itemid.'">';
					$closelink = '</a>';
				} else {
					$lightbox = '';
					$openlink = '';
					$closelink = '';
				}
				break;
				// Default
				default:
				$lightbox = '';
				$openlink = '';
				$closelink = '';
				break;		
			}
			$new_image = JURI::base(true).$local_image;
			$resized_image =  resizeImageHelper::getResizedImage($new_image, $img_width, $img_height, $option);								
			// Output options for the gallery
			switch ($output){
				// No Details
				case "0":
				$item = '<div class="prettydiv'.$id.'">'.$openlink.'<img class="prettyBox" src="'.$resized_image.'" title="'.$title.' - '.$description.'" />'.$closelink.'</div>';
				break;
				// Title Above Image
				case "1":
				$item = '<div class="prettydiv'.$id.'"><p class="prettyTitle">'.$title.' </p>'.$openlink.'<img class="prettyBox" src="'.$resized_image.'"  alt="'.$file.'" title="'.$title.' - '.$description.'" />'.$closelink.'</div>';
				break;
				// Title Below Image
				case "2":
				$item = '<div class="prettydiv'.$id.'">'.$openlink.'<img class="prettyBox" src="'.$resized_image.'"  alt="'.$file.'" title="'.$title.' - '.$description.'" />'.$closelink.'<p class="prettyTitle">'.$title.' </p></div>';
				break;
				// Title above plus details
				case "3":
				$item = '<div class="prettydiv'.$id.'"><p class="prettyTitle">'.$title.' </p>'.$openlink.'<img class="prettyBox" src="'.$resized_image.'"  alt="'.$file.'" title="'.$title.' - '.$description.'" />'.$closelink.'<p class="prettyDescription">'.$description.'</p><p class="prettyDate">'.$date.'</p><p class="prettyDescription">'.$author.'</p></div>';
				break;
				// Title Below plus all details
				case "4":
				$item = '<div class="prettydiv'.$id.'">'.$openlink.'<img class="prettyBox" src="'.$resized_image.'"  alt="'.$file.'" title="'.$title.' - '.$description.'" />'.$closelink.'<p class="prettyTitle">'.$title.' </p><p class="prettyDescription">'.$description.'</p><p class="prettyDate">'.$date.'</p><p class="prettyDescription">'.$author.'</p></div>';
				break;
				// Description Date Author
				case "5":
				$item = '<div class="prettydiv'.$id.'">'.$openlink.'<img class="prettyBox" src="'.$resized_image.'"  alt="'.$file.'" title="'.$title.' - '.$description.'" />'.$closelink.'<p class="prettyDescription">'.$description.'</p><p class="prettyDate">'.$date.'</p><p class="prettyAuthor">'.$author.'</p></div>';
				break;
				// Date Author
				case "6":
				$item = '<div class="prettydiv'.$id.'">'.$openlink.'<img class="prettyBox" src="'.$resized_image.'"  alt="'.$file.'" title="'.$title.' - '.$description.'" />'.$closelink.'<p class="prettyDate">'.$date.'</p><p class="prettyAuthor">'.$author.'</p></div>';
				break;
				// Author
				case "7":
				$item = '<div class="prettydiv'.$id.'">'.$openlink.'<img class="prettyBox" src="'.$resized_image.'"  alt="'.$file.'" title="'.$title.' - '.$description.'" />'.$closelink.'<p class="prettyAuthor">'.$author.'</p></div>';
				break;
				default :
				$item = '<div class="prettydiv'.$id.'">'.$openlink.'<img class="prettyBox" src="'.$resized_image.'"  alt="'.$file.'" title="'.$title.' - '.$description.'" />'.$closelink.'</div>';
				break;
			}
			$items[] = $item;
		}
        return $items;
	}
}
