
cms2.google.inlinemap = {}
cms2.google.inlinemap.InlineMap = function(){
    this.evAjaxLoaderAvailable = function(){
        //console.log("evAjaxLoaderAvailable")
        google.load("maps", "2", {
            "callback": cms2.google.inlinemap.instance.evMapsAvailable
        });
    }
    this.evMapsAvailable = function(){
        var cont = document.getElementById(cms2.google.maps.containerId)
        cont.style.backgroundImage = "none"
        var map = new google.maps.Map2(cont);
		map.enableScrollWheelZoom()
		if(cms2.google.maps.centerPosition){
			map.setCenter(new google.maps.LatLng(cms2.google.maps.centerPosition.lat, cms2.google.maps.centerPosition.lng), cms2.google.maps.centerPosition.zoom);
		}else{
			var defPlace=cms2.google.maps.places[cms2.google.maps.defaultPlace]
		 	map.setCenter(new google.maps.LatLng(defPlace.lat, defPlace.lng), defPlace.zoom);
		}
		for (var id in cms2.google.maps.places) {
            var place = cms2.google.maps.places[id]
            var point = new GLatLng(place.lat, place.lng)
            var icon = new GIcon()//TODO
            var gOptions={
                icon: GIcon.G_DEFAULT_ICON
                ,title: place.title
            }
			if(place.zIndex){
				gOptions.zIndexProcess=new Function("return " +place.zIndex)
			}
            var marker = new GMarker(point,gOptions )
            map.addOverlay(marker)
			var isExtended=typeof(place.extended) != "undefined"
			if(isExtended){
				var img=typeof(place.miniature) != "undefined"?"<img src='"+place.miniature+"' class='inlineMapInfoWindowImage'/>":""
                place.extended="<div class='inlineMapInfoWindowContent'>"+img+place.extended+"</div>"
			}
			 if (id == cms2.google.maps.defaultPlace && isExtended) {
				marker.openInfoWindowHtml(place.extended)
            }
			if(isExtended){
				var htmlCont=place.extended
				cms2.google.inlinemap.instance.assignMarkerEvents(map,marker,point,htmlCont)
			}
        }
        for (var i = 0; i < cms2.google.maps.controls.length; i++) {
            eval("var ctl=new " + cms2.google.maps.controls[i] + "()")
            map.addControl(ctl)
        }
        //todo implementacja eventów
        if (cms2.google.maps.drawPolylines) {
            var p = new cms2.google.polyline.Polyline()
            p.drawShape(cms2.google.polyline.fromString(cms2.google.polyline.config.shapes[cms2.google.polyline.config.defaultShape].polyline), map)
        }
    }
	this.assignMarkerEvents=function(map,marker,point,htmlCont){
				GEvent.addListener(marker, "click", function() {
					map.openInfoWindowHtml(point,htmlCont)
					});
	}
    var cont = document.getElementById(cms2.google.maps.containerId)
    cont.style.display = "block"
    cont.style.width = cms2.google.maps.mapWidth + "px"
    cont.style.height = cms2.google.maps.mapHeight + "px"
    cont.style.backgroundRepeat = "no-repeat"
    cont.style.backgroundImage = "url(/modules/layout/loading-100x100.gif)"
    cont.style.backgroundPosition = "center center"
    cms2.google.utils.initAjaxLoader("cms2.google.inlinemap.instance.evAjaxLoaderAvailable")
}
cms2.google.inlinemap.instance = new cms2.google.inlinemap.InlineMap()

