$(document).ready( function() { $('em.map').click(showMap) })

var maps = {}

function showMap () {
	var id = parseInt(this.getAttribute('rel'), 10)
	if (!maps[id]) maps[id] = new Map(this, id)
	else if (maps[id].active) maps[id].hide()
	else maps[id].show()
}

function Map (caller, id) {

	var o = this
	o.id =		id
	o.caller =	caller
	o.map =		null
	o.popup =	null
	o.active =	true

	caller.style.background = '#fff url("/i/loader.gif") no-repeat center'

	this.create =	function () { with (o) {

				popup = document.createElement('div')
					popup.className = 'popup'
					var popupTitleBar = document.createElement('div')
					popupTitleBar.className = 'title'
						var closeButton = document.createElement('a')
							closeButton.href = 'javascript: void(0)'
							closeButton.innerHTML = 'x'
							closeButton.onclick = function () { maps[id].hide() }
							closeButton.title = 'Скрыть карту'
						var titleText = document.createElement('span')
							titleText.innerHTML = caller.getAttribute('rev')
						map = document.createElement('div')
							map.className = 'map'

					popupTitleBar.appendChild(closeButton)
					popupTitleBar.appendChild(titleText)
				popup.appendChild(popupTitleBar)
				popup.appendChild(map)
				setPos()
				$.getJSON('/ajax/map_objects/', { id: id }, function (resp) { initMap(resp.O[o.id].lat, resp.O[o.id].lng) } )
				document.body.appendChild(popup)
			} }

	this.show =	function () { with (o) {
				active = true
				popup.style.display = 'block'
				caller.title = 'Скрыть карту'
				caller.style.background = '#fff url("/i/map.png") no-repeat center'
			} }

	this.hide =	function () { with (o) {
				active = false
				popup.style.display = 'none'
				caller.title = 'Показать объект на карте'
			} }
	this.setPos =	function () {
				var x = 0, y = 0, el = caller
				if (el.offsetParent) do {
					x += el.offsetLeft
					y += el.offsetTop
				} while (el = el.offsetParent)
				o.popup.style.left = x + 'px'
				o.popup.style.top = y-3 + 'px'
			}

	this.initMap =	function (lat, lng) { with (o) {
				var marker = new GMarker(new GLatLng(lat, lng), { draggable:false })
				map = new GMap2(map, { size: new GSize(240, 180) } )
				with (map) {
					enableScrollWheelZoom()
					enableContinuousZoom()
					disableDoubleClickZoom()
					addControl(new GMapTypeControl())
					addControl(new GSmallZoomControl())
					setCenter(marker.getLatLng(), 15)
					addOverlay(marker)
				}
				show()
			} }

	this.create()

}


$(document).ready( function() { $('span.citymap').click(showCityMap) })

var citymaps = {}

function showCityMap () {
	var cityid = parseInt(this.getAttribute('cityid'), 10)
	var devid = parseInt(this.getAttribute('devid'), 10)
	if (!maps[cityid]) citymaps[cityid] = new CityMap(this, cityid, devid)
	else if (citymaps[cityid].active) citymaps[cityid].hide()
	else citymaps[cityid].show()
}

function CityMap (caller, cityid, devid) {

	var o = this
	o.cityid = cityid
	o.devid = devid
	o.caller = caller
	o.citymap = null
	o.citypopup = null
	o.active =	true

	var bodyheight=document.getElementById("Body").offsetHeight;

	caller.style.background = '#fff url("/i/loader.gif") no-repeat center'

	this.create =	function () { with (o) {

				citypopup = document.createElement('div')
					citypopup.className = 'citypopup'
					var popupTitleBar = document.createElement('div')
					popupTitleBar.className = 'title'
						var closeButton = document.createElement('a')
							closeButton.href = 'javascript: void(0)'
							closeButton.innerHTML = 'x'
							closeButton.onclick = function () { citymaps[cityid].hide() }
							closeButton.title = 'Скрыть карту'
						var titleText = document.createElement('span')
							titleText.innerHTML = 'Объекты компании '+ caller.getAttribute('dev') + ' в ' + caller.getAttribute('sev')
						citymap = document.createElement('div')
							citymap.className = 'map'

					popupTitleBar.appendChild(closeButton)
					popupTitleBar.appendChild(titleText)
				citypopup.appendChild(popupTitleBar)
				citypopup.appendChild(citymap)
				setPos()
				$.getJSON('/ajax/map_objects/', { city_id: cityid, dev_id:devid }, function (resp) { initMap(resp.O) } )
				document.body.appendChild(citypopup)
			} }

	this.show =	function () { with (o) {
				active = true
				citypopup.style.display = 'block'
				caller.title = 'Скрыть карту'
				caller.style.background = '#fff url("/i/map.png") no-repeat center right'
			} }

	this.hide =	function () { with (o) {
				active = false
				citypopup.style.display = 'none'
				caller.title = 'Показать объект на карте'
			} }
	this.setPos =	function () {
				var x = 0, y = 0, el = caller
				if (el.offsetParent) do {
					x += el.offsetLeft
					y += el.offsetTop
				} while (el = el.offsetParent)
				o.citypopup.style.left = x-430 + 'px'

				var gmap=bodyheight-y;

				o.citypopup.style.top = y+(gmap < 100?(gmap-100):-5) + 'px'

			}
	this.message = function () {
					markerId = this.UserData.id
					$.get('/ajax/obj_map_info/', { id: this.UserData.id}, function (response) {markers[markerId].openInfoWindowHtml(response)})
				},


	this.initMap =	function (data) { with (o) {
                var marker;
				var a1=0, a2=0, b1=0, b2=0;

				citymap = new GMap2(citymap, { size: new GSize(550, 480) } )

				for(var counter in data) {
					if(a1 < data[counter].lat || a1==0 ){
						a1 = data[counter].lat;
					}
					if(a2 < data[counter].lng || a2==0 ){
						a2 = data[counter].lng;
					}
					if(b1 > data[counter].lat || b1==0 ){
						b1 = data[counter].lat;
					}
					if(b2 > data[counter].lng || b2==0 ){
						b2 = data[counter].lng;
					}
				}
				with (citymap) {
					enableScrollWheelZoom()
					disableDoubleClickZoom()
					addControl(new GMapTypeControl())

					var a = new GLatLng(b1, b2);
					var b = new GLatLng(a1, a2);
					var bounds = new GLatLngBounds(a, b);
					var zoomlevel = getBoundsZoomLevel(bounds,citymap.getSize());
					if(zoomlevel > 13)	zoomlevel=13
					setCenter(bounds.getCenter(),zoomlevel);

					markers = []
					for(var counter in data) {
						marker = new GMarker(new GLatLng(data[counter].lat, data[counter].lng), { draggable:false })
						markers[counter] = marker
						marker.UserData = { id: counter }
						GEvent.addListener(marker, "click", message)
						addOverlay(marker)
						marker.setImage('/i/gico.png')
					}

				}
				show()
			} }

	this.create()

}