﻿
try
{
    wedebug.addLogger("gl", "GIS/LINE");
}
catch(e)
{
    alert(e.message);
}

var map = null;
var GLMap = Class.create();

//Approx++ polygon of Norway
//GLMap.Coverage = {
//    LATITUDE : [58.84785, 59.06880, 57.82135,  58.86490,   61.73672,   72.65958, 69.65708, 68.33437, 63.01510, 61.28079, 61.28079, 58.84785],
//    LONGITUDE : [11.62353, 10.59082, 7.33886,   5.06469,    4.28466,    21.31347, 34.14550, 20.52246, 12.78808, 12.98583, 12.98583, 11.62353]
//}
//Object.extend(GLMap.Coverage, AbstractCoverage); //Extend with methods
GLMap.DEFAULT_MAP_MARKER = "/Resource/Map/icons/ix_op_all/24x24/plain/flag_red.gif";
GLMap.prototype = {
    //m_lat : 59.90891042881661,
    //m_lon : 10.789260864257801,
    //m_level : 0,
    m_mapDiv : null,
    
    /**
    * Constructor for object.
    * @argument position : json object:
    *   {
    *       level : zoom-level (optional)
    *       lat : latitude (optional)
    *       lon : longitude (optional)
    *       style : map style (optional)
    *   }
    */
    initialize : function(t_zoomLevel, lat, lon, style, wmsUrl)
    {  
        this.m_mapDiv = $("map");
        map = new AJAXMAP('map', true, 'http://ts1.webatlas.no/', style, wmsUrl);
        
        var t_utm = decimalDegreesToUTM({y:lat, x:lon});
        map.init(t_utm.y, t_utm.x, t_zoomLevel);

		/*Event.observe( window, "resize", map.resize.bind(map) );	*/
		
    },
    
    getType : function()
    {
        return "GLMap";
    },
    
    /**
    * Functon returning the internal zoom level of the map object.
    */
    getZoomLevel : function()
    {
        //return this.m_level;  
        return map.getZoomLevel();
    },
    
    setZoomLevel : function(level)
    {
        //TODO: Add setZoomLevel function
        this.centerAndZoom({level:level});
    },
        
    centerAndZoomUTM : function(northing, easting, zoom){
    var coordinate = UTMToLatLon(northing, easting, 33);
    this.setCenter(coordinate.lat,coordinate.lon,zoom);
    },
    
    /**
    * Center and zoom on point at given level.
    */
    centerAndZoom : function(position)
    {           
        var lat = (typeof position.lat != "undefined")?position.lat: null;
        var lon = (typeof position.lon != "undefined")?position.lon: null;
        
        if(lat == null || lon == null)
        {
            var center = map.getCenter();
            lat = center.lat;
            lon = center.lon;
        }
    
        var level = (typeof position.level != "undefined")?position.level:null;
        
        if( level == null )
        {
            level = map.getZoomLevel();
        }
        
        wedebug.log("gl", "centerAndZoom: " + [lat, lon, level]);
       
        map.centerAndZoom(
            lat, 
            lon,
            level);
        
        //this.m_level = map.getZoomLevel(); 
    },
    
    /**
    * Function for centering map on coordinate
    */
    center : function(position)
    {
        this.centerAndZoom(position);
    },
    
    /**
    * function returning postion.lat/lon with center coordinate of view
    */
    getCenter : function() 
    {
        var center = map.getCenterUTM();
        return UTMToLatLon(center.y /*north*/, center.x /*east*/);        
    },
    
    getCenterUTM : function()
    {
        return map.getCenterUTM();
    },
    
    _getDimension : function()
    {
        return {
            width : Element.getWidth(this.m_mapDiv),
            height : Element.getHeight(this.m_mapDiv)
        }
    },
    
    /**
    * function returning object with lat and lon property for given pixel.
    * @argument pixel object with properties x and y describing pixel.
    * @returns latlon object with properties lat and lon.
    */
    getPixelLatLon : function()
    {
        if( arguments.length == 2 )
        {
            var pixel = {x:arguments[0], y:arguments[1]};
            return map.getPixelLatLon(pixel);
        }
        else
        {
            return map.getPixelLatLon(arguments[0]);
        }
    },
    
    getLatLonPixel : function(t_lat, t_lon)
    {
        //this.m_activeMap.getPixelLatLon        
        var t_width = Element.getWidth(this.m_mapDiv);
        var t_height = Element.getHeight(this.m_mapDiv);
        
        var t_bottomRight = this.getPixelLatLon(t_width,t_height);
        var t_topLeft = this.getPixelLatLon(0, 0);
        
        //alert([t_topLeft.lat, lat, t_bottomRight.lat]);
        //alert([t_bottomRight.lon, lon, t_topLeft.lon]);
        	    
	    var y = (1-((t_lat - t_bottomRight.lat) / (t_topLeft.lat - t_bottomRight.lat))) * t_height;
	    //alert((1-((lat - t_bottomRight.lat) / (t_topLeft.lat - t_bottomRight.lat))));
	    var x = (t_lon - t_topLeft.lon) / (t_bottomRight.lon - t_topLeft.lon) * t_width;
	    
//	    try{
//        var icon = "media/icons/waypoint.png";
//	    map.addMarker(icon,t_lat,t_lon,"Easting:  "+t_lon ,"Northing:  "+t_lat + "   Easting:  "+t_lon );
//	    }catch(e){alert(e.message);}
	    	    
	    return {x:x, y:y};
    },
    
    /**
    * function for retrieving map style. E.g. hybrid or aerial
    * @returns constant representing style (MapController.Styles)
    */
    getMapStyle : function()
    {
        return map.getMapStyle();
    },
    
    /**
    * function for setting map style. E.g. hybrid or aerial
    *
    * @param style 0 = standard, 1 = aerial, 2 = hybrid
    * @returns true if style is changed, otherwise false.
    */
    _validStyles : [0,1,2],
    setMapStyle : function(style)
    {
        if( this._validStyles.include(style) )
        {
            map.setMapStyle(style);
            return true;
        }               
        return false;
    },
    
    isBirdseyeAvailable : function()
    {
        var center = map.getCenter();
        t_lat = center.lat;
        t_lon = center.lon;
        
        if( this.getZoomLevel() < 5 )
        {
            return this._isBirdseyeAvailable(t_lat,t_lon);
        }
        else
        {
            return false;
        }
    },
    
    pan : function(xdir,ydir)
    {
        wedebug.log("gl", "pan: " + [xdir,ydir]);
        
        if( xdir != 0 )
        {
            (xdir < 0)?map.slideLeft(Math.abs(xdir)):map.slideRight(xdir);
        }
        if( ydir != 0 )
        {
            (ydir < 0)?map.slideUp(Math.abs(ydir)):map.slideDown(ydir);
        }
        
        //this._setLocation();
    },
    
    _firstResize : false,
    resize : function(t_width, t_height) 
    {    
        if(t_width == undefined)
        {
            map.resize(undefined, undefined, false); //this._firstResize);
        }
        else
        {
            map.resize(t_width, t_height, false); //this._firstResize);
        }        
        this._firstResize = true;
    },
    /**
    * function returning maps current viewport.
    * @ returns object with fields minLat,minLon,maxLat,maxLon
    */
    getViewport : function()
    {
        var w = map.width;
        var h = map.height;
        var min = map.getPixelLatLon({x:0,y:h});
        var max = map.getPixelLatLon({x:w,y:0});
        
        return({minLat:min.lat, minLon:min.lon, maxLat:max.lat, maxLon:max.lon, height: h, width: w});
    },
    
    /**
    * addMarker( [link to iconfile], northing, easting, title, description, lowerbound, upperbound, int iconXOffset, int iconYOffset )
    */
    addMarker : function(
        icon, 
        northing, 
        easting, 
        title, //optional
        description, //optional
        lowerbound, //optional
        upperbound, //optional
        iconXOffset, //optional
        iconYOffset ) //optional
    {
        wedebug.log("gl", "addMarker : " + [icon,northing,easting,title,description,lowerbound,upperbound,iconXOffset,iconYOffset]);
        
        map.addMarker(
            icon, 
            northing, 
            easting, 
            title ? title : "", 
            description ? description : "", 
            lowerbound ? lowerbound : 0, 
            upperbound ? upperbound : 15, 
            iconXOffset ? iconXOffset : 0, 
            iconYOffset ? iconYOffset : 0);               
    },
    
    addWMSLayer : function(layer){
        //map.addNoTileLayer(layer, true);
        map.addWMSLayer(layer);
        map._createWMSLayer();
    },
    
    removeWMSLayer : function(layer){
        map.removeWMSLayer(layer);
        map._createWMSLayer();
    },
    
    drawLine : function(id, latPoints, lonPoints)
    {
        //TODO: implement if needed.
    },
    
    hideLine : function(id)
    {
        //TODO: implement if needed.
    },
    
    showLine : function(id)
    {
        //TODO: implement if needed.
    },
        
//    onWheel : function(event)
//    {        
//        wheel.bind(map.mouselayer)(event);
//        //this.m_level = map.getZoomLevel();
//        
//        //this._setLocation();
//    },
    
    internal_onMouseMove : function(event)
    {        
        AJAXMAP.mouseMovedHandler.bind(map.mouselayer)(event);
    },
    
    internal_onMouseDown : function(event)
    {
        AJAXMAP.mousePressedHandler.bind(map.mouselayer)(event);
    },
    
    internal_onMouseUp : function(event)
    {
        AJAXMAP.mouseUpHandler.bind(map.mouselayer)(event);
        //this._setLocation();
    },
    
    internal_onMouseOut : function(event)
    {
        AJAXMAP.mouseReleasedHandler.bind(map.mouselayer)(event);
        //this._setLocation();
    },
    
    onDblClick : function(event)
    {
        map.zoomIn(true);
    }
//    ,
//    
//    _setLocation : function()
//    {
//        var latlon = map.getCenter();
//        
//        this.m_lat = latlon.lat;
//        this.m_lon = latlon.lon;
//    }
}

Object.extend(GLMap, AbstractMap);
//Object.extend(GLMap.prototype, GLMap.Coverage);




