
jQuery.fn.googleMapsStaticConvert = function(options) {
		
	//jQuery.fn.googleMaps.includeGoogle(opts.key, opts.sensor);
	return this.each(function(count) {	
		var mapWidth = jQuery(this).width();
		var mapHeight = jQuery(this).height();
		if(mapHeight<50)mapHeight=300;
		var newMapCont = jQuery('<div class="googleMapsContainer" id="GoogleMap_'+count+'">')
			.css('width',mapWidth)
			.css('height',mapHeight)
			.css('margin-top',jQuery(this).css('margin-top'))
			.css('margin-right',jQuery(this).css('margin-right'))
			.css('margin-bottom',jQuery(this).css('margin-bottom'))
			.css('margin-left',jQuery(this).css('margin-left'))
			.css('float',jQuery(this).css('float'));
																						
		//get markers
		var src =jQuery.URLDecode(jQuery(this).attr("src"));
		//console.log(src);
		
		var markers = new Array();
		
		markers = src.match(/markers\=\|([^&]+)/);
		
		newMapCont.googleMap({'markers':markers[1].split('|')});
		jQuery(this).replaceWith(newMapCont);
		
	});

};

jQuery.fn.googleMap = function(options) {
	
  // define defaults and override with options, if available
  // by extending the default settings, we don't modify the argument
  var defaults = {
    markers: '',
		lat: 54.800685,  //centre UK
    long: -5.163574, //centre UK
    zoom: 7, 
		maxZoom: 15,
    MapTypeId: "roadmap",
		markerOpts: {color:'blue'}
  };

  options = jQuery.extend(defaults, options || {});

	//initiate map to default position
	var center = new google.maps.LatLng(options.lat, options.long);
  var map = new google.maps.Map(this.get(0), jQuery.extend(options, { center: center }));
	map.setZoom(options.zoom);	
	// default max and min lat and long for centring and zooming
	var mapBounds = {
		minLat: options.lat,
		maxLat : options.lat,
		minLong : options.long,
		maxLong : options.long
	};
	
	var defaultMarker = {
		color: 'blue',
		size:'large',
		address:'',
		title:''
	};
	
	var markerOpts = new Array();
	if(jQuery(options.markers).size()>0){
		jQuery(options.markers).each(function(ky,markersrc){
			
			//console.log(markersrc);
			//set up/retrieve marker parameters
			var markerParms = jQuery.URLDecode(markersrc).replace('markers=','').split('|');
			
			//console.info(markerParms);
			
			jQuery(markerParms).each(function(mk,markerParam){
			
				if(!markerParam.match(/([^\:]+)\:([^\:]+)/)){
					address = markerParam;
					//markerOpts[ky].address = address;					
				}
			});

			//console.info(markerOpts);
			//console.log('>>>'+address);			
			//console.info('##: '+address);
			var geocoder = new google.maps.Geocoder();
			geocoder.geocode({ address: address }, function(results, status) {
				
				if (status == google.maps.GeocoderStatus.OK && results.length) {
					if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
						// update centre to match first point
						if(ky==0){
							mapBounds.maxLat=results[0].geometry.location.lat();
							mapBounds.minLat=results[0].geometry.location.lat();
							mapBounds.maxLong=results[0].geometry.location.lng();
							mapBounds.minLong=results[0].geometry.location.lng();
						}
						// update max and min lat and long based on this marker
						if(results[0].geometry.location.b>mapBounds.maxLat){ mapBounds.maxLat=results[0].geometry.location.lat(); }
						if(results[0].geometry.location.b<mapBounds.minLat){ mapBounds.minLat=results[0].geometry.location.lat(); }
						if(results[0].geometry.location.c>mapBounds.maxLong){ mapBounds.maxLong=results[0].geometry.location.lng(); }
						if(results[0].geometry.location.c<mapBounds.minLong){ mapBounds.minLong=results[0].geometry.location.lng(); }
						//console.info(mapBounds);

						//console.info(results[0].formatted_address);
						// place marker
						if(options.markers.length>1){//more than 1 marker
							
							var marker = new google.maps.Marker({
									position: results[0].geometry.location,
									map: map,
									title: 'Location: '+results[0].formatted_address,
									icon: "http://maps.google.com/mapfiles/marker" + String.fromCharCode(ky + 65) + ".png"
	
							});
						}else{  
							var marker = new google.maps.Marker({
									position: results[0].geometry.location,
									map: map,
									title: 'Location: '+results[0].formatted_address								
							});

						}
												
						//centre map to markers
						var centerLat = mapBounds.minLat + (mapBounds.maxLat - mapBounds.minLat) / 2  ;
						var centerLong = mapBounds.minLong + (mapBounds.maxLong - mapBounds.minLong) / 2  ;
						map.setCenter(new google.maps.LatLng(centerLat, centerLong));
						
						//zoom map to markers
						for(z=options.maxZoom;z>1;z=z-1){  // cycle zoom levels from max upwards

							map.setZoom(z);
							zoomBounds = map.getBounds();
							
							zoomBounds.MIN = zoomBounds.getSouthWest();
							zoomBounds.minLat = zoomBounds.MIN.lat();
							zoomBounds.minLong = zoomBounds.MIN.lng();							
	
							zoomBounds.MAX = zoomBounds.getNorthEast();
							zoomBounds.maxLat = zoomBounds.MAX.lat();
							zoomBounds.maxLong = zoomBounds.MAX.lng();
											
							//compare this zoom bounds to min/max map bounds
							if(
								 zoomBounds.minLat <= mapBounds.minLat
								 &&
								 zoomBounds.minLong <= mapBounds.minLong
								 &&
								 zoomBounds.maxLat >= mapBounds.minLat
								 &&
								 zoomBounds.maxLong >= mapBounds.minLong
							 ){
								options.maxZoom = z;								
								break;
							}

						}
						
					}
				}
			}); 

		});//end each markers
		
	}
	
}

jQuery.extend({
	URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
		if(m!=null && m.length>1 && m[1]!=''){
			o+=m[1];x+=m[1].length;
		}else{
			if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
			o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;
	},
	URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});

