﻿
// JScript File


loadwindow("Default3.aspx",540,250)

var message="Prawy przycisk!";

///////////////////////////////////
function clickIE4(){
if (event.button==2){
elem.onclick = doSomething;
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
elem.onclick = doSomething;
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
elem.onclick = doSomething;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
elem.onclick = doSomething;
}

document.oncontextmenu=new Function("return false")





function info(x, y)
{

var leftViewPort = document.getElementById("viewport1").style.left
var topViewPort = document.getElementById("viewport1").style.top


//alert(x + ":" + y);
var MouseX = x - parseInt(leftViewPort);
var MouseY = y - parseInt(topViewPort);



if (MouseX < 1024){
var tx  = 3
}

if (MouseX < 768){
var tx  = 2
}

if (MouseX < 512){
var tx  = 1
}

if (MouseX < 256){
var tx  = 0
}


if (MouseY < 1024){
var ty  = 3
}

if (MouseY < 768){
var ty  = 2
}

if (MouseY < 512){
var ty  = 1
}

if (MouseY < 256){
var ty  = 0
}

//alert(tx + "," + ty);



var http = document.getElementById("mapa_img1").src;

//getQuery(http);

var option = http.toString();
var oarray = new Array();
var urlArg = new Array();
oarray = option.split("&");
// taken from cookie parsing code
     var property_value = "";

     for (var loop = 0; loop < oarray.length; loop++)
     {
          property_value = oarray[loop];
          var broken_info = oarray[loop].split("=");
          var the_property = broken_info[0];
          var the_value = broken_info[1];
          urlArg[the_property] = the_value;
     }

//document.getElementById("log").innerHTML  +=  ("FirstTile" + "X" + ': ' + urlArg['x'] + '<BR>') + ("Y" + ': ' + urlArg['y'] + '<BR>') + ("Zoom" + ': ' + urlArg['zoom'] + '<BR>');

//alert("first " + (urlArg['x']) + "firsty " + (urlArg['y']));

var TileX = parseInt(urlArg['x']) + tx ;
var TileY = parseInt(urlArg['y']) + ty ;

var xoff = (MouseX%256);
var yoff = (MouseY%256);

//alert("Numer KaflaX " + TileX + "Numer KaflaY " + TileY);
//alert("Xoff" + xoff + "Yoff" + yoff);

var zoom = parseInt(urlArg['zoom']);




var x = urlArg['x'].toString();
var y = urlArg['y'].toString();


var XY2LL = TileControler_XY2LL(TileX, xoff, TileY, yoff, zoom);

//document.getElementById("log").innerHTML += ("TileControler_XY2LL Lon3:" + XY2LL[0] + ", Lat:" + XY2LL[1] + "<br>" + "Cx = " + cX + ", cY = " + cY + "<br>");

function TileControler_XY2LL(xtile, xpos, ytile, ypos, zoom)
{
var x1, y1;
x1 = xtile * 256 + xpos;
x1 = x1 * (2*Math.PI) / (256 << zoom);
lon = (x1 - Math.PI) * 180 / Math.PI;

y1 = ytile * 256 + ypos;
y1 = y1 * (2*Math.PI) / (256 << zoom);
y1 = (Math.PI - y1);

if (y1 > 0)
y1 = 2 * Math.atan(Math.exp(y1)) - Math.PI / 2;
else
y1 = -(2 * Math.atan(Math.exp(-y1)) - Math.PI / 2);
lat = y1 * 180 / Math.PI;
return [lon, lat]
} 



//przeliczanie zoom


var PerfectSphereRadius = 6378137.0;
var FFOV = 45;
var TilesDim = 1 << zoom;

var FAltitude = (PerfectSphereRadius * 2 * Math.PI) / (TilesDim * Math.tan(FFOV * Math.PI/ 180) * 2);

//wyznaczanie promienia

var StartSeconds = TileControler_XY2LL(TileX, 1, TileY, 1, zoom);
var StartLongitudeSeconds = StartSeconds[0];
var StartLatitudeSeconds = StartSeconds[1];


var StopSeconds = TileControler_XY2LL(TileX, 6, TileY, 6, zoom);
var StopLongitudeSeconds = StopSeconds[0];
var StopLatitudeSeconds = StopSeconds[1];

var  CosD;

CosD = Math.sin(StartLatitudeSeconds*Math.PI/(180)) * Math.sin(StopLatitudeSeconds*Math.PI/(180)) + Math.cos(StartLatitudeSeconds*Math.PI/(180)) * Math.cos(StopLatitudeSeconds*Math.PI/(180)) * Math.cos((StopLongitudeSeconds - StartLongitudeSeconds)*Math.PI/(180));

var Result;

 if (CosD == 1){
   Result = 0;
   }
  else
  {
   Result = Math.acos(CosD);
   }
   
 var QueryRad  = Result * PerfectSphereRadius;



if (isNaN(XY2LL[0])) {
  } else {
  //EmapaAJAXMap._Default.info(XY2LL[0], XY2LL[1], FAltitude, QueryRad, info_callback);  // asynchroniczne zapytanie
  EmapaAJAXMap._Default.info2(XY2LL[0], XY2LL[1], FAltitude, QueryRad, info_callback2);  // asynchroniczne zapytanie
}

}

// Ta metoda zostanie wywolana po metodzie wykonaniej wczesniej,
// a jej rezultat zostanie wyslany do klienta.

function info_callback(res)
{

if (res.value.Street != null && res.value.Street.Length > 0){
if (res.value.Zip != ""){
var p = ", ";
}
var ulica = "ul. " + res.value.Street[0].Name + p;
}
else{
var ulica = "";
}



var ulica = ulica + " ";

if (res.value.City != null && res.value.City.Length > 0){
if (res.value.City[0].Name  == ""){
var przecinek = " ";
}
else
{
if (res.value.Zip != ""){
var przecinek = ", ";
}
else
{
var przecinek = " ";
}
}
}
else
{
var przecinek = " ";
}

var kodpocztowy = res.value.Zip + przecinek;
if (res.value.Street != null && res.value.Street.Length > 0){
if (res.value.City != null && res.value.City.Length > 0){
var miasto = res.value.City[0].Name  + "<br>";
}
else
{
var miasto = "";
}
}
else
{

if ((res.value.Zip != "")){
if (res.value.City != null && res.value.City.Length > 0){
	var miasto = res.value.City[0].Name  + "<br>";
}
else
{
var miasto = "";
}
}
else
{
if (res.value.City != null && res.value.City.Length > 0){
var miasto = res.value.City[0].Name  + " ";
}
else
var miasto = "";
}
}





if (res.value.Road != null && res.value.Road > 0){
var typdrogi = res.value.Road[0].TypeIndex + " ";

if (typdrogi == 0 ){
var typdrogi = "Autostarda ";
}
if (typdrogi == 1 ){
var typdrogi = "Droga ekspresowa ";
}
if (typdrogi == 2 ){
var typdrogi = "Droga krajowa ";
}
if (typdrogi == 3 ){
var typdrogi = "Droga wojewódzka ";
}
if (typdrogi == 7 ){
var typdrogi = "Prom";
}

}
else{
var typdrogi = "";
}

if (res.value.Road != null && res.value.Road.Length > 0){
var nazwadrogi = res.value.Road[0].Name + "<br>";
}
else{
var nazwadrogi = "";
}

if (res.value.OtherMapElements != null && res.value.OtherMapElements.Length > 0){
var obiekt = res.value.OtherMapElements[0].Name + "<br>";
}
else{
var obiekt = "";
}


if (res.value.Natural != null && res.value.Natural.Length > 0){
var natural = res.value.Natural[0].Name + "<br>";
}
else{
var natural = "";
}

if (res.value.AreaName1 != ""){
var woj = "w. " + res.value.AreaName1 + ", " ;
var pow = "p. " + res.value.AreaName2 + ", " ;
var gmi = "g. " + res.value.AreaName3 + "<br>";
var panstwo = res.value.AreaName0
}
else
{
var woj = "";
var pow = "";
var gmi = "";
var panstwo = res.value.AreaName0
}

ddrivetip(ulica + kodpocztowy + miasto + typdrogi + nazwadrogi + obiekt + natural + woj + pow + gmi + panstwo, 250);

positiontip();
document.onmouseover=positiontip
document.onmouseout=positiontip
  
  
}   

function info_callback2(res)
{
	ddrivetip(res.value, 250);
	positiontip();
	document.onmouseover = positiontip;
	document.onmouseout = positiontip;
}

