<?php
//define this module's name and path
$mn = "odyssey_related_images";
include("modules/modules.php");

/*
Available variables:
All system classes
All request variables
$p (the template path)
$mp (the module path)
$sufixID (id sufix for this module's divs)
*/

if($moduleParameters['moduleAlign'] && $moduleParameters['moduleHR']){ // there are system parameters...
$marginTop = ($moduleParameters['marginTop'] == "__EMPTY__" || empty($moduleParameters['marginTop'])) ? 0 : $moduleParameters['marginTop'];
$marginRight = ($moduleParameters['marginRight'] == "__EMPTY__" || empty($moduleParameters['marginRight'])) ? 0 : $moduleParameters['marginRight'];
$marginBottom = ($moduleParameters['marginBottom'] == "__EMPTY__" || empty($moduleParameters['marginBottom'])) ? 0 : $moduleParameters['marginBottom'];
$marginLeft = ($moduleParameters['marginLeft'] == "__EMPTY__" || empty($moduleParameters['marginLeft'])) ? 0 : $moduleParameters['marginLeft'];
$paddingTop = ($moduleParameters['paddingTop'] == "__EMPTY__" || empty($moduleParameters['paddingTop'])) ? 0 : $moduleParameters['paddingTop'];
$paddingRight = ($moduleParameters['paddingRight'] == "__EMPTY__" || empty($moduleParameters['paddingRight'])) ? 0 : $moduleParameters['paddingRight'];
$paddingBottom = ($moduleParameters['paddingBottom'] == "__EMPTY__" || empty($moduleParameters['paddingBottom'])) ? 0 : $moduleParameters['paddingBottom'];
$paddingLeft = ($moduleParameters['paddingLeft'] == "__EMPTY__" || empty($moduleParameters['paddingLeft'])) ? 0 : $moduleParameters['paddingLeft'];
$useModuleContainer = ($moduleParameters['useModuleContainer'] == "__EMPTY__" || empty($moduleParameters['useModuleContainer'])) ? 0 : (int)$moduleParameters['useModuleContainer'];

if(empty($marginTop) && empty($marginRight) && empty($marginBottom) && empty($marginLeft)){
	$Mmargin = "0";
}else{
	$Mmargin = $marginTop." ".$marginRight." ".$marginBottom." ".$marginLeft." ";
}
if(empty($paddingTop) && empty($paddingRight) && empty($paddingBottom) && empty($paddingLeft)){
	$Mpadding = "0";
}else{
	$Mpadding = $paddingTop." ".$paddingRight." ".$paddingBottom." ".$paddingLeft." ";
}

$dataSource = $moduleParameters['dataSource'];
$dataSource = ($dataSource == "auto" || empty($dataSource)) ? $section : $dataSource;
if($system->getTableSectionID($dataSource)) { // only if a valid section

$containerWidth = $moduleParameters['containerWidth'];
$relatedImagesHeader = $moduleParameters['relatedImagesHeader'];
if(!preg_match('/px/', $containerWidth)) $containerWidth.="px";
if(preg_match('/%/', $containerWidth)) $containerWidth = str_replace("px", null, $containerWidth);
$containerWidth = (empty($containerWidth)) ? "inherit" : $containerWidth; // make sure there is a value
$thumbType = (int)$moduleParameters['thumbType'];
$doNotIncludeDefault = ($moduleParameters['doNotIncludeDefault'] == "__EMPTY__" || empty($moduleParameters['doNotIncludeDefault'])) ? 0 : (int)$moduleParameters['doNotIncludeDefault'];
$doNotIncludeSlider = ($moduleParameters['doNotIncludeSlider'] == "__EMPTY__" || empty($moduleParameters['doNotIncludeSlider'])) ? 0 : (int)$moduleParameters['doNotIncludeSlider'];
$doNotIncludeLogo = ($moduleParameters['doNotIncludeLogo'] == "__EMPTY__" || empty($moduleParameters['doNotIncludeLogo'])) ? 0 : (int)$moduleParameters['doNotIncludeLogo'];
$thumbWidth = (int)$moduleParameters['thumbWidth'] ? (int)$moduleParameters['thumbWidth'] : 158; // make sure there is a value for width
$thumbHeight = (int)$moduleParameters['thumbHeight'] ? (int)$moduleParameters['thumbHeight'] : 158; // make sure there is a value for height
$borderColor = $moduleParameters['borderColor'] ? "#".$moduleParameters['borderColor'] : "transparent";

if($useModuleContainer ){
	
?>
<div id="<?php echo $mn.$sufixID;?>" data-editable="editable" class="responsive_float">
<?php
}
?>
<style type="text/css">
#<?php echo $mn.$sufixID;?>{
	<?php 
	$alignRule = ($moduleParameters['moduleAlign'] == "center") ? "text-align" : "float";
	echo $alignRule.": ".$moduleParameters['moduleAlign'].";".PHP_EOL;
	?>
	margin : <?php echo $Mmargin;?>;
	padding : <?php echo $Mpadding;?>;
<?php
if($widthInPixels){
	?>
	width: <?php echo $widthInPixels;?>px;
	<?php
}	
if($moduleParameters['moduleHR'] == "top" || $moduleParameters['moduleHR'] == "bottom"){
?>
	border-<?php echo $moduleParameters['moduleHR'];?> : 1px solid black;
<?php	
}
?>
}
.items_list_thumbnail_container{
	background: white;
	border: 1px solid <?php echo $borderColor;?>;
	display: inline-block;
	margin:0 10px;
}
.items_list_thumbnail{
	width: <?php echo $thumbWidth - 8;?>px; 
	height: <?php echo $thumbHeight - 8;?>px; 
}
.thumbWrapper {
    float: none;
    display: inline-table;
}
.jo_thumbs_container{
    text-align:center;
    padding-right: 30px;
}
</style>
<?php
}

