<?php
#CMS - CMS Made Simple
#(c)2004-2007 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 
function smarty_cms_function_gmapskml($params, &$smarty)
{
    $key     = ($params['key']);
    $zoom    = isset($params['zoom']) ? $params['zoom'] : 12; //setting default zoom value
    $width   = ($params['width']);
    $height  = ($params['height']);
    $maptype = isset($params['maptype']) ? $params['maptype'] : ROADMAP; //setting default Map Type  
	$kml1 = ($params['kml1']);
	$kml2 = ($params['kml2']);
	$kml3 = ($params['kml3']);
	$lon = ($params['lon']);
	$lat = ($params['lat']);
	$div = ($params['div']);
    $statkart = isset($params['statkart']) ? $params['statkart'] : 'false';
	
    if (empty($key)) {
        // if no key is entered display warning message
        echo '<p style="color:#000; background-color:#fdaea8; border:2px solid #f00; padding:10px;">Please enter your API key</p>';
    }
    
    if (isset($params['key']))
        echo '
    <script src="http://maps.googleapis.com/maps/api/js?key=' . $key . '&sensor=false" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8"> 
    //<![CDATA[
    
    ';
    if (empty($width) && empty($height)) {
        // if no width and height is set write div with default size
        echo '
    { document.write("<div id=\"' . $div . '\" style=\"width:250px; height:250px;\"><\/div>"); }
    ';
    } else {
        echo '
    { document.write("<div id=\"' . $div . '\" style=\"width:' . $width . 'px; height:' . $height . 'px;\"><\/div>"); }
	';
    }
    echo '
    //]]>
    </script>
    ';
    echo '
    <script type="text/javascript">//<![CDATA[

		  function StatkartMapType(name, layer) {
			  this.layer = layer
			  this.name = name
			  this.alt = name

			  this.tileSize = new google.maps.Size(256,256);
			  this.maxZoom = 19;
			  this.getTile = function(coord, zoom, ownerDocument) {
				    var div = ownerDocument.createElement("DIV");
				    div.style.width = this.tileSize.width + "px";
				    div.style.height = this.tileSize.height + "px";
				    div.style.backgroundImage = "url(http://opencache.statkart.no/gatekeeper/gk/gk.open_gmaps?layers=" + this.layer + "&zoom=" + zoom + "&x=" + coord.x + "&y=" + coord.y + ")";
				    return div;
				  };
		  }
	
    var ' . $div . ' = null;                   
	
	var myOptions = {
		zoom: ' . $zoom . ',
		';
		if (isset($lat) && isset($lon)) {
			echo '
			center: new google.maps.LatLng(' . $lat . ', ' . $lon .'),
			';
		}
		
		if ($statkart == 'true') {
			echo '
		      mapTypeControlOptions: {
		        mapTypeIds: ["kartdata2", "sjo_hovedkart2", "topo2", "topo2graatone", "toporaster2", "europa", google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN],
		        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
		      }
			';
		} else {
			echo '
				mapTypeId: google.maps.MapTypeId.' . $maptype . '
			';
		}
		
		echo '
		
		
	};
	' . $div . ' = new google.maps.Map(document.getElementById("' . $div . '"),myOptions);

	';
	if ($statkart == 'true') {
	echo '
		' . $div . '.mapTypes.set("sjo_hovedkart2",new StatkartMapType("Sjo hovedkart", "sjo_hovedkart2"));
		' . $div . '.mapTypes.set("kartdata2",new StatkartMapType("Kartdata 2", "kartdata2"));
		' . $div . '.mapTypes.set("topo2",new StatkartMapType("Topografisk", "topo2"));
		' . $div . '.mapTypes.set("topo2graatone",new StatkartMapType("Graatone", "topo2graatone"));
		' . $div . '.mapTypes.set("toporaster2",new StatkartMapType("Toporaster", "toporaster2"));
		' . $div . '.mapTypes.set("europa",new StatkartMapType("Europa", "europa"));
	
		' . $div . '.setMapTypeId("topo2");
	';
	}
	
	if (isset($params['kml1'])) {
	
	echo '
	var kmlLayer1 = new google.maps.KmlLayer("' . $kml1 . '");
	kmlLayer1.setMap(' . $div . ');

	';
	}

	if (isset($params['kml2'])) {
	
	echo '
	var kmlLayer2 = new google.maps.KmlLayer("' . $kml2 . '");
	kmlLayer2.setMap(' . $div . ');

	';
	}

	if (isset($params['kml3'])) {
	
	echo '
	var kmlLayer3 = new google.maps.KmlLayer("' . $kml3 . '");
	kmlLayer3.setMap(' . $div . ');

	';
	}
	
	echo '
           
    //]]>
    </script> 
    ';
}

