 /*!
 * jQuery UI Google Map 3.0-alpha
 * http://code.google.com/p/jquery-ui-map/
 * Copyright (c) 2010 - 2011 Johan S�ll Larsson
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 *
 * Depends:
 *      jquery.ui.map.js
 */
( function($) {

	$.extend($.ui.gmap.prototype, {
		 
		/**
		 * Gets the current position
		 * @param callback:function(position, status)
		 * @param geoPositionOptions:object, see https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIDOMGeoPositionOptions
		 */
		getCurrentPosition: function(a, b) {
			var c = this;
			if ( navigator.geolocation ) {
				navigator.geolocation.getCurrentPosition ( 
					function(position) {
						c._call(a, position, "OK");
					}, 
					function(error) {
						c._call(a, null, error);
					}, 
					b 
				);	
			} else {
				c._call(a, null, "NOT_SUPPORTED");
			}
		},
		
		/**
		 * Watches current position
		 * To clear watch, call navigator.geolocation.clearWatch(this.get('watch'));
		 * @param callback:function(position, status)
		 * @param geoPositionOptions:object, see https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIDOMGeoPositionOptions
		 */
		watchPosition: function(a, b) {
			var c = this;
			if ( navigator.geolocation ) {
				this.set('watch', navigator.geolocation.watchPosition ( 
					function(position) {
						c._call(a, position, "OK");
					}, 
					function(error) {
						c._call(a, null, error);
					}, 
					b 
				));	
			} else {
				c._call(a, null, "NOT_SUPPORTED");
			}
		},
		
		getShopMarkers: function(type, currentPosition) {
			
			var c = this;
			
			if (!c.request) {
				var map = c.get('map');
				
				c.request = jQuery.ajax('/interface/request.php', {
					accepts: 'json',
					cache: false,
					complete: function(result) {
						if (result.readyState == 4 && result.status == 200) {
							c.showShopMarkers(jQuery.parseJSON(result.responseText));
						}
					},
					context: c, 
					data: {type: type, pos: (currentPosition) ? currentPosition.toUrlValue() : null},
					type: 'post'
				});
			}
		},
		
		showShopMarkers: function(result) {

			var canvas = $('#map_canvas'); 
			var map = canvas.gmap('get', 'map');

			if (result && result.error) {
				alert('Error' + result.error);
			} else {

				var bounds = new google.maps.LatLngBounds();

				if (result.pos) {
					var pos = result.pos.split(',');
					bounds.extend(new google.maps.LatLng(pos[0], pos[1]));
				}
				
				if (result.pointers) {
					for (var i in result.pointers) {
						var latlng = new google.maps.LatLng(result.pointers[i].lat, result.pointers[i].lng);
						if (i < 2) {
							bounds.extend(latlng);
						}
						
						var marker = {
							bounds: false,
							position: latlng,
							title: htmldecode(result.pointers[i].text.replace(/(<br)(\s*)(\/{0,1})(>)/img, ", ")),
							icon: 'http://'+document.location.hostname+'/image/pin_'+result.pointers[i].markerType+'.png',
							text: result.pointers[i].text
						};
						
						canvas.gmap('addMarker', marker).click(function() {
			                $('#map_canvas').gmap('openInfoWindow', { 'content': this.text }, this);
				        });     
					} 
				}
				map.fitBounds(bounds);
			}
		}		
				
	
		
		
		
	});
	
} (jQuery) );
