﻿Type.registerNamespace("PEAB.Base");

PEAB.Base.GoogleMapExtender = function(element) {

	this.baseurl = null;
	this.types = new Array();

	this.startpos = null;

	PEAB.Base.GoogleMapExtender.initializeBase(this, [element]);
}

PEAB.Base.GoogleMapExtender.prototype =
{
	get_baseurl: function() {
		return this.baseurl;
	},

	set_baseurl: function(value) {
		this.baseurl = value;
	},

	get_basetypes: function() {
		return this.types.join(",");
	},

	set_basetypes: function(value) {
		if (value) {
			this.types = value.split(",");
		}
	},

	initialize: function() {
		PEAB.Base.GoogleMapExtender.callBaseMethod(this, "initialize");

		GEvent.bindDom($find(this.get_element().id).gMap, "movestart", this, this.movestart);
		GEvent.bindDom($find(this.get_element().id).gMap, "moveend", this, this.moveend);
	},

	dispose: function() {
		PEAB.Base.GoogleMapExtender.callBaseMethod(this, "dispose");
	},

	add: function(type) {
		if (!Array.contains(this.types, type)) {
			Array.add(this.types, type);
		}
	},

	remove: function(type) {
		if (Array.contains(this.types, type)) {
			Array.remove(this.types, type);
		}
	},

	loadurl: function() {
		var map = $find(this.get_element().id).gMap;

		var center = map.getCenter();
		var viewPortSize = map.getBounds().getSouthWest().distanceFrom(map.getBounds().getNorthEast());
		var zoom = map.getZoom();

		$find(this.get_element().id).loadurl(this.baseurl + "&zoom=" + zoom + "&lat=" + center.lat() + "&lng=" + center.lng() + "&size=" + viewPortSize + '&types=' + this.types.join(","));
	},

	movestart: function() {
		this.startpos = $find(this.get_element().id).gMap.getCenter();
	},

	moveend: function() {
		var map = $find(this.get_element().id).gMap;

		var metersMoved = 0;
		var endpos = map.getCenter();

		if (this.startpos && endpos) {
			metersMoved = endpos.distanceFrom(this.startpos);
		}

		var viewPortSize = map.getBounds().getSouthWest().distanceFrom(map.getBounds().getNorthEast());

		if (metersMoved > viewPortSize / 2) {
			this.loadurl();
		}
	}
}

PEAB.Base.GoogleMapExtender.registerClass("PEAB.Base.GoogleMapExtender", Sys.UI.Behavior);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();