﻿//<![CDATA[

	var mapspots = [];
	var mapspotsTmbidk = [];
	
	function cpeHintCntGen(anev,akatk,akatikonok) {
		var cnt = document.createElement('div');
		cnt.className = 'map_hint_mapspot';
		cnt.appendChild(document.createTextNode(anev));
		
/*
		if (akatk.length) {
			var akimg = document.createElement('img');
			akimg.setAttribute("src","portal/images/urestran.gif");
			akimg.setAttribute("width",100);
			akimg.setAttribute("height",1);
			cnt.appendChild(akimg);
		}
*/		
		for (var i in akatk) {
			
			cnt.appendChild(document.createElement('br'));
			
			var akimg = document.createElement('img');
			akimg.setAttribute("src",akatikonok[i]);
			akimg.setAttribute("width",16);
			akimg.setAttribute("height",16);
			var td1 = document.createElement('td');
			td1.appendChild(akimg);

			var ek = document.createTextNode(akatk[i]);
			//$(ek).css("style","vertical-align:middle");
			var td2 = document.createElement('td');
			if (0) {
				var b = document.createElement('B');
				b.appendChild(ek);
				td2.appendChild(b);
			}
			else {
				td2.appendChild(ek);
			}
			
			var tr = document.createElement('tr');
			tr.appendChild(td1);
			tr.appendChild(td2);
			var tbl = document.createElement('table');
			tbl.appendChild(tr);
			
			cnt.appendChild(tbl);
			
		}
		//
		//b.appendChild(document.createTextNode('bbb'));
		//cnt.appendChild(b);
		//cnt.appendChild(document.createTextNode('1'));
		//cnt.appendChild(document.createElement('br'));
		//cnt.appendChild(document.createTextNode(' kat11;\u00a0kat123'));
		return cnt;
	}
	
	function addMapspotClick(pmapspot,pjs) {
		GEvent.addListener(pmapspot,"click", function (latLng) {
			if (typeof latLng != 'undefined') eval(pjs);
		});
	}
	
	function addMapspotHint(pmapspot,html) {
 		var cnt;
		if (typeof html == 'undefined') {
		  cnt = document.createElement('div');
		  cnt.className = 'map_hint_mapspot';
          cnt.appendChild(document.createTextNode((typeof pmapspot.getTitle() == 'undefined') ? '' : pmapspot.getTitle() ));
		  //alert('1' + cnt.textContent + cnt.offsetHeight.toString());
         }
 		else if (typeof html == 'string') {
          cnt = document.createElement('div');
		  cnt.className = 'map_hint_mapspot';
          cnt.appendChild(document.createTextNode(html));
		  //alert('2' + cnt.textContent + cnt.offsetHeight.toString());
		 }
		else {
		  cnt = html;
		  //alert('3' + cnt.textContent + cnt.offsetHeight.toString());
		 };
		//alert(1);
		var tooltip = new Tooltip(pmapspot,cnt,4);
		//alert(2);
		pmapspot.tooltip = tooltip;
        //cpemap.addOverlay(tooltip);
		if (typeof pmapspot.cpeTerulet != 'undefined') pmapspot.cpeTerulet.tooltip = tooltip;
		if (typeof pmapspot.cpeTeruletKep != 'undefined') pmapspot.cpeTeruletKep.tooltip = tooltip;
/*
        GEvent.addListener(pmapspot,"mouseover", function() {
		  //this.tooltip.show();
		  mapspots[0].tooltip.addtext(html);
		  mapspots[0].tooltip.show();
        });        
        GEvent.addListener(pmapspot,"mouseout", function() {
          //this.tooltip.hide();
		  mapspots[0].tooltip.removetext(html);
		  mapspots[0].tooltip.hide();
        });        
*/
		//alert(3);
        cpeEventsGenForHint(pmapspot,0);
		if (typeof pmapspot.cpeTerulet != 'undefined') {
			cpeEventsGenForHint(pmapspot.cpeTerulet,1);
		};
		
		if (typeof mapspotsTmbidk[pmapspot.cpeSpid] == 'undefined') {
			mapspots.push(pmapspot);
			//alert(pmapspot.cpeSpid);
			mapspotsTmbidk[pmapspot.cpeSpid] = mapspots.length-1;
		}
		else {
			var dmapspot = mapspots[mapspotsTmbidk[pmapspot.cpeSpid]];
			mapspots.splice(mapspotsTmbidk[pmapspot.cpeSpid],1,pmapspot);
			cpeDelMapspot(dmapspot);
		}
		//alert(5);
	  }
	  
