﻿//<![CDATA[

	var lastPoint = new GLatLng(0,0);
	var cpePolyOver = false;
	var mapspotsinfo;
$(document).ready(function() { //alert(2);
	
	GEvent.addListener(cpemap, "click", function (olay,latLng) { if (typeof ckbiranyactstart == 'function') ckbiranyactstart(3,latLng); });

	GEvent.addListener(cpemap, "mousemove", function(point){
		mapspotsinfo.alltext(point,spots);
		mapspotsinfo.alltext(point,mapspots);
		//mapspots[0].tooltip.alltext(point,mapspots);
		lastPoint = point;
		//console.log('MMM '+lastPoint.lng()+' '+lastPoint.lat());
	});
	mapspotsinfo = new MapspotsInfo();
	cpemap.addOverlay(mapspotsinfo);
	GEvent.addListener(cpemap, "clearoverlays", function(){
		cpemap.addOverlay(mapspotsinfo);
	});
	GEvent.addListener(cpemap, "addoverlay", function(aolay){
		aolay.cpeMapon = true;
	});
	GEvent.addListener(cpemap, "removeoverlay", function(aolay){
		aolay.cpeMapon = false;
	});
          
	GEvent.addDomListener(cpemap.getContainer(),"mousemove", function(e) {
            //console.log('DL1');
			if (cpePolyOver) {
            //console.log('DL2');
              var mapLeft = cpemap.getBounds().getSouthWest().x;
              var mapTop = cpemap.getBounds().getNorthEast().y;
              var mapPoint = cpemap.fromLatLngToDivPixel(new GLatLng(mapTop,mapLeft));
              var offset = new GPoint(e.clientX,e.clientY);
              //var width =tooltip.clientWidth;
              //var height=tooltip.clientHeight;
              //console.log('DL3');
			  var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x + mapPoint.x /*- width/2*/ - document.getElementById('map').offsetLeft + f_scrollLeft(), offset.y /*- height */+ mapPoint.y/* - 10*/-document.getElementById('map').offsetTop + f_scrollTop())); 
              //pos.apply(tooltip);
              //console.log('DL4'+' '+mapspotsinfo.div_.style.top);
			  //pos.apply(mapspotsinfo);
              //console.log('DL5');
			  mapspotsinfo.show('a',pos);
              //console.log('DL6');
			  /*
			  var opoi = cpemap.fromLatLngToDivPixel(lastPoint);
			  var npoi = new GPoint(mapPoint.x + e.clientX,mapPoint.y + e.clientY);
			  lastPoint = cpemap.fromDivPixelToLatLng(npoi);
			  mapspotsinfo.alltext(lastPoint,mapspots);
			  mapspotsinfo.show(lastPoint);
			  console.log('DLMM '+lastPoint.lng()+' '+lastPoint.lat()+' '+opoi.x+' '+opoi.y+' ' +mapPoint.x + ' ' + e.clientX+' '+offset.x+' '+mapPoint.y + ' ' +offset.y+' '+ e.clientY);
			  */
            }
    });
});

var fullspotsxmlcontent = '';

function cpegmpos(pxml,pangle) {
	
	if (fullspotsxmlcontent=='') fullspotsxmlcontent = loadXML("utcanezet/spXML/"+swxmlfile[loc]);
	var xml = fullspotsxmlcontent;
	var slat,slng;
	if (xml) {
		var spots=xml.getElementsByTagName("spot");
		for (i=0;i<spots.length;i++) if (spots[i].getAttribute("panoXML")==pxml) {
			slat=spots[i].getAttribute("lat");
			slng=spots[i].getAttribute("lng");
		};
	};
	
	try {
		map.setCenter(new GLatLng(slat,slng));
		map.addOverlay(crMarker2(slat,slng,"walknow.png",pxml));
	} catch (e) {};
	
	//console.log(pxml+' '+pangle+' '+slat+' '+slng);
};

function cpechgpano(pxml) {
	return krpano().call("loadpano("+pxml+",null,KEEPPROGRESS,ZOOMBLEND(1,2));");
};

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
};

function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
};

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
};

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
};

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
};

//]]>
