<?
	require_once("XMLParser.php");
	require_once("Config.php");

	function getGMapEZ($cfile = "map.cfg", $extend = "default") {
		$c = new Config();
		$root = $c->parseConfig($cfile, "inifile");
		$config = $root->toArray();
		$config = $config['root'];

		if (is_file($config['map']['data'])) {
			$parser = new XMLParser($config['map']['data'], 'file', 1);
			$tree = $parser->getTree();

			//print ("<PRE>"); print_r($tree); exit;
			$points = $tree['POINTS']['POINT'];

			// get the right types for controls based on config
			$mapctrl = "GLargeMapControl";
			$typectrl = "GMapTypeControl";
			$scalectrl = "GScaleControl";
			if ($config['controls']['size'] == "small") {
				if ($config['controls']['mapctrl'] == 1) {
					$mapctrl = "GSmallZoomControl";
				} 

				if ($config['controls']['typectrl'] == 1) {
					$typectrl = "GSmallMapTypeControl";
				}
			}

			// construct the class line
			$class = "GMapEZ";
			if ($config['controls']['mapctrl'] == 1) {
				$class .= " $mapctrl";
			}

			if ($config['controls']['typectrl'] == 1) {
				$class .= " $typectrl";
			}

			if ($config['controls']['scalectrl'] == 1) {
				$class .= " $scalectrl";
			}

			if ($config['map']['type'] == "map") {
				$class .= " G_MAP_TYPE";
			} else if ($config['map']['type'] == "satelite") {
				$class .= " G_SATELITE_TYPE";
			} else if ($config['map']['type'] == "hybrid") {
				$class .= " G_HYBRID_TYPE";
			}

			// javascript for gmapez and googleapi
			$html .= "\t<meta name=\"gmapkey\" content=\"" . $config['misc']['apikey'] . "\" />\n";
			$html .= "\t<script src=\"" . $config['misc']['gmapez'] . "\" type=\"text/javascript\"></script>\n";

			// main div
			$html .= "\t<div class='$class' style='width: " . $config['map']['width'] . "px; height: " . $config['map']['height'] . "px; border: 1px #888 solid;'>\n";

			// set an extend if configured
			if ($config['extends'][$extend] <> "") {
				$html .= "\t\t<A HREF='" . $config['extends'][$extend] . "'>EXTENT</a>\n";
			}
			
			$numlines = 0;

			// draw href's for the points
			for ($i = 0; $i < count($points); $i++) {
				if (!isSet($points[$i]['LINEMEMBER']['VALUE'])) {
					$html .= _getGmapsEzPoint($points[$i], $config);
				} else {
					if ($points[$i]['LINEMEMBER']['VALUE'] > $numlines) {
						$numlines = $points[$i]['LINEMEMBER']['VALUE'];
					}
				}
			}

			// draw lines
			if ($numlines > 0) {
				for ($l = 1; $l <= $numlines; $l++) {
					$lconfig = "line" . $l;
					if ($config[$lconfig]['color']) {
						$lcolor = "color:" . $config[$lconfig]['color'];
					}

					if ($config[$lconfig]['width']) {
						$lweight = "weight:" . $config[$lconfig]['width'];
					}

					if ($config[$lconfig]['opacity']) {
						$lopacity = "opacity:" . $config[$lconfig]['opacity'];
					}

					$html .= "<ol class=\"$lcolor $lweight $lopacity\">\n";
					for ($i = 0; $i < count($points); $i++) {
						if (isSet($points[$i]['LINEMEMBER']['VALUE'])) {
							if ($points[$i]['LINEMEMBER']['VALUE'] == $l) {
								$html .= "<li>" .  _getGmapsEzPoint($points[$i], $config) . "</li>\n";
							}
						}
					}
					$html .= "</ol>\n";
				}
			}

			$html .= "\t</div>\n";
		}

		return($html);
	}

	function _getGmapsEzPoint($point, $config) {
		$comment = "\t\t\t<div style=\"width: " . $config['map']['divwidth'] . "px\"><B>" . $point['TITLE']['VALUE'] . "</B>";

		if ((isSet($point['COUNTRY']['VALUE'])) && ($config['map']['showcountry'] == 1)) {
			$comment .= "<BR><B>Country:</B> " . $point['COUNTRY']['VALUE'];
		}

		if ((isSet($point['TYPE']['VALUE'])) && ($config['map']['showtype'] == 1)) {
			$comment .= "<BR><B>Type:</B> " . $point['TYPE']['VALUE'];
		}

		$comment .= "<BR>";


		if (isSet($point['COMMENT']['VALUE'])) {
			$comment .= "<i>" . $point['COMMENT']['VALUE'] . "</i><br>\n";
		}


		if (isSet($point['HREF']['VALUE'])) {
			$comment .= "<BR><A HREF='" . $point['HREF']['VALUE'] . "'>";

			if (isSet($point['LINKIMG']['VALUE'])) {
				$comment .= "<IMG SRC='" . $point['LINKIMG']['VALUE'] . "' BORDER=0>";
			} else if (isSet($point['LINKTEXT']['VALUE'])) {
				$comment .= $point['LINKTEXT']['VALUE'];
			} else {
				$comment .= $point['HREF']['VALUE'];
			}

			$comment .= "</a>";
		}

		$comment .= "</div>\n";

		$color = "";
		if (isSet($point['TYPE']['VALUE'])) {
			$type = $point['TYPE']['VALUE'];
			if (isSet($config['types'][$type])) {
				$color = strtoupper($config['types'][$type]);
			}
		}


		$href = "\t\t<A TITLE=\"" . $point['TITLE']['VALUE'] . "\" ID=\"" . $point['TITLE']['VALUE'] . "\" HREF=\"http://maps.google.com/maps?ll=" . $point['LAT']['VALUE'] . "," . $point['LONG']['VALUE'] . "\">$color</a>\n";
		if (isSet($config['map']['open'])) {
			if ($config['map']['open'] == $point['TITLE']['VALUE']) {
				$href = "\t\t<A ID=\"" . $point['TITLE']['VALUE'] . "\"HREF=\"http://maps.google.com/maps?ll=" . $point['LAT']['VALUE'] . "," . $point['LONG']['VALUE'] . "\">OPEN</a>\n";
			}
		}

		$html = $href;
		if ($point['TYPE']['VALUE'] <> "none") {
			$html .= $comment;
		}

		return($html);
	}
?>	

