/**
 * Javascript for TYPO3 extension "chm_hiking"
 * 
 * @author Chris MŸller <chm@gmx.li> 
 * @version $Id$
 * @package tx_chmhiking
 */ 

/*jslint
	bitwise: true,
	browser: true,
	eqeqeq: true,
	immed: true,
	newcap: true,
	nomen: true,
	onevar: true,
	plusplus: true,
	regexp: true,
	undef: true
*/
/*global
	$,
	ChmHikingConfig,
	G_DEFAULT_ICON,
	GIcon,
	GLatLng,
	GMap2,
	GMarker,
	GPolygon,
	GPoint,
	GSize
*/


/***** Processing *************************************************************/

function ChmHiking() {
	/***** Object variables ***************************************************/

	/**
	 * Map object
	 * @type GMap2
	 * @private
	 */
	var map = null;



	/***** Initializing *******************************************************/

	/**
	 * Initializing
	 * This function will be called, when the html page is loaded
	 * @public
	 */	
	this.init = function () {
		/**
		 * Coordinates of the center of the map
		 * @type GLatLng
		 */
		var coordinates,

		/**
		 * Icon of a POI
		 * @type GIcon
		 */
		icon,

		/**
		 * Latitude/Longitude
		 * @type Array
		 */
		latlngs,

		/**
		 * Marker
		 * @type GMarker
		 */
		marker,

		/**
		 * Polygon
		 * @type GPolygon
		 */
		polygon,

		/**
		 * Helping variables
		 * @type Number
		 */
		polygonLen, i, j, latlngLen, len;


		// Neue Karte innerhalb des HTML-Containers erstellen
		map = new GMap2($(ChmHikingConfig.map.id), {mapTypes: ChmHikingConfig.map.types});

		// Set coordinates and zoom level
		coordinates = new GLatLng(ChmHikingConfig.map.centerLat, ChmHikingConfig.map.centerLng);
		map.setCenter(coordinates, ChmHikingConfig.map.zoomlevel);

		map.disableDragging();

		polygonLen = ChmHikingConfig.polygons.length;
		for (i = 0; i < polygonLen; i += 1) {
			latlngLen = ChmHikingConfig.polygons[i].latlng.length;

			latlngs = [];
			for (j = 0; j < latlngLen; j += 1) {
				latlngs[j] = new GLatLng(
					ChmHikingConfig.polygons[i].latlng[j][0],
					ChmHikingConfig.polygons[i].latlng[j][1]
				);
			}

			polygon = new GPolygon(
				latlngs,
				'#ffffff',
				0,
				0,
				ChmHikingConfig.polygons[i].fillcolor,
				ChmHikingConfig.polygons[i].opacity
			);
			map.addOverlay(polygon);
		}


		if (ChmHikingConfig.pois) {

			// set icon
			icon = new GIcon(G_DEFAULT_ICON);
			icon.image = ChmHikingConfig.pois.img.standard;
			icon.iconSize = new GSize(ChmHikingConfig.pois.img.width, ChmHikingConfig.pois.img.height);
			icon.shadow = null;
			icon.printShadow = null;
			icon.iconAnchor = new GPoint(
				Math.round(ChmHikingConfig.pois.img.width/2),
				Math.round(ChmHikingConfig.pois.img.height/2)
			);
	
			icon.imageMap = [
				0, 0,  // oben links
			    ChmHikingConfig.pois.img.width, 0,  // oben rechts
				ChmHikingConfig.pois.img.width, ChmHikingConfig.pois.img.height, // unten rechts
				0, ChmHikingConfig.pois.img.height // unten links
			];


			if (map.isLoaded()) {
				len = ChmHikingConfig.pois.marker.length;

				for (i = 0; i < len; i += 1) {
		
					marker = new GMarker(
						new GLatLng(ChmHikingConfig.pois.marker[i][0], ChmHikingConfig.pois.marker[i][1]),
						{icon: icon, clickable: false}
					);
	
					map.addOverlay(marker);
				}
	
			}
		}
	};
}



/***** Start *****************************************************************/

document.observe('dom:loaded', function() {
	var chmHiking = new ChmHiking();
	chmHiking.init();
});


