﻿
    
function LoadMap(mapObject){ 
    mapObject.map = new GMap2(jQueryCS("#theMap" + mapObject.mapSettings.Id)[0], 
        {size: new GSize(mapObject.mapSettings.Width, mapObject.mapSettings.Height), navigationControl:false});
                    
    configureMapViewSettings(mapObject.map, mapObject.mapSettings);                    
    configureMapLocations(mapObject);
    return mapObject.map;
} 

function LoadSettingsMap(mapObject){ 
    mapObject.map = new GMap2(jQueryCS("#theMap")[0], {size: new GSize(800, 400), navigationControl:false});    

    mapObject.map.setCenter(new GLatLng(mapObject.mapSettings.Longitude, mapObject.mapSettings.Latitude), mapObject.mapSettings.Zoom);        
    mapObject.map.addControl(new GScaleControl (), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10)));
    mapObject.map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,30)));
    mapObject.map.addControl(new GMapTypeControl());      
    mapObject.map.enableGoogleBar(); 
    GEvent.addListener(mapObject.map, "click", getAddress);
    for(var i in mapObject.locations)
    {
        mapObject.map.addOverlay(createMarker(mapObject, i));
    }
                    
    return mapObject.map;
} 
    
function getAddress(overlay, latlng) {
  if (latlng != null) {
   jQueryCS("#lg").children().val(latlng.y);  
   jQueryCS("#lt").children().val(latlng.x);  
  }
}    

    
function configureMapViewSettings(map, mapSettings)
{
    if(!mapSettings.DisableMapType)
        map.addControl(new GMapTypeControl());       
    
    if(mapSettings.ShowScale)  
        map.addControl(new GScaleControl (), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10)));
   
    map.addControl(mapSettings.ZoomButton ? new GSmallMapControl():new GLargeMapControl());
    map[mapSettings.ShowSearch ? "enableGoogleBar" : "disableGoogleBar"]();     
    map[mapSettings.DisableDragging ? "disableDragging" : "enableDragging"](); 
    map[mapSettings.DisableClickZoom ? "disableDoubleClickZoom" : "enableDoubleClickZoom"](); 
    map.setMapType(mapSettings.MapType == 0 ? G_NORMAL_MAP: 
        mapSettings.MapType == 1? G_SATELLITE_MAP : G_HYBRID_MAP); 
    showFirstZoom(map, mapSettings);
}

    
function configureMapLocations(mapObject)
{
    for(var i in mapObject.locations)
    {
       mapObject.map.addOverlay(createMarker(mapObject, i));
    }
    if(mapObject.locations.length == 1)
        showInfo(mapObject, 0);
}

function createMarker(mapObject, number)
{
    var location = mapObject.locations[number];
    if(location.HasImageMarker)
    {
        var customIcon = new GIcon(G_DEFAULT_ICON);
        customIcon.image = mapObject.imageHandler + '?imgid=' + location.Id  + '&imgMarker=true'; 
        customIcon.iconSize =  new GSize(location.ImageMarkerWidth,location.ImageMarkerHeight);       
        markerOptions = { icon:customIcon };
        var marker = new GMarker(new GLatLng(location.Longitude,location.Latitude), markerOptions);
    }
    else
         var marker = new GMarker(new GLatLng(location.Longitude,location.Latitude));
    marker.value = number;
    GEvent.addListener(marker,"click", function()
    {
        showInfo(mapObject, number);
    });
    return marker;
}

function showInfo(mapObject, index)
{
    var location = mapObject.locations[index];
    var point = new GLatLng(location.Longitude, location.Latitude);
    var t = (location.HasImage ? '<img align="left" src="' + mapObject.imageHandler + '?imgid=' + location.Id  + '" style="padding-right:7px;padding-bottom:5px;"/>'  : '');
    t = t+  '<div class="infoHeader" align="left">'+location.LocationName+ '</div><div  class="info">';
    if(location.Address != '')
        t = t+ mapObject.locationDictionary.Address + ' : ' + location.Address +'<br/>';
    if(location.Zip != -1)
        t = t+ mapObject.locationDictionary.Zip + ' : ' + location.Zip +'<br/>';
    if(location.City != '')
        t = t+ mapObject.locationDictionary.City + ' : ' + location.City +'<br/>';
    if(location.Country != '')
        t = t+ mapObject.locationDictionary.Country + ' : ' + location.Country +'<br/>';
    if(location.PhoneNumber != '')
        t = t+ mapObject.locationDictionary.PhoneNumber + ' : ' + location.PhoneNumber;
    t = t + '</div>';        
    mapObject.map.openInfoWindowHtml(point, t);
}  
    
function showFirstZoom(map, settings)
{
    map.setCenter(new GLatLng(settings.Longitude, settings.Latitude), settings.Zoom);
}     

function centerLocation(map, location, settings)
{
    map.setCenter(new GLatLng(location.Longitude, location.Latitude), settings.Zoom);
}

function SetUniqueRadioButton(current)
{
    for(i = 0; i < document.forms[0].elements.length; i++)
    {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio')
        {
           elm.checked = false;
        }
    }
    current.checked = true;
}


function getScrollXY() { 
    var scrOfX = 0, scrOfY = 0; 
    if( typeof( window.pageYOffset ) == 'number' ) { 
      //Netscape compliant 
      scrOfY = window.pageYOffset; 
      scrOfX = window.pageXOffset; 
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { 
      //DOM compliant 
      scrOfY = document.body.scrollTop; 
      scrOfX = document.body.scrollLeft; 
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { 
      //IE6 standards compliant mode 
      scrOfY = document.documentElement.scrollTop; 
      scrOfX = document.documentElement.scrollLeft; 
    } 
    return {X:scrOfX, Y:scrOfY}; 
} 

function getWindowSize() { 
     var myWidth = 0, myHeight = 0; 
     if( typeof( window.innerWidth ) == 'number' ) { 
       //Non-IE 
       myWidth = window.innerWidth; 
       myHeight = window.innerHeight; 
     } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 
       //IE 6+ in 'standards compliant mode' 
       myWidth = document.documentElement.clientWidth; 
       myHeight = document.documentElement.clientHeight; 
     } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 
       //IE 4 compatible 
       myWidth = document.body.clientWidth; 
       myHeight = document.body.clientHeight; 
     } 
     return{X:myWidth, Y:myHeight} 
} 

function centerPopup(id){ 
    var windowDim = getWindowSize(); 
    var popupHeight = jQueryCS("#popupMap"+id).height(); 
    var popupWidth = jQueryCS("#popupMap"+id).width(); 
    var scroll = getScrollXY(); 
    
    jQueryCS("#popupMap"+id).css({ 
        "position": "absolute", 
        "top": windowDim.Y/2-popupHeight/2 + scroll.Y-15, 
        "left": windowDim.X/2-popupWidth/2 + scroll.X-15 
    });

    jQueryCS("#backgroundPopup").css({ 
    "height": windowDim.Y 
    });  
}