//<![CDATA[

	var iconBlue = new GIcon(); 
	iconBlue.image = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-blue.png';
	iconBlue.shadow = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-shadow.png';
	iconBlue.iconSize = new GSize( 26, 39 );
	iconBlue.shadowSize = new GSize( 52, 39 );
	iconBlue.iconAnchor = new GPoint( 13, 39 );
	iconBlue.infoWindowAnchor = new GPoint( 5, 1 );

	var iconRed = new GIcon(); 
	iconRed.image = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-red.png';
	iconRed.shadow = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-shadow.png';
	iconRed.iconSize = new GSize( 26, 39 );
	iconRed.shadowSize = new GSize( 52, 39 );
	iconRed.iconAnchor = new GPoint( 13, 39 );
	iconRed.infoWindowAnchor = new GPoint( 5, 1 );

	var iconGreen = new GIcon(); 
	iconGreen.image = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-green.png';
	iconGreen.shadow = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-shadow.png';
	iconGreen.iconSize = new GSize( 26, 39 );
	iconGreen.shadowSize = new GSize( 52, 39 );
	iconGreen.iconAnchor = new GPoint( 13, 39 );
	iconGreen.infoWindowAnchor = new GPoint( 5, 1 );

	var iconYellow = new GIcon(); 
	iconYellow.image = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-purple.png';
	iconYellow.shadow = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-shadow.png';
	iconYellow.iconSize = new GSize( 26, 39 );
	iconYellow.shadowSize = new GSize( 52, 39 );
	iconYellow.iconAnchor = new GPoint( 13, 39 );
	iconYellow.infoWindowAnchor = new GPoint( 5, 1 );

	var iconLF = new GIcon(); 
	iconLF.image = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-home.png';
	iconLF.shadow = '/wp-content/themes/sharkworks/images/icons/littlefish-icon-shadow.png';
	iconLF.iconSize = new GSize( 33, 40 );
	iconLF.shadowSize = new GSize( 66, 40 );
	iconLF.iconAnchor = new GPoint( 16, 39 );
	iconLF.infoWindowAnchor = new GPoint( 5, 1 );

	var customIcons			= [];
	customIcons["class1"]		= iconBlue;
	customIcons["class2"]		= iconRed;
	customIcons["class3"]		= iconGreen;
	customIcons["class4"]		= iconYellow;
	customIcons["our-office"]	= iconLF;

	var map = null;
	var gdir = null;
	var pageMarkers = new Array();
	var arrIndex = 0;

	load = function() {
		if( GBrowserIsCompatible() ) {
		
			map = new GMap2( document.getElementById( "map-wrapper" ) );
			gdir = new GDirections( map, document.getElementById( "directions" ) );
			GEvent.addListener( gdir, "error", handleErrors ); 

			map.addControl( new GSmallMapControl() );
			map.addControl( new GMapTypeControl() );
			map.addControl( new GScaleControl() );
			map.setCenter( new GLatLng( 52.089949, -1.764336 ), 7 );
			
			jQuery( ".checkboxes[name=our-office]" ).attr( 'checked', true );
			jQuery( ".checkboxes[name=filterClass1]" ).attr( 'checked', false );
			jQuery( ".checkboxes[name=filterClass2]" ).attr( 'checked', false );
			jQuery( ".checkboxes[name=filterClass3]" ).attr( 'checked', false );
			jQuery( ".checkboxes[name=filterClass4]" ).attr( 'checked', false );
			jQuery( "#filterType option:0" ).attr( 'selected', true );

			GDownloadUrl( "/location-markers?our-office=true", function( data ) {
				var xml = GXml.parse( data );
				var markers = xml.documentElement.getElementsByTagName( "marker" );
				for (var i = 0; i < markers.length; i++) {
					if( markers[i].getAttribute( "lat" ) != '' && markers[i].getAttribute( "lng" ) != '' ) {
						var point = new GLatLng(
							parseFloat( markers[i].getAttribute( "lat" ) ),
							parseFloat( markers[i].getAttribute( "lng" ) )
						);
					}
					else if( markers[i].getAttribute( "postcode" ) != '' ) {
					}
					
					var type = markers[i].getAttribute( "class" );
					if( markers[i].getAttribute( "class" ) == 'our-office' ) {
						var name = markers[i].getAttribute( "name" );
						var comment = markers[i].firstChild.nodeValue;
						var image = markers[i].getAttribute( "image" );
					}
					else {
						var name = markers[i].getAttribute( "name" );
						var comment = markers[i].getAttribute( "client_comment" );
						var image = false;
					}
					var marker = createMarker( point, name, comment, type, image );
					map.addOverlay( marker );
					pageMarkers[arrIndex++] = marker;
					document.getElementById('filterType').selectedIndex = 0;
				}
				$('#map-loader').css('display','none');
			});
		}
	}

	function createMarker( point, name, message, type, image ) {
		var marker = new GMarker( point, customIcons[type] );
		var html = "<table class='gbubble'><tbody>";
		if( type == 'our-office' )
		{
			html += "<tr class='name'><th>" + name + "<\/th><\/td>";
			html += "<tr class='image'><td><img src='/wp-content/themes/sharkworks/images/locations/"+name.toLowerCase()+".jpg' alt='' style='height: 149px; width: 205px;' height='149' width='205' /><\/td><\/td>";
		}
		html += message;
		html += "<\/tbody><\/table>";
		marker.bindInfoWindowHtml( html );
		/*GEvent.addListener( marker, 'click', function() {
			marker.openInfoWindow();
		});*/
		return marker;
	}

	function usePointFromPostcode( postcode, callbackFunction ) {
		var localSearch = new GlocalSearch();
		localSearch.setSearchCompleteCallback(null, function() {
			if( localSearch.results[0] ) {
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point);
			}else{
				alert("Postcode not found!");
			}
		});  
		return localSearch.execute( postcode + ", UK" );
	}

	jQuery(document).ready( function() {
		load();
		var refreshSelections = function() {
			$('#map-loader').css('display','block');
			vars =	"?our-office=" + jQuery( ".checkboxes[name=our-office]" ).attr( 'checked' ) +
				"&class1=" + jQuery( ".checkboxes[name=filterClass1]" ).attr( 'checked' ) +
				"&class2=" + jQuery( ".checkboxes[name=filterClass2]" ).attr( 'checked' ) +
				"&class3=" + jQuery( ".checkboxes[name=filterClass3]" ).attr( 'checked' ) +
				"&class4=" + jQuery( ".checkboxes[name=filterClass4]" ).attr( 'checked' ) +
				"&filterType=" + jQuery( "#filterType" ).val();
			GDownloadUrl( "/location-markers" + vars, function( data ) {
				map.clearOverlays();
				var xml = GXml.parse( data );
				var markers = xml.documentElement.getElementsByTagName( "marker" );
				for( var i = 0; i < markers.length; i++ ) {
					if( markers[i].getAttribute( "lat" ) != '' && markers[i].getAttribute( "lng" ) != '' ) {
						var point = new GLatLng(
							parseFloat( markers[i].getAttribute( "lat" ) ),
							parseFloat( markers[i].getAttribute( "lng" ) )
						);
					}
					else {
					}
					var type = markers[i].getAttribute( "class" );
					var name = '';
					if( markers[i].getAttribute( "class" ) == 'our-office' ) {
						var name = markers[i].getAttribute( "name" );
					}
					var comment = markers[i].firstChild.nodeValue;
					var marker = createMarker( point, name, comment, type );
					map.addOverlay( marker );
				}
				$('#map-loader').css('display','none');
			});
		};
		
		jQuery(".checkboxes").click( refreshSelections );
		jQuery( "#filterType" ).change( refreshSelections );
	});

	getNearest = function( address ) {
		$('#map-loader').css('display','block');
		geoCoder = new GClientGeocoder();
		geoCoder.getLatLng( address, function( data ) {
			shortestDistance = 9999999999999;
			shortestDistanceIndex = 0;
			for( i = 0; i < pageMarkers.length; i++ ) {
				distance = data.distanceFrom( pageMarkers[i].getLatLng() );
				if( distance <= shortestDistance ) {
					shortestDistance = distance;
					shortestDistanceIndex = i;
				}
				//console.log( shortestDistance + '-' + shortestDistanceIndex );
			}
			map.setCenter( pageMarkers[shortestDistanceIndex].getLatLng(), 11 );
			
			jQuery( ".checkboxes[name=our-office]" ).attr( 'checked', true );
			jQuery( ".checkboxes[name=filterClass1]" ).attr( 'checked', true );
			jQuery( ".checkboxes[name=filterClass2]" ).attr( 'checked', true );
			jQuery( ".checkboxes[name=filterClass3]" ).attr( 'checked', true );
			jQuery( ".checkboxes[name=filterClass4]" ).attr( 'checked', true );
			jQuery( ".checkboxes[name=filterClass4]" ).attr( 'checked', true );
			jQuery( "#filterType option:0" ).attr( 'selected', true );
			vars =	"?our-office=true" +
				"&class1=true" +
				"&class2=true" +
				"&class3=true" +
				"&class4=true" +
				"&filterType=0";
			GDownloadUrl( "/location-markers" + vars, function( xml_data ) {
				map.clearOverlays();
				var xml = GXml.parse( xml_data );
				var markers = xml.documentElement.getElementsByTagName( "marker" );
				for( var i = 0; i < markers.length; i++ ) {
					if( markers[i].getAttribute( "lat" ) != '' && markers[i].getAttribute( "lng" ) != '' ) {
						var point = new GLatLng(
							parseFloat( markers[i].getAttribute( "lat" ) ),
							parseFloat( markers[i].getAttribute( "lng" ) )
						);
					}
					else {
					}
					var type = markers[i].getAttribute( "class" );
					var name = '';
					if( markers[i].getAttribute( "class" ) == 'our-office' ) {
						var name = markers[i].getAttribute( "name" );
					}
					var comment = markers[i].firstChild.nodeValue;
					var marker = createMarker( point, name, comment, type );
					map.addOverlay( marker );
				}
				$('#map-loader').css('display','none');
			});
		});
		
	}

	getDirections = function( from, to ) {
		too = to.split("#");
		gdir.load( "from: " + from + " to: "+too[0]+" Office@" + too[1] );
	}

	handleErrors = function(){
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
		else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
			alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
		else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
			alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
		else if (gdir.getStatus().code == G_GEO_BAD_KEY)
			alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
		else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
			alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
		else alert("An unknown error occurred.");
	}

//]]>