var googleMapsApiIcons = Class.create();

var googleMapsApiMode = '';

googleMapsApiIcons.prototype = {
	initialize : function(){

		this.points = new Array();
		if (GBrowserIsCompatible()) {

			this.points['point1'] = Object.extend(
				{
					iconSrc : "/img/map/pointa.png",
					shadowSrc : "/img/map/pointshadow.png",
		        	iconSize : new GSize(20, 34),
		        	iconAnchor : new GPoint(9, 16),
		        	infoWindowAnchor : new GPoint(9, 2),
		        	shadowSize : new GSize(34, 19),
		        	infoShadowAnchor : new GPoint(18, 25)
				}
			);
			this.points['point2'] = Object.extend(
				{
					iconSrc : "/img/map/pointb.png",
					shadowSrc : "/img/map/pointshadow.png",
		        	iconSize : new GSize(20, 34),
		        	iconAnchor : new GPoint(9, 16),
		        	infoWindowAnchor : new GPoint(9, 2),
		        	shadowSize : new GSize(34, 19),
		        	infoShadowAnchor : new GPoint(18, 25)
				}
			);
			this.points['point3'] = Object.extend(
				{
					iconSrc : "/img/map/pointc.png",
					shadowSrc : "/img/map/pointshadow.png",
		        	iconSize : new GSize(20, 34),
		        	iconAnchor : new GPoint(9, 16),
		        	infoWindowAnchor : new GPoint(9, 2),
		        	shadowSize : new GSize(34, 19),
		        	infoShadowAnchor : new GPoint(18, 25)
				}
			);
		}
	}
}


var googleMapsApi = Class.create();

