var gmap_uniq_id=1;

function uGmap(elm, lst_elm){

	this.gmap=null;
	this.map_elm=document.getElementById(elm);
	if(lst_elm!='') this.lst_elm=document.getElementById(lst_elm);

	this.landmarks=[]; this.hotels=[]; this.hotels_all=[]; this.hotels_store=[];
	this.loaded=false; this.upd_map=false; this.can_book=false;

	this.oname='map_htl';

	this.is_htl_map=0;

	this.map=null; this.clicked=false;
	this.ht={
		'current':0,
		'prev':0
	};
	this.price_sort_order='desc';

	this.upd_filter=false;

	this.clear_stored=function(){
		this.hotels_store=[];
	};

	this.add_lm=function(lat, lng, id, name, active){
		this.landmarks.push({
			'lat':lat,
			'lng':lng,
			'id':id,
			'name':name,
			'a':active
		});
	};

	this.add=function(lat, lng, bk_num, name, chain, address, img, stars, price, avl, url){
		this.hotels_store.push({
			'lat':lat,
			'lng':lng,
			'bk_num':bk_num,
			'name':name,
			'chain':chain,
			'address':address,
			'img':img,
			'stars':stars,
			'price':price,
			'avl':avl,
			'url':url,
			'hid':url.split('/')[5]
		});
	};

	this.htl_map=function(){
		this.is_htl_map=1;
		this.hotels=this.hotels_store;
	};

	this.make_allhotels=function(){
		this.hotels_all=this.hotels_store;
	};

	// hotels list
	this.draw_list=function(){
		this.hotels=[];
		s=''; n=1;
		for(i=0; i<this.hotels_all.length; i++){
			inf=this.hotels_all[i];
			this.hotels.push(inf);
			this.hotels[(this.hotels.length-1)]['num']=(i+1);
			if(inf.avl){
				price='<b>'+inf.price+'</b>';
				pimg='point_avl.gif';
				//url='javascript:book_htl('+inf.bk_num+');';
			}else{
				price='';
				pimg='point_avl.gif';
				//url=inf.url;
			}

			// список отелей
			s+='<div class=map_lst_hotel>';
			s+='<table cellspacing=0 cellpadding=0 style="margin-left:10px;">';
			s+='<tr><td width=28 onclick="'+this.oname+'.showhtl('+n+');" style="cursor:pointer;"><div align=center style="background:url(/img/gmap/'+pimg+') no-repeat; width:28px; height:32px; font-weight:bold; color:white; padding-top:2px;">'+(i+1)+'</div></td>';
			s+='<td align=left style="padding-left:5px; padding-top:8px;">';
			s+='<a href="javascript:void(0);" onclick="'+this.oname+'.showhtl('+n+');">'+inf.name+'</a><br>';
			s+='<div class="h_stars h_stars_nm"><div class="stars s'+inf.stars+'"></div></div>';
			s+=price;
			s+='</td></tr></table></div>';

			if(i<(this.hotels_all.length-1)) s+='<center><div class=clear_1px style="background-color:#cccccc; width:88%; margin-top:8px;"><img src="/img/spacer.gif"></div></center>';
			n++;
		}
		this.lst_elm.innerHTML=s;

		if(this.loaded) this.show();
	};

	this.showhtl=function(n){
		if(!this.clicked) this.gmap.setZoom(14);
		this.gmap.panTo(this.hotels[(n-1)].gmap_point);

		var parent=this;
		window.setTimeout(function () { parent.hotels[(n-1)].gmap_marker.openInfoWindowHtml(parent.makeInfoWindow((n-1))); }, 10);

		this.clicked=true;
	};

	// make GMap
	this.prev_labels=[];
	this.show=function(){
		preload_img(['/img/gmap/point_avl.gif', 'landmark.gif', 'landmark_na.gif', 'point_empty.gif', '/img/gmap/shadow.png']);

		var setMapMode=false;
		if(this.upd_map) this.gmap=null;

		if(GBrowserIsCompatible()){
			if(this.gmap==null){
				this.gmap=new GMap2(this.map_elm);
				this.gmap.addControl(new GLargeMapControl());
				this.gmap.addControl(new GMapTypeControl());
				this.gmap.enableDoubleClickZoom();
				this.gmap.enableContinuousZoom();

				GEvent.addDomListener(this.map_elm, 'DOMMouseScroll', this.wheelZoom);
				GEvent.addDomListener(this.map_elm, 'mousewheel', this.wheelZoom);

				setMapMode=true;
			}

			// центр карты
			var center_found=false; var is_landmark=false;
			for(i=0; i<this.landmarks.length; i++){
				if(this.landmarks[i].a){
					var point=new GLatLng(this.landmarks[i].lat, this.landmarks[i].lng);
					center_found=true;
					is_landmark=true;
					break;
				}
			}

			if(!center_found || this.is_htl_map)
				for(i in this.hotels){
					var point=new GLatLng(this.hotels[i].lat, this.hotels[i].lng);
					break;
				}

			if(this.is_htl_map || is_landmark) this.gmap.setCenter(point, 14);
			else this.gmap.setCenter(point, 12);

			if(setMapMode) this.gmap.setMapType(G_NORMAL_MAP);
			if(this.upd_map) this.gmap.clearOverlays();

			var baseIcon=new GIcon();
			baseIcon.shadow='/img/gmap/shadow.png';
			baseIcon.iconSize=new GSize(28, 29);
			baseIcon.shadowSize=new GSize(28, 29);
			baseIcon.iconAnchor=new GPoint(9, 34);
			baseIcon.infoWindowAnchor=new GPoint(9, 2);
			baseIcon.infoShadowAnchor=new GPoint(18, 25);

			var icon=new GIcon(baseIcon);
			icon.image='/img/gmap/point_empty.gif';

			// remove all markers
			if(!this.upd_map){
				for(i=0; i<this.prev_labels.length; i++) this.gmap.removeHotelLabel(this.prev_labels[i]);
				this.prev_labels=[];
			}

			// draw landmarks
			for(i=0; i<this.landmarks.length; i++){
				var point=new GLatLng(this.landmarks[i].lat, this.landmarks[i].lng);
				txt='<center><b>'+this.landmarks[i].name+'</b></center>';
				if(this.can_book && !this.is_htl_map && !this.landmarks[i].a) txt+='<p>&nbsp;</p><p><a href="javascript:void(0);" onclick="redirect(\'/search/result?wadv_nearby='+this.landmarks[i].id+'\'); show_waitme(); return false;">'+uGLOBALS.msg.maps_nearby+' '+this.landmarks[i].name+'</a></p>';

				marker=this.createMarker(point, icon, txt);
				this.gmap.addOverlay(marker);

				var label=new THotelLabel();
				label.anchorLatLng=new GLatLng(this.landmarks[i].lat, this.landmarks[i].lng);

				if(this.is_htl_map) label.nid='htl';
				else{
					gmap_uniq_id+=1;
					label.nid=gmap_uniq_id;
				}
				label.num='';
				if(this.landmarks[i].a) label.img='landmark.gif';
				else label.img='landmark_na.gif';
				label.markerOffset=new GSize(20, 20);
				this.gmap.addHotelLabel(label);

				if(!this.is_htl_map && this.landmarks[i].a) marker.openInfoWindowHtml(txt);

				if(!this.upd_map) this.prev_labels.push(label.id);
			}

			// draw hotels
			cs=0; n=1;
			for(i in this.hotels){
				var point=new GLatLng(this.hotels[i].lat, this.hotels[i].lng);

				txt=this.makeInfoWindow(i);

				marker=this.createMarker(point, icon, txt);
				this.hotels[i].gmap_point=point;
				this.hotels[i].gmap_marker=marker;

				this.gmap.addOverlay(marker);

				var label=new THotelLabel();
				label.anchorLatLng=new GLatLng(this.hotels[i].lat, this.hotels[i].lng);

				if(this.hotels[i].avl) label.img='point_avl.gif';
				else label.img='point_avl.gif';

				if(this.hotels[i].num==undefined) this.hotels[i].num='';
				label.num=this.hotels[i].num;
				label.markerOffset=new GSize(20, 20);
				this.gmap.addHotelLabel(label);

				if(!this.upd_map) this.prev_labels.push(label.id);

				n++;
			}

			if(this.is_htl_map) this.showhtl(1);
		}else{
			ShowAlert(uGLOBALS.msg.maps_not_supported);
			window.location=back(-1);
		}
	};

	// окно с информацией об отеле
	this.makeInfoWindow=function(i){
		var book_txt='';
		var onclick='';
		if(this.hotels[i].avl && this.can_book){
			url='javascript:void(0);';
			//onclick=' onclick="wzSubmit('+this.hotels[i].bk_num+', \'htl\'); return true;"';
			book_txt=' | <a href="javascript:wzSubmit('+this.hotels[i].hid+', \'htl\');">'+uGLOBALS.msg.maps_book+'</a>';
		}else
			url=this.hotels[i].url;

		if(this.hotels[i].avl==2){
			var txt='<div style="padding-bottom:5px;"><b>'+this.hotels[i].name+'</b></div>';
			txt+='<div style="padding-top:3px;">'+uGLOBALS.msg.maps_address+': '+this.hotels[i].address+'</div>';
		}else{
			var txt='<div style="padding-bottom:5px;"><a href="'+url+'"'+onclick+' style="color:black; font-size:12px;"><b>'+this.hotels[i].name+'</b></a></div>';
			txt+='<table width=350 cellpadding=0 cellspacing=0 border=0>';
			txt+='<tr><td width=70><div style="height:64px; width:64px; background:url('+this.hotels[i].img+') center no-repeat;"></div></td>';
			txt+='<td valign=top style="padding-left:5px;">';

			// draw stars
			txt+='<div class="h_stars h_stars_nm"><div class="stars s'+this.hotels[i].stars+'"></div></div>';

			txt+='<div style="padding-bottom:2px;">';
			if(this.hotels[i].avl) txt+='<b>'+uGLOBALS.msg.maps_price_starts+': '+this.hotels[i].price+'</b>';
			//else txt+='<b>Отель недоступен.</b>';
			txt+='</div>';

			txt+='<div style="padding-top:3px;">'+uGLOBALS.msg.maps_address+': '+this.hotels[i].address+'</div>';
			txt+='<div style="padding-top:3px;"><a href="'+this.hotels[i].url+'">'+uGLOBALS.msg.maps_hotel_info+'</a>'+book_txt+'</div>';
			txt+='</td></tr></table>';
		}

		return txt;
	};

	this.wheelZoom=function(a){
		//if ((a.detail || -a.wheelDelta)<0) this.gmap.zoomIn();
		//else this.gmap.zoomOut();
	};

	this.createMarker=function(point, icon, html){
		var marker=new GMarker(point, icon);
		GEvent.addListener(marker, 'click',
			function(){
				marker.openInfoWindowHtml(html);
			}
		);
		return marker;
	};
}

