function getFirstElementKey(data) {
  for (var prop in data)
    return prop;
}

function ifrondMap(options) {
	
	settings = {};	
	settings.lat = 54.31848;
	settings.lng = 48.39125;
	settings.zoom = 14;
	settings.width = '100%';
	settings.controls = true;
	settings.wheel = false;
	settings.type = google.maps.MapTypeId.ROADMAP;
	settings.wrap = "#geoMap";
	$.extend(settings, options);	
	this.markers = [];
	this.infoWindows = [];
	this.markers = [];
	this.map;
	this.options = settings;
	
	this.alertMessage = function(message) {
		alert(message);
	};
	
	this.getMarkerPath = function(name) {
		return this.options.iconPrefix + name + '.png';
	};
	
	this.setHeight = function(height) {
		this.wrap.height(height);
		google.maps.event.trigger(this.map, 'resize');
	};
	
	this.getMarkersFromJson = function(json) {
		var obj = $.parseJSON(json);
		this.setMarkers(obj);
	};
	
	this.setMarkers = function(markersArr) {
		for(var m in markersArr) {
			this.markerAdd(markersArr[m]);
		}
	};
		
	this.getCenter = function() {
		latLng = this.map.getCenter();
		return {lat: latLng.lat(), lng: latLng.lng()};
	};
	
	this.markerMove = function(id) {
		
	};
	
	this.onDragEnd = function(id) {
		
	};
	
	this.beforeMarkerNew = function() {
		
	};
	
	this.afterInfoWindowOpen = function(id) {		
		
	};
	
	this.infoWindowOpen = function(id) {
		if (typeof this.infoWindows[id] == 'undefined') {
			var tmap = this;
			var url = this.options.infowindowUpdateUrl;
			url = url.replace("?", id);			
			$.getJSON(url, function(item) {
				tmap.infoWindows[item.id] = new google.maps.InfoWindow({ 
					content: item.info,
					maxWidth: item.width
				});
				tmap.infoWindowOpenDo(id);
			});
		} else {
			this.infoWindowOpenDo(id);
		}
	};
	
	this.infoWindowOpenDo = function(id) {		
		for(var m in this.infoWindows) {
			if (this.infoWindows.length > 0) {
				if (typeof this.infoWindows[m] == 'object') {
					this.infoWindows[m].close();
				} 
			}
		}		
		this.infoWindows[id].open(this.map, this.markers[id]);
		this.afterInfoWindowOpen(id);	
	}
	
	
	
	this.markerRemove = function(id) {
		if (typeof this.infoWindows[id] != 'undefined') {
			this.infoWindows[id].close();
		}
		if (typeof this.markers[id] != 'undefined') {
			this.markers[id].setMap(null);
		}
	};
	
	this.center = function(latlng, level) {
		this.map.setCenter(latlng);
		if (typeof level != 'undefined') {
			this.map.setZoom(level);
		}
	};
	
	this.markerRemoveAll = function() {
		for(m in this.markers) {
			this.markerRemove(m);
		}
	};
		
	this.initMap = function(options) {
		var latlng = new google.maps.LatLng(options.lat, options.lng);
		var myOptions = {
			zoom: options.zoom,
			center: latlng,
			mapTypeControl: options.controls,
			scrollwheel: options.wheel,
			mapTypeId: options.type
		};
		
		this.map = new google.maps.Map(this.wrap.get(0), myOptions);
		if (options.editable) {
			var tMap = this.map;
			var tObj = this;
		}
	};
	
	this.wrap = $(this.options.wrap);
	
	this.initMap(this.options);
	
	this.shadow = new google.maps.MarkerImage(this.getMarkerPath(this.options.shadow),
		new google.maps.Size(25, 21),
		new google.maps.Point(0,0),
		new google.maps.Point(7, 21)
	);
	
	
	this.markerAdd = function(item) {
		if(typeof item == 'object') {
			
			var myLatlng = new google.maps.LatLng(item.lat, item.lng);
			if (typeof(item.title)=='number') item.title = item.title.toString();
			var settings = {
					position: myLatlng,
					map: this.map,
					title: item.title
				};
			
			if (typeof this.markers[item.id] !== 'undefined') this.markerRemove(item.id);	
			this.markers[item.id] = new google.maps.Marker(settings);
			
			if (typeof item.description != 'undefined' && item.description != '') {
				console.log(item.description);
				this.infoWindows[item.id] = new google.maps.InfoWindow({ 
					content: item.description,
					maxWidth: 300
				});		
			}
			var tmap = this;
			
			google.maps.event.addListener(this.markers[item.id], 'click', function() {					
				tmap.infoWindowOpen(item.id);
			});
			
			return this.markers[item.id];
		} else {
			return false;
		}
	};	
	
	this.fitToMarkers = function() {
		var bounds = new google.maps.LatLngBounds ();
		for(m in this.markers) {
			bounds.extend (this.markers[m].getPosition());
		}
		this.map.fitBounds (bounds);
	};
	
} 

$(function() {
	var theMap = new ifrondMap();
	var markersJson = $('#geoMapMarkers').text();
	theMap.getMarkersFromJson(markersJson);
	var firstKey = getFirstElementKey(theMap.markers);
	
	var marker = theMap.markers[firstKey];
	theMap.center(marker.getPosition());
	theMap.fitToMarkers();
	
});