function cpeEventsGenForHint(pmapspot,ppoly) {
	if (typeof pmapspot != 'undefined') {
		GEvent.addListener(pmapspot,"mouseover", function() {
		  //alert(pmapspot.getTitle());
		  //console.log(typeof pmapspot);
		  //mapspotsinfo.alltext(lastPoint,mapspots);
		  mapspotsinfo.show(lastPoint);
		  if (ppoly) cpePolyOver = true;
		  //alert(3);
		  //mapspots[0].tooltip.show();
        });        
        GEvent.addListener(pmapspot,"mouseout", function() {
		  mapspotsinfo.hide(lastPoint);
		  if (ppoly) cpePolyOver = false;
 		  //mapspots[0].tooltip.hide();
        });        
        GEvent.addListener(pmapspot,"remove", function() {
		  pmapspot.hide();
 		  //mapspots[0].tooltip.hide();
        });        
		//alert(4);
	};
};
		
/*
      var map = new GMap2(document.getElementById("map"));
      cpemap.setCenter(new GLatLng(43.907787,-79.359741), 9);
    
      var point = new GLatLng(43.65654,-79.90138);
      var marker = createMarker(point,'Some stuff to display in the<br>First Info Window <br>With a <a href="http://www.econym.demon.co.uk">Link<\/a> to my home page')
      cpemap.addOverlay(marker);

      var point = new GLatLng(43.91892,-78.89231);
      var marker = createMarker(point,'Some stuff to display in the<br>Second Info Window')
      cpemap.addOverlay(marker);

      var point = new GLatLng(43.82589,-79.10040);
      var marker = createMarker(point,'Some stuff to display in the<br>Third Info Window')
      cpemap.addOverlay(marker);
*/



GPolygon.prototype.containsLatLng = function(latLng) {
    var bounds = this.getBounds();
   
    if(!bounds.containsLatLng(latLng)) {
        return false;
    }
   
    var numPoints = this.getVertexCount();
    var inPoly = false;
    var i;
    var j = numPoints-1;
   
    for(var i=0; i < numPoints; i++) {
        var vertex1 = this.getVertex(i);
        var vertex2 = this.getVertex(j);
       
        if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() || vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng())  {
            if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) {
                inPoly = !inPoly;
            }
        }
       
        j = i;
    }
   
    return inPoly;
};


function MapspotsInfo(){
	this.text_ = 'aaa';
	//this.cnt_ = null;
	//this.div_ = null;
	//this.map_ = null;
	//this.tc_ = null;
}

var mspinfoover = false;

MapspotsInfo.prototype = new GOverlay();

MapspotsInfo.prototype.initialize = function(map){
	this.div_ = document.createElement("div");
	this.tc_ = (typeof this.text_ == 'string' ? document.createTextNode(this.text_) : this.text_);
	this.div_.appendChild(this.tc_);
	this.div_.className = 'map_hint';
	this.div_.style.position = 'absolute';
	this.div_.style.visibility = 'hidden';
	//this.div_.addEvent("mouseover", function () { mspinfoover = true; console.log("DM1"); } );
	//this.div_.addEvent("mouseout", function () { mspinfoover = false; console.log("DM2"); } );
	cpemap.getPane(G_MAP_FLOAT_PANE).appendChild(this.div_);
	this.map_ = map;
	this.textstr_ = '';
}

MapspotsInfo.prototype.remove = function(){
	this.div_.parentNode.removeChild(this.div_);
}

MapspotsInfo.prototype.copy = function(){
	return new MapspotsInfo();
}

