/*-----------------------------------------------------------
*(C) 2010 Palo TT (palinow at atlas dot cz)
*overrides for support baselayers with different projection, 
*code based on http://trac.openlayers.org/ticket/1249
*
*
*Overwriting OL classes  howto:
*http://openlayers.org/pipermail/users/2009-April/011227.html
----------------------------------------------------------*/


     /** 
     * APIMethod: setBaseLayer
     * Allows user to specify one of the currently-loaded layers as the Map's
     *     new base layer.
     * 
     * Parameters:
     * newBaseLayer - {<OpenLayers.Layer>}
     */
  OpenLayers.Map.prototype.setBaseLayer = function(newBaseLayer) {
        var oldExtent = null;
        var oldProj = null;  // Reproject centre point  - Palo TT, 10/2009 -----------------

        if (this.baseLayer) {
            oldExtent = this.baseLayer.getExtent();
            oldProj = this.baseLayer.projection;   // Reproject centre point  - Palo TT, 10/2009 -----------------
        }

        if (newBaseLayer != this.baseLayer) {
          
            // is newBaseLayer an already loaded layer?m
            if (OpenLayers.Util.indexOf(this.layers, newBaseLayer) != -1) {

                // make the old base layer invisible 
                if (this.baseLayer != null && !this.allOverlays) {
                    this.baseLayer.setVisibility(false);
                }

                // set new baselayer
                this.baseLayer = newBaseLayer;
                
                // Increment viewRequestID since the baseLayer is 
                // changing. This is used by tiles to check if they should 
                // draw themselves.
                this.viewRequestID++;
                if(!this.allOverlays) {
                    this.baseLayer.visibility = true;
                }

                //redraw all layers
                var center = this.getCenter();
                if (center != null) {

                    //either get the center from the old Extent or just from
                    // the current center of the map. 
                    var newCenter = (oldExtent) 
                        ? oldExtent.getCenterLonLat()
                        : center;
                    
                    // Reproject centre point  - Palo TT, 10/2009 -----------------
                    var newProj= this.baseLayer.projection;
                    if (this.baseLayer.projection.projCode!=oldProj.projCode){
                      newCenter.transform(oldProj,newProj); 
                    }                     
                    // Reproject centre point  - Palo TT, 10/2009 -----------------
                    

                    //the new zoom will either come from the old Extent or 
                    // from the current resolution of the map                                                
                    var newZoom = (oldExtent) 
                        ? this.getZoomForExtent(oldExtent, true)
                        : this.getZoomForResolution(this.resolution, true);

                    // zoom and force zoom change
                    this.setCenter(newCenter, newZoom, false, true);



                    // Reproject centre point  - Palo TT, 10/2009 -----------------
                     if(!oldProj.equals(newProj)) { 
	                      
				                                        
                                      if ((newProj.equals(vectorLayer2.projection))&&(vectorLayer.getVisibility()))
   					 {	
					 vectorLayer.setVisibility(false);
					 vectorLayer2.setVisibility(true);
 					 }
                              

				      if ((newProj.equals(vectorLayer.projection))&&(vectorLayer2.getVisibility()))
   					 {	
					 vectorLayer2.setVisibility(false);
					 vectorLayer.setVisibility(true);
 					 }
                                       

				        gpxTrack.delMarkerLines();
					markers.transform(oldProj, newProj);
                                        gpxTrack.drawMarkerLines();
                                   
		
	                    } 

                    // Reproject centre point  - Palo TT, 10/2009 -----------------

                }

                this.events.triggerEvent("changebaselayer", {
                    layer: this.baseLayer
                });
            }        
        }
    }





 
    //new function marker.js, Palo TT   10/2009
     /* APIMethod: transform 
     * Transform the Marker object from source to dest projection.  
     * 
     * Parameters:  
     * source  - {<OpenLayers.Projection>} Source projection.  
     * dest    - {<OpenLayers.Projection>} Destination projection. 
     * 
     * Returns: 
     * {<OpenLayers.Marker>} Itself, for use in chaining operations. 
     */ 
    OpenLayers.Marker.prototype.transform = function(source, dest) { 
                this.lonlat.transform(source, dest); 
	        return this; 
    }
 





    // new function layers/markers.js, Palo TT   2009 Palo TT, 10/2009 
    /* Transform the Markers layer from source to dest projection.  
    * 
    * Parameters:  
    * source - {<OpenLayers.Projection>} Source projection.  
    * dest   - {<OpenLayers.Projection>} Destination projection.  
    * 
    * Returns: 
    * {<OpenLayers.Layer.Markers>} Itself, for use in chaining operations. 
    */ 

    OpenLayers.Layer.Markers.prototype.transform = function(source, dest) { 
     for(var i=0; i < this.markers.length; i++) { 
      var marker = this.markers[i]; 
      marker.transform(source, dest); 
       } 
       this.redraw(); 
       return this; 
     }
 


    
  /**
  * Function: onImageLoadError 
  */
  OpenLayers.Util.onImageLoadError = function() {
    this._attempts = (this._attempts) ? (this._attempts + 1) : 1;
    if (this._attempts <= OpenLayers.IMAGE_RELOAD_ATTEMPTS) {
        var urls = this.urls;
        if (urls && urls instanceof Array && urls.length > 1){
            var src = this.src.toString();
            var current_url, k;
            for (k = 0; current_url = urls[k]; k++){
                if(src.indexOf(current_url) != -1){
                    break;
                }
            }
            var guess = Math.floor(urls.length * Math.random());
            var new_url = urls[guess];
            k = 0;
            while(new_url == current_url && k++ < 4){
                guess = Math.floor(urls.length * Math.random());
                new_url = urls[guess];
            }
            this.src = src.replace(current_url, new_url);
        } else {
            this.src = this.src;
        }
    } else {
        //this.style.backgroundColor = OpenLayers.Util.onImageLoadErrorColor;
        this.src = "http://cmaps.gpsteam.eu/pic/transparent256.png";      // Palo TT, 5/2010 
    }
    this.style.display = "";
  }



