/**(C)&(P) by Maciej Marczewski, Blue Media Sp. z o.o. 2008 All rights reserved */


  function MapToolsControl (){
    
    this.maptoolsUrl = itemsUrl + "mm/img/maptools/";
  };
 
  MapToolsControl.prototype = new GControl();
 
  MapToolsControl.prototype.initialize = function(map) {
    var container = document.createElement("div");
    
    var bg = this.createContainer();
    container.appendChild(bg);
    
    bg.appendChild(this.createNavigator());
/*    
    var zoomInDiv = document.createElement("div");
    //this.setButtonStyle_(zoomInDiv);
    bg.appendChild(zoomInDiv);
    zoomInDiv.appendChild(document.createTextNode("Zoom In2"));
    GEvent.addDomListener(zoomInDiv, "click", function() {
      map.zoomIn();
    });
    */
    bg.appendChild(this.createSlider());  
    bg.appendChild(this.createIcons());
    

  /*
    var zoomOutDiv = document.createElement("div");
    this.setButtonStyle_(zoomOutDiv);
    bg.appendChild(zoomOutDiv);
    zoomOutDiv.appendChild(document.createTextNode("Zoom Out"));
    GEvent.addDomListener(zoomOutDiv, "click", function() {
      map.zoomOut();
    });
    */
    map.getContainer().appendChild(container);
    this.initSlider();
    
    return container;
  }
  
  MapToolsControl.prototype.createContainer = function(){
    var el = document.createElement("div");
    el.id = "suwak";
    return el;
  };
  
  MapToolsControl.prototype.createIcons = function(){
    var el = document.createElement("div");
    el.id = "icons";

      var child = document.createElement("span");
      child.id = "polska_mapa";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "polska.png";
        img.width = "26";
        img.height = "22";
        img.className = "polska";
        child.appendChild(img);                  
        $(img).click(function(){
          resetMap();
        });

      el.appendChild(child);  
      
      var child = document.createElement("span");
      child.id = "refresh_map";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "odswiez.png";
        img.width = "20";
        img.height = "24";
        img.className = "odswiez";
        child.appendChild(img);
      el.appendChild(child);  

      $(child).click(function(){
        refreshMap();
      });                          
          
      var child = document.createElement("div");
      child.id = "legenda";
      el.appendChild(child);  
      
    return el;
  };
  
  
  MapToolsControl.prototype.createNavigator = function(){
    var el = document.createElement("table");
    $(el).append('<tr><td colspan="3" class="nawigacja_top"><img src="'+this.maptoolsUrl+'nawigacja_top.png" onclick="moveUp()"/></td></tr>');
    $(el).append('<tr><td class="nawigacja_left"><img src="'+this.maptoolsUrl+'nawigacja_left.png" onclick="moveLeft()"/></td><td class="nawigacja_center"><img src="'+this.maptoolsUrl+'nawigacja_center.png" onclick="refreshMap()"/></td><td class="nawigacja_right"><img src="'+this.maptoolsUrl+'nawigacja_right.png" onclick="moveRight()"/></td></tr>');
    $(el).append('<tr><td colspan="3" class="nawigacja_bottom"><img src="'+this.maptoolsUrl+'nawigacja_bottom.png" onclick="moveDown()"/></td></tr>');
    return el;
  }; 
  MapToolsControl.prototype.createSlider = function(){
    var el = document.createElement("div");
    el.className = "slider_wrapper";
         
      var child = document.createElement("div");
      child.id = "plus";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "plus.png";
        img.width = "16";
        img.height = "16";
        img.className = "plus";
        $(img).click(function(){
          zoomIn();
        });        
        child.appendChild(img);                  
      el.appendChild(child);  

      
      var img = document.createElement("div");
      img.id = "slider";
      //img.className = "ui-slider";
      
      el.appendChild(img);  

      var child = document.createElement("a");
      child.id = "minus";
      //child.href="javascript:zoomOut()";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "minus.png";
        img.width = "16";
        img.height = "16";
        img.className = "minus";
        $(img).click(function(){
          zoomOut();
        });
        
        child.appendChild(img);                  
      el.appendChild(child);  
      

    return el;
  } ; 
  MapToolsControl.prototype.initSlider = function(){
   setTimeout(function(){
     $("#slider").slider({
          orientation: 'vertical',          
          max: 18,
          value: 6, 
          change: function(e, ui) {
              var wartosc = ui.value;
              map.setZoom(wartosc);
              
          }
    });
    
   },1000);
  };
  // By default, the control will appear in the top left corner of the
  // map with 7 pixels of padding.
  MapToolsControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
  }
  

