var     map=null;
var     currentLocation=null;
var     DBLocationOverlay=null;
var     markers=Array();
google.load("maps", "2.x");
//==============================================================================
function initializeMap()
{
    var i;
    var bounds = new GLatLngBounds();
    if(locations!=null)
    {
        for(i=0;i<locations.length;i++)
            bounds.extend(new GLatLng(locations[i].lat,locations[i].lng));
    }
    else
        bounds.extend(new GLatLng(50.081158674053235, 14.426851272583008));

    map=new google.maps.Map2(document.getElementById("MapDiv"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.clearOverlays();
    map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
    CreateMarkers(map);
    if(showingImages)
        CreateImagesOnMap(map);
        
    InitializeLocationOverlay();
    var listener=GEvent.addListener(map, "click",
        function(marker, point)
        {
            var locationId;
            var tab=0;
            if(showingImages)
            {
                if(!marker || !marker['imageInfo'])
                    return;
                locationId=marker.imageInfo.location;
                tab=3;
            }
            else
            {
                if(!marker || !marker['location'])
                    return;
                locationId=marker['location'];
            }
            var srcNode=document.getElementById('LocationDetailDiv');
            if(srcNode==null)
                return;

            if(currentLocation)
                CloseLocationOverlay();

            currentLocation = marker;
            map.disableDragging();
            AjaxCall(locationDetailUrl+locationId,'LocationDetailDiv','DisplayLocationDetail('+locationId+','+tab+')');
        }
    );
    
    if(window.mapCallBack)
    {
        mapCallBack();
    }
}
google.setOnLoadCallback(initializeMap);
//==============================================================================
function CreateMarkers(map)
{
    markers=new Array();
    var iconRed = new GIcon();
    iconRed.image = "/gfx/bubble_purple2.png";
    iconRed.iconSize = new GSize(20, 35);
    iconRed.iconAnchor = new GPoint(12, 40);

    var iconGray = new GIcon();
    iconGray.image = "/gfx/bubble_gray2.png";
    iconGray.iconSize = new GSize(20, 35);
    iconGray.iconAnchor = new GPoint(12, 40);

    var iconBlue = new GIcon();
    iconBlue.image = "/gfx/bubble_blue2.png";
    iconBlue.iconSize = new GSize(20, 35);
    iconBlue.iconAnchor = new GPoint(12, 40);
    
    var marker;
    markers=Array();
    for(i=0;i<locations.length;i++)
    {
        switch(locations[i].zone)
        {
            case 4: { marker=new GMarker(new GLatLng(locations[i].lat,locations[i].lng),{ icon: iconRed} ); break ; }
            case 5: { marker=new GMarker(new GLatLng(locations[i].lat,locations[i].lng),{ icon: iconGray} ); break ; }
            case 6: { marker=new GMarker(new GLatLng(locations[i].lat,locations[i].lng),{ icon: iconBlue} ); break ; }
            default:{ marker=new GMarker(new GLatLng(locations[i].lat,locations[i].lng)); break ; }
        }
        marker['location']=locations[i].id;
        markers[locations[i].id]=marker;
        if(!showingImages)
            map.addOverlay(marker);
    }
}
//==============================================================================
function LoadAndDisplayLocationDetail(location)
{
    currentLocation=markers[location];
    if(currentLocation==null || currentLocation=='undefined')
        return;
    map.disableDragging();
    var url=locationDetailUrl+location;
    AjaxCall(url,'LocationDetailDiv','DisplayLocationDetail('+location+',0)');
}
//==============================================================================
function DisplayLocationDetail(location,tab)
{
    var i;
    var loc=null;
    for(i=0;i<locations.length;i++)
    {
        if(locations[i].id==location)
        {
            loc=new GLatLng(locations[i].lat,locations[i].lng);
            break;
        }
    }
    if(loc==null)
        return;
    var srcNode=document.getElementById('LocationDetailDiv');
    if(srcNode==null)
        return;
    srcNode=srcNode.cloneNode(true);
    srcNode.style.visibility='visible';
    srcNode.style.display='block';

    var pos=map.fromLatLngToContainerPixel(currentLocation.getPoint());
    pos.y-=250;
    point=map.fromContainerPixelToLatLng(pos);
    map.panTo(point);
    //currentLocation.hide();
    currentLocation.overlay = new DBLocationOverlay(currentLocation,srcNode);
    map.addOverlay(currentLocation.overlay);
    
    mapDetailLoaded=Array(true,false,false,false);
    if(tab<=0)
        mapActiveMenu=-1;
    else
        mapActiveMenu=0;
    SwitchMapMenu(tab);
}
//==============================================================================
function CloseLocationOverlay()
{
    if(currentLocation)
    {
        map.removeOverlay(currentLocation.overlay);
        currentLocation.show();
        map.enableDragging();
    }
}
//==============================================================================
function InitializeLocationOverlay()
{
    DBLocationOverlay=function(marker, contentNode)
    {
      this.marker = marker;
      this.contentNode = contentNode;
    }
    
    DBLocationOverlay.prototype = new GOverlay();
    DBLocationOverlay.prototype.initialize = function(map)
    {
        var div=this.contentNode;
        
        offsetX = 50;
        if(showingImages)
            offsetY=500;
        else
            offsetY=535;
        
        var a;
        div.style.top = (map.fromLatLngToDivPixel(this.marker.getPoint()).y - offsetY) + 'px';
        div.style.left = (map.fromLatLngToDivPixel(this.marker.getPoint()).x - offsetX) + 'px';
        this._map = map;
        this._div = div;
        
        map.getPane(G_MAP_FLOAT_PANE).appendChild(div);  
    }
  
    DBLocationOverlay.prototype.remove = function()
    {
      this._div.parentNode.removeChild(this._div);
    }
    
    DBLocationOverlay.prototype.redraw = function()
    {
    }
}
//==============================================================================
var mapDetailMenu=Array('MapPopupMenuInfo','MapPopupMenuCalendar','MapPopupMenuExhibitor','MapPopupMenuFoto');
var mapDetailDiv=Array('MapPopupInfoDiv','MapPopupCalendarDiv','MapPopupExhibitorDiv','MapPopupFotoDiv');
var mapTabNames=Array('info','calendar','exhibitors','fotovideo');
var mapDetailLoaded=Array(true,false,false,false);
var mapActiveMenu=0;
function SwitchMapMenu(activeIndex)
{
    var menuNode=document.getElementById(mapDetailMenu[activeIndex]);
    var infoNode=document.getElementById(mapDetailDiv[activeIndex]);
    if(menuNode==null || infoNode==null)
        return;
    
    var prevMenuNode=document.getElementById(mapDetailMenu[mapActiveMenu]);
    var prevInfoNode=document.getElementById(mapDetailDiv[mapActiveMenu]);
    if(prevMenuNode)
        prevMenuNode.className='';
    if(prevInfoNode)
    {
        prevInfoNode.style.visibility='hidden';
        prevInfoNode.style.display='none';
    }
    menuNode.className='Active';
    infoNode.style.visibility='visible';
    infoNode.style.display='block';
    mapActiveMenu=activeIndex;
    if(mapDetailLoaded[mapActiveMenu]==false)
    {
        var url=locationDetailUrl+currentLocation['location']+'&tab='+mapTabNames[mapActiveMenu]
        AjaxCall(url,mapDetailDiv[mapActiveMenu],'');
        mapDetailLoaded[mapActiveMenu]=true;
    }
}
//==============================================================================
function ViewPicturesOnMap(view)
{
    currentLocation=null;
    map.enableDragging();
    map.clearOverlays();
    showingImages=view;
    if(!view)
        CreateMarkers(map);
    else
        CreateImagesOnMap(map);
}
//==============================================================================
function CreateImagesOnMap(map)
{
    var icon;
    var marker;
    var m;

    for(i=0;i<images.length;i++)
    {
        image=images[i];
        m=markers[image.location];
        if(!m)
            continue;
        icon=new GIcon();
        icon.image = imageURLBase+image['t_link'];
        icon.iconSize = new GSize(image['t_width'],image['t_height']);
        icon.iconAnchor = new GPoint(image['t_width']/2,image['t_height']/2);
        marker=new GMarker(m.getLatLng(),{ icon: icon} );
        marker.imageInfo=image;
        marker.location=image.location;
        map.addOverlay(marker);
    }
}
//==============================================================================