function smarty_cms_help_function_gmapskml()
{
?>	<h3>What does this do?</h3>
<p>
	This adds a simple Google Map on your page. With support for up to 3 KML/KMZ files and the Norwegian Statkart service.<br /><br />
	In principle several maps can be shown on the same page using different map IDs - but this is currently buggy
</p>
<h3>How do I use it?</h3>
<p>
	Just insert the tag into your template/page like:<br />
	<code>
		{gmapskml div="map" key="your API key" lat="lattitude" lon="longitude"}<br />
		{gmapskml div="map" key="your API key" kml1="path to your KML file"}<br />
		{gmapskml div="map" key="your API key" kml1="path to your KML file" statkart="true"}
	</code>
	<br />
	<br />
	If you want to style your map, like adding a border, padding or whatever, simply use #map (id map) in your Stylesheet.
	<br />
	For example:
	<br />
	<code>
		#map {padding:10px: background:#f00;margin:10px; border:1px solid #000;}
	</code>
</p>
<h3>What parameters does it take?</h3>
<p></p>
<ul>
	<li>
		<em><strong>(required)</strong> </em>
		<tt>div </tt> - The map ID.
	</li>
	<li>
		<em><strong>(required if no KML file given)</strong> </em>
		<tt>lat </tt> - Lattitude.
	</li>
	<li>
		<em><strong>(required if no KML file given)</strong> </em>
		<tt>lon </tt> - Longitude.
	</li>
	<li>
		<em><strong>(required if no coordinates are given)</strong> </em>
		<tt>kml1 </tt> - Path for the KML file.
	</li>
	<li>
		<em>(optional) </em>
		<tt>kml2 </tt> - Path for the KML file.
	</li>
	<li>
		<em>(optional) </em>
		<tt>kml3 </tt> - Path for the KML file.
	</li>
	<li>
		<em><strong>(required)</strong> </em>
		<tt>key </tt> - Google Maps API key. You can signup for a key <a href="http://code.google.com/intl/en/apis/maps/signup.html" target="_blank">here</a>
	</li>
	<li>
		<em>(optional) </em>
		<tt>width </tt> - Set to 250 by default. Simply add a value like 500, no px needed.
	</li>
	<li>
		<em>(optional) </em>
		<tt>height </tt> -  Set to 250 by default. Simply add a value like 500, no px needed.
	</li>
	<li>
		<em>(optional) </em>
		<tt>zoom </tt> - Set to 12 by default.
	</li>
	<li>
		<em>(optional) </em>
		<tt>maptype </tt> - Set to ROADMAP by default. You can use SATELLITE for Satellite, HYBRID for Hybrid map or TERRAIN for a physical map.
	</li>
	<li>
		<em>(optional) </em>
		<tt>statkart </tt> - Set to true if the Norwegian Statkart service is required.
	</li>

</ul>
<?php
}
function smarty_cms_about_function_gmapskml()
{
?>
<p>
	Author:	Morten Bjergstrøm <a href="http://www.miljokemi.dk" target="_blank">www.miljokemi.dk</a>
	<br/ ><br/ >
	Based on the gmaps plugin by:
	Author: Goran Ilic <a href="http://www.ich-mach-das.at" target="_blank">www.ich-mach-das.at</a>
</p>
<p>
	Version: 1.0
</p>
<?php
}
?>