googleMapsApi.prototype = {

	initialize : function(contentId, options){

		this.options = Object.extend({
			mode : '',
			centerX : 52.1065,
			centerY : 18.9404,
			zoom : 5,
			showConterol: true,
			showType: true,
			maxPoints: 10,
			maxNewPoints: 3,
			lineColor: "#ff0000",
			lineWeight: 10,
			maxNewPointsId: 'point2',
			removePrevMode: false,
			rememberPosition: false,
			rememberPositionPrefix: '',
			rememberPositionTime: 365,
			getCurrentPoints: function() {}
		}, options || {});

		if (this.options.rememberPosition) {

			if (
				MP_getCookie(this.options.rememberPositionPrefix + 'X') &&
				MP_getCookie(this.options.rememberPositionPrefix + 'Y') &&
				MP_getCookie(this.options.rememberPositionPrefix + 'ZOOM')
			) {

				this.options.centerX = MP_getCookie(this.options.rememberPositionPrefix + 'X')*1;
				this.options.centerY = MP_getCookie(this.options.rememberPositionPrefix + 'Y')*1;
				this.options.zoom = MP_getCookie(this.options.rememberPositionPrefix + 'ZOOM')*1;
			}
		}

		this.contentId = $(contentId);
		if (GBrowserIsCompatible()) {

			this.map = new GMap2($(contentId));
			this.geocoder = new GClientGeocoder();

			this.pointsCount 	  = 0;
			this.newPointsCount   = 0;
			this.markers 		  = new Array();
			this.newMarker 		  = new Array();
			this.newPointsParams  = new Array();

			this.areaParams       = new Array();
			this.areaLines        = new Array();

			this.pointsIcons 	  = new googleMapsApiIcons().points;

			this.map.setCenter(new GLatLng(this.options.centerX, this.options.centerY), this.options.zoom);

			if (this.options.showConterol) {
//				this.map.addControl(new GLargeMapControl3D());
				this.map.addControl(new GLargeMapControl());

			}
			if (this.options.showType) {
        		this.map.addControl(new GMapTypeControl());
			}

			googleMapsApiMode = this.options.mode;
			GEvent.addListener(this.map, "moveend"	, this.options.getCurrentPoints);
			GEvent.addListener(this.map, "click"	, this.clickMapEvent);
			GEvent.addListener(this.map, "load"	, this.options.getCurrentPoints);

		}
	},

	changeMode : function (newMode) {

		if (this.options.removePrevMode) {
			this.removeNewPoints();
			this.removeArea();
		}
		googleMapsApiMode = newMode;
	},

	removeArea : function () {

		if (this.areaParams.length > 0) {

			for (i = 0 ; i < 4 ; i++) {

				if (this.areaLines[i]) {
					this.map.removeOverlay(this.areaLines[i]);
				}
			}
			this.areaParams = new Array();
		}
	},

	addAreaPoint : function (point) {

		if (point) {

			var index = this.areaParams.length;

			if (index > 3) {

				this.removeArea();
				index = 0;
			}

			this.areaParams[index] = point;

			if (index == 3) {

				this.drawArea();

			}
		}
	},

	drawArea : function () {

		var polyline = new Object();

		polyline = new GPolyline(
				[
					this.areaParams[0],
					this.areaParams[1]
				],
				this.options.lineColor,
				this.options.lineWeight
			);

		this.areaLines[0] = polyline;

		this.map.addOverlay(polyline);

		polyline = new GPolyline(
				[
					this.areaParams[1],
					this.areaParams[2]
				],
				this.options.lineColor,
				this.options.lineWeight
			);
		this.map.addOverlay(polyline);

		this.areaLines[1] = polyline;

		polyline = new GPolyline(
				[
					this.areaParams[2],
					this.areaParams[3]
				],
				this.options.lineColor,
				this.options.lineWeight
			);
		this.map.addOverlay(polyline);

		this.areaLines[2] = polyline;

		polyline = new GPolyline(
				[
					this.areaParams[3],
					this.areaParams[0]
				],
				this.options.lineColor,
				this.options.lineWeight
			);
		this.map.addOverlay(polyline);

		this.areaLines[3] = polyline;
	},

	clickMapEvent : function (marker, point) {

		if (point) {

			switch (googleMapsApiMode) {

				case "addPoint":
					googleMapsApiObj.addNewPoint(point)
					break;

				case "addArea":
					googleMapsApiObj.addAreaPoint(point)
					break;
			}
		}
	},

	removeNewPoints : function () {

		for (i = 0 ; i < this.newPointsCount ; i++) {

			this.map.removeOverlay(this.newMarker[i]);
		}

		this.newMarker 			= new Array();
		this.newPointsParams 	= new Array();
		this.newPointsCount 	= 0;
	},

	addNewPoint : function(point) {

		if (this.options.maxNewPoints == 1) {

			this.newPointsCount > 0;
			this.removeNewPoints();
			this.removePoints();

		} else if (this.newPointsCount >= this.options.maxNewPoints) {

			return;
		}

		var baseIcon = new GIcon();
        	baseIcon.shadow 			= this.pointsIcons[this.options.maxNewPointsId].shadowSrc;
        	baseIcon.iconSize 			= this.pointsIcons[this.options.maxNewPointsId].iconSize;
        	baseIcon.shadowSize 		= this.pointsIcons[this.options.maxNewPointsId].shadowSize;
        	baseIcon.iconAnchor 		= this.pointsIcons[this.options.maxNewPointsId].iconAnchor;
        	baseIcon.infoWindowAnchor 	= this.pointsIcons[this.options.maxNewPointsId].infoWindowAnchor;
        	baseIcon.infoShadowAnchor 	= this.pointsIcons[this.options.maxNewPointsId].infoShadowAnchor;

        var icon = new GIcon(baseIcon);
        icon.image = this.pointsIcons[this.options.maxNewPointsId].iconSrc;

        var marker = new GMarker(point, icon);

        this.newPointMarker = marker;

		this.map.addOverlay(marker);

		this.newMarker[this.newPointsCount] 		= marker;
		this.newPointsParams[this.newPointsCount] 	= point;

		this.newPointsCount++;
	},

	addNewPointParams : function (point) {

		this.newPointsParams[this.newPointsCount] = point;
		this.newPointsCount++;
	},


	addPointMessage : function (point, pointParams) {

        var baseIcon = new GIcon();

        	baseIcon.shadow 			= this.pointsIcons[pointParams.pointId].shadowSrc;
        	baseIcon.iconSize 			= this.pointsIcons[pointParams.pointId].iconSize;
        	baseIcon.shadowSize 		= this.pointsIcons[pointParams.pointId].shadowSize;
        	baseIcon.iconAnchor 		= this.pointsIcons[pointParams.pointId].iconAnchor;
        	baseIcon.infoWindowAnchor 	= this.pointsIcons[pointParams.pointId].infoWindowAnchor;
        	baseIcon.infoShadowAnchor 	= this.pointsIcons[pointParams.pointId].infoShadowAnchor;

        var icon = new GIcon(baseIcon);
        icon.image = this.pointsIcons[pointParams.pointId].iconSrc;

        var marker = new GMarker(point, icon);

        if (pointParams.textHtml && pointParams.textHtml.length) {

	        GEvent.addListener(marker, "click", function() {
	            marker.openInfoWindowHtml(pointParams.textHtml);
	          });
        }

        this.markers[this.pointsCount-1] = marker;
        return marker;
    },

	addPoint : function(pointParams) {


		if (this.pointsCount < this.options.maxPoints) {

			this.pointsCount++;


	        var point = new GLatLng(pointParams.posX, pointParams.posY);
			this.map.addOverlay(this.addPointMessage(point, pointParams));
		}
	},

	addPoints : function(pointsParams) {

		var marker 	= new Object();
		var point 	= new Object();

		if (pointsParams.length && pointsParams.length > 0) {

				for (i = 0 ; i < pointsParams.length ; i++) {

				if (this.pointsCount < this.options.maxPoints) {

					this.pointsCount++;

					var point = new GLatLng(pointsParams[i].posX, pointsParams[i].posY);
			        this.map.addOverlay(this.addPointMessage(point, pointsParams[i]));
				}
			}
		}
	},

	removePoints : function() {

		if (this.pointsCount > 0) {
			for(i = 0 ; i < this.pointsCount ; i++) {

				this.map.removeOverlay(this.markers[i]);
			}
		}
		this.pointsCount = 0;
	},

	getBounds : function() {

		if (googleMapsApiObj.options.rememberPosition) {

			var centerParams = googleMapsApiObj.map.getCenter();

			MP_setCookie(googleMapsApiObj.options.rememberPositionPrefix + 'ZOOM', googleMapsApiObj.map.getZoom(), googleMapsApiObj.options.rememberPositionTime);
			MP_setCookie(googleMapsApiObj.options.rememberPositionPrefix + 'X', centerParams.lat(), googleMapsApiObj.options.rememberPositionTime);
			MP_setCookie(googleMapsApiObj.options.rememberPositionPrefix + 'Y', centerParams.lng(), googleMapsApiObj.options.rememberPositionTime);

		}

		var bounds = this.map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();

		var result  = "left|" +  southWest.lng();
			result += "||right|" +  northEast.lng();
			result += "||top|" +  southWest.lat();
			result += "||bottom|" +  northEast.lat();

		return result;
	},

	getNewArea : function() {

		var result = this.map.getZoom() + '||' + this.map.getCenter() + '||' + this.areaParams;
		return result;
	},

	getNewPoints : function() {

		var result = this.map.getZoom() + '||' + this.map.getCenter() + '||' + this.newPointsParams;
		return result;
	},

	getMapZoom : function() {

		return this.map.getZoom();
	},

	getPosX : function() {

		var centerPoint = this.map.getCenter();

		return centerPoint.lng();
	},

	getPosY : function() {

		var centerPoint = this.map.getCenter();

		return centerPoint.lat();
	},

	setCenter : function(zoom, pos_x, pos_y) {

		var centerPoint = this.map.getCenter();

		this.map.setCenter(new GLatLng(pos_x, pos_y), zoom);
	},

	setStandardCenter : function() {

		this.map.setCenter(new GLatLng(52.1065, 18.9404), 5);
	},

	findAddress : function(address, suffix, markPlace) {
		suffix = typeof(suffix) != 'undefined' ? suffix : "krakow, poland";
		markPlace = typeof(markPlace) != 'undefined' ? markPlace : 1;

		this.geocoder.getLatLng(address + ', ' + suffix,

			function(placeFound) {
      			if (!placeFound) {
        			//alert(address + " not found");
      			} else {
      				// Tutaj wspolrzedne maja byc zamienione miejscami
        			this.setCenter(/*this.map.getZoom()*/ 15, placeFound.y, placeFound.x);
        			if(markPlace)
        				this.addNewPoint(placeFound);
				}
      		}.bind(this)

      	);

	}
};
