function SearchResultIndexMap(containerId, width, height, west, south, east, north, zoomLevel)
{
    this.m_iconWidth = 31;
    this.m_iconHeight = 40;
    this.m_iconOffsetX = -12;
    this.m_iconOffsetY = -31;
    
    this.m_popupSize = {width:260, height:210};
    
    this.m_resX = east - west;
    this.m_resY = north - south;
    
    this.m_container = $get(containerId);
    this.m_width = width;
    this.m_height = height;
    this.m_west = west;
    this.m_south = south;
    this.m_east = east;
    this.m_north=north;
    this.m_zoomLevel=zoomLevel;
    this.m_entries = null;
}

SearchResultIndexMap.addBookmark = function(title, url)
{
    if(document.all)
        window.external.AddFavorite(url, title);
    else if(window.sidebar)
        window.sidebar.addPanel(title, url, ''); //FF
    else if(window.opera && window.print)
        return true;
}

SearchResultIndexMap.prototype = {
    addListingEntries : function(t_entries)
    {
        this.m_entries = t_entries;
        //var t_containerLocation = Sys.UI.DomElement.getLocation(this.m_container);
                
        //Create hotspots.
        var i, length=t_entries.length;
        for(i=length-1; i>=0; i--)
        {            
            var t_element = document.createElement("img");
            t_element.setAttribute("src", t_entries[i].Icon);
            t_element.style.width = this.m_iconWidth + "px";
            t_element.style.height = this.m_iconHeight + "px";
            t_element.style.position = "absolute";
            t_element.style.cursor = "pointer";
            t_element.setAttribute("id", i);
            
            //Get position relative to map.
            var position = this._getPosition(t_entries[i]);
            t_element.style.left = position.x + this.m_iconOffsetX + "px";
            t_element.style.top = position.y + this.m_iconOffsetY + "px";
            
//            t_element.backingBean = this;
//            
//            $addHandler(t_element, "mouseover", 
//                function(e)
//                {
//                    t_element.style.zIndex = 1;
//                });
//            $addHandler(t_element, "mouseout",
//                function(e)
//                {
//                    t_element.style.zIndex = 0;
//                });
            $addHandler(t_element, "click", this._showListingDetails);
                
            this.m_container.appendChild(t_element);
        }
    },
    
    getEntries : function()
    {
        return this.m_entries;
    },
    
    getContainer : function()
    {
        return this.m_container;
    },
    
    getBubbleBase : function()
    {
        return this.m_bubbleBase;
    },
    
    getIconOffset : function()
    {
        return {
            x : this.m_iconOffsetX,
            y : this.m_iconOffsetY
        };
    },
    
    getWindowSize : function()
    {
        var t_calcWidth, t_calcHeight;
        if (window.innerWidth) {
			t_calcWidth = window.innerWidth;
			t_calcHeight = window.innerHeight;
		} else {
			t_calcWidth = document.compatMode == 'CSS1Compat' ? document.documentElement.clientWidth : document.body.clientWidth;
			t_calcHeight = document.compatMode == 'CSS1Compat' ? document.documentElement.clientHeight : document.body.clientHeight;
		}
		return {
		    width: t_calcWidth,
		    height : t_calcHeight
		}
    },
    
    getPopupSize : function()
    {
        return this.m_popupSize;
    },
    
    _showListingDetails : function(e)
    {
        window.location.href = document.getElementById('mapBig').href;
        //e.target.backingBean.showListingDetailsForElement(e.target);   
    },
    
    _previousElement : null,
    showListingDetailsForElement : function(t_element)
    {
        var t_controller = t_element.backingBean;
        var t_popupId = "mapPopup";
        var t_entry = t_controller.getEntries()[t_element.id];
        var t_containerLocation = Sys.UI.DomElement.getLocation(this.m_container);
        
        //Bring the index icon on top.
        if( t_controller._previousElement != null )
        {
            t_controller._previousElement.style.zIndex = "";
        }
        t_controller._previousElement = t_element;
        t_element.style.zIndex = 2;
                        
        var t_popup = $get(t_popupId);
        
        if( t_popup == null )
        {
            t_popup = document.createElement("div");
            t_popup.setAttribute("id", t_popupId);
            t_popup.style.position = "absolute";
            t_popup.style.display = "none";
            t_popup.style.zIndex = 3;
            t_popup.style.overflow = 'visible';
            t_controller.getContainer().appendChild(t_popup);
        }
        
        t_popup.innerHTML = t_controller.getBubbleBase();
        
        var t_content = t_entry.Content;
        var t_cdataLoc = t_content.indexOf("<![CDATA[");
        if( t_cdataLoc != -1 )
        {
            var t_length = t_content.length;
            t_content = t_content.substring(9, t_length - 3);		                    
        }
               
        //Get content placeholder
        //HACK: innerHTML threats &#39; as ', thus creating script errors.
        $get("bubbleContentPlaceholder").innerHTML = t_content.replace(/&#39;/g, " ");
        
        var t_pageSize = t_controller.getWindowSize();
        var t_iconOffset = t_controller.getIconOffset();
        var t_pointX = Number(t_element.style.left.replace(/px/g, "")) - t_iconOffset.x;
        var t_pointY = Number(t_element.style.top.replace(/px/g, "")) - t_iconOffset.y;
        
        var t_bounds = t_controller.getPopupSize();
        
        var t_scrollY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        var t_scrollX = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
        
        var t_pointRight = (t_pointX + t_bounds.width + t_containerLocation.x) > (t_pageSize.width + t_scrollX);
        var t_pointDown = (t_pointY + t_bounds.height + t_containerLocation.y) > (t_pageSize.height + t_scrollY)
        
        var t_class = !(t_pointDown || t_pointRight) ? "pos_top-left" :
                t_pointDown && t_pointRight ? "pos_bottom-right" :
                        t_pointRight ? "pos_top-right" : "pos_bottom-left";

        Sys.UI.DomElement.addCssClass(t_popup.firstChild, t_class );
           
        
        t_popup.style.left = t_pointX - (t_pointRight ? t_bounds.width:0) + "px";
        t_popup.style.top =  t_pointY - (t_pointDown ? t_bounds.height:0) + "px";
        
        t_popup.style.display = "";
    },
    
    // TODO NORKART: Rewrite to handle pixel coordinates from mapprovider.
    _getPosition : function(t_entry)
    {
        var t_pixelCoords=NGTools.latLonToPixelXY({ 'x': t_entry.Lon,
                                                    'y': t_entry.Lat},
                                                    this.m_zoomLevel);
        var t_xRat=(t_pixelCoords.x-this.m_west);
        var t_yRat=(t_pixelCoords.y-this.m_north);
        
        return { x: t_xRat,y: t_yRat };
    },
    
    _latlonToUTM : function(coord) {
        var Long = parseFloat(coord.Lon);
        var Lat  = parseFloat(coord.Lat);
    
        var PI = 3.14159265;
        var FOURTHPI = PI / 4;
        var deg2rad = PI / 180;
        var rad2deg = 180.0 / PI;
    
        var a = 6378137;
        var eccSquared = 0.00669438;
        var k0 = 0.9996;
        var LongOrigin;
        var eccPrimeSquared;
        var N;
        var T;
        var C;
        var A;
        var M;
    
        var LongTemp = (Long + 180) - parseInt((Long + 180) / 360) * 360 - 180; // -180.00 .. 179.9;
        var LatRad = Lat * deg2rad;
        var LongRad = LongTemp * deg2rad;
        var LongOriginRad;
        var ZoneNumber; //cast to integer
        ZoneNumber = parseInt((LongTemp + 180) / 6) + 1;
    
        ZoneNumber = 33; //should be set dynamically, but set to 33 as our wms server only uses that
    
        LongOrigin = (ZoneNumber - 1) * 6 - 180 + 3;  //+3 puts origin in middle of zone
        LongOriginRad = LongOrigin * deg2rad;
    
        eccPrimeSquared = (eccSquared) / (1 - eccSquared);
    
        N = a / Math.sqrt(1 - eccSquared * Math.sin(LatRad) * Math.sin(LatRad));
        T = Math.tan(LatRad) * Math.tan(LatRad);
        C = eccPrimeSquared * Math.cos(LatRad) * Math.cos(LatRad);
        A = Math.cos(LatRad) * (LongRad - LongOriginRad);
    
        M = a * ((1 - eccSquared / 4 - 3 * eccSquared * eccSquared / 64 - 5 * eccSquared * eccSquared * eccSquared / 256) * LatRad
            - (3 * eccSquared / 8 + 3 * eccSquared * eccSquared / 32 + 45 * eccSquared * eccSquared * eccSquared / 1024) * Math.sin(2 * LatRad)
            + (15 * eccSquared * eccSquared / 256 + 45 * eccSquared * eccSquared * eccSquared / 1024) * Math.sin(4 * LatRad)
            - (35 * eccSquared * eccSquared * eccSquared / 3072) * Math.sin(6 * LatRad));
    
        var UTMEasting = (k0 * N * (A + (1 - T + C) * A * A * A / 6
            + (5 - 18 * T + T * T + 72 * C - 58 * eccPrimeSquared) * A * A * A * A * A / 120)
            + 500000.0);
    
        var UTMNorthing = (k0 * (M + N * Math.tan(LatRad) * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24
            + (61 - 58 * T + T * T + 600 * C - 330 * eccPrimeSquared) * A * A * A * A * A * A / 720)));
    
        return {'x': UTMEasting,'y':UTMNorthing};
    }
}

