<?php
if($delay < 1000 || $delay == "")
{
	$delay = 1000;
}

$directory = ABSOLUTE_PATH . "/" . $base;

if($files == null)
{
	$files = scandir($directory);
}
if(count($files) <= 2)
{
	return;
}

$full_files = array();
$image_files = array();

$count = 0;
foreach($files as $file)
{
	$full_file = $file;

	if(is_file("$directory/$file") && preg_match('/\.(jpe?g|png|gif|webp)$/i', "$directory/$file"))
	{
		$thumbnail = Mainframe::thumbnail("$directory/$file", $max_width, $max_height, false, Mainframe::site()->webp ? IMAGETYPE_WEBP : null);

		// $size 	= @getimagesize(ABSOLUTE_PATH . $thumbnail);
		// $width 	= $size[0];
		// $height = $size[1];
		$file 	= $thumbnail;
		$full_files[count($image_files)] 	= "/" . $base . "/" . $full_file;
		$image_files[count($image_files)] 	= $file;

		$count ++;
	}
}
?>

<div id="module_<?php echo($module_instance_id); ?>" class="module nivoslider-module <?php echo($module_class); ?>">
	<div class="slider-wrapper theme-default">
		<div class="slider-overlay"></div>
			<div id="slider_<?php echo $module_instance_id?>" class="nivoSlider">
			<?php
			for($i=0; $i<count($image_files); $i++)
			{
				$img 		= $image_files[$i];
				$full 		= $full_files[$i];
				$link 		= $default_link;
				$target 	= "_self";
				$class 		= "";
				$onclick 	= "";

				if($lightbox)
				{
					$class 	= "lightbox";
					$link 	= $full;
				}
				else
				{
					//check if this image should be linked
					for($j=0; $j<20; $j++)
					{
						if(strtolower(${"image$j"}) == strtolower(basename($full)))
						{
							$link = ${"link$j"};

							if(substr($link, 0, 4) == "http")
							{
								$target = "_blank";
							}
							else
							{
								$target = "_self";
							}

							break;
						}
					}
				}

				if($img)
				{
					// Create a cleaned up version of the file name as a default alt/title.
					$title = str_replace("%20", " ", basename($img));
					//remove file extension
					$title = str_ireplace(array(".jpg", ".jpeg", ".gif", ".png", ".bmp", ".tiff", "/thumbnails/"), "", $title);
					//put a space before all capital letters (camel case)
					$title = preg_replace('/([A-Z][a-z0-9])/', ' $1', $title);
					//remove any thumbnail resizing (100x50) from the end of the filename
					$title = preg_replace('/(_thumb)?_?(\d+)x(\d+)/', '', $title);
					//remove @2x from the end of the filename
					$title = preg_replace('/@2x/', '', $title);
					//replace remaining underscores, dashes, and slashes with spaces
					$title = str_replace(array("_", "/", "\\"), " ", $title);
					$title = trim($title);

					$img = str_replace(" ", "%20", $img);

					if($link == "" && !$lightbox)
					{
						$onclick = "onclick='return false;'";
					}
					?><a href="<?php echo($link); ?>" <?php echo($onclick); ?> rel="slider_<?php echo $module_instance_id?>" class="<?php echo($class); ?>" target="<?php echo($target); ?>"><img src="<?php echo($img); ?>" data-thumb="<?php echo($img); ?>" alt="<?php echo($title); ?>" /></a><?php
				}
			}
			?>
		</div>
	</div>
	<?php
	if(!$captions)
	{
		?>
		<style>
		#slider_<?php echo $module_instance_id?> .nivo-caption
		{
			display: none !important;
		}
		</style>
		<?php
	}
	?>
</div>

<script>
jQuery(window).on("load", function(){
	jQuery('#slider_<?php echo $module_instance_id?>').nivoSlider({
    	effect: '<?php echo $effect?>',
    	pauseTime: '<?php echo $delay?>',
    	pauseOnHover: true,
    	randomStart: <?php echo ($random ? 'true' : 'false');?>,
    	directionNav: <?php echo ($directionNav ? 'true' : 'false');?>,
    	controlNav: <?php echo ($controlNav ? 'true' : 'false');?>,
    	controlNavThumbs: <?php echo ($controlNavThumbs ? 'true' : 'false');?>
    });

	<?php
	// Only include this JS if there's going to be a lightbox, otherwise it can conflict with the gallery.
	if($lightbox)
	{
		?>
		jQuery("a.lightbox").colorbox({
			'rel'			: 'slider_<?php echo $module_instance_id?>',
			'transition'	: 'elastic',
			'current'		: '{current}/{total}',
			'scalePhotos' 	: true,
			'width'			: '<?php echo($lightbox_width); ?>',
			'height'		: '<?php echo($lightbox_height); ?>',
			'maxWidth' 		: '100%',
			'maxHeight'		: '100%'
		});
		<?php
	}
	?>
});
</script>
