// A touchinfohoz: �sszevont ikonra kattintva nagyitunk
var	clusterSizeX = [0.001,0.002,0.008,0.016,0.032,0.064,0.128,0.256,0.512,1.024,10];
var	clusterSizeY = [0.001,0.002,0.008,0.016,0.032,0.064,0.128,0.256,0.512,1.024,10];
var Partner_PartnerDetailData = new Object;
var Event_EventDetailData = new Object;
var Cms_ContentDetailData = new Object;
var CurrentMarker = null;
var map;
var mapHandler = {
	tplBoundaryRegex: /(^|.|\r|\n)((?:%%|#\{)\s*(\w+)\s*(?:%|\}))/,
	batchInfoPrototypeTPL: null,
	partnerInfoPrototypeTPL: null,
	eventInfoPrototypeTPL: null,
	contentInfoPrototypeTPL: null,
	map: null,
	firstShow: true,
	prefilter: new Array,
	control: null,
	icons: null,
	baseIcon: null,
	objects: null,
	types: null,
	fix: false,
	typeFilters: null,
	filters: null,
	markerManager: null,
	clusterSizeX: [0.001,0.002,0.008,0.016,0.032,0.064,0.128,0.256,0.512,1.024,10],
	clusterSizeY: [0.001,0.002,0.008,0.016,0.032,0.064,0.128,0.256,0.512,1.024,10],
	locked: false,
	kiosk: false,
	points: null,

	addObjects: function(key,data) {
		if(!data) {
			data = new Array();
		}
		if(!this.objects) {
			this.objects = new Object;
		}
		if(!this.types) {
			this.types = new Array;
		}
		if(!this.filters) {
			this.filters = new Object;
		}
		this.objects[key] = data;
		this.types.push(key);
	},

	addPoint: function(x,y,txt) {
		if(!this.points) {
			this.points = new Array();
		}
		this.points.push({'gpsx': x, 'gpsy': y, 'text': txt});
	},

	init: function(key,x,y,zoom,kiosk,fix) {

		this.kiosk=kiosk;
		this.fix = fix;
		if ( $(key) != null && $(key).select('div.batchInfoTemplate').length>0 ) {
			var markerInfoPrototypeHTML = $(key).select('div.batchInfoTemplate')[0];
			markerInfoPrototypeHTML.remove();
			this.batchInfoPrototypeTPL = new Template( markerInfoPrototypeHTML.innerHTML, this.tplBoundaryRegex );
		}
		if ( $(key) != null && $(key).select('div.partnerInfoTemplate').length>0 ) {
			var markerInfoPrototypeHTML = $(key).select('div.partnerInfoTemplate')[0];
			markerInfoPrototypeHTML.remove();
			this.partnerInfoPrototypeTPL = new Template( markerInfoPrototypeHTML.innerHTML, this.tplBoundaryRegex );
		}
		if ( $(key) != null && $(key).select('div.eventInfoTemplate').length>0 ) {
			var markerInfoPrototypeHTML = $(key).select('div.eventInfoTemplate')[0];
			markerInfoPrototypeHTML.remove();
			this.eventInfoPrototypeTPL = new Template( markerInfoPrototypeHTML.innerHTML, this.tplBoundaryRegex );
		}
		if ( $(key) != null && $(key).select('div.contentInfoTemplate').length>0 ) {
			var markerInfoPrototypeHTML = $(key).select('div.contentInfoTemplate')[0];
			markerInfoPrototypeHTML.remove();
			this.contentInfoPrototypeTPL = new Template( markerInfoPrototypeHTML.innerHTML, this.tplBoundaryRegex );
		}
		if (GBrowserIsCompatible()&&!this.map) {
			this.map = new GMap2(document.getElementById(key));
			//this.map.addMapType(G_SATELLITE_3D_MAP);
			this.map.enableDragging();
			//this.map.enableDoubleClickZoom();
			this.map.enableContinuousZoom();
			//this.map.enableScrollWheelZoom();
			this.geocoder = new GClientGeocoder();


			this.control = new GMapTypeControl();
			var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
			var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
			this.map.addControl(this.control, topRight);

			GEvent.addListener(this.map, "dblclick", function() {
				if(this.map) {
					this.map.removeControl(this.control);
					this.map.addControl(new GMapTypeControl(), bottomRight);
				}
			});
			if(!this.kiosk) {
				this.map.addControl(new GLargeMapControl3D());
			}
			this.map.addControl(new GOverviewMapControl(new GSize(150,150)));
			this.map.setCenter(new GLatLng(x,y), zoom);
			if(this.kiosk) {
				map_center();
			}

			this.icons = new Object;
			this.icons.items = this.createIcon('items',true);

			this.icons['free'] = this.createIcon('free');
			this.icons['reserved'] = this.createIcon('reserved');

			this.icons['A.002.005'] = this.createIcon('A.002.005');
			this.icons['A.002.025'] = this.createIcon('A.002.025');
			this.icons['A.002.035'] = this.createIcon('A.002.035');
			this.icons['A.002.050'] = this.createIcon('A.002.050');
			this.icons['A.002.065'] = this.createIcon('A.002.065');
			this.icons['A.002.070'] = this.createIcon('A.002.070');
			this.icons['A.002.075'] = this.createIcon('A.002.075');
			this.icons['A.002.080'] = this.createIcon('A.002.080');
			this.icons['A.002.085'] = this.createIcon('A.002.085');
			this.icons['A.002.090'] = this.createIcon('A.002.090');

			this.icons['R.001.065'] = this.createIcon('R.001.065');
			this.icons['R.001.069'] = this.createIcon('R.001.069');
			this.icons['R.001.050'] = this.createIcon('R.001.050');
			this.icons['R.001.150'] = this.createIcon('R.001.150');
			this.icons['R.001.053'] = this.createIcon('R.001.053');
			this.icons['R.001.100'] = this.createIcon('R.001.100');
			this.icons['R.001.005'] = this.createIcon('R.001.005');
			this.icons['R.001.040'] = this.createIcon('R.001.040');
			this.icons['R.001.170'] = this.createIcon('R.001.170');
			this.icons['R.001.175'] = this.createIcon('R.001.175');

			this.icons['E.002.000'] = this.createIcon('E.002.000');
			this.icons['gasztronomia'] = this.createIcon('gasztronomia');
			this.icons['page'] = this.createIcon('page');
			this.icons['offer'] = this.createIcon('offer');

			this.icons['point_hu'] = this.createIcon('point_hu',true);
			this.icons['point_en'] = this.createIcon('point_en',true);
			this.icons['point_de'] = this.createIcon('point_de',true);


			setTimeout('mapHandler.start()',0);
			map = this.map;
		}


	},

	lock: function() {
		this.locked = true;
		document.body.style.cursor='wait';
	},

	unlock: function() {
		this.locked = false;
		document.body.style.cursor='default';
	},

	start: function() {
		this.lock();
		this.typefilters = new Object;
		for(var i=0;i<this.types.length;i++) {
			var filter = this.types[i]=='STD' ? 'categories' : 'types';
			this.typefilters[this.types[i]] = new Array;
			for(j=0;j<this.objects[this.types[i]].length;j++) {
				if(this.objects[this.types[i]][j].gpsx>0 && this.objects[this.types[i]][j].gpsy>0 && this.objects[this.types[i]][j][filter]) {
					if (!this.objects[this.types[i]][j][filter].length) {
						switch (this.types[i]) {
							case 'EVT':
								this.objects[this.types[i]][j][filter].push('E.000.000.000.000');
								break;
						}
					}
					for(var k=0;k<this.objects[this.types[i]][j][filter].length;k++) {
						if(this.typefilters[this.types[i]].indexOf(this.objects[this.types[i]][j][filter][k])==-1) {
							this.typefilters[this.types[i]].push(this.objects[this.types[i]][j][filter][k]);
						}
					}
				}
			}
			this.showTypeFilter(this.types[i]);
		}
		setTimeout('mapHandler.filter()',0);
		setTimeout('mapHandler.hideGoogle()',100);
	},

	showTypeFilter: function(type) {
		if(!$('map_menu_ext_'+type)) return;
		if(type=='PAGE' || type=='OFFER') return;
		var buff = new StringBuffer();
		var fd = $('map_menu_ext_'+type).innerHTML;
		if(this.typefilters[type].length>0) {
			buff.append('<table cellspacing="0" cellpadding="0" border="0" class="exp_list">');
			for(var j=0;j<this.typefilters[type].length;j++) {
				var tin = tinHandler.getTin(type,this.typefilters[type][j]);
				buff.append('<tr><td class="img"><a href="#" onclick="mapHandler.clickFilter(\'');
				buff.append(type);
				buff.append('\',\'');
				buff.append(this.typefilters[type][j]);
				buff.append('\');return false;"');
				buff.append('><img id="filter_icon_');
				buff.append(this.typefilters[type][j]);
				buff.append('" src="/map/img/');
				buff.append(this.getIconImg(type,this.typefilters[type][j]));
				buff.append('_small.gif" alt="#" border="0"/></a></td>');
				buff.append('<td><a href="#" onclick="mapHandler.clickFilter(\'');
				buff.append(type);
				buff.append('\',\'');
				buff.append(this.typefilters[type][j]);
				buff.append('\');return false;"');
				buff.append('>');
				buff.append(tin ? tin.name : this.typefilters[type][j]);
				buff.append('</a></td>');
				buff.append('<td><input id="filter_');
				buff.append(type);
				buff.append('_');
				buff.append(this.typefilters[type][j]);
				buff.append('" type="checkbox" name="filter[]" value="checkbox" onclick="mapHandler.clickFilter(\'');
				buff.append(type);
				buff.append('\',\'');
				buff.append(this.typefilters[type][j]);
				buff.append('\',true);"');
				if(type=='STD') {
					buff.append(' checked="checked"');
				}
				buff.append(' /></td></tr>');
//				buff.append('" type="checkbox" name="filter[]" value="checkbox" onclick="return mapHandler.switchSubFilter();" /></a></td></tr>');
			}
			buff.append('</table>');
		}
		buff.append(fd);
		$('map_menu_ext_'+type).innerHTML = buff.toString();
	},

	clickFilter: function(base,type,cb) {
		if(this.kiosk) {

			for(var i=0;i<this.types.length;i++) {
				var ftype = this.types[i]=='free' ? 'free' : this.types[i];
				if($('filter_'+this.types[i]+'_'+ftype)) {
					if(this.types[i]!=base||type!=ftype) {
						$('filter_'+this.types[i]+'_'+ftype).checked = false;
					}
					else {
						$('filter_'+this.types[i]+'_'+ftype).checked = true;
					}
					$('filter_icon_'+this.types[i]).src = '/map/img/'+this.getIconImg(this.types[i],ftype)+'_small'+($('filter_'+this.types[i]+'_'+ftype).checked ? '' : '_grey')+'.gif';
				}
			}
			if('STD'!=base||'free'!=type) {
				$('filter_STD_free').checked = false;
			}
			else {
				$('filter_STD_free').checked = true;
			}
			$('filter_icon_free').src = '/map/img/'+this.getIconImg('STD','STD')+'_small'+($('filter_STD_free').checked ? '' : '_grey')+'.gif';
			__pf = new Array;

			/*
			__pf = new Array;
			if($('filter_'+base+'_'+type).checked && base=='STD') {
				var ftype = type=='free' ? 'STD' : 'free';
				$('filter_'+base+'_'+ftype).checked = false;
				$('filter_icon_'+ftype).src = '/map/img/'+this.getIconImg(base,ftype)+'_small'+($('filter_'+base+'_'+ftype).checked ? '' : '_grey')+'.gif';
			}
			*/
			this.switchSubFilter();
		}
		else {
			if(!cb) {
				$('filter_'+base+'_'+type).checked = !$('filter_'+base+'_'+type).checked;
			}
			$('filter_icon_'+type).src = '/map/img/'+this.getIconImg(base,type)+'_small'+($('filter_'+base+'_'+type).checked ? '' : '_grey')+'.gif';
			if($('map_filter_'+base) && $('filter_'+base+'_'+type).checked) {
				$('map_filter_'+base).checked = true;

			}
			this.switchSubFilter();
		}
	},

	showAll: function(type,force) {
		if(force || ! this.locked) {
			this.lock();
			for(var j=0;j<this.typefilters[type].length;j++) {
				$('filter_'+type+'_'+this.typefilters[type][j]).checked = true;
				$('filter_icon_'+this.typefilters[type][j]).src = '/map/img/'+this.getIconImg(type,this.typefilters[type][j])+'_small.gif';
			}
			$('map_filter_'+type).checked = true;
			setTimeout('mapHandler.filter()',0);
		}
	},

	hideAll: function(type,force) {
		if(force || ! this.locked) {
			this.lock();
			for(var j=0;j<this.typefilters[type].length;j++) {
				$('filter_'+type+'_'+this.typefilters[type][j]).checked = false;
				$('filter_icon_'+this.typefilters[type][j]).src = '/map/img/'+this.getIconImg(type,this.typefilters[type][j])+'_small_grey.gif';
			}
			setTimeout('mapHandler.filter()',0);
		}
	},

	switchFilter: function(type) {
		if(!this.locked) {
			this.lock();
			if($('map_filter_'+type).checked) {
				for(var j=0;j<this.typefilters[type].length;j++) {
					$('filter_'+type+'_'+this.typefilters[type][j]).checked = true;
					$('filter_icon_'+this.typefilters[type][j]).src = '/map/img/'+this.getIconImg(type,this.typefilters[type][j])+'_small.gif';
				}
				this.openFilter(type);
			}
			else {
				for(var j=0;j<this.typefilters[type].length;j++) {
					$('filter_'+type+'_'+this.typefilters[type][j]).checked = false;
					$('filter_icon_'+this.typefilters[type][j]).src = '/map/img/'+this.getIconImg(type,this.typefilters[type][j])+'_small_grey.gif';
				}
				this.closeFilter(type);
			}
			setTimeout('mapHandler.filter()',0);
			return true;
		}
		return false;
	},
	switchAutoZoom: function(type) {
		if(!this.locked) {
			if($('auto_zoom').checked) {
				this.lock();
				setTimeout('mapHandler.filter()',0);
			}
			return true;
		}
		return false;
	},

	switchSubFilter: function() {
		if(!this.locked) {
			this.lock();
			setTimeout('mapHandler.filter()',0);
			return true;
		}
		return false;
	},

	createIcon: function(img,large) {
		var icon = new GIcon();
		icon.image = '/map/img/'+img+'.png';
		if(large) {
			icon.shadow = '/map/img/shadow_large.png';
			icon.iconSize = new GSize(50.0, 33.0);
			icon.shadowSize = new GSize(67.0, 33.0);
			icon.iconAnchor = new GPoint(25.0, 16.0);
			icon.infoWindowAnchor = new GPoint(25.0, 16.0);
		}
		else {
			icon.shadow = '/map/img/shadow_single.png';
			icon.iconSize = new GSize(28.0, 33.0);
			icon.shadowSize = new GSize(45.0, 33.0);
			icon.iconAnchor = new GPoint(24, 32);
			icon.infoWindowAnchor = new GPoint(14.0, 16.0);
		}
		return icon;

	},

	load: function() {
		loadScript('/index.php?page=Map_MapPage&event=loadObjects','maphandler_dataset','mapHandler.filter()');
	},

	hideGoogle: function() {
		if($('kioskMap')) {
			var as = $('kioskMap').getElementsByClassName('gmnoprint');
			var h = 0;
			for(var i=0;i<as.length;i++) {
				if(as[i].target=='_blank') {
					as[i].hide();
					h++;
				}
				if(as[i].childNodes.length==1) {
					if(as[i].childNodes[0].target=='_blank') {
						as[i].childNodes[0].onclick = function() {
							return false;
						}
						h++;
					}
				}
			}


			if(h<2) {
				setTimeout('mapHandler.hideGoogle()',100);
				return;
			}
		}
	},

	frontStartPoint: function() {
		if($('kioskMap')&&this.points) {
			var as = $('kioskMap').select('img[src="/map/img/point_'+__lang+'.png"]');
			if(as.length==0) {
				setTimeout('mapHandler.frontStartPoint()',100);
			}
			else {
				for(var i=0;i<as.length;i++) {
					as[i].style.zIndex = 100;
				}
			}
		}
	},

	filter: function() {


		var objects = new Array;
		var summary = new Object;
		var oids = new Array;

		if(this.kiosk) {
			summary.free = 0;
		}
		// Filter map object types
		for(var i=0;i<this.types.length;i++) {

			var filter = this.types[i]=='STD' ? 'categories' : 'types';
			summary[this.types[i]] = 0;
			// With valid gpsx and gpsy
			var cntcache = new Array;
			for(j=0;j<this.objects[this.types[i]].length;j++) {
				if(this.objects[this.types[i]][j].gpsx>0 && this.objects[this.types[i]][j].gpsy>0) {
					var ct = !this.textfilter;
					if(this.textfilter&&this.prefilter.length>0) {
						ct = this.prefilter.indexOf(this.objects[this.types[i]][j].id)>-1;
					}
					if(this.kiosk) {
						if(__pf.length>0 ) {
							ct = __pf.indexOf(this.objects[this.types[i]][j].id)>-1;
							if(ct) {
								if(oids.indexOf(this.objects[this.types[i]][j].id)==-1) {
									oids.push(this.objects[this.types[i]][j].id);
									this.objects[this.types[i]][j].code = this.types[i];
									objects.push(this.objects[this.types[i]][j]);
								}
							}
						}
						else {
							if(this.types[i]=='PAGE' || this.types[i]=='STD' || this.types[i]=='GAST' || this.types[i]=='EVT') {
								var free = this.types[i] == 'STD' ? 'free' : this.types[i];
								if( this.types[i]=='EVT'||(this.types[i]!='PAGE'&&this.objects[this.types[i]][j].kiosk==1) || this.objects[this.types[i]][j].tags.indexOf("KIOSK")>-1 ) {
									if(cntcache.indexOf(this.objects[this.types[i]][j].id)==-1) {
										summary[this.types[i]]++;
										cntcache.push(this.objects[this.types[i]][j].id);
									}
									if(this.objects[this.types[i]][j].lastMinRooms>0) {
										summary.free++;
									}
									if(ct && ( $('filter_'+this.types[i]+'_'+this.types[i]).checked || $('filter_'+this.types[i]+'_'+free).checked)) {
										if(this.types[i] == 'STD') {
											if($('filter_'+this.types[i]+'_'+free).checked) {
												if(this.objects[this.types[i]][j].lastMinRooms>0) {
													if(oids.indexOf(this.objects[this.types[i]][j].id)==-1) {
														this.objects[this.types[i]][j].code = this.types[i];
														oids.push(this.objects[this.types[i]][j].id);
														objects.push(this.objects[this.types[i]][j]);
													}
												}
											}
											else {
												if(oids.indexOf(this.objects[this.types[i]][j].id)==-1) {
													this.objects[this.types[i]][j].code = this.types[i];
													oids.push(this.objects[this.types[i]][j].id);
													objects.push(this.objects[this.types[i]][j]);
												}
											}
										}
										else {
											if(oids.indexOf(this.objects[this.types[i]][j].id)==-1) {
												this.objects[this.types[i]][j].code = this.types[i];
												oids.push(this.objects[this.types[i]][j].id);
												objects.push(this.objects[this.types[i]][j]);
											}
										}

									}
								}
							}
						}
					}
					else {
						summary[this.types[i]]++;
						if(this.types[i]=='PAGE' || this.types[i]=='OFFER') {
							if(ct && $('filter_'+this.types[i]+'_'+this.types[i]).checked) {
//		    					summary[this.types[i]]++;
								this.objects[this.types[i]][j].code = this.types[i];
								objects.push(this.objects[this.types[i]][j]);
							}
						}
						else {
							//summary[this.types[i]]++;
							if(ct && this.objects[this.types[i]][j][filter] && this.objects[this.types[i]][j][filter].length>0) {
								var cb = false;
								for(var k=0;k<this.objects[this.types[i]][j][filter].length;k++) {
									if($('filter_'+this.types[i]+'_'+this.objects[this.types[i]][j][filter][k]).checked) {
										cb =true;
										break;
									}
								}
								// Show on map;
								if(cb) {
//			    					summary[this.types[i]]++;
									if($('map_filter_'+this.types[i]).checked) {
										if(oids.indexOf(this.objects[this.types[i]][j].id)==-1) {
											oids.push(this.objects[this.types[i]][j].id);
											this.objects[this.types[i]][j].code = this.types[i];
											objects.push(this.objects[this.types[i]][j]);
										}
									}
								}
							}
						}
					}
				}
			}
		}

		this.showSummary(summary);

		this.clusters = new Array;
		this.clusters.push(objects);
		for(var i=0;i<11;i++) {
			//this.clusters.push(this.cluster(this.clusters[i],Math.pow(2,i)*0.0025));
			//var t0 = getTime();
			this.clusters.push(this.cluster(objects,i));
		}
		this.showObjects(this.clusters);
	},
	search: function() {
		var txt = $('map_search').value;
		if(!this.locked) {
			this.lock();
			this.prefilter = new Array();
			this.textfilter = txt.length>0 ? txt : null;
			if(txt.length>0) {
				$('map_search').value = '';
				//$('map_clear_search').style.display='';
				var reg = new RegExp(txt, "i");
				for(var i=0;i<this.types.length;i++) {
					for(j=0;j<this.objects[this.types[i]].length;j++) {
						if( this.objects[this.types[i]][j].name.search(reg)>-1 ) {
							this.prefilter.push(this.objects[this.types[i]][j].id);
						}
						else if( this.objects[this.types[i]][j].info.search(reg)>-1 ) {
							this.prefilter.push(this.objects[this.types[i]][j].id);
						}
					}
				}


				for(var i=0;i<this.types.length;i++) {
					for(var j=0;j<this.typefilters[this.types[i]].length;j++) {
						$('filter_'+this.types[i]+'_'+this.typefilters[this.types[i]][j]).checked = true;
						$('filter_icon_'+this.typefilters[this.types[i]][j]).src = '/map/img/'+this.getIconImg(this.types[i],this.typefilters[this.types[i]][j])+'_small.gif';
					}
					if($('map_filter_'+this.types[i])) {
						$('map_filter_'+this.types[i]).checked = true
						this.closeFilter(this.types[i]);
					}
				}


			}
			else {
				//$('map_clear_search').style.display='none';
			}
			setTimeout('mapHandler.filter()',0);
		}
	},
	clearSearch: function() {
		if(!this.locked) {
			this.lock();
			this.prefilter = new Array();
			this.textfilter = null;
			//$('map_clear_search').style.display='none';
			$('map_search').value = '';
			setTimeout('mapHandler.filter()',0);
		}
	},
	showObjects: function(clusters) {
		if(!this.markerManager) {
			this.markerManager = new MarkerManager(this.map);
		}
		else {
			this.markerManager.clearMarkers();
		}
		for(var i=0;i<clusters.length-1;i++) {
			this.markerManager.addMarkers(this.getItems(clusters[i]),17-i,17-i);
		}
		if(this.points) {
			if(this.points.length>0) {
				this.markerManager.addMarkers(this.getPoints(),0,17);
			}
		}
		this.markerManager.addMarkers(this.getItems(clusters[clusters.length-1]),0,18-clusters.length);


		var az = !this.kiosk || __pf.length>0;
		if($('auto_zoom')) {
			az = $('auto_zoom').checked;
		}
		if(this.firstShow && this.fix) {
			az = false;
			this.firstShow = false;
		}


		if(az) {

			var maxx = -180;
			var maxy = -90;
			var minx = 180;
			var miny = 90;

			for(var i=0;i<clusters[0].length;i++) {
				if(clusters[0][i].gpsx>0) {
					maxx = Math.max(clusters[0][i].gpsx,maxx);
					minx = Math.min(clusters[0][i].gpsx,minx);
				}
				if(clusters[0][i].gpsy>0) {
					maxy = Math.max(clusters[0][i].gpsy,maxy);
					miny = Math.min(clusters[0][i].gpsy,miny);
				}
			}

			var xo = (minx+maxx) / 2;
			var yo = (miny+maxy) / 2;
			var dx = xo-minx;
			var dy = yo-miny;

			for(var i=0;i<this.clusterSizeX.length;i++) {
				if(this.clusterSizeX[i]>=dx) {
					break;
				}
			}
			var zoomx = Math.max(0,i);
			for(var i=0;i<this.clusterSizeY.length;i++) {
				if(this.clusterSizeY[i]>=dy) {
					break;
				}
			}
			var zoomy = Math.max(0,i);
			var zoom = Math.min(14,18-Math.max(zoomx,zoomy));

			if(yo>0 && xo>0) {
				this.map.setCenter(new GLatLng(yo,xo), zoom);
			}
		}

		this.markerManager.refresh();
		this.frontStartPoint();
		this.unlock();
	},
	autoZoom: function() {

	},

	getItems: function(batch) {
		var items = new Array;
		for(var i=0;i<batch.length;i++) {
			//var o = new GMarker( new GLatLng(batch[i].gpsy,batch[i].gpsx));
			//var o = null;
			//if(batch[])
			var o = this.createMarker(batch[i],i);
			items.push(o);
		}
		return items;
	},
	getPoints: function() {
		var items = new Array;
		for(var i=0;i<this.points.length;i++) {
			var latlng = new GLatLng(this.points[i].gpsy,this.points[i].gpsx);
			var opts = {
					"icon": this.icons['point_'+__lang],
					"clickable": true,
					"draggable": false
			};
			var marker2 = new LabeledMarker(latlng,opts);
			items.push(marker2);
		}
		return items;
	},
	showSummary: function(summary) {
		var all = 0;
		for(var i=0;i<this.types.length;i++) {
			if($('map_menu_summary_'+this.types[i])) {
				$('map_menu_summary_'+this.types[i]).innerHTML= (summary[this.types[i]] ? summary[this.types[i]] : (this.kiosk ? '' : '&nbsp;'));
				all += summary[this.types[i]] ? summary[this.types[i]] : 0;
			}
		}
		if(this.kiosk) {
			if($('map_menu_summary_free')) {
				$('map_menu_summary_free').innerHTML= summary.free ? summary.free : '';
			}
		}
		if($('map_filter_value')) {
			$('map_filter_value').innerHTML = (this.textfilter ? jsLabels.txtSearch+': <span class="filter">'+this.textfilter+'</span>' : jsLabels.txtAll)+' - <span>'+all+'</span> '+(all==1 ? jsLabels.txtHit : jsLabels.txtHits );
		}
	},
	openFilter: function(id) {
		for(var i=0;i<this.types.length;i++) {
			if(this.types[i]!=id) {
				if($('map_menu_ext_'+this.types[i])) {
					$('map_menu_ext_'+this.types[i]).style.display='none';
				}
			}
		}
		$('map_menu_ext_'+id).style.display='';
	},
	closeFilter: function(id) {
		$('map_menu_ext_'+id).style.display='none';
	},

	distance: function(fromx,fromy,tox,toy) {
		return 1;
		//var rr = Math.pow(fromx-tox,2)+Math.pow(fromy-toy,2);
		//return Math.sqrt(rr);
	},
	cluster: function(objects,d) {
		var clusters = new Array;
		for(var i=0;i<objects.length;i++) {
			var x = Math.round(objects[i].gpsx/this.clusterSizeX[d]);
			var y = Math.round(objects[i].gpsy/this.clusterSizeY[d]);
			var idx = -1;
			for(var j=0;j<clusters.length;j++) {
				//this.distance( clusters[j].gpsx,clusters[j].gpsy,objects[i].gpsx,objects[i].gpsy)
				if(clusters[j].gpsx==x && clusters[j].gpsy==y) {
					idx = j;
					break;
				}
			}
			if(idx<0) {
				idx = clusters.length;
				var c = new Object;
				c.code = 'cluster';
				c.items = new Array;
				clusters.push(c);
				c.gpsx = x;
				c.gpsy = y;
				if(this.kiosk&&objects[i].code=='STD') {
					if(objects[i].lastMinRooms>0) {
						c.lastMinRooms = objects[i].lastMinRooms;
						c.lastMinBeds = objects[i].lastMinBeds;
						c.lastMinPlusBeds = objects[i].lastMinPlusBeds;
					}
					else {
						c.lastMinRooms = 0;
						c.lastMinBeds = 0;
						c.lastMinPlusBeds = 0;
					}
				}
			}
			else {
				if(this.kiosk && objects[i].lastMinRooms>0&&objects[i].code=='STD') {
					clusters[idx].lastMinRooms += objects[i].lastMinRooms;
					clusters[idx].lastMinBeds += objects[i].lastMinBeds;
					clusters[idx].lastMinPlusBeds += objects[i].lastMinPlusBeds;
				}
			}
			//if(objects[i].code=='cluster') {
				//for(var k=0;k<objects[i].items.length;k++) {
					//clusters[idx].items.push(objects[i].items[k]);
				//}
			//}
			//else {
			clusters[idx].items.push(objects[i]);
			//}
			//clusters[idx].gpsx = this.calccoord(clusters[idx].items,'gpsx');
			//clusters[idx].gpsy = this.calccoord(clusters[idx].items,'gpsy');
		}
		for(var j=0;j<clusters.length;j++) {
			clusters[j].gpsx = this.calccoord(clusters[j].items,'gpsx');
			clusters[j].gpsy = this.calccoord(clusters[j].items,'gpsy');
		}
		return clusters;
	},
	calccoord: function(items,idx) {

		return items[0][idx];
		var c = 0;
		var n = 0;
		for(var i=0;i<items.length;i++) {
			if(items[i][idx]>0) {
				c = c + items[i][idx];
				n++;
			}
		}
		return c / n;
	},
	createMarker: function(batch,i) {
		var latlng = new GLatLng(batch.gpsy,batch.gpsx);
		var opts = {
				"icon": this.getIcon(batch),
				"clickable": true,
				"draggable": false
		};
		var marker = null;
		if(batch.items && batch.items.length>1) {
			opts.labelText = '<div style="color: #fff;cursor: pointer;"><b>'+batch.items.length+'</b>';
			if(this.kiosk&&batch.lastMinRooms>0) {
				opts.labelText+= '<div style="border:1px solid;background-color:#fff;padding:2px;position: relative;left:32px;top:-21px;color: #007F00;cursor: pointer;font-size: 10px;line-height:10px;"><b>'+batch.lastMinRooms+'&nbsp;'+jsLabels.txtPartnerRooms+'</b><br/>'+batch.lastMinBeds+(batch.lastMinPlusBeds ? '+'+batch.lastMinPlusBeds : '')+'&nbsp;'+jsLabels.txtPartnerBeds+'</div>';
			}
			opts.labelText+= '</div>';
			opts.labelOffset = new GSize(-4, -10);
			marker = new LabeledMarker(latlng,opts);
		}
		else {
			if(this.kiosk&&batch.lastMinRooms>0) {
				opts.labelText= '<div style="border:1px solid;background-color:#fff;padding:2px;color: #007F00;cursor: pointer;font-size: 10px;line-height:10px;"><b>'+batch.lastMinRooms+'&nbsp;'+jsLabels.txtPartnerRooms+'</b><br/>'+batch.lastMinBeds+(batch.lastMinPlusBeds ? '+'+batch.lastMinPlusBeds : '')+'&nbsp;'+jsLabels.txtPartnerBeds+'</div>';
				opts.labelOffset = new GSize(7, -31);
				marker = new LabeledMarker(latlng,opts);
			}
			else {
				marker = new GMarker(latlng,opts);
			}
			//var name = batch.items && batch.items.length>0 ? batch.items[0].name : batch.name;
			//opts.labelText = '<div style="text-align: center;font-size: 9px;border: 1px solid #777; padding: 1px; background: lightyellow; color: #00007f; width: 150px;">'+name+'</div>';
			//opts.labelText = '<div style="font-size: 9px;color: #000; width: 180px;background: #fff;line-height: 9px;">'+name+'</div>';
			//opts.labelOffset = new GSize(-80, 18);

		}
		marker.batch = batch;

		GEvent.addListener(marker, "click", function() {
			if(!mapHandler.locked) {
				mapHandler.lock();
				if(marker.batch.items && marker.batch.items.length>1) {
					// szebb megoldas
					var maxx = -180;
					var maxy = -90;
					var minx = 180;
					var miny = 90;

					for(var i=0;i<marker.batch.items.length;i++) {
						if(marker.batch.items[i].gpsx>0) {
							maxx = Math.max(marker.batch.items[i].gpsx,maxx);
							minx = Math.min(marker.batch.items[i].gpsx,minx);
						}
						if(marker.batch.items[i].gpsy>0) {
							maxy = Math.max(marker.batch.items[i].gpsy,maxy);
							miny = Math.min(marker.batch.items[i].gpsy,miny);
						}
					}

					var xo = (minx+maxx) / 2;
					var yo = (miny+maxy) / 2;
					var dx = xo-minx;
					var dy = yo-miny;

					for(var i=0;i<clusterSizeX.length;i++) {
						if(clusterSizeX[i]>=dx) {
							break;
						}
					}
					var zoomx = Math.max(0,i);
					for(var i=0;i<clusterSizeY.length;i++) {
						if(clusterSizeY[i]>=dy) {
							break;
						}
					}
					var zoomy = Math.max(0,i);
					var zoom = Math.min(17,17-Math.max(zoomx,zoomy));

					if(yo>0 && xo>0) {
						this.map.setCenter(new GLatLng(yo,xo), zoom);
					}
					mapHandler.frontStartPoint();
					mapHandler.unlock();
				}
				else {
					var pid = marker.batch.items&&marker.batch.items.length>0 ? marker.batch.items[0].id : marker.batch.id;
					var type = marker.batch.items&&marker.batch.items.length>0 ? marker.batch.items[0].code : marker.batch.code;
					// CHECK IF PRODUCT_DETAILDATA OBJECT EXIST OR NOT
					marker.openInfoWindowHtml('loading...');
					CurrentMarker = marker;
					if(type=='EVT') {
						if(Event_EventDetailData[pid]){
							return mapHandler.showInfoWindow(pid,type);
						}
						loadScript('/index.php?page=Event_SearchPage&event=EventDetail&id='+pid,'mapHandler_dataset',"mapHandler.showInfoWindow('"+pid+"','"+type+"');");
					}
					else if (type=='PAGE' || type=='OFFER') {
						if(Cms_ContentDetailData[pid]){
							return mapHandler.showInfoWindow(pid,type);
						}
						loadScript('/index.php?page=Cms_IndexPage&event=ContentDetail&id='+pid+'&type='+type,'mapHandler_dataset',"mapHandler.showInfoWindow('"+pid+"','"+type+"');");
					}
					else {
						if(Partner_PartnerDetailData[pid]){
							return mapHandler.showInfoWindow(pid,type);
						}
						loadScript('/index.php?page=Partner_SearchPage&event=PartnerDetail&id='+pid,'mapHandler_dataset',"mapHandler.showInfoWindow('"+pid+"','"+type+"');");
					}
				}
			}
		});
		return marker;
	},
	showInfoWindow: function(pid,type) {
		var c = '';
		if(type=='EVT') {
			var event = Event_EventDetailData[pid];
			c = mapHandler.eventInfoPrototypeTPL.evaluate(
				{ 'event_title':			''+event.title,
				'event_picture':			event.picture ? ' src="'+event.picture+'" ' : ' src="/map/img/event_nopic.gif" ',
				'event_url':				this.kiosk ? '/index.php?page=Kiosk_HomePage&event=event&rid='+event.id : ''+event.url,
				'lang':					''+__lang,
				'event_short':			''+event.short,
				'event_date':				''+event.date }
			);
		}
		else if(type=='PAGE' || type=='OFFER') {
			var content = Cms_ContentDetailData[pid];
			c = mapHandler.contentInfoPrototypeTPL.evaluate(
				{ 'content_title':			''+content.title,
				'content_picture':		content.picture ? ' src="'+content.picture+'" ' : ' src="/map/img/content_nopic.gif" ',
				'content_url':			''+content.url,
				'lang':					''+__lang,
				'content_short':			''+content.short }
			);
		}
		else {
			var partner = Partner_PartnerDetailData[pid];
			var stars = new StringBuffer();
			for(var i=0;i<partner.stars;i++) {
				stars.append('<img src="/images/icon_star.gif" width="12" height="13" alt="" />');
			}
			if(partner.halfStar>0) {
				stars.append('<img src="/images/icon_halfstar.gif" width="12" height="13" alt="" />');
			}

			var lm = '';
			var lms = 'style="display: nobe;"';
			if(mapHandler.kiosk) {
				var pdata = mapHandler.getObject(type,pid);
				if(pdata && pdata.lastMinRooms>0) {
					lms = 'class="text"';
					lm = '<div class="partnerRoomtype" style="border: 0px;padding: 0px;"><span class="left">'+jsLabels.txtPartnerListAllBOOK+': <span>'+pdata.lastMinRooms+'</span></span>';
					lm+= '<span class="right">'+pdata.lastMinBeds+'&nbsp;'+jsLabels.txtKioskFreeBeds;
					if(pdata.lastMinPlusBeds>0) {
						lm+= ' +'+pdata.lastMinPlusBeds+'&nbsp;'+jsLabels.txtKioskSpareBed;
					}
					lm+= '</span></div>';
				}
			}
			c = mapHandler.partnerInfoPrototypeTPL.evaluate(
				{ 'partner_name':			''+partner.name,
				'partner_picture':		partner.picture ? ' src="'+partner.picture+'"' : ' src="/map/img/nopic_partner.gif"',
				'partner_short':			''+partner.short,
				'partner_url':			''+partner.url,
				'partner_offer_text':		type=='GAST' ? jsLabels.txtPartnerReserveTable : jsLabels.txtPartnerContact,
				'partner_offer_click':	type=='GAST' ? 'onclick="'+partner.offerCommandGAST+'"' : 'onclick="'+partner.offerCommand+'"',
				'partner_price':			''+partner.price,
				'partner_euroRate':		''+partner.euroRate,
				'partner_stars':			stars.toString(),
				'partner_command':		' onclick="'+partner.command+'" ',
				'partner_lastminute':		lm,
				'partner_lms':			lms,
				'partner_zip':			''+partner.zip,
				'partner_city':			''+partner.city,
				'lang':					''+__lang,
				'partner_adress':			''+partner.address }
			);
		}
		CurrentMarker.openInfoWindowHtml(c,{maxWidth: 420});
		mapHandler.unlock();
	},
	getIcon: function(batch) {
		if(batch.items&&batch.items.length==1) {
			var img = this.getBatchImg(batch.items[0]);
			return this.icons[img];
		}
		else {
			var img = this.getBatchImg(batch);
			return this.icons[img];
		}
	},
	getBatchImg: function(item) {
		var cat = null;
		if(item.code=='STD') {
			if(item.categories && item.categories.length>0) {
				cat = item.categories[0];
			}
		}
		else {
			if(item.types && item.types.length>0) {
				cat = item.types[0];
			}
		}
		return this.getIconImg(item.code,cat,item);
	},
	getIconImg: function(code,cat,item) {
		var c = cat ? cat.substring(0,9) : '';
		if(code=='point') {
			return 'point_'+__lang;
		}
		if(code=='cluster') {
			return 'items';
		}
		else if(code=='STD') {
			if(this.kiosk) {
				if(item) {
					if(item.lastMinRooms>0) {
						return 'free';
					}
					else {
						return 'reserved';
					}
				}
				else {
					return cat=='free' ? 'free' : 'A.002.035';
				}
			}
			else {
				if( this.icons[c] ) {
					return c;
				}
				return 'A.002.035';
			}
		}
		else if(code=='GAST') {
			if( this.icons[c]) {
				return c;
			}
			return 'gasztronomia';
		}
		else if(code=='EVT') {
			return 'E.002.000';
		}
		else if(code=='PAGE') {
			return 'page';
		}
		else if(code=='OFFER') {
			return 'offer';
		}
		return 'hotel';
	},

	getObject: function(type,id) {
		for(j=0;j<this.objects[type].length;j++) {
			if(this.objects[type][j].gpsx>0 && this.objects[type][j].id==id) {
				return this.objects[type][j];
			}
		}
		return null;
	}


}
