$(function() {
  $("#map").css({
		height:320,
		width:527
	});
    var myLatLng = new google.maps.LatLng(-28.516969,23.631104);
    MYMAP.init('#map', myLatLng, 5);
    MYMAP.placeMarkers('cron/create_xml.php?type=all');


    $("#SouthernAfrica").click(function() {
        var pos = new google.maps.LatLng(-28.516969,23.631104);
        MYMAP.map.setCenter(pos);
        MYMAP.map.setZoom(5);
    })
    $("#Gauteng").click(function() {
        var pos = new google.maps.LatLng(-26.001253,28.118134);
        MYMAP.map.setCenter(pos);
        MYMAP.map.setZoom(9);
    })
    $("#NorthWest").click(function() {
        var pos = new google.maps.LatLng(-26.66386,25.283758);
        MYMAP.map.setCenter(pos);
        MYMAP.map.setZoom(7);
    })

    $("#Limpopo").click(function() {
        var pos = new google.maps.LatLng(-23.045546,29.873625);
        MYMAP.map.setCenter(pos);
        MYMAP.map.setZoom(7);
    })
    $("#Mpumalanga").click(function() {
        var pos = new google.maps.LatLng(-28.530554,30.895824);
        MYMAP.map.setCenter(pos);
        MYMAP.map.setZoom(7);
    })
    $("#FreeState").click(function() {
        var pos = new google.maps.LatLng(-28.45411,26.796785);
        MYMAP.map.setCenter(pos);
        MYMAP.map.setZoom(7);
    })
    $("#KwazuluNatal").click(function() {
        var pos = new google.maps.LatLng(-29.857003,31.024794);
        MYMAP.map.setCenter(pos);
        MYMAP.map.setZoom(7);
    })
    $("#WesternCape").click(function() {
        var pos = new google.maps.LatLng(-33.923776,18.423346);
        MYMAP.map.setCenter(pos);
        MYMAP.map.setZoom(9);
    })
    $("#EasternCape").click(function() {
        var pos = new google.maps.LatLng(-32.29684,26.419389);
        MYMAP.map.setCenter(pos);
        MYMAP.map.setZoom(7);
    })


});


var MYMAP = {
    map: null,
    bounds: null
}
MYMAP.init = function(selector, latLng, zoom) {
  var myOptions = {
    zoom:zoom,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  this.map = new google.maps.Map($(selector)[0], myOptions);
	this.bounds = new google.maps.LatLngBounds();
}

MYMAP.placeMarkers = function(filename) {
	$.get(filename, function(xml){
		$(xml).find("marker").each(function(){
			var name = $(this).find('name').text();
                        var mapicon = $(this).find('icon').text();
			var address = $(this).find('address').text();
			var areas = $(this).find('areas').text();
			var sub  = $(this).find('sub').text();
			var linkd = $(this).find('linkd').text();

			var lat = $(this).find('lat').text();
			var lng = $(this).find('lng').text();
			var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
			MYMAP.bounds.extend(point);
                        var marker = new google.maps.Marker({
				position: point,
				map: MYMAP.map,
                                icon: mapicon
			});
			var infoWindow = new google.maps.InfoWindow();
			var html='<strong>'+name+'</strong><br /><span style="font-size:12px;">Type: '+sub+'</span><br /><span style="font-size:12px;">Areas serviced: '+areas+'</span><br /><a href="'+linkd+'" title="'+name+'">View this page</a>';
			google.maps.event.addListener(marker, 'click', function() {
				infoWindow.setContent(html);
				infoWindow.open(MYMAP.map, marker);
			});
			//MYMAP.map.fitBounds(MYMAP.bounds);
		});
	});
}
