﻿try
{
    wedebug.addLogger("coverage", "AbstractCoverage");
}
catch(e)
{
    alert(e.message);
}

var AbstractCoverage = {
    m_polygonBBox : null,
    
    intersects : function(minLat, minLon, maxLat, maxLon)
    {
        if( typeof this.LATITUDE == "undefined" )
        {
            wedebug.log("coverage", "  no coverage polygon specified for map.");
            return true;
        }
    
        //First check if bbox encapsulates polygon.
        this._setBBox();
        wedebug.log("coverage", "zoom bbox: " + [minLat, minLon, maxLat, maxLon]);
        wedebug.log("coverage", "coverage bbox: " + [this.m_polygonBBox.minLat, this.m_polygonBBox.minLon, this.m_polygonBBox.maxLat, this.m_polygonBBox.maxLon]);
        if( this.m_polygonBBox.minLat > maxLat || //Is bbox outside polygon bbox        
            this.m_polygonBBox.maxLat < minLat ||
            this.m_polygonBBox.minLon > maxLon ||
            this.m_polygonBBox.maxLon < minLon )
        {
            wedebug.log("coverage", "  boundingbox is outside coverage area.");
        
            //bbox is outside
            return false;
        }
        else if( this.m_polygonBBox.maxLat <= maxLat && //does bbox encapsulate polygon.
            this.m_polygonBBox.minLat >= minLat &&
            this.m_polygonBBox.minLon <= minLat &&
            this.m_polygonBBox.maxLon >= maxLon )
        {
            wedebug.log("coverage", "  boundingbox encapsulates coverage area.");
        
            //polygon is encapsulated by bbox
            return true;
        }
        
    
        var length = 10;
        var resLat = maxLat - minLat;
        var resLon = maxLon - minLon;
        var latStep = resLat / length;
        var lonStep = resLon / length;
        var upperBoundaryLat = new Array(length);
        var upperBoundaryLon = new Array(length);
        var leftBoundaryLat = new Array(length);
        var leftBoundaryLon = new Array(length);
        var rightBoundaryLat = new Array(length);
        var rightBoundaryLon = new Array(length);
        var bottomBoundaryLat = new Array(length);
        var bottomBoundaryLon = new Array(length);
        
        //Create points covering edge of boundingbox
        for( var i=0; i<length; i++ )
        {
            upperBoundaryLon[i] = bottomBoundaryLon[i] = minLon + lonStep * i;
            leftBoundaryLat[i] = rightBoundaryLat[i] = minLat + latStep * i;
            
            upperBoundaryLat[i] = maxLat;            
            leftBoundaryLon[i] = minLon;
            bottomBoundaryLat[i] = minLat;
            rightBoundaryLon[i] = maxLon;
            //bottomBoundaryLon[i] = upperBoundaryLon[i];
            //rightBoundaryLat[i] = leftBoundaryLat[i];    
            /*
            this.addMarker(null, upperBoundaryLat[i],    upperBoundaryLon[i]);
            this.addMarker(null, leftBoundaryLat[i],     leftBoundaryLon[i]);
            this.addMarker(null, bottomBoundaryLat[i],   bottomBoundaryLon[i]);
            this.addMarker(null, rightBoundaryLat[i],    rightBoundaryLon[i]);
            */
            //Check if any point are inside the polygon using within-method.   
            if( this.within(upperBoundaryLat[i],    upperBoundaryLon[i])    ||
                this.within(leftBoundaryLat[i],     leftBoundaryLon[i])     ||
                this.within(bottomBoundaryLat[i],   bottomBoundaryLon[i])   ||
                this.within(rightBoundaryLat[i],    rightBoundaryLon[i]) )
            {
                wedebug.log("coverage", "  boundingbox intersects coverage area.");
            
                return true;
            }   
        }   
        
        wedebug.log("coverage", "  boundingbox does not intersect coverage area.");  
        return false;   
    },
    
    //point, polySides, points
    within : function(lat, lon)
    {
        wedebug.log("coverage", "coverage.within: " + [lat,lon] + " " + [this.LATITUDE[0],this.LONGITUDE[0]]);
        var i, j=0 ;
		var oddNODES=false ;
        var polySides = this.LATITUDE.length;
		for (i=0; i<polySides; i++) 
		{
			j++; if (j==polySides) j=0;			
			if (this.LONGITUDE[i]<lon && this.LONGITUDE[j]>=lon
				||  this.LONGITUDE[j]<lon && this.LONGITUDE[i]>=lon) 
			{
				if (this.LATITUDE[i]+(lon-this.LONGITUDE[i])/(this.LONGITUDE[j]-this.LONGITUDE[i])*(this.LATITUDE[j]-this.LATITUDE[i])<lat) 
				{
					oddNODES=!oddNODES; 
				}
			}
		}

		return oddNODES; 
    },
    
    _setBBox : function()
    {
        //Get bbox of polygon
        if( this.m_polygonBBox == null )
        {
            var polylength = this.LATITUDE.length;        
            var polMinLat = 90, polMinLon = 180, polMaxLat = -90, polMaxLon = -180;
            //create bbox from coverage polygon
            for(var i=0; i<polylength; i++)
            {
                if( this.LATITUDE[i] < polMinLat ) polMinLat = this.LATITUDE[i];
                if( this.LATITUDE[i] > polMaxLat ) polMaxLat = this.LATITUDE[i];
                if( this.LONGITUDE[i] < polMinLon ) polMinLon = this.LONGITUDE[i];
                if( this.LONGITUDE[i] > polMaxLon ) polMaxLon = this.LONGITUDE[i];
            }
            
            this.m_polygonBBox = {minLat:polMinLat,minLon:polMinLon,maxLat:polMaxLat,maxLon:polMaxLon};
            
            wedebug.log("coverage", "Coverage bbox: " + [polMinLat,polMinLon,polMaxLat,polMaxLon]);
        }
    }
}