// add hotel point
function THotelLabel(){}
THotelLabel.prototype.initialize=function(a){
	this.parentMap=a;
	var b=document.createElement('div');
	if(this.nid) this.id='gmap-lpoint-'+this.nid;
	else this.id='gmap-hpoint-'+this.num;

	b.setAttribute('id', this.id);
	if(this.className!==undefined){
		if(this.num!==undefined)
			b.innerHTML='<div class="io_sf_gmap_marker io_sf_map_marker '+this.className+'"><div class="io_sf_map_marker_num">'+this.num+'</div></div>';
		else
			b.innerHTML='<div class="io_sf_gmap_marker io_sf_map_marker '+this.className+'"></div>';
	}else
		b.innerHTML='<center><div style="position:absolute; padding-top:2px; width:28px; font-weight:bold; color:white;">'+this.num+'</div><img src="/img/gmap/'+this.img+'"></center>';
	document.body.appendChild(b);
	b.style.position='absolute';

	this.mapTray=a.getPane(G_MAP_MAP_PANE);
	this.mapTray.appendChild(b);
	if(!this.markerOffset) this.markerOffset=new GSize(0,0);
	this.setPosition();

	GEvent.bind(a, 'zoomend', this, function(){this.setPosition()});
	GEvent.bind(a, 'moveend', this, function(){this.setPosition()});
}
THotelLabel.prototype.setPosition=function(a){
	if(a){this.anchorLatLng=a;}
	var b=this.parentMap.fromLatLngToDivPixel(this.anchorLatLng);
	var x=parseInt(b.x);
	var y=parseInt(b.y);
	if(d=document.getElementById(this.id)){
		y-=14; x+=11; // point image half-size
		d.style.left=x-this.markerOffset.width+'px';
		d.style.top=y-this.markerOffset.height+'px';
	}
}
GMap2.prototype.addHotelLabel=function(a){
	a.initialize(this);
}
GMap2.prototype.removeHotelLabel=function(a){
	var b=document.getElementById(a);
	this.getPane(G_MAP_MAP_PANE).removeChild(b);
	delete(b);
}
GMap2.prototype.setLabelZIndex=function(a, i){
	var b=document.getElementById(a);
	b.style.zIndex=i;
}

var htl_bad_coord_cur=0;
function htl_bad_coord(id){
	if(!confirm(uGLOBALS.msg.bad_coord_msg_q)) return false;

	if(id==undefined) id=htl_bad_coord_cur;

	xLoad=new oXMLload();
	xLoad.get('/xml/bad_htl_coord/'+id);
	alert(uGLOBALS.msg.bad_coord_msg);
}
