﻿// JScript File
var PATH = '';//zmienna nadpisywana
   function EInfoWindow(marker,map,style) {
        // parameters
        this._map=map;
        this._style=style;
        this._visible = false;
        this._maxHeight = 200; //maksymalna wysokosc elementu bodyContainer okna
        this._maxWidth = 200;       //maksymalna szerokość elementu bodyContainer okna
        this._marker = marker;
       }
       
       
  EInfoWindow.prototype = new GOverlay();
    
  EInfoWindow.prototype.initialize = function(map) {
       
       this._map = map;
       this._div = document.createElement('div');//całość
       this._hdiv = document.createElement('div');//naglowek
       this._sdiv = document.createElement('div');//rozklad
       this._fdiv = document.createElement('div');//stopka rozkladu 
        
       this._div.appendChild(this._hdiv);this._div.appendChild(this._sdiv);
       this._div.appendChild(this._fdiv);
       this._sdiv.style.overflow = 'auto';
       this._div.style.position = 'absolute';
       this._div.className = this._style; 
       //this._sdiv.style.height = '200px';
       var me = this;
       this._hdiv.innerHTML = '<table style="border-collapse:collapse;width:100%"><thead><tr><th style=\'text-align:left\'>' + Global.lang.linia + '<b>' + this._marker.CnrInfo.routeNr + '</b></th><th style="text-align:right">'
                            + '<img id="button" src=\'' + PATH + 'img/close.png\' style=\'cursor:pointer\');"></img>';
                            + '</th></tr></thead></table>';
       
       this._fdiv.innerHTML  ='<div style="border:1px solid white;background:black"><input type="checkbox" id="chStopsIw"  />' + Global.lang.pokPrz 
           + '<input type="checkbox" id="chRoutesIw"  />' + Global.lang.rysLinie + '<br />'
           + '<input type="checkbox" id="chLabelsIw"  />' + Global.lang.pokNr +  '<br />'
           + '<a href="#" style="color:white">' + Global.lang.centrDoTr + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" style="color:white"> >><< </a></div> ';
       
       
       this._closeBut = this._hdiv.firstChild.firstChild.firstChild.childNodes[1];
       this._chStops = this._fdiv.firstChild.childNodes[0];
       this._chLine = this._fdiv.firstChild.childNodes[2];
       this._chLabels = this._fdiv.firstChild.childNodes[5];
       this._aTCentre = this._fdiv.firstChild.childNodes[8];
       this._aVCentre = this._fdiv.firstChild.childNodes[10];
      
       
       GEvent.addDomListener(this._closeBut,"click",function(){me.onCloseClick();});
       GEvent.addDomListener(this._chStops,"click",function(){me.chSchowStopsChanged(me._chStops.checked);});
       GEvent.addDomListener(this._chLine,"click",function(){me.chSchowLineChanged(me._chLine.checked);});
       GEvent.addDomListener(this._chLabels,"click",function(){me.chSchowLabelsChanged(me._chLabels.checked);});
       GEvent.addDomListener(this._aTCentre,"click",function(){me.centr2TClick();});
       GEvent.addDomListener(this._aVCentre,"click",function(){me.centre2VClick();});
       
       map.getContainer().appendChild(this._div);
   
  }
      
      
  EInfoWindow.prototype.openOnMouseCursor = function(headerHtml,bodyHtml,footHtml)
  {
    var point = null;
   // GEvent.trigger
  }
   
 
      
  EInfoWindow.prototype.openOnMarker = function() {
        this._point = this._marker.getPoint();
        var vx = this._marker.getIcon().iconAnchor.x - this._marker.getIcon().infoWindowAnchor.x;
        var vy = this._marker.getIcon().iconAnchor.y - this._marker.getIcon().infoWindowAnchor.y;
        
        
        if(this._isVariance)
            this._varSpan.innerHTML = this._marker.Variance;
        
        
        this._sdiv.innerHTML = "<span style=\"text-align:center\">" + Global.lang.czekaj +"</span>"
        this._visible = true;
        this.redraw(true);
      }
      
