var pointsOnMap = new Hash();

var Map = Class.create({ 

	initialize: function() { 			
		$$("a.clsShowMap").invoke("observe","click", this.getCategoryCoordinates.bindAsEventListener(this));
		$$("a.clsHideMap").invoke("observe","click", this.hideMap.bindAsEventListener(this));
	},
	
	getCategoryCoordinates: function(event) {
		event.stop();

        $$(".clsHideForMap").invoke("addClassName","clsDisplayNone");
        $$(".clsShowForMap").invoke("removeClassName","clsDisplayNone");

		this.points = new Array();
		this.currentBounds = new GLatLngBounds(); 		                
        
        this.map = new GMap2($("idMap"));
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
        
		this.map.setCenter(new GLatLng(56.664895,16.636477), 7);

		pointsOnMap.each(function(objectPoints) {
			objectPoints[1].points.each(function(point) {
				this.addPoint(point);
			}.bind(this));
		}.bind(this));
		
		this.points.each(function(point) {
			this.addPointOnMap(point);
		}.bind(this));
		
		if (this.points.size() == 1) {
			this.map.setCenter(new GLatLng(parseFloat(this.points[0].get("latitude")),parseFloat(this.points[0].get("longitude"))),parseInt(this.points[0].get("zoomlevel")));			
		} else if (this.points.size() > 1) {
			this.map.setCenter(this.currentBounds.getCenter(), this.map.getBoundsZoomLevel(this.currentBounds));
		}
	},
	
	addPoint: function(point) {
		var found = false;
		var pointHash = $H(point);
		this.points.each(function(oldPoint,index) {
			if (oldPoint.get("latitude") == pointHash.get("latitude") && oldPoint.get("longitude") == pointHash.get("longitude")) {
				found = true;
				oldPoint.set("objectid",oldPoint.get("objectid")+","+pointHash.get("objectid"));
			}
		}.bind(this));
		if (!found) this.points.push($H(point));
	},
	
	
	addPointOnMap: function(markerConfig) {

        var icon = new GIcon();
        icon.image = markerConfig.get("icon");
        icon.iconSize = new GSize(32, 37);
        icon.iconAnchor = new GPoint(16, 37);
        icon.infoWindowAnchor = new GPoint(16,0);

		var markerPoint = new GLatLng(parseFloat(markerConfig.get("latitude")),parseFloat(markerConfig.get("longitude")));
        var marker = new GMarker(markerPoint, {draggable: false, icon:icon});
        
        this.currentBounds.extend(markerPoint);
        
        this.map.addOverlay(marker);
       	
       	GEvent.addListener(marker, "click", function() {
            this.openInfoWindow(marker,markerConfig);
        }.bindAsEventListener(this));
       	
    },
	
	
	openInfoWindow: function (marker,markerConfig) {
		this.currentContent = "<h2>"+markerConfig.get("address")+"</h2>";
		var ids = markerConfig.get("objectid").split(",");
		ids.each(function(objectid,index) {
			new Ajax.Request("http://localhost/basic-updater/index.php?action=get_template&template=EvenemangInfoWindow&id="+markerConfig.get("objectid")+"&options[category]="+markerConfig.get("category"), {
	           onComplete: function (response) {
	           		this.currentContent = this.currentContent+response.responseText+((ids.size()>1 && index < ids.size()-1) ? "<hr />" : "");
	           		if (index == ids.size()-1) marker.openInfoWindowHtml(this.currentContent);		        
	           }.bind(this)
	   		});
   		}.bind(this));
	},	
	
	hideMap: function(event) {
		event.stop();
        $$(".clsShowForMap").invoke("addClassName","clsDisplayNone");
        $$(".clsHideForMap").invoke("removeClassName","clsDisplayNone");		
	}
		
});

$(document).observe("dom:loaded", function() {
	var map = new Map();
});
