<?php

function preprint($s, $return=false) { 
    $x = "<pre>"; 
    $x .= print_r($s, 1); 
    $x .= "</pre>"; 
    if ($return) return $x; 
    else print $x; 
    } 
/**
 * Prints the Metadata data of the current image
 *
 * @param string $title title tag for the class
 * @param bool $toggle set to true to get a javascript toggle on the display of the data
 * @param string $id style class id
 * @param string $class style class
 * @author Ozh
 */
function printImageMetadataMod($title = NULL, $toggle = true, $id = 'imagemetadata', $class = null, $span = NULL) {
	global $_zp_exifvars, $_zp_current_image;
	if (false === ($exif = getImageMetaData($_zp_current_image, true))) {
		return;
	}
	if (is_null($title)) {
		$title = gettext('Image Info');
	}
	if ($class) {
		$class = ' class="' . $class . '"';
	}
	if (!$span) {
		$span = 'exif_link';
	}
	$dataid = $id . '_data';
	if ($id) {
		$id = ' id="' . $id . '"';
	}
	$refh = $refa = $style = '';
	if ($toggle == 'colorbox' && zp_has_filter('theme_head', 'colorbox::css')) {
		$refh = '<a href="#" class="colorbox" title="' . $title . '">';
		$refa = '</a>';
		$style = ' style="display:none"';
	} else if ($toggle) {
		$refh = '<a href="javascript:toggle(\'' . $dataid . '\');" title="' . $title . '">';
		$refa = '</a>';
		$style = ' style="display:none"';
	}
	?>
	<span id="<?php echo $span; ?>" class="metadata_title">
		<?php echo $refh; ?><?php echo $title; ?><?php echo $refa; ?>
	</span>
	<div id="<?php echo $dataid; ?>"<?php echo $style; ?>>
		<div<?php echo $id . $class; ?>>
			<table>
				<?php
				foreach ($exif as $field => $value) {
					$label = $_zp_exifvars[$field][2];
					echo "<tr><td class=\"label\">$label:</td><td class=\"value\">";
					switch ($_zp_exifvars[$field][6]) {
						case 'time':
							//echo zpFormattedDate(DATE_FORMAT, strtotime($value));
							echo date('d.m.Y à H:i',strtotime($value));
							break;
						default:
							echo html_encode($value);
							break;
					}
					echo "</td></tr>\n";
				}
				?>
			</table>
		</div>
	</div>
	<?php
}

/**
 * Puts up random image thumbs from the gallery
 *
 * @param int $number how many images
 * @param string $class optional class
 * @param string $option what you want selected: all for all images, album for selected ones from an album
 * @param string $rootAlbum optional album from which to get the images
 * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size.
 * @param integer $height the height/cropheight of the thumb if crop=true else not used
 * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
 */
function printRandomImagesCustom($number=5, $class=null, $option='all', $rootAlbum='',$width=100,$height=100,$crop=true) {
	if (!is_null($class)) {
		$class = ' class="' . $class . '"';
	}
	for ($i=1; $i<=$number; $i++) {
		switch($option) {
			case "all":
				$randomImage = getRandomImages();
				break;
			case "album":
				$randomImage = getRandomImagesAlbum($rootAlbum);
				break;
		}
		//print_r($randomImage);
		
		if (is_object($randomImage) && $randomImage->exists) {
			//$randomImageURL = html_encode(getURL($randomImage));
			$randomImageURL = $randomImage->getLink();  // ZP 1.4.6
			
			echo '<a href="' . $randomImageURL . '" title="'.sprintf(gettext('View image: %s'), html_encode($randomImage->getTitle())) . '">';
			if($crop) {
				$html = "<img src=\"".html_encode($randomImage->getCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, TRUE))."\" alt=\"" . html_encode($randomImage->getTitle()) . "\" />\n";
			} else {
				$html =  "<img src=\"".html_encode($randomImage->getCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, TRUE))."\" alt=\"" . html_encode($randomImage->getTitle()) . "\" />\n";
			}
			echo zp_apply_filter('custom_image_html', $html, false);
			echo "</a>";
		}
	}
}

