﻿/* OVERLAY OBJECT BEGIN */
	    function mapOverlay(_name, _maxLat, _minLong, _maxZoom, _minZoom, _startZoom, _scale, _width, _height, _top, _left, _imgURL)
	    {
		    //Public Variables
		    this.name = _name;                                          //Layer name
		    this.moveable = true;                                       //Used for setting up the positioning
		    this.scale = _scale;                                        //Scale of the orginal image to fit the start google zoom
		    this.maxLat = _maxLat;                                      //Latitude of the uppder left hand corner of the image
		    this.minLong = _minLong;                                    //Longitude of the upper left hand corner of the image 
		    this.midLat = _maxLat;
		    this.midLong = _minLong;
		    this.startZoom = _startZoom;
            
		    //Private Variables
		    var layer = document.getElementById(this.name);
		    var image = document.getElementById(this.name + "Img");     //Image Location
           
		    var maxZoom = _maxZoom;                                     //Maximum zoom level of the layer ( zoomed out too far: alignent off due to curvature of earth etc)
		    var minZoom = _minZoom;                                     //Minimum zoom level of the layer (zoomed in to far: slow client processing of big image resizing.)
		    var startZoom = _startZoom;                                 //Google zoom level the image has been designed to fit
		    var width = _width;                                         //Width of the bounding container of the image (px)
		    var height = _height;                                       //Height of the bounding container of the image (px)
		    var top = _top;                                             //"Whitespace" above the image.  Area not wanted to be visible (px)
		    var left = _left;                                         //"Whitespace" to the left of the image.  Area not wanted to be visible (px)	
		    
		    //IMAGE PRELOAD
		    var imgURL = new Image();
		    imgURL.src = _imgURL;                                		    
		    var visible = false;                                        //Determines if the laver is visible
		    var visibilityChk = document.getElementById(this.name + "Visible"); 
		    var staticChk = document.getElementById(this.name + "Static");
		    var scaleText = document.getElementById(this.name + "Scale");
            var origImageWidth = image.offsetWidth;
            
		    //Public Functions
		    this.center = findOffset;
		    this.move = moveLayer;
		    this.resize = resizeImage;
		    this.mapZoom = googleZoom;
		    this.loadLayer = initialize;
            this.isVisible = changeVisibility;
            this.setNewBounds = changeLatLngBounds;
            this.getoffsetTop = layer.offsetTop;
            this.getoffsetLeft = layer.offsetLeft;
            
		    //When the object loads make sure it fits to the current zoom level and adjust the sizing
		    this.loadLayer();

		    //Object Functions
		    function initialize()
		    {		    
		        image.src = imgURL.src;
		        
		        //Set up the initial heights/widths/offsets
		        if(width=="")
		        {
			        //if no width is established then set the layer width to the size of the image
			        width = image.offsetWidth;
			        height = image.offsetHeight;
			    }

			    layer.style.width = width;
			    layer.style.height = height;
				   
			    image.style.top = top;
			    image.style.left = left;
				
		        this.midLat = _maxLat - pixelsInDegreesLat(parseInt(layer.style.height) + parseInt(mapLayer.offsetTop)) / 2;
		        this.midLong = _minLong + pixelsInDegreesLong(parseInt(layer.style.width) - parseInt(mapLayer.offsetLeft)) / 2;
				
			    //Set up the legend items
			    if(!this.moveable)
			    {
			        staticChk.checked = true;
			    }
				
				if(scaleText)
				{
			        scaleText.value = this.scale;
			    }
				
			    this.isVisible(visible);
				
		        //Check the zoom level
			    if(zoom!=startZoom)
			    {
				    //Do Something to fix the scale!: Find a multiplier between layers
				    var multiplier;
					
				    if(zoom > startZoom)
				    {
					    multiplier = 1  / ((zoom - startZoom) * 2);
				    }
				    else
				    {
					    multiplier = (startZoom - zoom) * 2;
				    }
				    this.mapZoom(multiplier);
			    }
				
			    //Resize the original image to the correct dimensions
			    this.resize();
		    }

		    function findOffset(boundryX, boundryY)
		    {
		        if(this.moveable==true)
		        {	        
			        var moveX = degreesLongInPixels(boundryX - this.minLong) + mapLayer.offsetLeft;
			        var moveY = degreesLatInPixels(boundryY - this.maxLat) + mapLayer.offsetTop;
			        
			        var el = document.getElementById('debug2');
			        el.value=boundryY;					        
			        
			        this.move(-moveX,moveY);
			    }
		    }
            
		    function moveLayer(pixelsX, pixelsY)
		    {
			    layer.style.left = pixelsX + 'px';
			    layer.style.top = pixelsY + 'px';
		    }

            function googleZoom(multiplier)
            {
                if(zoom>=minZoom || zoom<=maxZoom)
			    {
			        //Hide the layer if it falls out of the range
				    changeVisibility(false);
			    }
				
			    this.scale = this.scale * multiplier;
			    if(scaleText)
			    {			    
			        scaleText.value = this.scale;
			    }
				
			    this.resize();
            }

		    function resizeImage()
		    {
		        //IMAGE IS ASSUMED TO MAINTAIN ASPECT WITH THE WIDTH BEING THE DRIVING FACTOR
		        if(visible)
		        {
			        image.style.width = (origImageWidth * this.scale) + 'px';
			        image.style.left = (left * this.scale) + 'px';
			        image.style.top = (top * this.scale) + 'px';
			        layer.style.width = (width * this.scale) + 'px';
			        layer.style.height = (height * this.scale) + 'px';			
			    }
		    }

		    function changeVisibility(visibility)
		    {   
		        visible = visibility;
		        visibilityChk.checked = visibility;
		        if(visibility)
		        {
		            image.style.visibility = "visible";
		            this.resize();
		        }
		        else
		        {
		            image.style.visibility = "hidden";
		        }
		    }

		    function changeLatLngBounds(mapMaxLat, mapMinLong)
		    {
		        this.maxLat = mapMaxLat - pixelsInDegreesLat(parseInt(layer.offsetTop));
		        this.minLong = mapMinLong + pixelsInDegreesLong(parseInt(layer.offsetLeft));
		    }
	    }
    /* OVERLAY OBJECT END */