function NGTools()
{
}

NGTools.prototype = {
    t_earthRadius: 6378137,
    t_earthCircum: null,
    t_earthHalfCirc: null,
    t_minLatitude: -85.05112878,
    t_maxLatitude: 85.05112878,
    t_minLongitude: -180,
    t_maxLongitude: 180,
    t_tileSize: 256,

    initialize: function() {
        this.t_earthCircum = this.t_earthRadius * 2.0 * Math.PI;
        this.t_earthHalfCirc = this.t_earthCircum / 2;
    },

    /**
    * Convert from latitude ang longitude to pixel x/y at a given level of detail.
    */
    latLonToPixelXY: function(t_coordinate, t_levelOfDetail) {
        var t_latitude = t_coordinate.y;
        var t_longitude = t_coordinate.x;
        t_latitude = this.t_clip(t_latitude, this.t_minLatitude, this.t_maxLatitude);
        t_longitude = this.t_clip(t_longitude, this.t_minLongitude, this.t_maxLongitude);

        var t_x = (t_longitude + 180) / 360;
        var t_sinLatitude = Math.sin(t_latitude * Math.PI / 180);
        var t_y = 0.5 - Math.log((1 + t_sinLatitude) / (1 - t_sinLatitude)) / (4 * Math.PI);

        var t_mapSize = this.t_mapSize(t_levelOfDetail);
        var t_pixelX = parseInt(this.t_clip(t_x * t_mapSize + 0.5, 0, t_mapSize - 1));
        var t_pixelY = parseInt(this.t_clip(t_y * t_mapSize + 0.5, 0, t_mapSize - 1));
        return {'x':t_pixelX, 'y':t_pixelY};
    },

    t_clip: function(t_n, t_minValue, t_maxValue) {
        return Math.min(Math.max(t_n, t_minValue), t_maxValue);
    },

    t_mapSize: function(t_levelOfDetail) {
        return this.t_tileSize << t_levelOfDetail;
    }
}
// Make singleton
var NGTools = new NGTools();


