SB.Maps = {};

var gmarkers = [];
var gmlabels = [];


SB.Maps.load = function () {
    if (GBrowserIsCompatible()) {
  		var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(-12.226005232925365,130.5450439453125),8);
        var mt = map.getMapTypes();
      // Overwrite the getMinimumResolution() and getMaximumResolution() methods
      for (var i=0; i<mt.length; i++) {
        mt[i].getMinimumResolution = function() {return 8;}
        mt[i].getMaximumResolution = function() {return 8;}
      }

        map.setMapType(G_SATELLITE_MAP);
        var bounds = new GLatLngBounds( );

        var greenIcon = new GIcon(G_DEFAULT_ICON);
        greenIcon.image = "/images/interface/icons/green.png";
        greenIcon.iconSize = new GSize(32,32);
        greenIcon.iconAnchor = new GPoint(3,32);

        var redIcon = new GIcon(G_DEFAULT_ICON);
        redIcon.image = "/images/interface/icons/red.png";
        redIcon.iconSize = new GSize(32,32);
        redIcon.iconAnchor = new GPoint(4,32);
        
        var fishIcon = new GIcon(G_DEFAULT_ICON);
        fishIcon.image = "/images/interface/icons/fish.png";
        fishIcon.iconSize = new GSize(27,44);
        fishIcon.iconAnchor = new GPoint(4,38);
        
        var placeInterestIcon = new GIcon(G_DEFAULT_ICON);
        placeInterestIcon.image = "/images/interface/icons/place-interest.png";
        placeInterestIcon.iconSize = new GSize(32,32);
        placeInterestIcon.iconAnchor = new GPoint(4,38);
        
        var noIcon = new GIcon(G_DEFAULT_ICON);
        noIcon.image = "/images/interface/blank.png";
        noIcon.iconSize = new GSize(0,0);
        noIcon.iconAnchor = new GPoint(0,0);

        $.get("/common/map/", {}, function(xml){
            $('place', xml).each(function(i){
            	if ($(this).find("marker_type").text() != "label") {
                	if ($(this).find("marker_type").text() == "home") {
                	    markerOptions = { icon:redIcon };
                	} 
                	else if ($(this).find("marker_type").text() == "fish-spot") {
                	    markerOptions = { icon:fishIcon };
                	}
                	else if ($(this).find("marker_type").text() == "place-interest") {
                	    markerOptions = { icon:placeInterestIcon };
                	}
                	else {
                	    markerOptions = { icon:greenIcon };
                	}
                		var newLat = parseFloat($(this).find("latitude").text());
                		var newLong = parseFloat($(this).find("longitude").text());
                		var new_point = new GLatLng(newLat,newLong);
                		var theHTMLa = $(this).find("place_name").text();
                		var theHTMLb = $(this).find("place_loc").text();
                		var theHTMLall = theHTMLa + "<br>" + theHTMLb;
                		var marker = SB.Maps.createMarker(new_point,markerOptions,theHTMLall);
                		map.addOverlay(marker);
                		bounds.extend(new_point);
                }
                else if ($(this).find("marker_type").text() == "label") {
                		var newLabLat = parseFloat($(this).find("latitude").text());
                		var newLabLong = parseFloat($(this).find("longitude").text());
                		var labelpoint = new GLatLng(newLabLat,newLabLong);
                		var labeltxt = $(this).find("label_txt").text();
                		var labelcss = $(this).find("label_css").text();
                		var label = SB.Maps.createLabels(labelpoint,labeltxt,labelcss);
                		map.addOverlay(label);
                }
                });
            });
        }
    };

SB.Maps.createMarker = function (point,markerOptions,code) {
    var marker = new GMarker(point,markerOptions);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(code);
    });
    gmarkers.push(marker);
    return marker;
};

SB.Maps.createLabels = function (labelpoint,labeltxt,labelcss) {
    var label = new ELabel(labelpoint,labeltxt,labelcss);
    gmlabels.push(label);
    return label;
};