EInfoWindow.prototype.redraw = function(force) {
       if (!this._visible) return;
        
       if(this._marker!=null)
            this._point = this._marker.getPoint();
            
 
        var bounds = this._map.getBounds();
        var swp = this._map.fromLatLngToContainerPixel(bounds.getSouthWest());
        var nep = this._map.fromLatLngToContainerPixel(bounds.getNorthEast());
 
       //wysokosć oraz szerokość mapy
        var width =  this._map.getContainer().offsetWidth;
        var height = this._map.getContainer().offsetHeight;
 
        var p = this._map.fromLatLngToContainerPixel(this._point);
        var center = p.x;
        var top = p.y - 30;;
        
        
        if(this._div != null){//jeśli otwarte jest okno z rozkładem
           //ustawienie odpowiedniego rozmiaru sekcji body
           if (this._div.offsetHeight>this._maxHeight) this._div.childNodes[1].style.height =  this._maxHeight-this._div.childNodes[0].offsetHeight - this._div.childNodes[2].offsetHeight +"px";
           if (this._div.offsetWidth>this._maxWidth) this._div.childNodes[1].style.width    =  this._maxWidth + "px";
            
            var left = center - this._div.offsetWidth/2; //przesunięcie okna na środek markera
            top =  top - this._div.offsetHeight; //przeunięcie okna nad marker
           //okno nie może wychodzić poza mapę
            if(left < swp.x) left=swp.x;
            if(left + this._div.offsetWidth  > nep.x) left=nep.x - this._div.offsetWidth;
            if(top < nep.y) top = nep.y;
            if(top + this._div.offsetHeight > swp.y) top = swp.y - this._div.offsetHeight;
            
            this._div.style.left   = left + "px";
            this._div.style.top    = top + "px";
       }
        
}
      
 EInfoWindow.prototype.setPoint = function(point) {
        this._point = point;
        this.redraw(true);
 }   
      
 EInfoWindow.prototype.remove = function() {
        
        GEvent.clearListeners(this._closeBut);
        GEvent.clearListeners(this._chStops);
        GEvent.clearListeners(this._chLine);
        GEvent.clearListeners(this._chLabels);
        GEvent.clearListeners(this._aTCentre);
        GEvent.clearListeners(this._aVCentre);
              
        
        this._div.parentNode.removeChild(this._div);
        if(this._div.outerHTML) this._div.outerHTML = "";
       
        this._div.innerHTML="";
        this._div=null;
     }
EInfoWindow.prototype.getHeaderContainer = function()
{
    return this._div.firstChild;
}
EInfoWindow.prototype.getBodyContainer = function()
{
    return this._div.childNodes[1];
}
EInfoWindow.prototype.getFoorContainer = function()
{
    return this._div.childNodes[2];
}
EInfoWindow.prototype.hideSchedules = function(){
    this._isSchedules = false;
    this._sdiv.innerHTML = "";
    this._fdiv.inenrHTML = "";

}

