var MiniMap = function()
{
//privat
    var userId;
    var refresh = null;
    var map;
    
    var UpdateRealtime = function(response, status)
    {
        map.SetUsers(response);
        map.UpdateRealtimeDynamicObjects();
        map.ShowDynamicObjectAtCenter(userId);
        map.ShowInfo(userId);
    }
    

//public
return {
    isStarted: function(){
        return  refresh != null;
    },
    Initialize: function(response, status)
    {
        
        if (response.status == 'FAILD') {
            return;
        }
    
        map = "MiniMapContainer";
        userId = response.users[0].id;
        if (response.status == "OK")
        {
            WhatMapCreated = 'user';
            map = new Maps(map, 
               {
                   users: response.users,
                   trackingType: "realtime"
               });
            map.ShowDynamicObjectAtCenter(userId);
            map.ShowInfo(userId);
            MiniMap.SetRefresh();
        }
        else
        {
            MiniMap.ClearRefresh();
            jQuery("#" + map).html("");
            WhatMapCreated = null;
        }
    },
    
    GetUserId: function()
    {
        return userId;
    },
    
    SetRefresh: function()
    {
        refresh = window.setInterval(MiniMap.RefreshRealtime, 20000);
    },
    
    ClearRefresh: function()
    {
        if (refresh != null)
            window.clearInterval(refresh)
        refresh = null;
    },

    RefreshRealtime: function()
    {
        jQuery.ajax(
            {
                type: "POST",
                url: "index.php",
                data: "AJAX=1&do=map&type=user&id=" + userId,
                dataType: "json",
                success: UpdateRealtime
            });
    }
};
}();

function OnLoadStaticObjects(map, _lat, _lng)
{
    var marker = _lat || _lng;

    _lat = _lat || 48.129434;
    _lng = _lng || 10.019531;

    var point = new GLatLng(_lat, _lng);

    if (GBrowserIsCompatible()) {
        var map = new GMap2(map);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setMapType(G_HYBRID_MAP);
        map.setCenter(new GLatLng(_lat, _lng), 12);
        if (marker){
           map.addOverlay(createMarker(point, "", "", ""));
        }
      }
    
    SetClickEvent(map);

}

function SetClickEvent(map){
        if (GBrowserIsCompatible()) { 
            var _region = document.getElementById('_region');
            var clickListener = GEvent.addListener(map, "click", function(overlay, point) { 
                if (point){     // background clicked
                    map.clearOverlays();
                    _region.value = point;
                    map.addOverlay(createMarker(point, "", "", ""));
                } else if (overlay) {   // marker clicked
                    map.removeOverlay(overlay);
                    _region.value = '';
                } 
            }); 
        } else { 
            alert("Sorry, the Google Maps API is not compatible with this browser"); 
        } 
    }
    
function createMarker(point,name,type,html)
{ 
    var marker = new GMarker(point); 
    marker.name = name;
    marker.type = type;
    marker.html = html;
    return marker; 
}

function OnLoadEdit(point, map)
{
    var k;
    if (GBrowserIsCompatible()) {
        var map = new GMap2(map);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setMapType(G_HYBRID_MAP);
        map.setCenter(new GLatLng(48.129434, 10.019531), 12);
      }
    SetClickEvent(map);
    for(i=0; i<point.length; i++)
    {
        if(point.charAt(i)==",")
        k=i;
    }
        
    GEvent.trigger(map,  "click", 0, new GLatLng(eval(point.substr(0,k)), eval(point.substr(k+1,point.length))));
}

function SetListeners (map) {
        var mouseoverListener = GEvent.addListener(map, "click", function(overlay, point) { 
                if (point){     // background clicked
                    
                } else if (overlay.parentGeoXml) {   // marker clicked
                    coord = overlay.getPoint();
                    coord = coord.toString();
            lol=function(o){
                if(o[0]['type']=='foto'){
                html = "<div><b>Photo Album: "+o[0]['name_albom']+"</b><br>by "+o[0]['fullName']+"<br><span onclick=\"filter_fotos('"+o[0]['id']+"---', 0, 'alboms');document.getElementById('foto_filter_wnd_c').style.visibility='visible';\" style='cursor: pointer; color: blue;font-weight: bold;'>Show album</span></div>";
                }
                else {
                html = "<div><b>Video Album: "+o[0]['name']+"</b><br>by "+o[0]['fullName']+"<br><span onclick=\"filter_vd('"+o[0]['id']+",');document.getElementById('foto_filter_wnd_c').style.visibility='hidden';\" style='cursor: pointer; color: blue;font-weight: bold;'>Show album</span></div>";
                }
                overlay.openInfoWindow(html);
                } 
                //html=$.getJSON("","point="+coord,lol);    
                jQuery.ajax(
                {
                    type: "GET",
                    url: "index.php",
                    data: "point="+coord,
                    dataType: "json",
                    success: lol
                                });
                
                }
            });

}
function OnLoadAll (map) {
            
            var srv = location.hostname;
    
             mapobj = new GMap2(map);
             mapobj.addControl(new GSmallMapControl());
             mapobj.addControl(new GMapTypeControl());
             mapobj.setMapType(G_HYBRID_MAP);
             mapobj.setCenter(new GLatLng(48.129434, 10.019531), 4);
             time = Math.random();
             layer = new GGeoXml("http://"+srv+"/?type=getStatObj&rand="+time);

             GEvent.addListener(layer, "load", function()
               {    
                    this.gotoDefaultViewport(mapobj);
                    
               });
              mapobj.addOverlay(layer);
             SetListeners(mapobj);  
}