var bounds = new GLatLngBounds();

function init() {
        
    map = new GMap2(document.getElementById(divid));
    map.addControl(new GSmallZoomControl());
   // map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	
	_mPreferMetric=true;
	

  	retrieveMarkers();
}

window.onload = init;

function storeMarker(){
    var lng = document.getElementById("longitude").value;
    var lat = document.getElementById("latitude").value;

    var getVars =  "?found=" + document.getElementById("found").value
        + "&left=" + document.getElementById("left").value
        + "&icon=" + document.getElementById("icon").value
        + "&lng=" + lng
        + "&lat=" + lat ;

    var request = GXmlHttp.create();

    //open the request to storeMakres.php on your server
    request.open('GET', '/includes/storeMarker.php' + getVars, true);
    request.onreadystatechange = function() {
       if (request.readyState == 4) {
            //the request in complete

            var xmlDoc = request.responseXML;

            //retrieve the root document element (response)
            var responseNode = xmlDoc.documentElement;

            //retrieve the type attribute of the node
            var type = responseNode.getAttribute("type");

            //retrieve the content of the responseNode
            var content = responseNode.firstChild.nodeValue;

            //check to see if it was an error or success
            if(type!='success') {
                alert(content);
            } else {
                //Create a new marker and add it's info window.
                var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));

                var iconImage = responseNode.getAttribute("icon");
                var marker = createMarker(latlng, content, iconImage)

                map.addOverlay(marker);
                map.closeInfoWindow();
            }
       }
    }
    request.send(null);
    return false;
}

// function createMarker(latlng, html) {
// 	var marker = new GMarker(latlng);
// 	GEvent.addListener(marker, 'click', function() {
// 		var markerHTML = html;
// 		marker.openInfoWindowHtml(markerHTML);
// 	});
// 	return marker;
// }

function createMarker(latlng, html, iconImage, tooltiptext, pinurl, urltype) {
    if(iconImage) {
        var icon = new GIcon();
        icon.image = iconImage;
        icon.iconSize = new GSize(12, 20);
        icon.iconAnchor = new GPoint(14, 25);
        icon.infoWindowAnchor = new GPoint(14, 14);
        var marker = new GMarker(latlng,{icon:icon, title:tooltiptext});
    } else {
        var marker = new GMarker(latlng);
    }

    GEvent.addListener(marker, 'click', function() {       
      
     // var pinurl = "http://www.bbc.co.uk/";
     
    if( urltype == 'new' ) {
    	window.open(pinurl);
    } else {
    	document.location.href = pinurl;
    }
       
    });


//     GEvent.addListener(marker, 'click', function() {
//         var markerHTML = html;
//         marker.openInfoWindowHtml(markerHTML);
//     });

    return marker;
}

function retrieveMarkers() {
    var request = GXmlHttp.create();
  
    //tell the request where to retrieve data from.
    request.open('GET', '/includes/retrieveMarkers.php?theurl=' + theurl + '&propertyid=' + devid, true);

    //tell the request what to do when the state changes.
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
              var xmlDoc = request.responseXML;

              var markers = xmlDoc.documentElement.getElementsByTagName("marker");
              for (var i = 0; i < markers.length; i++) {
                   var lng = markers[i].getAttribute("lng");
                   var lat = markers[i].getAttribute("lat");
                   //check for lng and lat so MSIE does not error
                   //on parseFloat of a null value
                   
                   var point = new GLatLng(lat,lng);
                   
                   bounds.extend(point);
                   
                   // if( ! bounds.extend(point)){
//                       	alert('bounds error');
//                       } else {
//                       	alert('bounds OK');
//                       }
                     
                     
                    // alert(lng + " | " + lat );
                   if(lng && lat) {
                      var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));
                      
						var tooltiptext = markers[i].getAttribute("tooltip");
						var pinurl = markers[i].getAttribute("pinurl");
						var urltype = markers[i].getAttribute("urltype");
                                           
                     // alert(pinurl);
                     
                     var html = '<div><b>Found</b> '
                            + markers[i].getAttribute("found")
                            + '</div><div><b>Left</b> '
                            + markers[i].getAttribute("left")
                            + '</div>';

                      var iconImage =  markers[i].getAttribute("icon");
                      // alert(iconImage);
                      var marker = createMarker(latlng, html, iconImage, tooltiptext, pinurl, urltype);
                      map.addOverlay(marker);
                      
                   }
              } //for
                              
              if( markers.length > 1) {
              	map.setCenter(bounds.getCenter(),(map.getBoundsZoomLevel(bounds)));
              }

        } //if
    } //function

    request.send(null);
}