function printHDRandomImagesCustom($number=5, $class=null, $option='all', $rootAlbum='',$width=100,$height=100,$crop=true) {
	if (!is_null($class)) {
		$class = ' class="' . $class . '"';
	}
	for ($i=1; $i<=$number; $i++) {
		switch($option) {
			case "all":
				$randomImage = getRandomImages();
				break;
			case "album":
				$randomImage = getRandomImagesAlbum($rootAlbum);
				break;
		}
		//print_r($randomImage);
		
		if (is_object($randomImage) && $randomImage->exists) {
			//$randomImageURL = html_encode(getURL($randomImage));
			$randomImageURL = $randomImage->getLink();  // ZP 1.4.6
			
			echo '<a href="' . $randomImageURL . '" title="'.sprintf(gettext('View image: %s'), html_encode($randomImage->getTitle())) . '">';
			if($crop) {
				$html = "<img src=\"".html_encode($randomImage->getCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, TRUE))."\" alt=\"" . html_encode($randomImage->getTitle()) . "\" />\n";
			} else {
				$html =  "<img src=\"".html_encode($randomImage->getCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, TRUE))."\" alt=\"" . html_encode($randomImage->getTitle()) . "\" />\n";
			}
			echo zp_apply_filter('custom_image_html', $html, false);
			echo "</a>";
		}
	}
}

function printLatestImagesCustom($number=8, $class=null, $option='latest', $width=320,$height=240,$crop=true) {
	if (!is_null($class)) {
		$class = ' class="' . $class . '"';
	}
	
	$images = getImageStatistic($number, $option);
	foreach ($images as $image) { 
		//$latestImageURL = html_encode(getURL($image));
		$latestImageURL = $image->getLink(); // ZP 1.4.6
		//echo $latestImageURL;
		echo '<a href="' . $latestImageURL . '" title="' . html_encode($image->getTitle()) . '">';
		echo '<img src="' . html_encode($image->getCustomImage(null,$width,$height,null,null,null,null,true)) . '" alt="' . html_encode($image->getTitle()) . '" />';
		echo '</a>';
		}
}

function printHDLatestImagesCustom($number=8, $class=null, $option='latest', $width=320,$height=240,$crop=true) {
	if (!is_null($class)) {
		$class = ' class="' . $class . '"';
	}
	
	$images = getImageStatistic($number, $option);
	foreach ($images as $image) { 
		//$latestImageURL = html_encode(getURL($image));
		$latestImageURL = $image->getLink(); // ZP 1.4.6
		//echo $latestImageURL;
		echo '<a href="' . $latestImageURL . '" title="' . html_encode($image->getTitle()) . '">';
		echo '<img src="' . html_encode($image->getCustomImage(null,$width,$height,null,null,null,null,true)) . '" alt="' . html_encode($image->getTitle()) . '" />';
		echo '</a>';
		}
}

/**
 * Output the google map
 *
 * @param string $text text for the "toggle" link that shows/hides the map. Set empty to omit (then Map is always displayed)
 * @param string $id used to set the IDs for the toggle href element ($id_toggle) and the map element ($id_data)
 * @param string $hide initial map state: "hide", "show", or "colorbox"
 * @param object $obj optional image/album object. Pass string for generic map and use callback to set points
 * @param function $callback optional callback function to set map options.
 */
