//
// GMap2 extension for flagatrip :)
// Author: Alexey V. Ivanov <mail@aivanov.com>
//

if (!window.GMap2) { alert('GMap2 not found') }

window.flagatripBaseHost = 'http://flagatrip.ru/';
window.flagatripIcon = new GIcon();
window.flagatripRedIcon = new GIcon();

flagatripIcon.image    = flagatripBaseHost + "styles/images/map-flag.png";
flagatripRedIcon.image = flagatripBaseHost + "styles/images/map-flag-red.png";
flagatripRedIcon.shadow     = flagatripIcon.shadow = flagatripBaseHost + "styles/images/map-flag-shadow.png";
flagatripRedIcon.iconSize   = flagatripIcon.iconSize = new GSize(26, 30);
flagatripRedIcon.shadowSize = flagatripIcon.shadowSize = new GSize(38, 28);
flagatripRedIcon.iconAnchor = flagatripIcon.iconAnchor = new GPoint(5, 27);
flagatripRedIcon.infoWindowAnchor = flagatripIcon.infoWindowAnchor = new GPoint(10, 4);

GMap2.prototype.fCenter = new GLatLng(35.74, 14.06);
GMap2.prototype.trip = [];

GMap2.prototype.flagTo = function (location, update) {
	update |= (update == null);
	location.point = new GLatLng(location.lat, location.lng);
	this.trip[this.trip.length] = location;
	if (update) {
		this.redrawTrip();
	}
}

GMap2.prototype.addFlag = function (options) {
	var marker = new GMarker(options.point, { icon: options.icon || flagatripIcon, title: options.title });
	this.addOverlay(marker);
	marker.options = options;
	return marker;
}

GMap2.prototype.redrawTrip = function() {
	// Removing old polyline
	if (this.tripPoly) {
		map.removeOverlay(this.tripPoly);
	}
	// Removing all markers
	if (this.flags) {
		for (var i = 0; i < this.flags.length; ++i) {
			this.removeOverlay(this.flags[i]);
		}
	}
	// Adding new markers
	this.flags = [];
	var poly = [];
	for (var i = 0; i < this.trip.length; ++i) {
		this.flags[this.flags.length] = this.addFlag(this.trip[i]);
		poly[poly.length] = this.trip[i].point;
	}
	// Adding new polyline
	this.tripPoly = new GPolyline(poly, "#619902", 1, 1);
	this.addOverlay(this.tripPoly);
}

// route = [
//   {lat: ... , lng: ... , title: ...},
//   {lat: ... , lng: ... , title: ...},
//   ...
// ]
GMap2.prototype.drawTrip = function (route) {
	if (route.length) {
		route[0].icon = flagatripRedIcon;
		for (var i = 0; i < route.length; ++i) {
			this.flagTo(route[i], false);
		}
		this.redrawTrip();
	}
}

GMap2.prototype.zoomTo = function(lat1, lng1, lat2, lng2){
	var b = new GLatLngBounds(new GLatLng(lat1, lng1), new GLatLng(lat2, lng2));
	this.fZoom   = Math.min(10, Math.max(1, this.getBoundsZoomLevel(b) - 1));
	this.setCenter(b.getCenter(), this.fZoom);
}

GMap2.prototype.showCoutriesOverlay = function (u) {
	var tlo = new GTileLayerOverlay(
		new GTileLayer(null, 1, 3, {
			tileUrlTemplate: flagatripBaseHost + 'gmap_tile.php?x={X}&y={Y}&z={Z}&u=' + u, 
			isPng: true
		})
	);
	this.addOverlay(tlo); 
}

GMap2.prototype.highlightCoutries = function (c) {
	if (this.tlo) {
		this.removeOverlay(this.tlo)
	}
	if (c) {
		this.tlo = new GTileLayerOverlay(
			new GTileLayer(null, 1, 3, {
				tileUrlTemplate: flagatripBaseHost + 'gmap_tile.php?x={X}&y={Y}&z={Z}&c=' + c, 
				isPng: true
			})
		);
		this.addOverlay(this.tlo);
	} 
}

GMap2.prototype.fMapView = false;
GMap2.prototype.fMapDefaultWidth = 0;

GMap2.prototype.shitchMapView = function(expand, addControls) {
	if (expand == null) {
		expand = !this.fMapView;
	}
	if (addControls == null) {
		addControls = true;
	}

	var mapElement = $(this.getContainer());

	if (expand && this.fMapDefaultWidth == 0) {
		this.fMapDefaultWidth = mapElement.clientWidth;
	}
	
	mapElement.addClass('animation');
	new Fx.Morph(mapElement, {
		duration: 2000,
		link: 'cancel',
		transition: Fx.Transitions.Back.easeInOut,
		onComplete: function() {
			mapElement.removeClass('animation');
			if (addControls) {			
				if (expand) {
					//this.fZoom = 2;
					//this.enableDragging();
					this.zoomControl = new GLargeMapControl()
					this.addControl(this.zoomControl);
					this.fCheckResize();
					this.returnToSavedPosition();
				} else {
					//this.fZoom = 1;
					this.removeControl(this.zoomControl);
					this.fCheckResize();
					//this.disableDragging();
					this.returnToSavedPosition();
					
				}
			} else {
				this.checkResize();
				this.returnToSavedPosition();
			}
		}.bind(this)
	}).start({'width': expand ? 942 : this.fMapDefaultWidth});
	this.fMapView = expand;
	return this.fMapView;
}

GMap2.prototype.focusTripFlag = false;

GMap2.prototype.enableFocusTrip = function() {
	this.focusTripFlag = true;
	this.fCheckResize();
}

GMap2.prototype.disableFocusTrip = function() {
	this.focusTripFlag = false;
}

GMap2.prototype.fCheckResize = function() {
	this.checkResize();
	if (this.focusTripFlag && this.tripPoly) {
		var b = this.tripPoly.getBounds()
		if (this.trip.length < 2) return;
		this.fCenter = b.getCenter();
		this.fZoom   = this.getBoundsZoomLevel(b) - 1;
		if (this.fZoom < 1) this.fZoom = 1;
		this.setCenter(this.fCenter, this.fZoom);
	} else {
		this.setCenter(this.fCenter, this.fZoom);
	}
}