/*
Google map API here
*/

var map;

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 stristr( haystack, needle, bool ) {  
     var pos = 0;  
   
     haystack += '';  
     pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );  
     if( pos == -1 ){  
         return false;  
     } else{  
         if( bool ){  
             return haystack.substr( 0, pos );  
         } else{  
             return haystack.slice( pos );  
         }  
     }  
}

function createMap(targetId) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(targetId));
		map.addControl(new GSmallZoomControl());
		map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(25,10)));
		map.enableContinuousZoom();
		
		var franceCoords = new GLatLng(47, 3);
        var ukCoords = new GLatLng(51.5338, -0.2026);
        if(stristr(window.location.href, 'chateau'))
        {
		  map.setCenter(franceCoords, 5, G_HYBRID_MAP);
		}
		else if(stristr(window.location.href, 'castle'))
		{
		  map.setCenter(ukCoords, 5, G_HYBRID_MAP);
		}
	}
}

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


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

function addPostcodeMarker(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);
				
				if(index==markers.length-1)
					autoCentre();
				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:balloonHTML }
	  	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);
	GEvent.addListener(marker, "click", function() {
      map.openInfoWindowHtml(point, balloonHTML);
    });
}


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

// Mootools
window.addEvent('domready', function() {
	if( document.getElementById('map') ) {
		createMap('map');
		//setTimeout('autoCentre();', 5000);
		for( var i = 0; i < markers.length; i++ ) {
			addPostcodeMarker( markers[i][0], markers[i][1], markers[i][2] );
		}
	}
});