if($thumbType > 1) { // αν έχει επιλεγεί αυτό το plugin
	if(!count(preg_grep( "/jOthumb\.js/", $this->loadedFiles))){ // αν δεν έχει φορτωθεί το jquery plugin jOthumb
		?>
		<script type="text/javascript" src="<?php echo getBase();?>js/jOplugins/jOthumb.js"></script>
		<?php
	}
	?>
		<script type="text/javascript">
		$().ready(function(){
			$('div[data-related_images<?php echo $sufixID;?>="jOthumb"]').each(function(){
				$(this).jOthumb(<?php if($thumbType != 3) { echo "{'static': 1}";}?>);
			});
		});
		</script>
	<?php
}
	global $langID;
	$dataSource = ($moduleParameters['dataSource'] == "auto") ? $section : $moduleParameters['dataSource'];
	$sectionID = $system->getTableSectionID($dataSource);
	$id = (int)$_REQUEST['id']; // id of current record
	$exceptions = array();
	if($doNotIncludeDefault) $exceptions[] = "default";
	if($doNotIncludeSlider) $exceptions[] = "slider";
	if($doNotIncludeLogo) $exceptions[] = "logo";
	$theImages = $content->getImages($dataSource, $id, $langID, $exceptions); // get images
	if($theImages){
		if($relatedImagesHeader){
			echo "<div class='related_images'>".t($relatedImagesHeader)."</div>";
		}
		?>
        <div class="jo_thumbs_container">
        <?php
		foreach($theImages as $image){
			$thumbURL = ($thumbType > 1) ? "mediabank/medium/medium_".$image : "mediabank/thumbs/small_".$image;
			if($image ==".") $thumbURL = ($thumbType > 1) ? "images/no_image.png" : "images/small_no_image.png";
			$imgDim = GetImageSize($thumbURL);
			for($i=0; $i <100000; $i++){
				// delay
				};
			$w = ($thumbType > 1) ? $thumbWidth : ($imgDim[0]);
			$h = ($thumbType > 1) ? $thumbHeight : ($imgDim[1]);
			$imgDescription = $content->getImageDescription($image, $id, $sectionID, $langID);
			$visibility = ($thumbType > 1) ? "hidden" : "visible";
			$link = "mediabank/images/".$image;	
			$expandImage = $thumbType == 3;	
			$fancybox = $id; // for fancybox activation and use as rel attribute (fancybox gallery)
			$content->displayThumbnail	($thumbURL, $w, $h, $link, $expandImage, $fancybox);
		}
		?>
        </div>
        <?php
	}

if($useModuleContainer ){
?>
</div>
<?php
}
}
?>