var metersPerUnit = 111319.4908;  //value returned from mapguide
var inPerUnit = OpenLayers.INCHES_PER_UNIT.m * metersPerUnit;
OpenLayers.INCHES_PER_UNIT["dd"] = inPerUnit;
OpenLayers.INCHES_PER_UNIT["degrees"] = inPerUnit;
OpenLayers.DOTS_PER_INCH = 96;

OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;

OpenLayers.Util.onImageLoadErrorColor = "transparent";
OpenLayers.ImgPath = OpenLayers._getScriptLocation() + "../images/";

Proj4js.defs["EPSG:31467"] = "+proj=tmerc +lat_0=0 +lon_0=9 +k=1.000000 +x_0=3500000 +y_0=0 +ellps=bessel +units=m +no_defs  no_defs";
Proj4js.defs["EPSG:4326"]  = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ";

OpenLayers.Control.PanZoomBarCustom = OpenLayers.Class(OpenLayers.Control.PanZoomBar,
			{
				panIconW: 18,
				panIconH: 18,
				zoomWorldIcon: true,
			    draw: function(px) {
					//OpenLayers.Control.PanZoomBar.prototype.draw.apply(this, arguments);
			
					// initialize our internal div
					OpenLayers.Control.prototype.draw.apply(this, arguments);
					px = this.position.clone();
			 	
			 		var global = (GeoBIP.hasProjectSetting()) ? 'global-' : '';
			 		
			 	    // place the controls
			 	    this.buttons = [];
			 	
			 	    var sz = new OpenLayers.Size(this.panIconW,this.panIconH);
			 	    var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y);
			 	    var wposition = sz.w;
			 					
			 	    if (this.zoomWorldIcon) {
			 	    	centered = new OpenLayers.Pixel(px.x+sz.w, px.y);
			 		}
			 	
			 	    this._addButton("panup", "map/" + global + "north.png", centered, sz);
			 	    px.y = centered.y+sz.h;
			 	    this._addButton("panleft", "map/" + global + "west.png", px, sz);
				    if (this.zoomWorldIcon) {
			 	    	this._addButton("zoomworld", "map/" + global + "zoom-extent.png", px.add(sz.w, 0), sz);
			 	        wposition *= 2;
			 		}
			 	    this._addButton("panright", "map/" + global + "east.png", px.add(wposition, 0), sz);
			 	    this._addButton("pandown", "map/" + global + "south.png", centered.add(0, sz.h*2), sz);
			 	    this._addButton("zoomin", "map/" + global + "zoom-plus.png", centered.add(0, sz.h*3+5), sz);
			 	    centered = this._addZoomBar(centered.add(0, sz.h*4 + 5));
			 	    this._addButton("zoomout", "map/" + global + "zoom-minus.png", centered, sz);
			
					// Override repositioning
			        this.div.style.top		= "10";
			        this.div.style.right	= "67";
			        this.div.style.left		= "";
			        this.div.onmouseover = function() { this.style.cursor = "pointer"; };
					this.div.onmouseout = function() { this.style.cursor = ''; };
			        
			        return this.div;
			    },
			     _addZoomBar: function(centered) {
			        var imgLocation = OpenLayers.Util.getImagesLocation();
					
					var global = (GeoBIP.hasProjectSetting()) ? 'global-' : '';
			 		 
			        var id = this.id + "_" + this.map.id;
			        var zoomsToEnd = this.map.getNumZoomLevels() - 1 - this.map.getZoom();
			        var slider = OpenLayers.Util.createAlphaImageDiv(id,
			                       centered.add(-1, zoomsToEnd * this.zoomStopHeight), 
			                       new OpenLayers.Size(20,9), 
			                       imgLocation + "map/" + global + "zoom-slider.png",
			                       "absolute");
			        this.slider = slider;
			        
			        slider.onmouseover = function() { this.style.cursor = "pointer"; };
					slider.onmouseout = function() { this.style.cursor = ''; };
			        
			        this.sliderEvents = new OpenLayers.Events(this, slider, null, true,
			                                            {includeXY: true});
			        this.sliderEvents.on({
			            "mousedown": this.zoomBarDown,
			            "mousemove": this.zoomBarDrag,
			            "mouseup": this.zoomBarUp,
			            "dblclick": this.doubleClick,
			            "click": this.doubleClick
			        });
			        
			        var sz = new OpenLayers.Size();
			        sz.h = this.zoomStopHeight * this.map.getNumZoomLevels();
			        sz.w = this.zoomStopWidth;
			        var div = null;
			        
			        if (OpenLayers.Util.alphaHack()) {
			            var id = this.id + "_" + this.map.id;
			            div = OpenLayers.Util.createAlphaImageDiv(id, centered,
			                                      new OpenLayers.Size(sz.w, 
			                                              this.zoomStopHeight),
			                                      imgLocation + "map/" + global + "zoom-bar.png", 
			                                      "absolute", null, "crop");
			            div.style.height = sz.h + "px";
			        } else {
			            div = OpenLayers.Util.createDiv(
			                        'OpenLayers_Control_PanZoomBar_Zoombar' + this.map.id,
			                        centered,
			                        sz,
			                        imgLocation + "map/" + global + "zoom-bar.png");
			        }
			        
			   		div.onmouseover = function() { this.style.cursor = "pointer"; };
					div.onmouseout = function() { this.style.cursor = ''; };
			        
			        this.zoombarDiv = div;
			        
			        this.divEvents = new OpenLayers.Events(this, div, null, true, 
			                                                {includeXY: true});
			        this.divEvents.on({
			            "mousedown": this.divClick,
			            "mousemove": this.passEventToSlider,
			            "dblclick": this.doubleClick,
			            "click": this.doubleClick
			        });
			        
			        this.div.appendChild(div);
			
			        this.startTop = parseInt(div.style.top);
			        this.div.appendChild(slider);
			
			        this.map.events.register("zoomend", this, this.moveZoomBar);
			
			        centered = centered.add(0, 
			            this.zoomStopHeight * this.map.getNumZoomLevels());
			        return centered; 
			    }, 
			    CLASS_NAME: "OpenLayers.Control.PanZoomBarCustom"
			});