function _printGoogleMap($text = NULL, $id = NULL, $hide = NULL, $obj = NULL, $callback = NULL) {
	global $_zp_current_album, $_zp_current_image, $_x, $_y, $_z, $_n;

	/* controls of parameters */
	if (is_null($obj)) {
		if (is_null($_zp_current_image)) {
			$obj = $_zp_current_album;
		} else {
			$obj = $_zp_current_image;
		}
	}
	if (is_null($obj)) {
		return false;
	}
	if (is_object($obj)) {
		$type = $obj->table;
		$typeid = $obj->getID();
	} else {
		$type = $obj;
		$typeid = '';
	}
	if (is_null($text)) {
		$text = gettext('Google Map');
	}
	if (empty($text)) {
		$hide = 'show';
	}
	if (is_null($hide)) {
		$hide = getOption('gmap_display');
	}
	if (!is_string($hide)) {
		if ($hide) {
			$hide = 'hide';
		} else {
			$hide = 'show';
		}
	}

	/* map configuration */
	$mapControls = getOption('gmap_control_type');
	if ($mapControls == 'none') {
		$mapTypeControl = false;
	} else {
		$mapTypeControl = true;
		$map_control_type = $mapControls;

		$allowedMapTypes = array();
		if (getOption('gmap_map_hybrid'))
			$allowedMapTypes[] = 'HYBRID';
		if (getOption('gmap_map_roadmap'))
			$allowedMapTypes[] = 'ROADMAP';
		if (getOption('gmap_map_satellite'))
			$allowedMapTypes[] = 'SATELLITE';
		if (getOption('gmap_map_terrain'))
			$allowedMapTypes[] = 'TERRAIN';
	}

	$config['center'] = '0, 0';
	$config['zoom'] = '13';
	$config['cluster'] = true;
	$config['zoomControlStyle'] = getOption('gmap_zoom_size');
	if ($mapTypeControl) {
		$config['map_type'] = getOption('gmap_starting_map');
		$config['map_types_available'] = $allowedMapTypes;
	} else {
		$config['disableMapTypeControl'] = true;
	}
	$config['map_width'] = getOption('gmap_width') . "px";
	$config['map_height'] = getOption('gmap_height') . "px";
	$config['clusterMaxZoom'] = getOption('gmap_cluster_max_zoom');
	$config['clusterAverageCenter'] = true;
	$config['onclick'] = "iw.close();";
	$config['minifyJS'] = !TEST_RELEASE;

	$map = new Googlemaps($config);

	/* add markers from geocoded pictures */
	switch ($type) {
		case 'images':
			if (getImageGeodata($obj, $map)) {
				break;
			} else {
				$map = NULL;
				return false;
			}
		case 'albums':
			if (getAlbumGeodata($obj, $map)) {
				break;
			} else {
				$map = NULL;
				return false;
			}
		default:
			break;
	}

	if ($_n == 1)
		$map->zoom = 13;

	if ($_n) {
		$_x = $_x / $_n;
		$_y = $_y / $_n;
		$_z = $_z / $_n;
		$lon = number_format(atan2($_y, $_x) * 180 / M_PI, 12, '.', '');
		$hyp = sqrt($_x * $_x + $_y * $_y);
		$lat = number_format(atan2($_z, $hyp) * 180 / M_PI, 12, '.', '');
		$map->center = $lat . ', ' . $lon;
	}

	if (!is_null($callback)) {
		call_user_func($callback, $map);
	}

	/* map display */
	if (is_null($id)) {
		$id = $type . $typeid . '_googlemap';
	}
	$id_toggle = $id . '_toggle';
	$id_data = $id . '_data';

	switch ($hide) {
		case 'show':
			$map->create_map();
			?>
			<script type="text/javascript">
				//<![CDATA[
			<?php
			echo $map->output_js_contents;
			echo omsAdditions();
			?>

				function image(album, image) {
					window.location = '<?php echo WEBPATH ?>/index.php?album=' + album + '&image=' + image;
				}
				//]]>
			</script>
			<div id="<?php echo $id_data; ?>">
				<?php echo $map->output_html; ?>
			</div>
			<?php
			break;
		case 'hide':
			$map->create_map();
			?>
			<script type="text/javascript">
				//<![CDATA[
			<?php
			echo $map->output_js_contents;
			echo omsAdditions();
			?>

				function image(album, image) {
					window.location = '<?php echo WEBPATH ?>/index.php?album=' + album + '&image=' + image;
				}

				function toggle_<?php echo $id_data; ?>() {
					if ($('#<?php echo $id_data; ?>').hasClass('hidden_map')) {
						$('#<?php echo $id_data; ?>').removeClass('hidden_map');
					} else {
						$('#<?php echo $id_data; ?>').addClass('hidden_map');
					}
				}
				//]]>
			</script>
			<a id="<?php echo $id_toggle; ?>" href="javascript:toggle_<?php echo $id_data; ?>();" title="<?php echo gettext('Display or hide the Google Map.'); ?>">
				<?php echo $text; ?>
			</a>
			<div id="<?php echo $id_data; ?>" class="hidden_map">
				<?php echo $map->output_html; ?>
			</div>
			<?php
			break;
		case 'colorbox':
			if (zp_has_filter('theme_head', 'colorbox::css')) {
				$map->create_map();
				$map_data["output_js_contents"] = $map->output_js_contents;
				$map_data["output_html"] = $map->output_html;

				if (getOption('gmap_sessions')) {
					$param = '';
					$_SESSION['GoogleMapVars'] = $map_data;
				} else {
					$serializedData = serialize($map_data);
					if (function_exists('bzcompress')) {
						$data = bzcompress($serializedData);
					} else {
						$data = gzcompress($serializedData);
					}
					$param = '?map_data=' . base64_encode($data);
				}
				?>
				<a href="<?php echo WEBPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/GoogleMap/Map.php' . $param ?>" title="<?php echo $text; ?>" class="google_map">
					<?php echo $text; ?>
				</a>
				<script type="text/javascript">
					//<![CDATA[
					$(document).ready(function() {
						$(".google_map").colorbox({
							iframe: true,
							innerWidth: '<?php echo (int) (getOption('gmap_width') + 20) ?>px',
							innerHeight: '<?php echo (int) ($cbox_h = getOption('gmap_height') + 20) ?>px',
							close: '<?php echo gettext("close"); ?>'
						});
					});
					//]]>
				</script>
				<?php
			}
			break;
	}
}

function hasMapData() {
	$exif = getImageMetaData(NULL, false);
	if(!empty($exif['EXIFGPSLatitude']) && !empty($exif['EXIFGPSLongitude'])){
		return true;
	}
	return false;
}

?>
