/*
 * This file contains all functions to setup
 * and manipulate the layer displaying the results
 * of the address search in the header of the freemaps
 * page. 
 */


/**
 * Initiates the creation of the markers layer
 * and displays a marker if coordinates are given
 * in the URL. 
 */
function init_markers(){
    var markersLayer = createMarkersLayer();

    var coords = getUrlParameter("marker").split(",");
    var addresses = getUrlParameter("address").split(",");
    //update references for getmap feature
    markersCoordinates = coords;
    markersAddresses = addresses;

    for(i = 0; i<coords.length/2;i++)
    {
        var lon = coords[i*2];
        var lat = coords[i*2+1];
        var address = i<addresses.length ? addresses[i] : null;   //if we run out of addresses before we run out of coordinates ...use a null address
        putMarker(markersLayer,lon,lat,address);
    }
}

/**
 * create markers layer
 */
function createMarkersLayer(){

    var markersLayer = map.getLayersByName("Locations")[0]; //get markers layer

    if (markersLayer == null) {
        //create markers layer       
        markersLayer = new OpenLayers.Layer.Vector('Locations');
        markersLayer.displayInLayerSwitcher = false;
    } else {
        //remove old markers
        markersLayer.destroyFeatures(markersLayer.features);
    }

    return markersLayer;
}

/**
 *  Zooms to given lat lon and displays a icon there.
 */
function getMap(lon, lat){
   $("#footerDiv").hide();
   $("#tabs").show().tabs('select', 0);
   
   //the markers layer is created
   var markersLayer = createMarkersLayer();
   address = hotLocality.get("current");
   var marker = putMarker(markersLayer,lon,lat,address);

   function clickLabel(lat, lon) {
   	                        var point = new OpenLayers.LonLat(lon, lat).transform(proj_4326, map.getProjectionObject());
   	                        return function() {
								map.panTo(point);
							}
                        };

   setMaplabel(address, clickLabel(lat, lon));
   $("#freeFormAddressField").val("");

   map.panTo(new OpenLayers.LonLat(lon, lat).transform(proj_4326, map.getProjectionObject()));
   //don't zoom if we are already zoomed in enough
   if(map.getZoom()<15)
   {
       map.zoomTo(15);
   }

   //update references for getmap feature
   markersCoordinates = [lon+","+lat];
   markersAddresses = [address];
}

/**
 * Puts a feature on the given layer.
 * 
 * @param markersLayer layer to which the feature is added
 * @param lon   longitude of the feature
 * @param lat   latitude of the feature
 * @param address   feature's address
 * @return the  added feature.
 */
function putMarker(markersLayer, lon, lat, address)
{
    if(!lon || !lat) return;
    
    var data = {};
    
    var style = {
        fillOpacity: 1,
        pointRadius: 25,
        graphicXOffset: -44,
        graphicYOffset: -46,
        externalGraphic: "./images/flag.png"
    };
    
    var features = new Array(1);  

    if(address){
        //add a tooltip if there is an address
        var popUpHtml = "<div align='center'><img src='./images/marker_hover.jpg'/><br>"
            +"<strong>" + address + "</strong>"
            +"</div>";

        data.address = popUpHtml;
    }
    
    var ft = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(lon,lat).transform(proj_4326, map.getProjectionObject())
                                            ,data,style); 
            
    features[0] = ft;
    ft.type = 'address';
    ft.hovering = true;

    markersLayer.addFeatures(features);
    return ft;
}
