/* 
 * jQuery - Static Gmap Zoom - Plugin v1.0
 * www.phpgeek.dk www.twitter.com/phpgeek
 * Copyright 2010, Johan Holst Nielsen
 * Released under the MIT License.
 */

(function($) {
	$.fn.staticgmapzoom = function(coords, options) {
		var opts = $.extend({}, $.fn.staticgmapzoom.defaults, options);
		
		return this.each(function() {
			$this = $(this);
			$this.css({
				width: opts.mapWidth,
				height: opts.mapHeight,
				position: 'relative'
			});
			var maps = '';
			var gmapZoomedIn = false;
			var gmapXY = $this.offset();
			
			maps+=$.fn.staticgmapzoom.getStaticMap(coords,1,opts);
			maps+=$.fn.staticgmapzoom.getStaticMap(coords,2,opts);
			maps+=$.fn.staticgmapzoom.getStaticMap(coords,3,opts);
			$this.html(maps);
			
			var gmapLayer1 = $this.find('div').eq(0);
			var gmapLayer2 = $this.find('div').eq(1);
			
			$this.hover(function(){ gmapLayer1.stop(true,true).fadeOut('slow'); },
						function(){ gmapLayer1.stop(true,true).fadeIn('slow'); });
			
			$this.bind('mousemove', function(event) {
				var mX = (event.pageX-gmapXY.left);
				var mY = (event.pageY-gmapXY.top);
				if(Math.abs((opts.mapWidth/2)-mX) <= (opts.mapWidth*opts.zoomInDistance) && Math.abs((opts.mapHeight/2)-mY) <= (opts.mapHeight*opts.zoomInDistance)) {
					if(!gmapZoomedIn) { gmapLayer2.stop(true,true).fadeOut('slow'); gmapZoomedIn = true; }
				}
				else {
					if(gmapZoomedIn) {  gmapLayer2.stop(true,true).fadeIn('slow'); gmapZoomedIn = false; }
				}
			});
			
		});
	};
	$.fn.staticgmapzoom.getStaticMap = function(coords,level,opts) {
		var o = '<div style="position:absolute;left:0;top:0;z-index:'+(11-level)+';" class="gmaplayer'+level+'">';
		o = o+'<img src="http://staticmap.openstreetmap.de/staticmap.php?size='+opts.mapWidth+'x'+opts.mapHeight;
		o = o+'&zoom='+eval('opts.mapZoom'+level)+'&center='+coords+'&markers='+coords+',pink-pushpin"></div>';
		return o;
	};	
	$.fn.staticgmapzoom.defaults = {
		mapType: 'roadmap',
		mapWidth : 840,
		mapHeight : 350,
		mapZoom1 : 8,
		mapZoom2 : 10,
		mapZoom3 : 16,
		zoomInDistance : .25
	};
})(jQuery);