EInfoWindow.prototype.getScheduleData = function(id_kursu,nr_lini,war_trasy,id_prz,isStops,isPoly,isLabels)
{

     var result = new StringBuffer();
     var me = this;
     var wbs = new WebServiceClient("CNR_DajStatTabliczkeAutobusow")
     wbs.appendProperty('id_kursu',id_kursu);
     wbs.appendProperty('nr_lini',nr_lini);
     wbs.appendProperty('war_trasy',war_trasy);
     wbs.appendProperty('id_przyst',id_prz);
     
     
     this._sdiv.innerHTML = "<span style=\"text-align:center\">" + Global.lang.czekaj + "</span>"
     //this._fdiv.innerHTML  ='<div style="border:1px solid white;background:black"><input type="checkbox" '+(isStops?'checked="checked"':"")+'   onclick="setOption(\'chStopsIw\')" id="chStopsIw"  />Pokaż przystanki'
       //    + '<input type="checkbox" '+(isPoly?'checked="checked"':"")+'onclick="setOption(\'chRoutesIw\')" id="chRoutesIw"  />Rysuj linie<br />'
         //  + '<input type="checkbox" '+(isLabels?'checked="checked"':"")+' onclick="setOption(\'chLabelsIw\')" id="chLabelsIw"  />Pokaż numery przystanków<br /></div>';
 
    
     this._isSchedules = true;
     this._chLine.checked = isPoly;
     this._chLabels.checked = isLabels;
     this._chStops.checked = isStops; 
     
       
     wbs.send(function(xmlDoc){
         me.beginLoading();
         var drive = xmlDoc.getElementsByTagName("Schedules");
         
         if (drive == null || drive[0]==null || !drive[0].hasChildNodes()){me._sdiv.innerHTML ="<span style=\"text-align:center\">" + Global.lang.nieznal + "</span>";return;}
         var stops = drive[0].childNodes;
         if(stops == null) {this._sdiv.innerHTML ="<center><span style=\"text-align:center\">" + Global.lang.nieznal + "</span></center>";return;};
         
         var result = new StringBuffer()
         result.append("<table class='schedules'>");
         result.append('<thead><tr><th colspan=2>' + Global.lang.przystanki +  ':<b>'+ stops.length +'</b></th></tr></thead>')   ;
         
         for(var i=0;i<stops.length;++i){
            var time = stops[i].getAttribute("th")==""?stops[i].getAttribute("tm"):stops[i].getAttribute("th")+":"+stops[i].getAttribute("tm");
            result.append('<tr><td>' + stops[i].getAttribute("name") + '</td><td><b>'+ time +'<b></td></tr>');
       }
       
         result.append("</table>");
         me._sdiv.innerHTML = result.toString();
         me.redraw(true);
         me.endLoading();
     });   
}


 //pokaż odchylenie
 EInfoWindow.prototype.showVariance =  function(value){
    this._isVariance = true;
    this._varSpan.style.display='block';
 
 
 
 }
 //ukryj odchylenie
 EInfoWindow.prototype.hideVariance =  function(){
    this._isVariance = false;
    this._varSpan.style.display='none';
 }
 //pokaż różnice
 EInfoWindow.prototype.showDif =  function(){
    this._isDif = true;
    this._varSpan.style.display='block';
 }
//ukryj różnice
 EInfoWindow.prototype.hideDif =  function(){
    this._isDif = false;
    this._varSpan.style.display='none';
 }
 


EInfoWindow.prototype.beginLoading = function(){};
EInfoWindow.prototype.endLoading = function(){};
//odpalane gdy zmieni się stan checkBoxa
EInfoWindow.prototype.chSchowStopsChanged = function(){}
//odpalane gdy zmieni się stan checkBoxa
EInfoWindow.prototype.chSchowLineChanged = function(){}
//odpalane gdy zmieni się stan checkBoxa
EInfoWindow.prototype.chSchowNumsChanged = function(){}
EInfoWindow.prototype.centr2TClick = function(){}
EInfoWindow.prototype.centre2VClick = function(){}

/*

 
 //pokaż odchylenie
 EInfoWindow.prototype.showVariance =  function(value){
    this._isVariance = true;
    this._varSpan.style.display='block';
 }
 //ukryj odchylenie
 EInfoWindow.prototype.hideVariance =  function(){
    this._isVariance = false;
    this._varSpan.style.display='none';
 }
 //pokaż różnice
 EInfoWindow.prototype.showDif =  function(){
    this._isDif = true;
    this._varSpan.style.display='block';
 }
//ukryj różnice
 EInfoWindow.prototype.hideDif =  function(){
    this._isDif = false;
    this._varSpan.style.display='none';
 }
 */
 
 EInfoWindow.prototype.onCloseClick = function(){}
 
 




      