MapspotsInfo.prototype.redraw = function(force){
	if (!force) return;
	/*
	var aktPoint = this.map_.fromLatLngToDivPixel(lastPoint);
	this.div_.style.top = aktPoint.y + 'px';
	this.div_.style.left = aktPoint.x + 'px';
	*/
	this.div_.style.top = '10px';
	this.div_.style.left = '10px';
}

MapspotsInfo.prototype.show = function(point,gcontpos){
	//alert(this.div_.textContent + '##' + this.div_.offsetHeight.toString()/* + '##' + this.div_.childNodes[0].offsetHeight.toString()*/);
	//console.log("MS1");
	//if (this.div_.style.visibility == 'hidden') {
		if (typeof point == 'string') {
		//console.log('sat1');
			this.div_.style.visibility = 'hidden';
			gcontpos.apply(this.div_);
			this.alltext('a',mapspots,this.div_.style.left.replace('px',''),this.div_.style.top.replace('px',''));
			this.div_.style.top = (this.div_.style.top.replace('px','') - this.div_.offsetHeight) + 'px';
		//console.log('sat2');
		}
		else {
			var aktPoint = this.map_.fromLatLngToDivPixel(point);
			this.div_.style.top = (aktPoint.y - this.div_.offsetHeight) + 'px';
			this.div_.style.left = aktPoint.x + 'px';
		};
		this.div_.style.visibility = 'visible';
		//if (this.textstr_) alert(this.textstr_);
		//alert(1);
		//if (this.textstr_) 
		//alert(this.div_.textContent + '##' + this.div_.offsetHeight.toString()/* + '##' + this.div_.childNodes[0].offsetHeight.toString()*/);
		//alert(4);
	//}
}

MapspotsInfo.prototype.hide = function(point){
	 //console.log("MS0");
	if (this.div_.style.visibility == 'visible') {
		var aktPoint = this.map_.fromLatLngToDivPixel(point);
		if (!Belulvane(aktPoint,this.div_.style.left,this.div_.style.right,this.div_.style.top,this.div_.style.bottom)) {
			this.div_.style.visibility = 'hidden';
			//alert(5);
		}
	}
}

MapspotsInfo.prototype.addtext = function(text){
	this.text_ = this.text_ + text;
	this.div_.removeChild(this.tc_);
	var textchild = document.createTextNode(this.text_);
	this.div_.appendChild(textchild);
	this.tc_ = textchild;
}

function Belulvane(point,x1Pos,x2Pos,y1Pos,y2Pos,px,py) {
  //alert(point.x);
    var pointx;
	var pointy;
	if (typeof point == 'string') {
		//if (numbers.length && (numbers[0]==346)) console.log('str');
		pointx = px;
		pointy = py;
	}
	else {
		pointx = point.x;
		pointy = point.y;
	}
	
	return ((pointx >= x1Pos) && (pointx <= x2Pos) && (pointy >= y1Pos) && (pointy <= y2Pos));
}

function Belulvane2(point,x1Pos,y1Pos,numbers,relkoord,px,py) {
	if (typeof relkoord == 'undefined') relkoord = 1;
	var pointx;
	var pointy;
	if (typeof point == 'string') {
		//if (numbers.length && (numbers[0]==346)) console.log('str');
		pointx = px;
		pointy = py;
	}
	else {
		pointx = point.x;
		pointy = point.y;
	}
	var inPoly = false;
	//alert(numbers.length);
    //alert(pointx + ' ' + pointy + ' ' + x1Pos + ' ' + x2Pos + ' ' + numPoints + ' ' + numbers);
    var numPoints = numbers.length / 2;
	var j = numPoints - 1;
    for(var i=0; i < numPoints; i++) {
        var vertex1x = numbers[i*2] + x1Pos * relkoord;
        var vertex1y = numbers[i*2+1] + y1Pos * relkoord;
        var vertex2x = numbers[j*2] + x1Pos * relkoord;
        var vertex2y = numbers[j*2+1] + y1Pos * relkoord;
/*
		if (numbers.length && (numbers[0]==346)) console.log(vertex1x+' '+vertex1y);
		if (numbers.length && (numbers[0]==346)) console.log(vertex2x+' '+vertex2y);
		if (numbers.length && (numbers[0]==346)) console.log(pointx+' '+pointy);
 */		
		if ((vertex1x < pointx && vertex2x >= pointx) || (vertex2x < pointx && vertex1x >= pointx))  {
			if (vertex1y + (pointx - vertex1x) / (vertex2x - vertex1x) * (vertex2y - vertex1y) < pointy) {
       /*if (inPoly)*/ //alert(pointx + ' ' + pointy + ' ' + vertex1x + ' ' + vertex1y + ' ' + vertex2x + ' ' + vertex2y + ' ' + j);
				inPoly = !inPoly;
			}
		}
		//if (numbers.length && (numbers[0]==346)) console.log(inPoly);
		
		j = i;
	}
	return inPoly;
}

