/*
 * options:
 * 	zoom - int - map zoom
 * 	latitude - int - center latitude 
 *  longitude - int - center longitude
 *  center - google.maps.LatLng(latitude, longitude) - map center position
 *	type - mapTypeId (google.maps.MapTypeId.ROADMAP) - map type
 */

function googlemap(id, options)
{
	this.id = id;
	this.options = {};
	this.map;
	this.markers = new Array();
	this.markerOptions = new Array();
	this.infoWindows = new Array();
	this.images = {
		hotel: '/img/markers/color/hotel.png',
		nearbyHotel: '/img/markers/nearby_hotel.png',
		landmark: '/img/markers/color/landmark.png'
		};
	
	this.init = function(options) {
		if(options==undefined) {
			var options = {};
		}
		this.options['zoom'] = options['zoom'] ? options['zoom'] : 12;
		if(options['latitude'] && options['longitude']) {
			this.options['center'] = options['center'] ? options['center'] : new google.maps.LatLng(options['latitude'], options['longitude']);
		}
		this.options['mapTypeId'] = options['type'] ? options['type'] : google.maps.MapTypeId.ROADMAP;
		this.options['scrollwheel'] = false;
	}

	this.append = function(type, latitude, longitude, options) {
		if(options==undefined) {
			var options = {};
		}
		var markerOptions = {
			type: type,
			latitude: latitude,
			longitude: longitude,
			options: options
			};
		if(options['id']!=undefined)
			this.markerOptions[options['id']] = markerOptions;
		else
			this.markerOptions.push(markerOptions);
	}

	this.addMarker = function(type, latitude, longitude, options) {
		if(options==undefined) {
			var options = {};
		}
		var image = this.images[type];
		var position = new google.maps.LatLng(latitude, longitude);
		var params = {
			position: position,
			map: this.map,
			icon: image
			};
		if(options['title']!=undefined) {
			params['title'] = options['title'];
		}
		var marker = new google.maps.Marker(params);
		if(options['id']!=undefined)
			this.markers[options['id']] = marker;
		else
			this.markers.push(marker);
		if(options['content']!=undefined) {
			var infoOptions = {
				id: options['id'],
				title: options['title'],
				content: options['content'],
				notice: options['notice'],
				link: options['link']
			}
			this.addInfoWindow(marker, infoOptions);
		}
	}
	
	this.addInfoWindow = function(marker, options)
	{
		if(options['notice']==undefined) {
			options['notice'] = '';
		}
		if(options['link']!=undefined) {
			options['title'] = '<a href="' + options['link'] + '">' + options['title'] + '</a>';
		}
		var content = '<div id="content">'+
			'<div id="siteNotice">'+
			options['notice'] +
			'</div>'+
			'<h1 id="firstHeading" class="firstHeading">' + options['title'] + '</h1>'+
			'<div id="bodyContent">'+
			'<p>' + options['content'] + '</p>'+
			'</div>'+
			'</div>';
		
		var infowindow = new google.maps.InfoWindow({
			content: content
		});
		
		if(options['id']!=undefined)
			this.infoWindows[options['id']] = infowindow;
		else
			this.infoWindows.push(infowindow); //TODO: môžu sa prekrývať indexy
		var self = this;
		google.maps.event.addListener(marker, 'click', function() {
			self.hideInfoWindows();
			infowindow.open(self.map, marker);
			self.map.setCenter(marker.getPosition());
		});
		return infowindow;
	}
	
	this.showInfoWindow = function(id) {
		this.hideInfoWindows();
		if(this.infoWindows[id]!=undefined) {
			this.infoWindows[id].open(this.map, this.markers[id]);
			this.map.setCenter(this.markers[id].getPosition());
		}
	}
	
	this.hideInfoWindows = function() {
		for(i in this.infoWindows) {
			this.infoWindows[i].close();
		}
	}

	this.show = function() {
		if(this.markerOptions.length > 0) {
			if(!this.options['center']) {
				var latitude = 0;
				var longitude = 0;
				var count = 0;
				for(i in this.markerOptions) {
					latitude += this.markerOptions[i]['latitude'];
					longitude += this.markerOptions[i]['longitude'];
					count += 1;
				}
				latitude = latitude / count;
				longitude = longitude / count;
				this.options['center'] = new google.maps.LatLng(latitude, longitude);
			}

			this.map = new google.maps.Map(document.getElementById(id), this.options);

			for(i in this.markerOptions) {
				this.addMarker(this.markerOptions[i]['type'], this.markerOptions[i]['latitude'], this.markerOptions[i]['longitude'], this.markerOptions[i]['options']);
			}
		}
	}

	this.init(options);
}
