//Map              
var map = null;
var mapLat = null;
var mapLng = null;

//Item
var imageItem = null;

//WCF
var gisService = null;

//Interest markers
var interestsBuffer = new Array();
var interestsImage= new Array();
var interestRadius = null;

////Geocoder
//var geocoder = null;
//var geoFinded = null;
                                    
//Process
function InitializeMapDetail() 
{ 
    if (GBrowserIsCompatible()) 
    { 
        //Init Map
        map = new GMap2($get("mapImg"));
        
        //Init Map Control
        InitMapControl();
        
        //Init Map Zoom Resolution
        var mapZoomMin = $get("HiddenZoomMin").value;
        var mapZoomMax = $get("HiddenZoomMax").value;
        InitMapZoom(mapZoomMin, mapZoomMax);
                
        //Center Map
        var mapInitialZoom = $get("HiddenInitialZoom").value;
        mapLat = $get("HiddenCenterLatitude").value;
        mapLng = $get("HiddenCenterLongitude").value;
        var reg = new RegExp("(,)", "g");
        mapLat = mapLat.replace(reg,".");
        mapLng = mapLng.replace(reg,".");
        CenterToCoord(mapLat,mapLng,parseInt(mapInitialZoom));

        //Init Gis Service
        gisService = new Portia.Frontend.ProxyLayer.GisProxy();
        document.cookie = "googleScriptLoaded=1; path=/";
       }
}

function InitMapControl()
{
    //Set Map Standard Control
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.disableDoubleClickZoom();
    map.enableScrollWheelZoom();
    map.enableContinuousZoom();
}

function InitMapZoom(p_zoomMin, p_zoomMax)
{
    //Zoom min
    G_NORMAL_MAP.getMinimumResolution = function () { return p_zoomMin };
    G_SATELLITE_MAP.getMinimumResolution = function () { return p_zoomMin };
    G_HYBRID_MAP.getMinimumResolution = function () { return p_zoomMin };

    //Zoom max
    G_NORMAL_MAP.getMaximumResolution = function () { return p_zoomMax };
    G_SATELLITE_MAP.getMaximumResolution = function () { return p_zoomMax };
    G_HYBRID_MAP.getMaximumResolution = function () { return p_zoomMax }; 
}


function MapToolsCheckClick(p_obj)
{
    //Draw interest points
    if(p_obj.checked && p_obj.attributes['typeid'] != undefined) doGetInterestByTypeFromCircle(p_obj.attributes['typeid'].nodeValue);
    //Clear interest points
    if(!p_obj.checked && p_obj.attributes['typeid'] != undefined) ClearMarkers(p_obj.attributes['typeid'].nodeValue);
}

function doGetInterestByTypeFromCircle(p_typeID)
{   
    //Get location JSON from WCF   
    var l_bounds = map.getBounds();
     
    gisService.GetInterestByTypeFromCircle(
      mapLng
     ,mapLat
     ,interestRadius
     ,p_typeID
    ,onGetInterestByTypeFromCircleSuccess
    ,onGetInterestByTypeFromCircleFailure, null);
}

function onGetInterestByTypeFromCircleSuccess(p_result)
{
    if(p_result != null)
    {
        var jsonResult = p_result;
        DrawMarkers(jsonResult);
    }
}

function onGetInterestByTypeFromCircleFailure(p_result)
{
    alert('Erreur WCF Get Interset' + p_result.get_message());
}
        
function DrawMarkers(p_jsonMarkers)
{    
    //Get type interest
    var typeID = null;
    if(p_jsonMarkers != null && p_jsonMarkers != 0)
    {
        typeID = p_jsonMarkers[0].InterestTypeId;
        
        //Browse buffer and draw markers
        for (var i in p_jsonMarkers) 
        {
            var tempBuffer = new Array();
            var center = new GLatLng(p_jsonMarkers[i].Latitude, p_jsonMarkers[i].Longitude);
            var marker = new GMarker(center,GetIcon(interestsImage[typeID]),true,{clickable:false, draggable:false})
            map.addOverlay(marker);
            tempBuffer.push(typeID,marker);
            interestsBuffer.push(tempBuffer);
        }
    }
}

function ClearMarkers(p_typeID)
{               
    //Browse collection and clear if need
    for(var i in interestsBuffer)
    {
        if(interestsBuffer[i][0] == p_typeID)
            map.removeOverlay(interestsBuffer[i][1]);
    }
}
            
function GetIcon(p_imageUrl)
{
    //Init icon size and image 
    var baseIcon = new GIcon();
    var iconUrl = p_imageUrl;
    baseIcon.iconSize = new GSize(30,30);
    baseIcon.iconAnchor = new GPoint(15,15);
    
    return new GIcon(baseIcon, iconUrl, null, null);
}

function InitMapConfig(p_jsonConfig)
{
    //Init map settings
    var jsonConfig = eval('('+ p_jsonConfig +')');
            
    //Init image location
    imageItem = jsonConfig.mapItemLayoutField.mapItemImageField.imageField;
    
    //Init radius
    interestRadius
     = jsonConfig.mapItemLayoutField.mapInterestsField.radiusField;
    
    //Init interset attributes
    for(var i in jsonConfig.mapItemLayoutField.mapInterestsField.mapInterestField)
    {
        //Images
        interestsImage[jsonConfig.mapItemLayoutField.mapInterestsField.mapInterestField[i].typeIDField] =
                                jsonConfig.mapItemLayoutField.mapInterestsField.mapInterestField[i].imageMapField;                                                           
    }
}

function CenterToCoord(p_lat, p_lng, p_zoom)
{
    //Center Map with x,y
    if((p_lat != '' && p_lng !='')&&(p_lat != null && p_lng !=null))
    {
        var center = new GLatLng(p_lat, p_lng);
        map.setCenter(center, p_zoom);
        map.addOverlay(new GMarker(center,GetIcon(imageItem),true,{clickable:false, draggable:false}));
    }
    else $get("zoneMap").style.display ='none';
}