MapspotsInfo.prototype.alltext = function(point,mapspotok,px,py){
    //var pointstr = point.lat().toFixed(14) + ', ' + point.lng().toFixed(14);
	var aktPoint;
	//console.log('AT11 '+mapspotok.length);
	if (typeof point == 'string') {
		aktPoint = point;
	}
	else {
		aktPoint = this.map_.fromLatLngToDivPixel(point);
	};
	//console.log('AT12 '+mapspotok.length);
	var descLst = document.createElement('div');
	$(descLst).addClass('map_hint_belso');
/*			
			var akimg = document.createElement('img');
			akimg.setAttribute("src","portal/images/urestran.gif");
			akimg.setAttribute("width",100);
			akimg.setAttribute("height",1);
			descLst.appendChild(akimg);
*/
	this.textstr_ = '';
	var avizsg = 0;
	var atalalt = 0;
	//console.log('AT13 '+mapspotok.length);
	for (var i in mapspotok) if ( ((!mapspotok[i].isHidden()) || ((typeof mapspotok[i].cpeTerulet != 'undefined')&&(!mapspotok[i].cpeTerulet.isHidden()))) && (mapspotok[i].cpeMapon)) {
	//pointstr += i + "\n";
	  //if (mapspotok[i].cpeSpid==1832) Stamp = new Date();
	  //if (mapspotok[i].cpeSpid==1832) console.log('AT2 '+i+' '+Stamp.getSeconds());
	  avizsg++;
	  var markerPos = this.map_.fromLatLngToDivPixel(mapspotok[i].getPoint());
	  var iAnchor = mapspotok[i].getIcon().iconAnchor;
	  var iSize = mapspotok[i].getIcon().iconSize;
	  var x1Pos = markerPos.x - iAnchor.x;
	  var y1Pos = markerPos.y - iAnchor.y;
	  var x2Pos = x1Pos + iSize.width;
	  var y2Pos = y1Pos + iSize.height;
	  //nem rossz, de ez a terulet kene igazabol:
	  var iMap = mapspotok[i].getIcon().imageMap;
	  var aHS = new Array();
	  if ((typeof mapspotok[i].cpeTerulet != 'undefined')&&(!mapspotok[i].cpeTerulet.isHidden())) {
		//console.log('ahs '+i);
		for (var ixi = 0; ixi < mapspotok[i].cpeTerulet.getVertexCount(); ixi++) {
			var vt = this.map_.fromLatLngToDivPixel(mapspotok[i].cpeTerulet.getVertex(ixi));
			aHS.push(vt.x);
			aHS.push(vt.y);
		};
	  }; 
  /*
	  pointstr += ' m(' + markerPos.x + ',' + markerPos.y + ')';
	  pointstr += ' a(' + iAnchor.x + ',' + iAnchor.y + ')';
	  pointstr += ' i(' + iSize.width + ',' + iSize.height + ')';
	  //pointstr += ' imap(';
	  //for (var j in iMap) pointstr += iMap[j] + ',';
	  //pointstr += ')';
	  */
/*	  
  var points = [];
  //alert(mapspotok[i].getIcon().imageMap);
  var pu = false;
  var j;
  for(var i in mapspotok[i].getIcon().imageMap) {
      //points.push(new GLatLng(40,60));
      if (pu) points.push(this.map_.fromDivPixelToLatLng(new GPoint(x1Pos + j, y1Pos + i)));
	  j = i;
	  pu = !pu;
	}
  //alert(points);

  var xpoli = new GPolygon(points, '#0000FF', 1, 0.5, "#00FF00", 0.5);
 */
 //cpemap.addOverlay(xpoli);
	 //alert( mapspotok[i].getIcon().imageMap);
	  //if (aHS.length && (aHS[0]==346)) console.log('ahs '+i+' '+aHS.length+' '+aHS[0]+','+aHS[1]+' '+aHS[2]+','+aHS[3]+' '+aHS[4]+','+aHS[5]+' '+aHS[6]+','+aHS[7]+' '+aHS[8]+','+aHS[9]+' '+aktPoint.x+',' +aktPoint.y+' '+px+','+py);
	  //if (mapspotok[i].cpeSpid==1832) console.log('mps '+i+' '+aHS.length+' '+aktPoint.x+',' +aktPoint.y+' '+px+','+py+' '+x1Pos+','+y1Pos+' '+x2Pos+','+y2Pos);
	  if (aHS.length ? Belulvane2(aktPoint,x1Pos,y1Pos,aHS,0,px,py) : ( (typeof mapspotok[i].getIcon().imageMap == 'undefined') ? Belulvane(aktPoint,x1Pos,x2Pos,y1Pos,y2Pos,px,py) : Belulvane2(aktPoint,x1Pos,y1Pos,mapspotok[i].getIcon().imageMap) )) {
	    //if (mapspotok[i].cpeSpid==1832) console.log('mpsB '+i+' '+aHS.length);
        //alert(this.div_.textContent + '##' + this.div_.offsetHeight.toString());
	    //console.log($(mapspotok[i].tooltip.text_).html());
	    //console.log($(descLst).html());
		atalalt++;
		//if (aHS.length) aent=
		if (typeof mapspotok[i].tooltip == 'undefined') {
		  descLst.appendChild(document.createTextNode((typeof mapspotok[i].getTitle() == 'string') ? mapspotok[i].getTitle() : ''));
		  aktjar=1;
		 }
		else {
		  typeof mapspotok[i].tooltip.text_ == 'string'
		  ? descLst.appendChild(document.createTextNode(mapspotok[i].tooltip.text_))
		  : descLst.appendChild(mapspotok[i].tooltip.text_);
		  aktjar= (mapspotok[i].tooltip.text_ == 'string') ? 21 : 22;
		 };
	    if (typeof mapspotok[i].getTitle() == 'string') this.textstr_ += '###'+mapspotok[i].getTitle();
	    //console.log($(descLst).html());
        //alert(this.div_.textContent + '##' + this.div_.offsetHeight.toString());
	   };
	  //pointstr += aktPoint.x + ',' + aktPoint.y+ ';' + x1Pos + ',' +y1Pos+ ';' +x2Pos+ ',' +y2Pos;
     }
	//if (this.textstr_) alert(descLst.childNodes.length + this.textstr_);
	//if (this.textstr_) alert(descLst.textContent);
    //descLst += pointstr;
	//if (this.textstr_) alert(this.div_.textContent + '##' + this.div_.offsetHeight.toString() + '##' + aktjar.toString());
	
	//descLst.appendChild(document.createTextNode(atalalt + '/' + avizsg + '/' + (cpemmgr ? cpemmgr.getMarkerCount(cpemap.getZoom()):-1)));
	
	this.div_.removeChild(this.tc_);
	this.text_ = descLst; //console.log($(descLst).html());
	var cch = (typeof descLst == 'string' ? document.createTextNode(descLst) : descLst);
	this.div_.appendChild(cch);
	this.tc_ = cch;
	//if (this.textstr_) alert(this.div_.textContent + '##' + this.div_.offsetHeight.toString());
    // if show:
	this.div_.style.top = (aktPoint.y - this.div_.offsetHeight) + 'px';
	this.div_.style.left = aktPoint.x + 'px';
}

