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

/** imap - map interface */
  function MapToolsControl (imap,smapid){
    
    this.maptoolsUrl = itemsUrl + "mm/images/maptools/";
    this.imap = imap;
    this.smapid = smapid;
  };
 
  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" + this.sufix;
    el.className = "suwak";
    return el;
  };
  
  MapToolsControl.prototype.createIcons = function(){
    var ref = this;
    var el = document.createElement("div");
    //el.id = "icons" + this.sufix;
    el.className = "icons";  
      var child = document.createElement("span");
      //child.id = "polska_mapa" + this.sufix;
      child.className = "polska_mapa";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "polska.png";
        img.width = "32";
        img.height = "28";
        img.className = "polska";
        child.appendChild(img);                  
        $(img).click(function(){
          //console.debug(ref.imap);
          ref.imap.resetMap();
        });

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

      $(child).click(function(){
        ref.imap.refreshMap();
      });                          
          
      var child = document.createElement("div");
      //child.id = "legenda" + this.sufix;
      child.className = "legenda";
      el.appendChild(child);  
      
    return el;
  };
  
  
  MapToolsControl.prototype.createNavigator = function(){
    var ref = this;
    var el = document.createElement("table");
    
      var oBody = document.createElement("tbody");
        var oTr = document.createElement("tr");
         
          var oTd = document.createElement("td");
          oTr.appendChild(oTd);
          
          var oTd = document.createElement("td");
          oTd.className = "nawigacja_top";
          //oTd.setAttribute("colspan","3");
                     
            var img = document.createElement("img");
            img.src = this.maptoolsUrl + "nawigacja_top.png";
            
            oTd.appendChild(img);
            $(img).click(function(){
              ref.imap.moveUp();
            });             
          //oTd.appendChild(div);
                    
          oTr.appendChild(oTd);

          var oTd = document.createElement("td");
          oTr.appendChild(oTd);

          
        oBody.appendChild(oTr);  
        
        var oTr = document.createElement("tr");
          var oTd = document.createElement("td");
          oTd.className = "nawigacja_left";          
            var img = document.createElement("img");
            img.src = this.maptoolsUrl + "nawigacja_left.png";
            oTd.appendChild(img);
            $(img).click(function(){              
              ref.imap.moveLeft();
            });             
          oTr.appendChild(oTd);

          var oTd = document.createElement("td");
          oTd.className = "nawigacja_center";          
            var img = document.createElement("img");
            img.src = this.maptoolsUrl + "nawigacja_center.png";
            oTd.appendChild(img);
            $(img).click(function(){
              ref.imap.resetMap();
            });             
          oTr.appendChild(oTd);

          var oTd = document.createElement("td");
          oTd.className = "nawigacja_right";          
            var img = document.createElement("img");
            img.src = this.maptoolsUrl + "nawigacja_right.png";
            oTd.appendChild(img);
            $(img).click(function(){
              ref.imap.moveRight();
            });             
          oTr.appendChild(oTd);
          
        oBody.appendChild(oTr);  

        var oTr = document.createElement("tr");
        
          var oTd = document.createElement("td");
          oTr.appendChild(oTd);
 
          var oTd = document.createElement("td");
          oTd.className = "nawigacja_bottom";
          //oTd.setAttribute("colspan","3");
            var img = document.createElement("img");
            img.src = this.maptoolsUrl + "nawigacja_bottom.png";
            oTd.appendChild(img);
            $(img).click(function(){
              ref.imap.moveDown();
            });             
          oTr.appendChild(oTd);

          var oTd = document.createElement("td");
          oTr.appendChild(oTd);

        oBody.appendChild(oTr);  
        
      el.appendChild(oBody);
       
    return el;
  }; 
  MapToolsControl.prototype.createSlider = function(){
    var ref = this;
    var el = document.createElement("div");
    el.className = "slider_wrapper";
         

      var child = document.createElement("div");
      //child.id = "minus" + this.sufix;
      child.className = "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(){
          ref.imap.zoomOut();
        });
        
        child.appendChild(img);                  
      el.appendChild(child);  
      
      var img = document.createElement("div");
      img.id = "slider" + this.smapid;
      //img.className = "ui-slider";
     
      el.appendChild(img);  


      var child = document.createElement("div");
      //child.id = "plus" + this.sufix;
      child.className = "plus";
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "plus.png";
        img.width = "16";
        img.height = "16";
        img.className = "plus";
        $(img).click(function(){
          ref.imap.zoomIn();
        });        
        child.appendChild(img);                  
      el.appendChild(child);  


      

    return el;
  } ; 
  MapToolsControl.prototype.initSlider = function(){
   var ref = this;
   //value: ref.imap.czoom,
   setTimeout(function(){   
   
   var slider =  $("#slider"+ref.smapid).slider({
    		axis: "vertical",
    		max: "18",		
    		value: "6",
    		change: function(e, ui) {
    			var wartosc = ui.value;
    			if (bChangeMapZoom === true){
      			map.setZoom(wartosc);      
          }else {
            //przywroc domyslne ustawienie
            bChangeMapZoom = true;
          }
    		}
    });
    ref.imap.slider = slider;
    slider.slider('moveTo',6);
   },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(-2, 0));
  }
  

