<?php if(!defined('master_script')) die();

/**
 * Default settings
 *
 * @category   Module (images)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-08-08
 */
	
	global $modules, $page_info, $system_data, $user_data, $images, $images_files, $id, $images_dir, $system_data, $template_data;
	
	if(!isset($module_settings['images']['images dir'])) $module_settings['images']['images dir'] = '';
	if(!isset($module_settings['images']['images sort order'])) $module_settings['images']['images sort order'] = 'none';
	if(!isset($module_settings["images"]["images per row"])) $module_settings["images"]["images per row"] = 5;
	if(!isset($module_settings["images"]["thumbnail size"])) $module_settings["images"]["thumbnail size"] = 156;
	if(!isset($module_settings["images"]["thumbnail crop width"])) $module_settings["images"]["thumbnail crop width"] = 156;
	if(!isset($module_settings["images"]["thumbnail crop height"])) $module_settings["images"]["thumbnail crop height"] = 156;
	if(!isset($module_settings["images"]["thumbnail mode"])) $module_settings["images"]["thumbnail mode"] = 'normal';
	if(!isset($module_settings["images"]["thumbnail background"])) $module_settings["images"]["thumbnail background"] = '#000000';
	if(!isset($module_settings["images"]["thumbnail quality"])) $module_settings["images"]["thumbnail quality"] = 85;
	if(!isset($module_settings['images']['thumbnail reflection'])) $module_settings['images']['thumbnail reflection'] = 0;
	if(!isset($module_settings['images']['thumbnail rotation'])) $module_settings['images']['thumbnail rotation'] = 0;
	if(!isset($module_settings['images']['thumbnail rotate cw'])) $module_settings['images']['thumbnail rotate cw'] = 1;

	if(!isset($module_settings["images"]["image width"])) $module_settings["images"]["image width"] = 800;
	if(!isset($module_settings["images"]["image height"])) $module_settings["images"]["image height"] = 600;
	if(!isset($module_settings["images"]["image fullscreen"])) $module_settings["images"]["image fullscreen"] = 0;
	if(!isset($module_settings["images"]["image properties"])) $module_settings["images"]["image properties"] = 1;
	if(!isset($module_settings["images"]["open image"])) $module_settings["images"]["open image"] = 'page';
	if(!isset($module_settings["images"]["popup width"])) $module_settings["images"]["popup width"] = 800;
	if(!isset($module_settings["images"]["popup height"])) $module_settings["images"]["popup height"] = 600;
	if(!isset($module_settings["images"]["page start full"])) $module_settings["images"]["page start full"] = 0;
	if(!isset($module_settings["images"]["page show controls"])) $module_settings["images"]["page show controls"] = 1;
	if(!isset($module_settings["images"]["page show images"])) $module_settings["images"]["page show images"] = 1;
	if(!isset($module_settings["images"]["link attributes"])) $module_settings["images"]["link attributes"] = '';
	if(!isset($module_settings["images"]["slideshow"])) $module_settings["images"]["slideshow"] = 0;
	if(!isset($module_settings["images"]["slideshow pause start"])) $module_settings["images"]["slideshow pause start"] = 0;
	if(!isset($module_settings["images"]["slideshow loop"])) $module_settings["images"]["slideshow loop"] = 1;
	if(!isset($module_settings["images"]["slideshow seconds"])) $module_settings["images"]["slideshow seconds"] = 5;

	if(!isset($module_settings["images"]["gallery description"])) $module_settings["images"]["gallery description"] = '';
	if(!isset($module_settings["images"]["gallery link text"])) $module_settings["images"]["gallery link text"] = '';

	if(!is_dir($module_settings['images']['images dir'])) $module_settings['images']['images dir'] = $system_data['folder']['pages'] . $page_info->alias . '/';

/**
 * Languages
 *
 * @category   Module (images)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-07-28
 */

	$modules['images']['key'] = 'images';
	include_txt($modules['images']['key'], $system_data['folder']['modules'] . $modules['images']['key'] . '/languages/');
	include_txt($modules['images']['key'], $system_data['folder']['settings'] . 'modules/' . $modules['images']['key'] . '/languages/');

