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

/**
 * Default settings
 *
 * @category   Module (image-set)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-06-15
 */
	
	global $modules, $page_info, $system_data, $user_data, $images, $images_files, $id, $images_dir, $system_data, $template_data;
	
	if(!isset($module_settings['image-set']['images dir'])) $module_settings['image-set']['images dir'] = $system_data['folder']['pages'] . $_GET['page'] . '/';
	if(!isset($module_settings['image-set']['images sort order'])) $module_settings['image-set']['images sort order'] = 'none';
	if(!isset($module_settings["image-set"]["image width"])) $module_settings["image-set"]["image width"] = 800;
	if(!isset($module_settings["image-set"]["image height"])) $module_settings["image-set"]["image height"] = 600;
	if(!isset($module_settings["image-set"]["open image"])) $module_settings["image-set"]["open image"] = 'disabled';
	if(!isset($module_settings["image-set"]["image properties"])) $module_settings["image-set"]["image properties"] = 1;
	if(!isset($module_settings["image-set"]["link attributes"])) $module_settings["image-set"]["link attributes"] = 'rel=&quot;lytebox[' . $_GET['module-position'] . ']&quot;';
	if(!isset($module_settings["image-set"]["popup width"])) $module_settings["image-set"]["popup width"] = 800;
	if(!isset($module_settings["image-set"]["popup height"])) $module_settings["image-set"]["popup height"] = 600;
	
	if(!is_dir($module_settings['image-set']['images dir'])) $module_settings['image-set']['images dir'] = $system_data['folder']['pages'] . $_GET['page'] . '/';

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

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

/**
 * Get data for images
 *
 * @category   Module (image-set)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-06-15
 */

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

	// Simulate content if feed strip tags
	if(defined('feed_script')) 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['image-set']['images sort order'] == 'none') {
		$images_array = file($catalogue_file);
		$images_files = array();
		
		foreach($images_array as $image_entry) {
			$image_entry = $module_settings['image-set']['images dir'] . trim($image_entry);
			if(is_file($image_entry)) $images_files[] = $image_entry;
		}
	} else {
		$images_files = filelist($module_settings['image-set']['images dir'], 'image', array(), array(), true);
	}
	
	switch($module_settings['image-set']['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++) {
		// Set path info
   		$images[$images_files[$i]]['path'] = $images_dir . $images_files[$i];
   		$images[$images_files[$i]]['basename'] = $images_files[$i];

    	// Set dimensions
   		$imagedata = getimagesize($images_dir . $images_files[$i]);
   		
   		if($module_settings["image-set"]["image width"] > $imagedata[0]) $module_settings["image-set"]["image width"] = $imagedata[0];
   		if($module_settings["image-set"]["image height"] > $imagedata[1]) $module_settings["image-set"]["image height"] = $imagedata[1];
   		
    	if($module_settings["image-set"]["image width"] != 0 && $module_settings["image-set"]["image height"] != 0)
    		$image_static_dimensions = $imagedata[0] > $imagedata[1] ? 'horizontal' : 'vertical';
    	else
    		$image_static_dimensions = 'none';
    	
    	if(($module_settings["image-set"]["image width"] == 0 && $module_settings["image-set"]["image height"] != 0) || $image_static_dimensions == 'vertical') {
   			$images[$images_files[$i]]['width'] = floor($module_settings["image-set"]["image height"] * $imagedata[0] / $imagedata[1]);
   			$images[$images_files[$i]]['height'] = $module_settings["image-set"]["image height"];
    	} else if(($module_settings["image-set"]["image height"] == 0 && $module_settings["image-set"]["image width"] != 0) || $image_static_dimensions == 'horizontal') {
   			$images[$images_files[$i]]['width'] = $module_settings["image-set"]["image width"];
   			$images[$images_files[$i]]['height'] = floor($module_settings["image-set"]["image width"] * $imagedata[1] / $imagedata[0]);
    	}
    	
		// Set default dimensions
		if(!isset($images[$images_files[$i]]['width'])) $images[$images_files[$i]]['width'] = $imagedata[0];
		if(!isset($images[$images_files[$i]]['height'])) $images[$images_files[$i]]['height'] = $imagedata[1];

    	// Get image properties
    	$images[$images_files[$i]]['xml file'] = $images_dir . '/storage/' . $images[$images_files[$i]]['basename'] . '.xml';
    	$images[$images_files[$i]]['title'] = '';
    	$images[$images_files[$i]]['description'] = '';
    	
    	if(is_file($images[$images_files[$i]]['xml file'])) {
    		$image_xml = simplexml_load_file($images[$images_files[$i]]['xml file']);
    		$images[$images_files[$i]]['title'] = $image_xml->title[0];
    		$images[$images_files[$i]]['description'] = $image_xml->description[0];
    	}
	}

	// Handle full view
	switch($module_settings["image-set"]["open image"]) {
		case 'popup' : $module_settings["image-set"]["open image attribute"] = 'onclick="imagesPopup(this.href); return false"'; break;
		case 'custom' : $module_settings["image-set"]["open image attribute"] = html_entity_decode(stripslashes($module_settings["image-set"]["link attributes"])); break;
	}

/**
 * Include module frontend template
 *
 * @category   Module (image-set)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-06-15
 */

	include_template('image-set');

?>