function Tooltip(marker, text, padding){
	this.marker_ = marker;
	this.text_ = text;
	this.padding_ = padding;
}

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(map){
	this.div_ = document.createElement("div");
	//var innerContainer = this.div.cloneNode(false);
	var textchild = (typeof this.text_ == 'string' ? document.createTextNode(this.text_) : this.text_);
	this.div_.appendChild(textchild);
	this.div_.className = 'tooltip';
	this.div_.style.position = 'absolute';
	this.div_.style.visibility = 'hidden';
	cpemap.getPane(G_MAP_FLOAT_PANE).appendChild(this.div_);
	this.map_ = map;
	this.tc_ = textchild;
}

Tooltip.prototype.remove = function(){
	this.div_.parentNode.removeChild(this.div_);
}

Tooltip.prototype.copy = function(){
	return new Tooltip(this.marker_,this.text_,this.padding_);
}

Tooltip.prototype.redraw = function(force){
	if (!force) return;
	var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	var iconAnchor = this.marker_.getIcon().iconAnchor;
	var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
	var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
	this.div_.style.top = yPos + 'px';
	this.div_.style.left = xPos + 'px';
}

Tooltip.prototype.show = function(){
	this.div_.style.visibility = 'visible';
}

Tooltip.prototype.hide = function(){
	this.div_.style.visibility = 'hidden';
}