//--------------------------------------------------------------------------------
  function MapTypeControl (){
    this.maptoolsUrl = itemsUrl + "mm/img/maptools/";
  };
 
  MapTypeControl.prototype = new GControl();
  
  MapTypeControl.prototype.initialize = function(map) {

    var container = this.createMapTypeIcons();
    map.getContainer().appendChild(container);        
    return container;
  }
  
  MapTypeControl.prototype.createContainer = function(){
    var el = document.createElement("div");
    el.id = "suwak";
    return el;
  };
  
  MapTypeControl.prototype.createMapTypeIcons = function(){
    var el = document.createElement("div");
    el.id = "przyciski";
    /*
    var ref = this;
      var child = document.createElement("a");
      child.className = "active left";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "mapa_a.png";
        img.width = "75";
        img.height = "20";
        child.appendChild(img);                  
      el.appendChild(child);  
      
      $(child).click(function(){
        resetMapTypeIcons();
        if($.browser.msie){
          if ($.browser.version === "6.0"){
            $(this).css("filter",'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/_items/bzwbk.pl/mm/img/maptools/mapa_a.png", sizingMethod="image")');          
          }        
        } else {
          $("#a_but_mapa img").attr("src", ref.maptoolsUrl + "mapa_a.png");
          
        }
        showNormalMap();
      });   
         
      var child = document.createElement("a");
      child.className = "left";
      child.id = "a_but_satelita";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "satelita_n.png";
        
        img.width = "75";
        img.height = "20";
        child.appendChild(img);                  
      el.appendChild(child);  
      
      $(child).click(function(){
        resetMapTypeIcons();
        if($.browser.msie){
          if ($.browser.version === "6.0"){
            $(this).css("filter",'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/_items/bzwbk.pl/mm/img/maptools/satelita_a.png", sizingMethod="image")');          
          }        
        } else {
          $("#a_but_satelita img").attr("src", ref.maptoolsUrl + "satelita_a.png");
        
        }
        showSatelliteMap();
      });
      
      var child = document.createElement("a");
      child.className = "left";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "hybryda_n.png";
        img.width = "75";
        img.height = "20";
        child.appendChild(img);                  
      el.appendChild(child);              
      $(child).click(function(){
        resetMapTypeIcons();
        if($.browser.msie){
          if ($.browser.version === "6.0"){
            $(this).css("filter",'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/_items/bzwbk.pl/mm/img/maptools/hybryda_a.png", sizingMethod="image")');          
          }        
        }else {
          $("#a_but_hybryda img").attr("src", ref.maptoolsUrl + "hybryda_a.png");
        
        }
        showHybridMap();
      });              
     */
    var ref = this;
      var child = document.createElement("a");
      child.className = "active left";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "mapa_a.png";
        img.width = "75";
        img.height = "20";
        $(img).click(function(){
          resetMapTypeIcons();
          this.src = ref.maptoolsUrl + "mapa_a.png";
          showNormalMap();
        });
        child.appendChild(img);                  
      el.appendChild(child);  
      
      var child = document.createElement("a");
      child.className = "left";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "satelita_n.png";
        img.width = "75";
        img.height = "20";
        $(img).click(function(){
          resetMapTypeIcons();
          this.src = ref.maptoolsUrl + "satelita_a.png";
          showSatelliteMap();
        });

        child.appendChild(img);                  
      el.appendChild(child);  

      var child = document.createElement("a");
      child.className = "left";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "hybryda_n.png";
        img.width = "75";
        img.height = "20";
        $(img).click(function(){
          resetMapTypeIcons();
          this.src = ref.maptoolsUrl + "hybryda_a.png";
          showHybridMap();
        });

        child.appendChild(img);                  
      el.appendChild(child);                          
    return el;
  };
  
  
  // By default, the control will appear in the top left corner of the
  // map with 7 pixels of padding.
  MapTypeControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0, 0));
  }

//------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
  function LoadingIcon (){
    this.maptoolsUrl = itemsUrl + "mm/img/maptools/";
  };
 
  LoadingIcon.prototype = new GControl();
  
  LoadingIcon.prototype.initialize = function(map) {

    var container = this.createIcon();
    map.getContainer().appendChild(container);        
    return container;
  }
    
  LoadingIcon.prototype.createIcon = function(){
    var el = document.createElement("div");
    el.id = "loading_icon";
    
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "ajax-loader-big.gif";
        img.width = "128";
        img.height = "15";
        el.appendChild(img);                         
            
    return el;
  };
  
 
  // By default, the control will appear in the top left corner of the
  // map with 7 pixels of padding.
  LoadingIcon.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(280, 5));
  }
