﻿//var hasDialogPropNames = new Array("Nazwa planu", "RODZAJ"); 
var hasDialogPropNames = new Array("Nazwa planu"); 
var propertyUrl = null;
var propertyName = null;
var dialogWidth = null;
var dialogHeight = null;

var OnLoadDlg = function(Sender, EventArgs)
{  
}

//tu umieszczaj zdarzenia wywoływane raz przy starcie serwisu po załadowaniu mapy
function OnInitLoadMap()
{  
  if (top.ShowOrtoOnInit)  
    ChangeVisibilityLayer(top.Ortofotomapa);  
}

function GetMap()
{
  return top.getMapWindow();  
}

function GetMapFrame()
{
  var map = GetMap();
  try {
    if (map)
      return map.GetMapFrame();
    else 
      return null;  
   }
   catch (Error) {
     return null;
   }    
}

function GetMapLastAction()
{
  var mapFrame = GetMapFrame();
  if (mapFrame)    
    return mapFrame.lastAction; 
  else 
    return 1;  
}

function RefreshMap()
{
    var mapFrame = GetMapFrame(); 
    if (mapFrame)
        mapFrame.Refresh();
    top.Loading('');
}

function MapUnitsToScreen(x, y) 
{ 
    var mapFrame = GetMapFrame();
    
    if (mapFrame)
    { 
        xScr = parseInt( (x - mapFrame.extX1) * mapFrame.mapDevW / (mapFrame.extX2 - mapFrame.extX1) ); 
        yScr = parseInt( (y - mapFrame.extY1) * mapFrame.mapDevH / (- mapFrame.extY1 + mapFrame.extY2) ); 
        
        if(xScr > mapFrame.mapDevW - 1) xScr = mapFrame.mapDevW - 1; 
        else if(xScr < 0) xScr = 0; 

        if(yScr > mapFrame.mapDevH - 1) yScr = mapFrame.mapDevH - 1; 
        else if(yScr < 0) y = 0; 
        
        return new Point(xScr, yScr); 
    }

    return null;
} 


function hasDialogProperty(properties)
{ 
  for (i=0; i < properties.length; i++) 
  {
    var name = properties[i].name.toUpperCase();
    for (j=0; j < hasDialogPropNames.length; j++)
    { 
      if (name == hasDialogPropNames[j].toUpperCase())
        return name;     
    }
  }
  return null; 
}

function hasUrl(xmlIn)
{
  if (xmlIn) 
  {
    var childNode = xmlIn.childNodes[0];
    if (childNode) 
      return childNode.nodeValue;
    else 
      return null;         
  } else
    return null;
}

function GetProp(properties,start,end,sep,nextLine,boldValue,dataPath)
{
  var bs = boldValue?'<b>':'';
  var be = boldValue?'</b>':'';
  var br = nextLine?'<br/>':''; 
  top.DialogDataFramesPath = dataPath?dataPath : '';  
  var res = '';
  for (i=start; i<=end; i++)
    res += properties[i].name + sep + bs + properties[i].value + be + br;
  return res;  
}

function CreatePropHTML(name,properties)
{   
   var iHTML = '';   
   switch (name) {        
     case 'NAZWA PLANU': iHTML = GetProp(properties,0,2,": ",true,true,"Dane/Plany");  break;    
   }  
  return iHTML;
}

function SetFeatureDialog(url,name,width,height)
{
    propertyUrl = url;
    propertyName = name;
    dialogWidth = width;
    dialogHeight = height;
}

function ShowFeatureDialog(properties,xmlIn)
{  
  var dlgPropertyName = hasDialogProperty(properties);
  var url = null;
  var view = false;
  
  //rozklady jazdy jeżeli przystanek 
  if (dlgPropertyName == "RODZAJ")
  {
     var id = properties[0].value;    
     SetFeatureDialog('View.aspx?stop_id='+id,'Przystanek',500,600);   
     view = true;
  } else   
    SetFeatureDialog(null,null,null,null);
    
  if (!dlgPropertyName)
  {
    dlgPropertyName = propertyName;
    url = propertyUrl;   
    propertyUrl = null;
    propertyName = null; 
  }
  if (dlgPropertyName)  {        
    if (!url)         
        url =  hasUrl(xmlIn);  
    if (!url)    
        url = propertyUrl;           
    var propHTML = CreatePropHTML(dlgPropertyName, properties);   
    var width = dialogWidth ? dialogWidth : 200; 
    var dlgLeft = view ? top.ViewLeft : top.DialogLeft;
    var dlgTop = view ? top.ViewTop : top.DialogTop;          
    top.showDynamicFrame('iDialog', 'Dialog.aspx', 220 ,100, 220, 100, 220, 0, null,  
        'Information', 'Styles/Img/Glyphs/Info24.png', url, propHTML);          
   // showFrameDialog('iDialog','dragDialog', null, null, null, dlgLeft, dlgTop, width, dialogHeight, 'Information', 'Styles/Img/Glyphs/Info24.png', url, propHTML);           
    dialogWidth = null;
    dialogHeight = null;
  }   
}