//--------------------------------------------------------------------------------
  function MapTypeControl (imap,smapid){
    this.maptoolsUrl = itemsUrl + "mm/images/maptools/";
    this.imap = imap;
    this.smapid = smapid;    
  };

  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";
    el.className = "suwak";
    return el;
  };
  MapTypeControl.prototype.resetMapTypeIcons = function(elClicked){
    
    elClicked.parent().find("span").removeClass("active"); //each a
    elClicked.addClass("active");    
  };
  MapTypeControl.prototype.createMapTypeIcons = function(){
    var el = document.createElement("div");
    el.className = "przyciski";

    var ref = this;
      var child = document.createElement("span");
      child.className = "active left";
      //child.href = "#";
      //child.title = "Mapa";
      if (typeof(lang) !=='undefined'){
        if (lang === "en"){
          child.innerHTML = "Map";  
        } else {
          child.innerHTML = "Mapa";
        }
      } else {
          child.innerHTML = "Mapa";      
      }
      
        /*
        var img = document.createElement("img");        
        img.src = this.maptoolsUrl + "mapa_a.png";
        img.width = "75";
        img.height = "20";
        
        $(img).click(function(){
          console.debug($(this).parent());
          resetMapTypeIcons();
          this.src = ref.maptoolsUrl + "mapa_a.png";
          ref.imap.showNormalMap();
        });
        child.appendChild(img);
        */                  
      el.appendChild(child); 
       
      $(child).click(function(){
        //console.debug(ref.imap);
        
        ref.resetMapTypeIcons($(this));
        //this.src = ref.maptoolsUrl + "mapa_a.png";
        ref.imap.setMapType(G_NORMAL_MAP);
      });
            
      var child = document.createElement("span");
      child.className = "left";
      //child.href = "#";
      //child.title = "Satelita";
      if (typeof(lang) !=='undefined'){
        if (lang === "en"){
          child.innerHTML = "Satellite";  
        } else {
          child.innerHTML = "Satelita";
        }
      } else {
          child.innerHTML = "Satelita";      
      }   
        /*
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "satelita_n.png";
        img.width = "75";
        img.height = "20";
        $(img).click(function(){
          ref.resetMapTypeIcons($(this));
          //this.src = ref.maptoolsUrl + "satelita_a.png";
          ref.imap.showSatelliteMap();
        });

        child.appendChild(img);
        */                  
      el.appendChild(child);  
      $(child).click(function(){
        //console.debug($(this).parent());
        
        ref.resetMapTypeIcons($(this));
        //this.src = ref.maptoolsUrl + "mapa_a.png";
        ref.imap.setMapType(G_SATELLITE_MAP);
      });
      var child = document.createElement("span");
      child.className = "left";
      //child.href = "#";
      //child.title = "Hybryda";
      if (typeof(lang) !=='undefined'){
        if (lang === "en"){
          child.innerHTML = "Hybrid";  
        } else {
          child.innerHTML = "Hybryda";
        }
      } else {
          child.innerHTML = "Hybryda";      
      }   
      /*      
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "hybryda_n.png";
        img.width = "75";
        img.height = "20";
        $(img).click(function(){
          ref.resetMapTypeIcons($(this));
          //this.src = ref.maptoolsUrl + "hybryda_a.png";
          ref.imap.showHybridMap();
        });

        child.appendChild(img);
      */                  
      el.appendChild(child);
 
      $(child).click(function(){
        //console.debug($(this).parent());
        
        ref.resetMapTypeIcons($(this));
        //this.src = ref.maptoolsUrl + "mapa_a.png";
        ref.imap.setMapType(G_HYBRID_MAP);
      });          
      
      var child = document.createElement("span");
      child.className = "left";
      //child.href = "#";
      //child.title = "Hybryda";
      if (typeof(lang) !=='undefined'){
        if (lang === "en"){
          child.innerHTML = "Terrain";  
        } else {
          child.innerHTML = "Terenowa";
        }
      } else {
          child.innerHTML = "Terenowa";      
      }         
      
      /*      
        var img = document.createElement("img");
        img.src = this.maptoolsUrl + "hybryda_n.png";
        img.width = "75";
        img.height = "20";
        $(img).click(function(){
          ref.resetMapTypeIcons($(this));
          //this.src = ref.maptoolsUrl + "hybryda_a.png";
          ref.imap.showHybridMap();
        });

        child.appendChild(img);
      */                  
      el.appendChild(child);
 
      $(child).click(function(){
        //console.debug($(this).parent());
        
        ref.resetMapTypeIcons($(this));
        //this.src = ref.maptoolsUrl + "mapa_a.png";
        ref.imap.setMapType(G_PHYSICAL_MAP);
      });             
 
 /*
      var child = document.createElement("span");
      child.className = "left";
      //child.href = "#";
      //child.title = "Hybryda";
      child.innerHTML = "3D";               
      el.appendChild(child);
 
      $(child).click(function(){
        //console.debug($(this).parent());
        
        ref.resetMapTypeIcons($(this));
        //this.src = ref.maptoolsUrl + "mapa_a.png";
        ref.imap.setMapType(G_SATELLITE_3D_MAP);
      });   
       */                     
    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/images/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.className = "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 AddNewIcon (imap){
    this.maptoolsUrl = itemsUrl + "mm/images/maptools/";
    this.imap = imap;
  };
 
  AddNewIcon.prototype = new GControl();
  
  AddNewIcon.prototype.initialize = function(map) {

    var container = this.createIcon();
    map.getContainer().appendChild(container);        
    return container;
  }
    
  AddNewIcon.prototype.createIcon = function(){
    var ref = this;
    var el = document.createElement("div");
    el.className = "przyciski_addnew_icon";
    
        var but = document.createElement("span");
        but.className = "left";
        //but.type = "button";
        //but.value = "dodaj swoje miejsce";
        but.appendChild(document.createTextNode("dodaj nowe miejsce"));
        el.appendChild(but);                         
        
        $(but).click(function(){
          //console.debug(ref.imap);
          $(this).addClass("active");
          ref.imap.startAddNewPointMode();          
        });
            
    return el;
  };
  
 
  // By default, the control will appear in the top left corner of the
  // map with 7 pixels of padding.
  AddNewIcon.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(15, 0));
  }
//------------------------------------------------------------------------------
//--------------------------------------------------------------------------------

function moveSliderTo(zoomLevel) {
   // $(".ui-slider").slider('option','value', zoomLevel);
    var bChangeMapZoom= false;
   	$(".ui-slider").slider("moveTo", zoomLevel);
}


