// Copy Object Function
function copy_obj(o) {
	var c = new Object();
	for (var e in o) {
		if (typeof o[e] =='array' && o[e].constructor == Array) {
			c[e] = o[e].copy();
		}
		else {
			c[e] = o[e]; 
		}
	}
	return c;
}

function gmob(sName, nWd, nHi, sClass) { this._initOb(sName, nWd, nHi, sClass); }

gmob.prototype = {
	idShow: "",
	idVal: "",
	isShow: false,
	elShow: null,
	oMap: null,
	lastScale: null,
	lastOvr: null,
	bDisEdit: false,
	bAbsolut: false,
	oNavigator: {},
	tm_sh: null,
	nxShift: 0,
	nyShift: 0,
	bUseShift: true,
	bMoveTo0: true,
	sMsg: 'Шелчком мышки установите точку на карте и нажмите -&gt;',
	sTopString: '',
	sEdUrl: '',
	aPoint: {},
	sPointFilt: '',
	aMarkers: [],

	_initOb: function (sName, nWd, nHi, sClass) {
		this.idShow = sName;
		var isNetsc=(document.layers)?true:false;
		var isOpera=(navigator.userAgent.indexOf('Opera')!=-1)?true:false;
		var isIe=(document.all&&(!this.o))?true:false;
		var isElId=(document.getElementById)?true:false;
		this.oNavigator={'isNetsc': isNetsc, 'isOpera': isOpera, 'isIe': isIe, 'isElId': isElId};

		var idm = 'mapdiv';
/*		var e = null;
	  if( !e && document.all ) e = document.all[idm];
	  if( !e && document.getElementById ) {
			e = document.getElementById(idm);
			if( e && e.length && (e.length > 1) ) e = e[0];
		}
*/
//		if( !e ) {
//			alert(this.sMsg);
			this.sTopString = this.sMsg+' [ <a href="#" style="color: #990000; font-weight: bold;" onclick="ogmap.hideMap(); return false;">close</a> ]';
			var s1 = '<div id="'+sName+'" style="position:absolute; visibility: hidden; z-index: 32; width:'+(nWd+4)+'px; height:'+(nHi+20)+'px; margin: 0; padding: 0; border: solid 1px #000000; text-align: center; background-color: #ffffff;"><p id="top_str_msg" style="text-align: right; margin: 0; padding: 0;">'+this.sTopString+'</p><div id="'+idm+'" style=" width:'+nWd+'px; height:'+nHi+'px; margin-top: 4px;"></div></div>'; // class="'+sClass+'" 
/*			var panel = document.createElement('DIV');
			panel.id = sName;
			var sst = 'position:absolute; visibility: hidden; width:'+(nWd+4)+'px; height:'+(nHi+20)+'px; margin: 0; padding: 0; border: solid 1px #000000; text-align: center; background-color: #ffffff;';
			var sint = '<p id="top_str_msg" style="text-align: right; margin: 0; padding: 0;">'+this.sMsg+' [ <a href="#" style="color: #990000; font-weight: bold;" onclick="ogmap.hideMap(); return false;">close</a> ]</p><div id="'+idm+'" style=" width:'+nWd+'px; height:'+nHi+'px; margin-top: 4px;"></div>';
			if (panel.getAttribute('style')) {
				panel.style.cssText = sst;
			}
			else {
				panel.setAttribute('style', sst);
			}
			panel.innerHTML = sint;
			document.documentElement.appendChild(panel); */
//			alert(s1);
			document.write(s1);
/*		}
		else {
			alert('e');
//			e.style.cssText = 'width:'+x+';height:'+y+'; background-color: #ff8080;';
		}*/
	},
 
	setTopMsg: function (s1) {
		var e = null;
		var idm = 'top_str_msg';
	  if( !e && document.all ) e = document.all[idm];
	  if( !e && document.getElementById ) {
			e = document.getElementById(idm);
			if( e.length && (e.length > 1) ) e = e[0];
		}
		if( !e ) return;
		var s2 = e.innerHTML;
		var p1 = s2.indexOf('[');
		if( p1 == -1 ) return;
		e.innerHTML = s1+' '+s2.substr(p1);
		this.sTopString = e.innerHTML;
//		alert(this.sTopString);
	},
 
	setWinSize: function (x, y) {
		var e = null;
		var e1 = null;
		var idm = this.idShow;
	  if( !e && document.all ) e = document.all[idm];
	  if( !e && document.getElementById ) {
			e = document.getElementById(idm);
			if( e.length && (e.length > 1) ) e = e[0];
		}
		if( !e ) return;

		var idm = 'mapdiv';
	  if( !e1 && document.all ) e1 = document.all[idm];
	  if( !e1 && document.getElementById ) {
			e1 = document.getElementById(idm);
			if( e1.length && (e1.length > 1) ) e1 = e1[0];
		}
		if( !e1 ) return;
		if (e.getAttribute('style')) {
			e.style.cssText = 'width:'+(x+4)+'px; height:'+(y+20)+'px;'; // width = x+4;
//			e.style.height = y+20;
			e1.style.cssText = 'width:'+x+'px; height:'+y+'px;'; // width = x;
//			e1.style.height = y;
//			ct.style.cssText = styles;
		}
		else {
			e.setAttribute('style', 'width:'+(x+4)+'px; height:'+(y+20)+'px;');
			e1.setAttribute('style', 'width:'+x+'px; height:'+y+'px;');
//			e1.style.width = x;
//			e1.style.height = y;
//			ct.setAttribute('style', styles);
		}
	},

	setPoint: function (ap) {
		this.aPoint = ap;
	},

	shPoints: function (s1) {
		this.sPointFilt = s1;
	},

	setShift: function (nX, nY) {
		this.nxShift = nX;
		this.nyShift = nY;
	},
 
	setScale: function (nScale) {
		this.lastScale = nScale;
	},

	setEditUrl: function (s1) {
		this.sEdUrl = s1;
	},
 
	setShowClose: function (b1) {
		var e = null;
		var idm = 'top_str_msg';
	  if( !e && document.all ) e = document.all[idm];
	  if( !e && document.getElementById ) {
			e = document.getElementById(idm);
			if( e.length && (e.length > 1) ) e = e[0];
		}
		if( !e ) return;
		if( b1 ) e.innerHTML = this.sTopString;
		else {
			this.sTopString = e.innerHTML;
			e.innerHTML = '&nbsp;';
		}

	},
 
	addMarker: function (point, name, html) {
		var marker = new GMarker(point);
		if( this.bDisEdit )
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
			});
		return marker;
	},

	createMarker: function (nLat, nLon, stit, shtml) {
		var point = new GLatLng(nLat, nLon);
//					alert('point = '+this.aPoint[i][j][0]+', '+this.aPoint[i][j][1]);
		var marker = this.addMarker(point, stit, stit+'<br>'+shtml);
		this.oMap.addOverlay(marker);
		this.aMarkers.push(marker);
		return marker;
	},

	mkMap: function (nLat, nLon, nScale) {
		if( nLat == 0 ) nLat = 55.99;
		if( nLon == 0 ) nLon = 37.21;
		if( !this.lastScale ) this.lastScale = nScale;
		if( this.oMap ) {
			this.oMap.setCenter(new GLatLng(nLat, nLon), this.lastScale);
			return;
		}
		if (GBrowserIsCompatible()) {
			this.oMap = new GMap2(document.getElementById("mapdiv"));
			this.oMap.setCenter(new GLatLng(nLat, nLon), this.lastScale);
/*
			var a1 = this.oMap.getMapTypes();
			var a2 = a1[0].getTileLayers();
			var sret = "";

			var tilelayers = [new GTileLayer(new GCopyrightCollection("Map Data: Zel"), 12, 17)];
			tilelayers[0].getTileUrl = function (tile, zoom) {return "http://izcom.ru/download/map/" + zoom+"_"+tile.x+"_"+tile.y+".png";};
			tilelayers[0].getCopyright = function(a,b) {return {prefix:" ", copyrightTexts:["TeleAtlas"]};}
// == Create the GMapType, copying most things from G_SATELLITE_MAP ==
			var custommap = new GMapType(tilelayers, G_SATELLITE_MAP.getProjection(), "Mymap", {errorMessage:_mMapError});

// == Add the maptype to the map ==
//			this.oMap.addMapType(custommap);
//			this.oMap.setMapType(custommap);
*/
			this.oMap.addControl(new GSmallMapControl());
			GEvent.bind(this.oMap, "click", this, this.onMapClick);
		}
		else {
			var s1 = "<p>Ваш браузер не поддерживает карту.</p>";
			var s2 = this.elShow.innerHTML;
//			alert(s2.toLowerCase() + '\\n' + s1.toLowerCase());
			if( s2.toLowerCase().indexOf(s1.toLowerCase()) == -1 ) this.elShow.innerHTML = this.elShow.innerHTML + s1;
		}
	},

	onMapClick: function (marker, point) {
		if( this.bDisEdit ) return;
		var e = null;
	  if( !e && document.all ) e = document.all[this.idVal];
	  if( !e && document.getElementsByName ) e = document.getElementsByName(this.idVal)[0];
		if (marker) {
			this.oMap.removeOverlay(marker);
			if( e ) e.value = "";
		}
		else {
			if( this.lastOvr ) {
				this.oMap.removeOverlay(this.lastOvr);
			}
			this.oMap.clearOverlays();
			this.lastOvr = new GMarker(point);
//			alert('point = ' + point.x + ' ' + point.y);
			this.oMap.addOverlay(this.lastOvr);
			if( e ) e.value = point.toUrlValue();
//			alert('url = ' + this.sEdUrl);
			if( this.sEdUrl.length > 0 ) {
//       map.clearOverlays(); 
				var request = GXmlHttp.create(); 
//				alert('url full = ' + this.sEdUrl+point.toUrlValue());
				request.open("GET", this.sEdUrl+point.toUrlValue(), true); 
				request.onreadystatechange = function() { 
					if (request.readyState == 4) {
//						alert('Set');
					}
				}
				request.send(null);
			}
//				alert('e.value = ' + e.value);
//			this.hideMap();
		}
	},

	shMap: function (esrc, idVal, nLat, nLon, nScale) {
		if (!this.elShow) {
//			this.elShow = null;
		  if (!this.elShow && document.all) this.elShow = document.all[this.idShow];
		  if (!this.elShow && document.getElementById) this.elShow = document.getElementById(this.idShow);
		}
//		alert('this.elShow = '+this.elShow);
	  if (!this.elShow) return;
		this.idVal = idVal;
		var o1 = this.getBounds(esrc);
		var dx = 0;
		var dy = 0;
		if( this.bUseShift ) {
			dx = o1.width/3 + this.nxShift;
			dy = o1.height/3 + this.nyShift;
		}
//		alert(o1.left+' ('+dx+') : '+o1.top+' ('+dy+')');
		if( this.bAbsolut ) this.setElementPos(this.nxShift, this.nyShift);
		else this.setElementPos(o1.left + dx+1, o1.top + dy+1);
		
	  if (this.tm_sh) {
  	  clearTimeout(this.tm_sh);
    	delete this.tm_sh;
	  }
		this.mkMap(nLat, nLon, nScale);
		var e = null;
	  if( !e && document.all ) e = document.all[this.idVal];
//		if( !e && document.getElementById ) e = document.getElementById(this.idVal);
	  if( !e && document.getElementsByName ) e = document.getElementsByName(this.idVal)[0];
		if( this.lastOvr ) {
//			alert(this.lastOvr)
			this.oMap.removeOverlay(this.lastOvr);
		}
//		this.oMap.clearOverlays();
//		alert(e.value);
		if( e && (e.value != "")) {
			var a1 = e.value.split(",");
//			this.lastOvr = this.oMap.addOverlay(new GMarker(new GLatLng(a1[0], a1[1])));
//			alert(a1);
			this.lastOvr = new GMarker(new GLatLng(a1[0], a1[1]));
//			alert(this.lastOvr);
			this.oMap.addOverlay(this.lastOvr);
		}

//		alert('this.aPoint.length = '+this.aPoint.length);
//		if( this.aPoint.length > 0 ) {
			if( this.aMarkers.length > 0 ) {
				for(var i in this.aMarkers) {
					this.oMap.removeOverlay(this.aMarkers[i]);
					GEvent.clearListeners(this.aMarkers[i], "click");
					this.aMarkers.pop();
				}
			}
			var bcen = false;
			var nmaxlat = 0, nminlat = 0, nmaxlon = 0, nminlon = 0;
			for(var i in this.aPoint) {
				if( (this.sPointFilt != '') && (this.sPointFilt != i) ) continue;
				for(var j in this.aPoint[i]) {
					bcen = true;
					if( nmaxlat == 0 ) {
						nmaxlat = this.aPoint[i][j][0];
						nminlat = this.aPoint[i][j][0];
						nmaxlon = this.aPoint[i][j][1];
						nminlon = this.aPoint[i][j][1];
					}
					if( nmaxlat < this.aPoint[i][j][0] ) nmaxlat = this.aPoint[i][j][0];
					if( nminlat > this.aPoint[i][j][0] ) nminlat = this.aPoint[i][j][0];
					if( nmaxlon < this.aPoint[i][j][1] ) nmaxlon = this.aPoint[i][j][1];
					if( nminlon > this.aPoint[i][j][1] ) nminlon = this.aPoint[i][j][1];
					var marker = this.createMarker(this.aPoint[i][j][0], this.aPoint[i][j][1], this.aPoint[i][j][2], this.aPoint[i][j][3])
//					var point = new GLatLng();
//					alert('point = '+this.aPoint[i][j][0]+', '+this.aPoint[i][j][1]);
//					var marker = this.addMarker(point, this.aPoint[i][j][2], this.aPoint[i][j][2]+'<br>'+this.aPoint[i][j][3]);
//					this.oMap.addOverlay(marker);
//					this.aMarkers.push(marker);
				}
			}
			if( bcen ) {
				if( (nminlat == nmaxlat) || (nmaxlon == nminlon) ) this.lastScale = 14;
				else this.lastScale = this.oMap.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(nminlat, nminlon), new GLatLng(nmaxlat, nmaxlon)));
				if( this.lastScale > 14 ) this.lastScale = 14;
				this.oMap.setZoom(this.lastScale);
				this.oMap.setCenter(new GLatLng((nmaxlat + nminlat)/2, (nmaxlon + nminlon)/2), this.lastScale);
			}
