﻿/***********************************************************************
* YP - JavaScript file 
* Author: Iaan Roux
* Date: 2007-05-10
***********************************************************************/


/**********************************************
***   DISPLAY COORDINATES BELOW THE MAP *******
**********************************************/
function PixelXYToMapCoord(BoundingBox,MapWidth,MapHeight,PointX,PointY)
{
    var _sw = BoundingBox.getSouthWest();
    var _ne = BoundingBox.getNorthEast();
    minLon = _sw.Longitude;
    minLat = _sw.Latitude;
    maxLon = _ne.Longitude;
    maxLat = _ne.Latitude;
    LonDiff = maxLon - minLon;
    LatDiff = maxLat - minLat;	
    xRel = PointX / MapWidth;
    yRel = PointY / MapHeight;
    dLon = (xRel * (LonDiff)) + minLon;
    dLat = maxLat - (yRel * (LatDiff));
    return "Lat: " + dLat.toFixed(4) + " Long: " + dLon.toFixed(4);
}
function fnTrackMouse(obj)
{
    if(g_map)
    {
        var sBoundBox = g_map.getBoundingBox();
            
        //Get the top left corner of the moving container:
        var objMov = document.getElementById("MapPanel")
        oNotice = document.getElementById("CoordsTxt")
        if(document.all)
        {
            try
            {
                oNotice.innerText=PixelXYToMapCoord(sBoundBox,obj.clientWidth,obj.clientHeight,event.clientX-objMov.offsetLeft,event.clientY-objMov.offsetTop);
            }
            catch(err){}
        }
    }
} 
/**********************************************
***   END: DISPLAY COORDINATES BELOW THE MAP **
**********************************************/


   
/**********************************************
***   POINTS OF INTEREST (POI) MOUSE OVERS ****
**********************************************/
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}


var xmlHttpPOI
function GetPOIFromServer()
{
//Check if the points are visible
    cur_scale = document.getElementById(MapControlName +"_hdCurrentZoomScale").value
    if(parseInt(cur_scale) <= 50000)
    { 
        xmlHttpPOI=GetXmlHttpObject()
        if (xmlHttpPOI==null)
        {
            alert ("Your browser does not support AJAX!");
            return;
        } 
        var url="ajax_search_points.aspx";
        url=url+"?"+ getBoundingBoxWidthHeight();
        url=url+"&sid="+Math.random();
        xmlHttpPOI.onreadystatechange=receivePOI;
        xmlHttpPOI.open("GET",url,true);
        xmlHttpPOI.send(null);
     }
     else
     {
        document.getElementById("MovingContainer").innerHTML = ""
     }
} 

function receivePOI()
{

    if (xmlHttpPOI.readyState==4)
    {
    //clear the old values
    document.getElementById("MovingContainer").innerHTML = ""
        if(xmlHttpPOI.responseText == ""){
            return
        }        
        var arr_points = xmlHttpPOI.responseText.split("|")
        for(var i = 0; i < arr_points.length; i++)
        {
            var point = arr_points[i].split(';')
            //get the point info
            v_name = point[0]                    
            v_left = point[1]              
            v_top = point[2]

            LabelPOI(parseInt(v_top),parseInt(v_left),v_name,i+1000)
        }
    }
}

function LabelPOI(yOff, xOff,name,z_idx)
{
     //Add to the map
    poi_id = 'poi_' + z_idx
    
    var dv_poi = document.createElement("div");
    dv_poi.id = poi_id;
    dv_poi.style.cursor = 'pointer';
    dv_poi.title = name;
    dv_poi.alt = name;
    dv_poi.style.position = 'absolute';
    dv_poi.style.zIndex = z_idx;
    dv_poi.style.top = yOff + 'px';
    dv_poi.style.left = xOff + 'px';
    dv_poi.style.border = '0px solid red'
    dv_poi.style.width = '25px';
    dv_poi.style.height = '25px';
    
    document.getElementById("MovingContainer").appendChild(dv_poi)
}

/**********************************************
*** END POINTS OF INTEREST (POI) MOUSE OVERS **
**********************************************/



function getBoundingBoxWidthHeight()
{
    var state = document.getElementById("ctl00_ContentPlaceHolder1_AfriGISMapServerMap1_hdMapState").value
    var bb_start_idx = state.indexOf("=",state.indexOf("BoundingBox"))+1;
    var boundingboxval = state.substring(bb_start_idx,state.indexOf(";",bb_start_idx))
    
    var width_start_idx = state.indexOf("=",state.indexOf("MapWidth"))+1;
    var MapWidth = state.substring(width_start_idx,state.indexOf(";",width_start_idx))
    
    var height_start_idx = state.indexOf("=",state.indexOf("MapHeight"))+1;
    var MapHeight = state.substring(height_start_idx,state.indexOf(";",height_start_idx))  
    //alert(state + "\r\n" + boundingboxval);        
    return "bb=" + boundingboxval + "&width=" +  MapWidth + "&height=" +  MapHeight
}

/**********************************************
*** LEGEND **
**********************************************/
function ToggleLegend()
{
    objLegend = document.getElementById("MapLegend")
    if(bLegendVisible)
        HideLegend()
    else
        ShowLegend()
}

var bLegendVisible = false;
var top_destination = 0
var amount = 1    
var dist_left = 0
var objLegend = document.getElementById("MapLegend")
function MoveLegend()
{
    if(dist_left > 0)
    {
        objLegend.style.top = (parseInt(objLegend.style.top) + amount) + 'px'
        dist_left = dist_left - Math.abs(amount)
        setTimeout("MoveLegend()",1) 
    }
}

function ShowLegend()
{
    objLegend = document.getElementById("MapLegend")
    objLegend.style.visibility="visible";
//    top_destination = 2
//    amount = 5
//    //Check how many pixels the image must be moved
//    adjust_top = (Math.abs(parseInt(objLegend.style.top)) + top_destination)%amount
//    //Move the legend to a position dividably by amount
//    objLegend.style.top = (parseInt(objLegend.style.top) - adjust_top) + 'px'
//    dist_left = Math.abs(parseInt(objLegend.style.top))
//    MoveLegend()
//    bLegendVisible = true
    document.getElementById("LegendText").style.visibility="hidden";
}

function HideLegend()
{
    objLegend = document.getElementById("MapLegend")
    objLegend.style.visibility="hidden";
//    top_destination = objLegend.height*-1
//    amount = -5
//  
//    //Check how many pixels the image must be moved
//    adjust_top = (parseInt(objLegend.style.top) - top_destination)%amount
//    //Move the legend to a position dividably by amount
//    objLegend.style.top = (parseInt(objLegend.style.top) - adjust_top) + 'px'
//    dist_left = parseInt(objLegend.style.top) - top_destination
//    MoveLegend()
//    bLegendVisible = false
    document.getElementById("LegendText").style.visibility="visible";
}

/**********************************************
*** END: LEGEND **
**********************************************/