//--------------------------------------------------------------------------------
  function VisaLoadingIconBig (){
    this.maptoolsUrl = itemsURL + "mm/img/maptools/";
  };
 
  VisaLoadingIconBig.prototype = new GControl();
  
  VisaLoadingIconBig.prototype.initialize = function(map) {

    var container = this.createIcon();
    map.getContainer().appendChild(container);        
    return container;
  }
    
  VisaLoadingIconBig.prototype.createIcon = function(){
    var el = document.createElement("div");
    el.className = "loading_icon";
    
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "ajax-loader-visa-big.gif";
        img.width = "128";
        img.height = "15";
        el.appendChild(img);                         
            
    return el;
  };
  
 
  // By default, the control will appear in the top left corner of the
  // map with 7 pixels of padding.
  VisaLoadingIconBig.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(280, 5));
  }

//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
  function ShowVisaControl (){
    this.maptoolsUrl = itemsUrl + "mm/img/maptools/";
  };
 
  ShowVisaControl.prototype = new GControl();
  
  ShowVisaControl.prototype.initialize = function(map) {

    var container = this.createMapTypeIcons();
    map.getContainer().appendChild(container);        
    return container;
  }
  
  ShowVisaControl.prototype.createContainer = function(){
    var el = document.createElement("div");
    el.id = "suwak";
    return el;
  };
  
  ShowVisaControl.prototype.createMapTypeIcons = function(){
    var el = document.createElement("div");
    el.className = "przyciski";
    el.id = "visa_loader_button";
    
    var ref = this;
      var child = document.createElement("a");
      child.className = "active left";
      child.title = "pokaż/ukryj punkty VISA CashBack";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "visa_n.png";
        img.width = "75";
        img.height = "20";
        $(img).toggle(function(){
          //resetMapTypeIcons();
          mVisaLoader.activate();
          //this.src = ref.maptoolsUrl + "visa_a.png";
          //showNormalMap();
          //showVisaPoints();
        },function(){
          mVisaLoader.deactivate();
          //this.src = ref.maptoolsUrl + "visa_n.png";
        });
        child.appendChild(img);                  
      el.appendChild(child);  
      
    return el;
  };
  
  
  // By default, the control will appear in the top left corner of the
  // map with 7 pixels of padding.
  ShowVisaControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(80, 0));
  }

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
  function VisaLoadingIcon (){
    this.maptoolsUrl = itemsURL + "mm/img/maptools/";
  };
 
  VisaLoadingIcon.prototype = new GControl();
  
  VisaLoadingIcon.prototype.initialize = function(map) {

    var container = this.createIcon();
    map.getContainer().appendChild(container);        
    return container;
  }
    
  VisaLoadingIcon.prototype.createIcon = function(){
    var el = document.createElement("div");
    el.id = "visa_loading_icon";
    
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "ajax-loader-visa.gif";
        img.width = "16";
        img.height = "11";
        el.appendChild(img);                         
            
    return el;
  };
  
 
  // By default, the control will appear in the top left corner of the
  // map with 7 pixels of padding.
  VisaLoadingIcon.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(170, 15));
  }

//-----------------------------------------------------------------------------------------------------
//maptools

function resetMapTypeIcons() {
    $("#przyciski a img").each(
      function () {
        var src = $(this).attr("src");
        $(this).attr("src", src.replace(/_a.png/i, "_n.png"));
        if($.browser.msie){
          if ($.browser.version === "6.0"){
            $(this).css("filter", src.replace(/_a.png/i, "_n.png"));

           // $(this).css("filter",'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/_items/bzwbk.pl/mm/img/maptools/satelita_a.png", sizingMethod="image")');          
          }        
        }        
        
        
      });
}

function moveSliderTo(zoomLevel) {
    $(".ui-slider").slider('option','value', zoomLevel);
}

function searchClicked(){
  var idcity = $("#city").val();
  var idregion = $("#region").val();
  if  ( idcity !== "0"){
      citySelect(idcity,1,0);
  } else if (idregion !== "0"  ){
      regionSelect(idregion,1,0);
  }else {
      resetMap();
  }

}