Tooltip.prototype.addtext = function(text){
	this.text_ = this.text_ + text;
	this.div_.removeChild(this.tc_);
	var textchild = document.createTextNode(this.text_);
	this.div_.appendChild(textchild);
	this.tc_ = textchild;
}

Tooltip.prototype.removetext = function(text){
	this.text_ = this.text_.replace(text,'');
	this.div_.removeChild(this.tc_);
	var textchild = document.createTextNode(this.text_);
	this.div_.appendChild(textchild);
	this.tc_ = textchild;
}

Tooltip.prototype.alltext = function(point,mapspotok){
    var pointstr = point.lat().toFixed(14) + ', ' + point.lng().toFixed(14);
	var aktPoint = this.map_.fromLatLngToDivPixel(point);
	var descLst = '';
	for (var i in mapspotok) {
	  //pointstr += i + "\n";
	  
	  var markerPos = this.map_.fromLatLngToDivPixel(mapspotok[i].getPoint());
	  var iAnchor = mapspotok[i].getIcon().iconAnchor;
	  var iSize = mapspotok[i].getIcon().iconSize;
	  var x1Pos = markerPos.x - iAnchor.x;
	  var y1Pos = markerPos.y - iAnchor.y;
	  var x2Pos = x1Pos + iSize.width;
	  var y2Pos = y1Pos + iSize.height;
	  //nem rossz, de ez a terulet kene igazabol:
	  var iMap = mapspotok[i].getIcon().imageMap;	  
	  /*
	  pointstr += ' m(' + markerPos.x + ',' + markerPos.y + ')';
	  pointstr += ' a(' + iAnchor.x + ',' + iAnchor.y + ')';
	  pointstr += ' i(' + iSize.width + ',' + iSize.height + ')';
	  //pointstr += ' imap(';
	  //for (var j in iMap) pointstr += iMap[j] + ',';
	  //pointstr += ')';
	  */
	  if ((aktPoint.x >= x1Pos) && (aktPoint.x <= x2Pos) && (aktPoint.y >= y1Pos) && (aktPoint.y <= y2Pos)) descLst += 'b'+i + mapspotok[i].tooltip.text_;
	  //pointstr += aktPoint.x + ',' + aktPoint.y+ ';' + x1Pos + ',' +y1Pos+ ';' +x2Pos+ ',' +y2Pos;
     }
	
    this.text_ = descLst;
	//this.text_ += pointstr;
	this.div_.removeChild(this.tc_);
	var textchild = document.createTextNode(this.text_);
	this.div_.appendChild(textchild);
	this.tc_ = textchild;
}

Tooltip.prototype.gettext = function(){
	return(this.text_);
}

	
	
//]]>