//		}

		var e = this.elShow;
		if( this.bMoveTo0 ) scrollTo(0, 0);
		this.tm_sh = setTimeout(function() {
			e.style.visibility = 'visible';
			ogmap.isShow = true;
		}, 10);

	},

	hideMap: function () {
	  if (!this.elShow) return;
	  if (this.tm_sh) {
  	  clearTimeout(this.tm_sh);
    	delete this.tm_sh;
	  }
		var e = this.elShow;
		if( this.oMap ) this.lastScale = this.oMap.getZoom()
		this.tm_sh = setTimeout(function() {
			e.style.visibility = 'hidden';
			ogmap.isShow = false;
			if( ogmap.aMarkers.length > 0 ) {
				for(var i in ogmap.aMarkers) {
					ogmap.oMap.removeOverlay(ogmap.aMarkers[i]);
					GEvent.clearListeners(ogmap.aMarkers[i], "click");
					ogmap.aMarkers.pop();
				}
			}
		}, 20);
	},

	getBounds: function (element) {
		var left = element.offsetLeft;
		var top = element.offsetTop;
		for (var parent = element.offsetParent; parent; parent = parent.offsetParent) {
			left += parent.offsetLeft;
			top += parent.offsetTop;
		}
		return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
	},
	
	setElementPos: function (x,y) {
	  if (!this.elShow) return;
		x = Math.floor(x);
		y = Math.floor(y);
//		alert('setElementPos('+x+','+y+')');
		if(this.oNavigator.isNetsc) {
			this.elShow.moveTo(x,y);
		}
		else if(this.oNavigator.isIe) {
			this.elShow.style.pixelLeft=x;
			this.elShow.style.pixelTop=y;
		} else {
			if (this.elShow.getAttribute('style')) {
				this.elShow.style.cssText = 'position: absolute; left:'+x+'px; top:'+y+'px; border: solid 1px #000000; background-color: #ffffff;'; // width = x+4;
			}
			else {
				this.elShow.setAttribute('style', 'position: absolute; left:'+x+'px; top:'+y+'px; border: solid 1px #000000; background-color: #ffffff;');
			}
//			this.elShow.style.left=x;
//			this.elShow.style.top=y;
		}
//		alert('setElementPos() end');
	}
}

ogmap = new gmob('idOutMap', 500, 500, '');