OpenLayers.Control.StartMap = OpenLayers.Class(OpenLayers.Control, {
		        type: 3,
				initialize: function(options) {
						OpenLayers.Control.prototype.initialize.apply(this, [options]);
						this.displayClass = 'olControlStartMap';
			    },
        		draw: function() {
		        },
	          	activate: function(e) {
					MAP.doStartMap();

					var panels = this.map.getControlsByClass('OpenLayers.Control.MouseDefaults');
					for(var p=0;p<panels.length;p++) {
						panels[p].activate();
					}
			    },
			    CLASS_NAME: "OpenLayers.Control.StartMap"
			});

OpenLayers.Control.Scale = OpenLayers.Class(OpenLayers.Control, {
			    element: null,
			    initialize: function(element, options) {
			        OpenLayers.Control.prototype.initialize.apply(this, [options]);
			        this.element = OpenLayers.Util.getElement(element);        
			    },
			    draw: function() {
			        OpenLayers.Control.prototype.draw.apply(this, arguments);
			        if (!this.element) {
		            	this.element = document.createElement("div");
        			    this.div.appendChild(this.element);
			        }
			        this.map.events.register( 'movestart', this, this.updateScale);
		        	this.updateScale();
		    	    return this.div;
			    },
			    updateScale: function() {
			        var scale = this.map.getScale();
			        if (!scale) {
			            return;
			        }

			        if (scale >= 9500 && scale <= 950000) {
			            scale = Math.round(scale / 1000) + ".000";
			        } else if (scale >= 950000) {
			            scale = Math.round(scale / 1000000) + ".000.000";
			        } else {
			            scale = Math.round(scale);
			        }    
			
			        this.element.innerHTML = "1 : " + scale + "&nbsp;&nbsp;&nbsp;(Zoom: <b>" + this.map.getZoom() + "</b>)";
			   }, 
			   CLASS_NAME: "OpenLayers.Control.Scale"
			});
			
OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
		        defaultHandlerOptions: {
                    'single': true,
                    'double': true,
                    'pixelTolerance': 0,
                    'stopSingle': false,
                    'stopDouble': false
                },
                initialize: function(options) {
                    this.handlerOptions = OpenLayers.Util.extend( {}, this.defaultHandlerOptions );
                    OpenLayers.Control.prototype.initialize.apply( this, arguments ); 
                    this.handler = new OpenLayers.Handler.Click( this,
                    					{
				                            'click': this.onClick,
				                            'dblclick': this.onDblclick
										},
										this.handlerOptions );
                }, 
				onClick: function(evt) {
					var lonlat = this.map.getLonLatFromViewPortPx(evt.xy);
					this.map.panTo(lonlat);
                },
                onDblclick: function(evt) {
					var lonlat = this.map.getLonLatFromViewPortPx(evt.xy);
					this.map.panTo(lonlat);
                },
			    CLASS_NAME: "OpenLayers.Control.Click"
			});
						
OpenLayers.Control.Copyright = OpenLayers.Class(OpenLayers.Control, {
			    element: null,
			    initialize: function(element, options) {
			        OpenLayers.Control.prototype.initialize.apply(this, [options]);
			        this.element = OpenLayers.Util.getElement(element);        
				},
			    draw: function() {
			        OpenLayers.Control.prototype.draw.apply(this, arguments);
			        if (!this.element) {
		            	this.element = document.createElement("div");
        			    this.div.appendChild(this.element);
			        }

					var imgLocation = OpenLayers.Util.getImagesLocation();
					
			        var logo = OpenLayers.Util.createAlphaImageDiv(
                                    this.id + "_Copyright", 
                                    new OpenLayers.Pixel(0, 0),
                                    new OpenLayers.Size(280,36),
                                    imgLocation + "map/logo_geobip.png",
                                    "absolute");

					this.div.appendChild(logo);

			        OpenLayers.Event.observe(logo, "mousedown", 
           				OpenLayers.Function.bindAsEventListener(function() {
	           				GeoBIP.openWindow('http://www.lgl-bw.de/');
	           				//GeoBIP.openBox('', 'Terms', 'Nutzungsbedingungen', CONF_BOX_DETAIL_SMALL_WIDTH, CONF_BOX_DETAIL_SMALL_HEIGHT, 'view.terms', '');
           				}, logo));
             
			        // Override repositioning
			        this.div.style.top		= "";
			        this.div.style.right	= "";
			        this.div.style.left		= "5";
			        this.div.style.bottom	= "41";
			        this.div.onmouseover = function() { this.style.cursor = "pointer"; };
					this.div.onmouseout = function() { this.style.cursor = ''; };

			        return this.div;
			    },
				CLASS_NAME: "OpenLayers.Control.Copyright"
			});