/**
 * Get data for images
 *
 * @category   Module (images)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-07-29
 */

	// Module ID
	$id = !isset($id) ? 1 : $id++;

	// Change defaults for feed content
	if(defined('feed_script')) {
		$module_settings["images"]["image properties"] = 0;
		$module_settings["images"]["open image"] = 'disabled';
		$module_settings["images"]["page start full"] = 0;
		$module_settings["images"]["page show controls"] = 0;
		
		// Simulate content if feed strip tags
		echo '&shy;';
	}
	
	// Get images
	$images_dir = $system_data['folder']['pages'] . $page_info->alias . '/';
	$catalogue_file = $images_dir . 'storage/imagelist.cat';
	
	if(is_file($catalogue_file) && $module_settings['images']['images sort order'] == 'none') {
		$images_array = file($catalogue_file);
		$images_files = array();
		
		foreach($images_array as $image_entry) {
			$image_entry = $module_settings['images']['images dir'] . trim($image_entry);
			if(is_file($image_entry)) $images_files[] = $image_entry;
		}
	} else {
		$images_files = filelist($module_settings['images']['images dir'], 'image', array(), array(), true);
	}
	
	switch($module_settings['images']['images sort order']) {
		case 'reverse' : $images_files = array_reverse($images_files); break;
		case 'random' : shuffle($images_files); break;
		case 'name_descending' : sort($images_files); break;
		case 'name_ascending' : rsort($images_files); break;
	}
	
    for($i = 0; $i < count($images_files); $i++) {
    	$pathinfo = pathinfo($images_files[$i]);
    	$images_dir = $pathinfo['dirname'] . '/';
    	$images_files[$i] = $pathinfo['basename'];
    }
    
    for($i = 0; $i < count($images_files); $i++) {
   		$images[$images_files[$i]]['path'] = $images_dir . $images_files[$i];
   		$images[$images_files[$i]]['basename'] = $images_files[$i];

   		$imagedata = getimagesize($images_dir . $images_files[$i]);
   		$images[$images_files[$i]]['width'] = $imagedata[0];
   		$images[$images_files[$i]]['height'] = $imagedata[1];
   		
   		$thumbnail_size = $module_settings["images"]["thumbnail size"];

		$width_percentage = 100 * $module_settings["images"]["thumbnail size"] / $imagedata[0];
		$height_percentage = 100 * $module_settings["images"]["thumbnail size"] / $imagedata[1];

   		if($module_settings["images"]["thumbnail mode"] == 'normal') {
   			$images[$images_files[$i]]['thumb width'] = $imagedata[0] > $imagedata[1] ? $thumbnail_size : round($imagedata[0] * $height_percentage / 100);
   			$images[$images_files[$i]]['thumb height'] = $imagedata[0] > $imagedata[1] ? round($imagedata[1] * $width_percentage / 100) : $thumbnail_size;
   		}

   		if($module_settings["images"]["thumbnail mode"] == 'crop') {
   			$images[$images_files[$i]]['thumb width'] = $module_settings["images"]["thumbnail crop width"];
   			$images[$images_files[$i]]['thumb height'] = $module_settings["images"]["thumbnail crop height"];
   		}

   		if($module_settings["images"]["thumbnail mode"] == 'variable width') {
			if($imagedata[0] > $imagedata[1]) $thumbnail_size =  round($imagedata[0] * $height_percentage / 100) + 1;
   			$images[$images_files[$i]]['thumb width'] = $imagedata[0] > $imagedata[1] ? $thumbnail_size : round($imagedata[0] * $height_percentage / 100);
   			$images[$images_files[$i]]['thumb height'] = $module_settings["images"]["thumbnail size"];
   		}

   		if($module_settings["images"]["thumbnail mode"] == 'variable height') {
			if($imagedata[0] < $imagedata[1]) $thumbnail_size = round($imagedata[1] * $width_percentage / 100) + 1;
   			$images[$images_files[$i]]['thumb width'] = $module_settings["images"]["thumbnail size"];
   			$images[$images_files[$i]]['thumb height'] = $imagedata[0] < $imagedata[1] ? $thumbnail_size : round($imagedata[1] * $width_percentage / 100);
   		}
   		
   		$images[$images_files[$i]]['img src'] = $system_data['folder']['plugins'] . 'thumbnail/thumbnail.php?image=' . $images_dir . $images_files[$i];
		$images[$images_files[$i]]['img src'] .= '&amp;thumbnail_size=' . $thumbnail_size;
		$images[$images_files[$i]]['img src'] .= '&amp;thumbnail_crop_width=' . $module_settings["images"]["thumbnail crop width"];
		$images[$images_files[$i]]['img src'] .= '&amp;thumbnail_crop_height=' . $module_settings["images"]["thumbnail crop height"];
		$images[$images_files[$i]]['img src'] .= '&amp;thumbnail_crop=' . ($module_settings["images"]["thumbnail mode"] == 'crop' ? 1 : 0);
		$images[$images_files[$i]]['img src'] .= '&amp;thumbnail_background=' . trim($module_settings["images"]["thumbnail background"], '#');
		$images[$images_files[$i]]['img src'] .= '&amp;thumbnail_quality=' . $module_settings["images"]["thumbnail quality"];
		$images[$images_files[$i]]['img src'] .= '&amp;thumbnail_reflection=' . $module_settings["images"]["thumbnail reflection"];
		$images[$images_files[$i]]['img src'] .= '&amp;thumbnail_rotation=' . $module_settings["images"]["thumbnail rotation"];
		$images[$images_files[$i]]['img src'] .= '&amp;thumbnail_rotate_cw=' . $module_settings["images"]["thumbnail rotate cw"];
		
   		$images[$images_files[$i]]['last image'] = isset($images_files[$i - 1]) ? $images_files[$i - 1] : '';
   		$images[$images_files[$i]]['next image'] = isset($images_files[$i + 1]) ? trim($images_files[$i + 1]) : '';
	}

	// Handle full view
	switch($module_settings["images"]["open image"]) {
		case 'popup' :
			$module_settings["images"]["open image attribute"] = 'onclick="imagesPopup(this.href); return false"';
			$module_settings["images"]["page show images"] = 1;
			$module_settings["images"]["page start full"] = 0;
			break;

		case 'page' :
			$module_settings["images"]["open image attribute"] = 'onclick="pagemapImagesPageOpen(this.href); window.scroll(0, 0); return false"';
			break;
		
		case 'custom' :
			$module_settings["images"]["open image attribute"] = html_entity_decode(stripslashes($module_settings["images"]["link attributes"]));
			$module_settings["images"]["page show images"] = 1;
			$module_settings["images"]["page start full"] = 0;
			break;
	}

/**
 * Check whether gallery description should be visible
 *
 * @category   Module (images)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-08-08
 */

	$module_settings["images"]["gallery description visible"] = !empty($module_settings["images"]["gallery description"]) && get_url_value($page_info->alias) != 'images' ? 1 : 0;

/**
 * Include module frontend template
 *
 * @category   Module (images)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   1.0
 */

	include_template('images');

?>