/*
Google map API here
*/
var map=null;

var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);

function explode( delimiter, string, limit ) {
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

function createMap(targetId) {
	if (GBrowserIsCompatible()) {

if(map!=null) {
	map = null;
}


		var mapTypes = G_DEFAULT_MAP_TYPES;
		for(var i = 0; i < mapTypes.length; i++){
			mapTypes[i].getMaximumResolution = function(latlng){ return 12;};
			mapTypes[i].getMinimumResolution = function(latlng){ return 1;};
		}


		map = new GMap2(document.getElementById(targetId), mapTypes);
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
		map.addControl(new GScaleControl());
		map.enableContinuousZoom();
		map.setCenter(new GLatLng(47, 3), 5, G_HYBRID_MAP);
	}
}

var pointLatLngStack = [];
var bounds = new GLatLngBounds();
var markers = new Array();

function flushMarkers() {
   markers = [];
}

function addMarker(town, postcode, balloonHTML, index) {
	markers[markers.length] = new Array(town, postcode, balloonHTML, index );
}

function addPostcodeMarker(town, postcode, balloonHTML, index) {
	var localSearch = new GlocalSearch();
	localSearch.setSearchCompleteCallback( null, 
		function( balloonHTML, index ) {
			if (localSearch.results[0])
			{
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				var letter = -1;
				if( index >= 0 ) {
					letter = String.fromCharCode( parseInt("A".charCodeAt(0)) + parseInt(index) );
				}
				placeMarkerAtPoint(point, balloonHTML, map, letter);
				//map.panTo(point);
				//map.setZoom(15);
				
				pointLatLngStack.push({
					lat : resultLat,
					lng : resultLng
				});
			}
		}, [ balloonHTML, index ] );
	var location = explode('-', postcode, 2);

	if(location[0] == 'be'){
		localSearch.execute( location[1] + ", Belgium");
	} else if(location[0] == 'fr') {
		localSearch.execute( location[1] + ", France, France");
	} else if(location[0] == 'uk') {
	   localSearch.execute( location[1] + ", UK");
	} else if(location[0] == 'ir') {
	   localSearch.execute( location[1] + ", Ireland");
	}
}


function placeMarkerAtPoint(point, balloonHTML, map, letter) {
	var marker = null;
	if( letter == -1 ) {
		markerOptions = { icon:icon, title:'' }
	  	marker = new GMarker(point, markerOptions);
  	} else {
		var letteredIcon = new GIcon( icon );
		letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";		
	  	marker = new GMarker(point,letteredIcon);
  	}
	bounds.extend(point);
	map.addOverlay(marker);
	autoCentre();
	GEvent.addListener(marker, "click", function() {
      map.openInfoWindowHtml(point, balloonHTML);
    });
}


function autoCentre() {
	var zoom = map.getBoundsZoomLevel(bounds);
	if( zoom > 10 )
		map.setZoom(10);
	else
		map.setZoom(map.getBoundsZoomLevel(bounds));
	//map.setCenter(bounds.getCenter());
	map.panTo(bounds.getCenter());
}

if(document.getElementById && document.createTextNode) {
	window.onload=function(){
		create();
	}
}

function create() {
	if( document.getElementById('map') ) {
	try {
		createMap('map');
		setTimeout('autoCentre();', 2000);
	} catch(e) {}
		for( var i = 0; i < markers.length; i++ ) {
			addPostcodeMarker( markers[i][0], markers[i][1], markers[i][2], markers[i][3] );
		}
	}
}
