;(function($) {

	$.widget('deal.dealoffer', {
	
		options:{
			url_places: '/test/dealoffer_map.json',
			center_default: { lat:50, lng:17 },
			map_options: {},
            zoom_default: 14,
			zoom_max: 18,
			id_offer: 0,
			markers: [],
			addresses:[]
		},

		_init:function(){
			return this._bind();
		},
		
		_bind:function(){
			var self = this;
			var map = $('.deal-js-offer_map', this.element);
			this.options.el_addresses = $('.deal-js-addresses', this.element);
			if (map.length){
				$.ajax({
					url: this.options.url_places,
					data: { id:this.options.id_offer },
					type:'GET',
					dataType:'json',
					success: function(data){ 
						self.options.addresses = data.addresses;
						self._init_map(data.places, data.select, data.city); 
					},
					error: function(){ _log('error'); }
				});
			}
			deal_external_url(this.element);
			return this;
		},
		
		_init_map:function(places, select, city){
			if (places.length==0) {
				$('.deal-js-offer_map-city_select, .deal-js-offer_map', this.element).hide();
				return this;
			}
			var self = this;
			$('.deal-js-offer_map-city_select', this.element).html(select);
			$('.deal-js-offer_map-city_select select', this.element).unbind('change').bind('change', function(){ self._city_fit_bounds($(this).val()); return true; });
			//_log(places);
			cms_googlemaps.execute(function(){
				var latlng = new google.maps.LatLng(self.options.center_default.lat, self.options.center_default.lng);
				var map_options = $.extend({ zoom:self.options.zoom_default, center:latlng, mapTypeId:google.maps.MapTypeId.ROADMAP }, self.options.map_options);
				self.options['googlemap'] = new google.maps.Map($('.deal-js-offer_map',self.element).get(0), map_options);
				var bounds = new google.maps.LatLngBounds();
				$(places).each(function(i, item){
						if (!(item.lat!=null && item.lng!=null)) return;
						//item.icon = parseInt(item.color,10);
						//if (isNaN(item.icon)) item.color = 0;
						//if (item.color>=self.options.url_marker_icons.length) item.color=self.options.url_marker_icons.length-1;
						var marker = new google.maps.Marker({
							position: new google.maps.LatLng(item.lat, item.lng),
							//icon: self.options.url_marker_icons[item.color]
							map: self.options['googlemap']
						});
						marker.city = item.city;
						self.options.markers.push(marker)
						bounds.extend(marker.getPosition());
					}
				);
				self._city_fit_bounds(city); 
			});
			return this;			
		},
		
		_city_fit_bounds:function(city){
      var self = this;                       
			if (!this.options['googlemap']) return this;
			var bounds = new google.maps.LatLngBounds();
			$(this.options.markers).each(function(i, item){
				if (!city || item.city==city) {
					bounds.extend(item.getPosition());
				}
			});
			//_log(city);
			this.fitbounds(bounds, this.options.zoom_max);
			if (this.options.el_addresses) $(this.options.el_addresses).html(this.options.addresses[city]);
			return this;
		},
		
		fitbounds:function(bounds, zoommax){
			var self = this;
			if (this.options['googlemap']){
				google.maps.event.addListener(this.options['googlemap'], 'zoom_changed', function() {
							var zoomChangeBoundsListener = google.maps.event.addListener(self.options['googlemap'], 'bounds_changed', function(event) {
								google.maps.event.removeListener(zoomChangeBoundsListener);
								if (this.getZoom() > zoommax) {
                                  this.setZoom(self.options.zoom_default);
                                   self.options.zoom_default = zoommax;
                                }
							});
				});
				this.options['googlemap'].fitBounds(bounds);
			}
			return this;
		}			
		

	});

})(jQuery);
	

