﻿//zakończenie sesji
var TimeOut =  0;
var sessionTimeOut = -1;

function RunCheckSessionExpired()
{    
    top.window.clearTimeout(TimeOut); 
    TimeOut = top.window.setTimeout('DoSessionExpired();', sessionTimeOut);
}

function DoSessionExpired()
{   
    PageMethods.DestroySession(RedirectLogout, null, null);            
}

function RedirectLogout()
{   
    top.window.location = "Logout.aspx";    
}

function RedirectPage(url)
{
    top.window.location = url;
}

//zakończenie aplikacji

function DoServiceUnload()
{    
   DisposeListResult();         
   MapService.DestroySession(null, null, null);                
}

//parametry mapy
var sessionId;
var mapLang;
var mapService = '';
var mapParams = '';
var mapLocation;

function SaveMapParams(_session, _lang, _service, _maplocation)
{
    sessionId = _session; 
    mapLang = _lang.toString().substr(3,2);   
    mapService = _service;  
    mapLocation = _maplocation;
}

function SetUserMapLang(lang)
{
    mapLang = lang;  
}

//stałe globalne
var Ortofotomapa = 'Ortofotomapa 2008';
var MainPage = 'Mapa.aspx';

//załadownie listy obiektów sportowych
var LoadSportList = false;

//zmienne globalne dialogi
var DialogPropertiesInnerHTML = '';
var DialogDataFramesPath = '';
var DialogTop = 70;
var DialogLeft = 200;
var ViewTop = 70;
var ViewLeft = 200;
var MenuLeft = 200; 
var MenuTop = 62;

function SaveDialogPosition(obj)
{
  if (obj) {      
    var iframe = obj.contentWindow ? obj.contentWindow.frames[1] : null; 
    var viewDlg = false;
    if (iframe)
    {        
        var loc = iframe.location.toString();
        if (loc)
          viewDlg = loc.indexOf("View.aspx") > -1; 
    }     
    if (obj.style.top)
    {  
      if (!viewDlg)   
        DialogTop = parseInt(obj.style.top, 10);      
      else
        ViewTop = parseInt(obj.style.top, 10);   
    }
    if (obj.style.left)
    {
      if (!viewDlg) 
        DialogLeft = parseInt(obj.style.left, 10);            
      else
        ViewLeft = parseInt(obj.style.left, 10);            
    }  
  }     
}

//zmienne globalne help
var StartHelpPage = GetHostPath() +  'localized/help/pl/main.htm';

//aktualny widok mapy
var initialViewScale = 128000;
var RestoreMapView = false;
var ShowOrtoOnInit = false;

function Point(x, y)
{
  this.X = x;
  this.Y = y;
}

function UserMapView(center,scale)
{
  this.center = center;  
  this.scale = scale;
}

function SetUserMapView(x,y,scale)
{
  if (mapView) {
    mapView = new UserMapView(new Point(x, y), scale);      
    RestoreMapView = true; 
  } 
}

var mapView = new UserMapView(null,0);  

function SaveMapView()
{                                
  var ajaxmap = GetAjaxMap();  
  if (ajaxmap)
    top.RestoreMapView = true;                 
}

function SetShowOrtoOnInit(visible)
{
  ShowOrtoOnInit = visible;
} 

//tworzenie linku widoku mapy
function GetCurrentViewLink()
{
  var appPath = GetAppPath(); 
  var res = appPath;
  if (top.mapView.center) {  
     var page = '';  
     if (res.indexOf(MainPage) < 0)
       page = MainPage;  
     var orto = top.IsLayerVisible(top.Ortofotomapa) ? "&O=1" : "&O=0";       
     res += page + '?service=' + top.mapService + '&L=' + top.mapLang +
       '&X=' + top.mapView.center.X + '&Y=' + top.mapView.center.Y + '&S=' + top.mapView.scale + orto;
  }      
  return res; 
}

//scieżka hosta
function GetHostPath()
{
   var hostPath = top.window.location.hostname;            
   var path = "http://" + hostPath + "/";      
   return path; 
}

//scieżka aplikacji z parametrami
function GetAppPathWithParams()
{   
  return top.window.location.toString();     
}

//scieżka aplikacji bez parametrów
function GetAppPath()
{   
  var loc = top.window.location.toString();     
  var index = loc.indexOf('?',0);
  if (index < 1)
    return top.window.location.toString();     
  else {
    var path = loc.substr(0, index);   
    return path;
  }  
}

function GetAppVirtualPath()
{
    var path = GetAppPath();
    path = path.toString().replace("Mapa.aspx","");
    return path;
}

//id czasu
function GetTimeId()
{
    var dt = new Date();   
    var t = dt.getHours().toString() + dt.getMinutes().toString() + dt.getSeconds().toString() + 
       dt.getMilliseconds().toString();  
    return t;   
}

//ramki
function GetFrameFromMainFrame(idMainFrame, idFrame)
{
  var obj = null;
  var iMainFrame = document.getElementById(idMainFrame)  
  if (iMainFrame) {  
     var doc = iMainFrame.contentWindow.document;
     if (doc) 
       obj = doc.getElementById(idFrame);
     else
       obj = iMainFrame;   
  }
  return obj;              
}

//nawigacja - historia
var mapHistory = new Array();
var mapHistoryIndex = -1; 
var mapNeedSaveHistory = true;

function ClearHistory()
{
  mapHistoryIndex = -1;
  mapHistory.length = 0; 
}

function SaveHistory(obj)
{ 
  if (obj && obj.contentWindow && mapNeedSaveHistory) {
    var url = obj.contentWindow.location.toString();       
    if (url == 'about:blank') //w przypadku chrome
      return; 
    mapHistory.length = mapHistoryIndex + 1;         
    mapHistory.push(url);                      
    mapHistoryIndex += 1;                   
  }     
  mapNeedSaveHistory = true;     
}

function GotoPage(idMainFrame, idFrame, delta)
{
  var iFrame = GetFrameFromMainFrame(idMainFrame, idFrame);   
  if (iFrame && iFrame.contentWindow) {                
    mapHistoryIndex = mapHistoryIndex + delta;      
    if (mapHistoryIndex < 0) 
      mapHistoryIndex = 0;
    if (mapHistoryIndex > mapHistory.length - 1)
      mapHistoryIndex = mapHistory.length - 1;            
    mapNeedSaveHistory = false;               
    iFrame.contentWindow.location.replace(mapHistory[mapHistoryIndex]);              
  }    
  return false;      
}

function GotoBackPage(idMainFrame, idFrame)
{
  return  GotoPage(idMainFrame, idFrame, -1);
}

function GotoNextPage(idMainFrame, idFrame)
{
  return  GotoPage(idMainFrame, idFrame, 1);
}

function GotoStartPage(idMainFrame, idFrame, url)
{  
  var obj = GetFrameFromMainFrame(idMainFrame,idFrame);
  if (obj) 
  {
    var doc = obj.contentWindow;
    if (doc)
        doc.location.replace(url);               
  }  
  return false;
}