Type.registerNamespace("Fazer.Base");

Fazer.Base.MapInstance = null;

Fazer.Base.GoogleMap = function(element) {
	this.panel = null;
	this.map = null;
	this.latitude = null;
	this.longitude = null;
	this.loader = null;
	this.zoomLevel = null;
	this.draggable = null;
	this.geoXmlUrl = null;
	this.forceMapCenter = null;
	this.markerImage = '/Frameworks/Default/Images/Map/Default_Pin.png';

	this.gMap = null;
	this.gMarker = null;
	this.gGeoXml = null;

	this.showMarker = false;
	this.defaultLatitude = 59.32436;
	this.defaultLongitude = 18.0677;

	Fazer.Base.GoogleMap.initializeBase(this, [element]);
}

Fazer.Base.GoogleMap.prototype =
{
	get_options: function() {
		return null;
	},

	set_options: function(value) {
		this.panel = $get(value.panelID);
		this.map = $get(value.mapID);
		this.latitude = $get(value.latitudeID);
		this.longitude = $get(value.longitudeID);
		this.loader = $get(value.loaderID);
		this.zoomLevel = value.zoomLevel;
		this.draggable = value.draggable;
		this.geoXmlUrl = value.geoXmlUrl;
		this.forceMapCenter = value.forceMapCenter;
		this.showMarker = value.showMarker || this.draggable;
	},

	initialize: function() {
		Fazer.Base.GoogleMap.callBaseMethod(this, "initialize");

		if (GBrowserIsCompatible()) {
			this.gMap = new GMap2(this.map);

			GEvent.addListener(this.gMap, "infowindowopen", function() {
				// Get a reference to the infoWindow 
				var infoWindow = $(this.getInfoWindow().getContentContainers());
				// Find all <a> tags in the infoWindow and reset their target attribute 
				jQuery("a", infoWindow).attr("target", "_self");
			});

			var mapControl = new GLargeMapControl3D();
			this.gMap.addControl(mapControl);
			this.gMap.addControl(new GMapTypeControl());

			if (this.latitude.value && this.longitude.value) {
				this.setPosition(new GLatLng(parseFloat(this.latitude.value), parseFloat(this.longitude.value)));
				this.gMap.setZoom(this.zoomLevel);
			}
			else {
				this.setPosition(new GLatLng(parseFloat(this.defaultLatitude), parseFloat(this.defaultLongitude)));
				this.gMap.setZoom(this.zoomLevel);
			}

			if (this.geoXmlUrl) {
				this.loadurl(this.geoXmlUrl);
			}

			Fazer.Base.MapInstance = this;
		}
	},

	dispose: function() {
		Fazer.Base.GoogleMap.callBaseMethod(this, "dispose");
	},

	setPosition: function(latlng) {

		this.gMap.setCenter(latlng);

		if (this.showMarker) {
			this.setMarkerPosition(latlng);
		}

		this.map.style.display = 'block';
	},

	setMarkerPosition: function(latlng) {
		if (!this.gMarker) {

			var gIcon = new GIcon();
			gIcon.image = this.markerImage;
			gIcon.iconSize = new GSize(27, 44);
			gIcon.iconAnchor = new GPoint(15, 45);

			this.gMarker = new GMarker(latlng, { icon: gIcon, draggable: this.draggable });
			// this.gMarker = new GMarker(latlng, { draggable: this.draggable });
			GEvent.bindDom(this.gMarker, "dragend", this, this.dragend);
			this.gMap.addOverlay(this.gMarker);
		}
		else {
			this.gMarker.setLatLng(latlng);
		}

		this.gMarker.show();
	},

	dragend: function() {
		var latlng = this.gMarker.getLatLng();
		this.latitude.value = latlng.lat();
		this.longitude.value = latlng.lng();
	},

	search: function(query) {
		this.showLoader();

		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(query, this.searched);
	},

	searched: function(latlng) {
		if (!latlng) {
			alert("Adressen hittades inte!");
		}
		else {
			var current = Fazer.Base.MapInstance;

			current.latitude.value = latlng.lat();
			current.longitude.value = latlng.lng();

			current.showMarker = true;
			current.setPosition(latlng);
			current.gMap.setZoom(current.zoomLevel);
			current.hideLoader();
		}
	},

	clear: function() {
		this.latitude.value = '';
		this.longitude.value = '';
		this.gMap.clearOverlays();

		this.gMarker = null;

		this.map.style.display = 'none';
	},

	loadurl: function(url) {
		if (this.gGeoXml) {
			this.gMap.removeOverlay(this.gGeoXml);
		}

		this.showLoader();

		this.gGeoXml = new GGeoXml(url);
		GEvent.bindDom(this.gGeoXml, "load", this, this.urlloaded);
		this.gMap.addOverlay(this.gGeoXml);

		this.map.style.display = 'block';
	},

	urlloaded: function() {
		this.hideLoader();
	},

	checkResize: function() {
		this.gMap.checkResize();
		if (this.gGeoXml) {
			this.gGeoXml.gotoDefaultViewport(this.gMap);
		}
		else if (this.gMarker) {
			this.gMap.setCenter(this.gMarker.getLatLng());
		}
		else if (this.gMap) {
			this.gMap.setCenter(this.gMap.getCenter());
		}

		if (this.forceMapCenter) {
			this.gMap.setZoom(this.zoomLevel);
			this.setPosition(new GLatLng(parseFloat(this.latitude.value), parseFloat(this.longitude.value)));
		}
	},

	hideLoader: function() {
		this.loader.style.display = 'none';
	},

	showLoader: function() {
		this.loader.style.display = 'block';
	}
}

Fazer.Base.GoogleMap.registerClass("Fazer.Base.GoogleMap", Sys.UI.Control);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();