﻿function gdClearMapTips()
{
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    geodata.maptips.currentMapTipsLayer.mapTipItems = [];
    gdMap.graphics.clear();
}

function gdClearWeather()
{
    dojo.byId('txtWeatherTool').title = gdGetTooltip("/map/alt/showweather");
    dojo.byId('imgSelectWeather').title = gdGetTooltip("/map/alt/showweather");
    //dojo.byId('txtWeatherTool').innerHTML = gdGetText("/map/alt/showweather");
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    geodata.maptips.currentMapTipsLayer.weatherItems = [];
    gdMap.graphics.clear();
}

function gdClearMapTipsAndWeather()
{
    dojo.byId('txtWeatherTool').title = gdGetTooltip("/map/alt/showweather");
    dojo.byId('imgSelectWeather').title = gdGetTooltip("/map/alt/showweather");
    //dojo.byId('txtWeatherTool').innerHTML = gdGetText("/map/showweather");
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    geodata.maptips.currentMapTipsLayer.mapTipItems = [];
    geodata.maptips.currentMapTipsLayer.weatherItems = [];
    gdMap.graphics.clear();
}

function gdMapTipDragStart()
{
    gdMap.graphics.disableMouseEvents();
}

function gdMapTipDragEnd()
{
    gdMap.graphics.enableMouseEvents();
}

function gdMapTipPanStart()
{
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    if (dojo.isOpera) gdClearMapTipsAndWeather(); // Graphics display poorly in Opera while panning
}

function gdMapTipZoomStart()
{
    gdClearMapTipsAndWeather();
}

function gdMapTipMouseOver(evt)
{
    var gr = evt.graphic;
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    if (gr.infoTemplate != null) {
        var sp = gdMap.toScreen(gr.geometry);
        gdMap.infoWindow.setTitle(gr.getTitle());
        gdMap.infoWindow.setContent(gr.getContent());
        var anchor = gdMap.getInfoWindowAnchor(sp);

        // Info window container has been moved from map root div to document.body,
        // so we must offset the position of the infowindow to get the correct position:
        sp = sp.offset(gdMap.position.x, gdMap.position.y);

        // Offset the position horizontally depending on anchor position, to get the arrow to
        // point at the graphic element:
        if (anchor == esri.dijit.InfoWindow.ANCHOR_LOWERRIGHT || anchor == esri.dijit.InfoWindow.ANCHOR_UPPERRIGHT)
            sp = sp.offset(-51, 0);
        else if (anchor == esri.dijit.InfoWindow.ANCHOR_LOWERLEFT || anchor == esri.dijit.InfoWindow.ANCHOR_UPPERLEFT)
            sp = sp.offset(43, 0);
        // Offset the position vertically to make sure the arrow doesn't obscure the graphic element:
        if (anchor == esri.dijit.InfoWindow.ANCHOR_UPPERRIGHT || anchor == esri.dijit.InfoWindow.ANCHOR_UPPERLEFT)
            sp = sp.offset(0, 5);
        else
            sp = sp.offset(0, 10);

        if (dojo.isOpera) {
            // Left style is not reset in opera even though right style is set, etc.
            dojo.style(gdMap.infoWindow._window, "left", "");
            dojo.style(gdMap.infoWindow._window, "right", "");
            dojo.style(gdMap.infoWindow._window, "top", "");
            dojo.style(gdMap.infoWindow._window, "bottom", "");
        }
        gdMap.infoWindow.show(sp, anchor);
    }
    else if(gr.attributes && gr.attributes.index)
        geodata.maptips.currentMapTipsLayer.showTip(gr.attributes.index, null, null, gr.attributes.selected);
    return true;
}
function gdMapTipMouseOut()
{
//  if (typeof (e) == "undefined" && typeof (window.event) != "undefined")
//        e = window.event;
//    if (e == null) return;
//    
//    var gr = e.graphic;
//    
//    var hide = true;
//    var element = e.toElement;
//    if (element == null && e.relatedTarget) element = e.relatedTarget;
//    //if(element == null) alert('e.toElement is null!');
//    while (element != null) {
//        if (element == gdMap.infoWindow._window || element == gdMap.infoWindow._pointer) {
//            hide = false;
//            element = null;
//        }
//        else {
//            try {
//                if (element.parentElement)
//                    element = element.parentElement;
//                else if (element.parentNode)
//                    element = element.parentNode;
//                else
//                    element = null;
//            }
//            catch (ex) {
//                element = null;
//            }
//        }
//    }
//    if (hide) {
//        geodata.maptips.currentMapTipsLayer.hideCurrentItem();
//         
//    }   
    return true;
}
function gdInfoWindowMouseOut(e)
{
    if (typeof (e) == "undefined" && typeof (window.event) != "undefined")
        e = window.event;
    if (e == null) return;
    
    var hide = true;
    var element = e.toElement;
    if (element == null && e.relatedTarget) element = e.relatedTarget;
    //if(element == null) alert('e.toElement is null!');
    while (element != null) {
        if (element == gdMap.infoWindow._window || element == gdMap.infoWindow._pointer) {
            hide = false;
            element = null;
        }
        else {
            try {
                if (element.parentElement)
                    element = element.parentElement;
                else if (element.parentNode)
                    element = element.parentNode;
                else
                    element = null;
            }
            catch (ex) {
                element = null;
            }
        }
    }
    if (hide) {
        geodata.maptips.currentMapTipsLayer.hideCurrentItem();
       }
    return true;
}
function gdMapTipHoverInfoMouseOut(e)
{
    if( typeof( e ) == "undefined" && typeof( window.event ) != "undefined" )
        e = window.event;
    if(e == null) return;
     //alert('hepp2');
    var hide = true;
    var element = e.toElement;
    if(element == null && e.relatedTarget) element = e.relatedTarget;
    //if(element == null) alert('e.toElement is null!');
    while(element != null)
    {
        var id = null;
        try
        {
            id = element.id;
        }
        catch(ex)
        {
        }
        //alert('To element: ' + element.id + ' (' + element.tagName+')');
        if(id != null && (id == geodata.maptips.currentMapTipsWindow.hoverHeaderId || id == geodata.maptips.currentMapTipsWindow.hoverContentId || id == geodata.maptips.currentMapTipsWindow.hoverDivId))
        {
            hide = false;
            element = null;
        }
        else
        {
            try
            {
                if(element.parentElement)
                    element = element.parentElement;
                else if(element.parentNode)
                    element = element.parentNode;
                else
                    element = null;
            }
            catch(ex)
            {
                element = null;
            }
        }
    }
    if(hide)
    {
        geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    }
    return true;
}
