$(document).ready ( function () {

if (document.getElementById('mapdiv')) {
    AutoSizeFramedCloud = new OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
        'autoSize': true
    });
        
    $('#select_map').change(function () {        
         coord = $('#select_map').attr('value').toString().split(',');
         map.setCenter (new OpenLayers.LonLat(coord[1], coord[0]).transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()), coord[2]);  
    });
    
    map = new OpenLayers.Map("mapdiv");
    map.addLayer(new OpenLayers.Layer.OSM());
 
    markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);                                               
    var size = new OpenLayers.Size(24,24);
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new OpenLayers.Icon('/templates/original/images/icon.png',size,offset)
    $.each ($('#select_map option'), function (key , obj) {
	 if (key != 0) {  
		 coord = $(obj).attr('value').toString().split(',');
	         addMarker(
	         	new OpenLayers.LonLat(coord[1], coord[0]).transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()),
	         	icon.clone (),
	         	AutoSizeFramedCloud,
	         	$(obj).attr('label'),
	         	true,
	         	true
	         	);
	         
         } else  
	         coord_center = $(obj).attr('value').toString().split(',');
         
    });          
    
    //Set start centrepoint and zoom    
    var lonLat = new OpenLayers.LonLat(coord_center[1], coord_center[0])
          .transform(
            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
            map.getProjectionObject() // to Spherical Mercator Projection
          );
    var zoom=coord_center[2];
    map.setCenter (lonLat, zoom);  
} 
});
        var currentPopup = null;
        
        function addMarker(ll, icon, popupClass, popupContentHTML, closeBox, overflow) {

            var feature = new OpenLayers.Feature(markers, ll, {'icon':icon}); 
            feature.closeBox = closeBox;
            feature.popupClass = popupClass;
            feature.data.popupContentHTML = popupContentHTML;
            feature.data.overflow = (overflow) ? "auto" : "hidden";
                    
            var marker = feature.createMarker();

            var markerClick = function (evt) {
                if (this.popup == null) {
                    this.popup = this.createPopup(this.closeBox);
                    map.addPopup(this.popup);
                    this.popup.show();
                } else {
                    this.popup.toggle();
                }                       
                if (currentPopup != null && currentPopup != this.popup) currentPopup.hide();
                currentPopup = this.popup;
                OpenLayers.Event.stop(evt);
            };
            marker.events.register("mousedown", feature, markerClick);

            markers.addMarker(marker);
        }    
        

