var dodano = false;
var globaltekst = null;
var poslano = false;

try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) { }
    
    function handleHttpResponse() {
        if (http.readyState == 4) {
            results = http.responseText;
            //alert(results);
            dodano = true;
        }
    }
    
    function dodaj(tekst,www,tocka) {
               
        if (tekst) {
			poslano = true;
            var encoded = escape(tekst);
			var encodedwww = escape(www);
			globaltekst = tekst;
            http.open("GET", "http://www.milosm.net/wp-content/themes/equix_v11/map/dodaj.php?tekst=" + encoded + "&www=" + encodedwww + "&tocka=" + tocka, true);
            http.onreadystatechange = handleHttpResponse;
            http.send(null);
        }

        map.closeInfoWindow();

    }
    
    function getHTTPObject() {
        var xmlhttp;
        /*@cc_on
        @if (@_jscript_version >= 5)
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                xmlhttp = false;
            }
        }
        @else
        xmlhttp = false;
        @end @*/
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            try {
                xmlhttp = new XMLHttpRequest();
                
                } catch (e) {
                
                xmlhttp = false;
            }
        }
        return xmlhttp;
    }
    
    var http = getHTTPObject();
    
    
    var map = null;
    
    function load() {
        
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map"));
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.setCenter(new GLatLng(46.21025,14.95239), 8);
            map.setMapType(G_HYBRID_MAP);
            
            var baseIcon = new GIcon();
            baseIcon.shadow = "http://www.milosm.net/wp-content/themes/equix_v11/map/senca.png";
            baseIcon.iconSize = new GSize(15, 26);
            baseIcon.shadowSize = new GSize(28, 26);
            baseIcon.iconAnchor = new GPoint(7, 25);
            baseIcon.infoWindowAnchor = new GPoint(9, 2);
            baseIcon.infoShadowAnchor = new GPoint(18, 25);
            
            function createMarker(point, tekst, www, x, y) {

                var iconA = new GIcon(baseIcon);
                iconA.image = "http://www.milosm.net/wp-content/themes/equix_v11/map/a.png";
                
                var marker2 = new GMarker(point,iconA);
                GEvent.addListener(marker2, "click", function() {
                    link = 'javascript:zoom(' + x + ',' + y + ');';
					if (www && www != "http://") {
						tekst = "<a href='" + www + "'>" + tekst + "</a>";
					}
                    marker2.openInfoWindowHtml("<span style='font-family: Arial; font-size: 12px;'>Tukaj je doma <b>" + tekst + "</b><br /><br /><div align='center'><a href='"+link+"'><img src='http://www.milosm.net/wp-content/themes/equix_v11/map/zoom.gif' alt='+' border='0'/></a> <a href='javascript:odzoom();'><img src='http://www.milosm.net/wp-content/themes/equix_v11/map/odzoom.gif' alt='-' border='0'/></a></div></span>");
				});
                return marker2;
            }
            
            GDownloadUrl("http://www.milosm.net/wp-content/themes/equix_v11/map/podatki.php", function(data, responseCode) {
				var wwwlink = null; 
                var xml = GXml.parse(data);
                var markers = xml.documentElement.getElementsByTagName("marker");
                for (var i = 0; i < markers.length; i++) {
                    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                    parseFloat(markers[i].getAttribute("lng")));
					if (markers[i].getAttribute("www")) {
						wwwlink = markers[i].getAttribute("www");
					} else {
						wwwlink = null;
					}
                    map.addOverlay(createMarker(point,markers[i].getAttribute("tekst"),wwwlink,parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng"))));
                }
            });
            
            var iconB = new GIcon(baseIcon);
            iconB.image = "http://www.milosm.net/wp-content/themes/equix_v11/map/b.png";
            var tocka = new GMarker(map.getCenter(),iconB);
            var postavljeno = false;

			GEvent.addListener(map, "infowindowclose", function() {
					if (!poslano) {
						postavljeno = false;
						map.removeOverlay(tocka);
					}
			});

			GEvent.addListener(map, "infowindowopen", function() {
					postavljeno = true;
			});
            
            GEvent.addListener(map, "click", function(marker, point) {
                
                if (!postavljeno && !marker) {
                    
                    map.addOverlay(tocka);
                    tocka.setPoint(point);
                    tocka.openInfoWindowHtml("<span style='font-family: Arial; font-size: 12px;'>Tvoje ime: <input type='text' id='ime' size='20'/><br />WWW: <input type='text' id='www' size='23' value='http://'/><br /><br /><input onclick=\"javascript:dodaj(document.getElementById('ime').value,document.getElementById('www').value,'" + point.y + "," + point.x + "');\" type='button' value='Dodaj na zemljevid'/></span>");
                    
                }

				if (postavljeno && !marker) {
					map.closeInfoWindow();
				}
                
                if (dodano) {
                    GEvent.addListener(tocka, "click", function() {
                        tocka.openInfoWindowHtml("<span style='font-family: Arial; font-size: 12px;'>Tukaj je doma <b>" + globaltekst + "</b></span>");
                    });
                }
                
            });
            
        }
    }
    
    function zoom(x,y) {
        map.setCenter(new GLatLng(x,y), 14);
    }
    
    function odzoom() {
        map.setCenter(new GLatLng(46.21025,14.95239), 8);
    }