﻿function gdClearMapTips()
{
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    geodata.maptips.currentMapTipsLayer.mapTipItems = [];
    gdMapPage.map.graphics.clear();
}

function gdClearWeather()
{
    var txt = dojo.byId('txtWeatherTool');
    if (txt) txt.title = gdGetTooltip("/map/alt/showweather");
    var img = dojo.byId('imgSelectWeather');
    if (img) img.title = gdGetTooltip("/map/alt/showweather");
    //dojo.byId('txtWeatherTool').innerHTML = gdGetText("/map/alt/showweather");
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    geodata.maptips.currentMapTipsLayer.weatherItems = [];
    geodata.maptips.currentMapTipsLayer.skiResorts = [];
    gdMapPage.map.graphics.clear();
}

function gdClearMapTipsAndWeather()
{
    var txt = dojo.byId('txtWeatherTool');
    if(txt) txt.title = gdGetTooltip("/map/alt/showweather");
    var img = dojo.byId('imgSelectWeather');
    if(img) img.title = gdGetTooltip("/map/alt/showweather");
    //dojo.byId('txtWeatherTool').innerHTML = gdGetText("/map/showweather");
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    geodata.maptips.currentMapTipsLayer.mapTipItems = [];
    geodata.maptips.currentMapTipsLayer.weatherItems = [];
    geodata.maptips.currentMapTipsLayer.skiResorts = [];
    gdMapPage.map.graphics.clear();
}

function gdMapTipDragStart()
{
    gdMapPage.map.graphics.disableMouseEvents();
}

function gdMapTipDragEnd()
{
    gdMapPage.map.graphics.enableMouseEvents();
}

function gdMapTipPanStart()
{
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    if (dojo.isOpera) gdClearMapTipsAndWeather(); // Graphics display poorly in Opera while panning
}

function gdMapTipZoomStart()
{
    gdClearMapTipsAndWeather();
}

function gdMapTipClick(evt) {
    geodata.maptips.currentMapTipsLayer.graphicClicked(evt);
}

function gdMapTipMouseOver(evt)
{
    var gr = evt.graphic;
    geodata.maptips.currentMapTipsLayer.hideCurrentItem();
    if (gr.infoTemplate != null) {
        var sp = gdMapPage.map.toScreen(gr.geometry);
        gdMapPage.map.infoWindow.setTitle(gr.getTitle());
        gdMapPage.map.infoWindow.setContent(gr.getContent());
        var anchor = gdMapPage.map.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(gdMapPage.map.position.x, gdMapPage.map.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(gdMapPage.map.infoWindow._window, "left", "");
            dojo.style(gdMapPage.map.infoWindow._window, "right", "");
            dojo.style(gdMapPage.map.infoWindow._window, "top", "");
            dojo.style(gdMapPage.map.infoWindow._window, "bottom", "");
        }
        gdMapPage.map.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(evt) {
    if (!(gdClientData.hideInfoWindowOnGraphicMouseOut))
        return true;
    var gr = evt.graphic;
    if (gr.symbol.declaredClass == "esri.symbol.TextSymbol")
        return true; // Never hide anything on mouseout from text symbol, this sabotages the Opera workaround and it causes blinking in IE    
    var hide = true;
    var element = evt.toElement;
    if (element == null && evt.relatedTarget) element = evt.relatedTarget;
    //if(element == null) alert('e.toElement is null!');

    // If the to element is a text symbol we ignore the mouse out event to avoid blinking in IE
    //alert(element.firstChild.tagName);
    if (element && element.tagName && element.tagName == 'shape' && element.firstChild && element.firstChild.tagName && element.firstChild.tagName == 'path')
        return true;
    
    while (element != null) {
        if (element == gdMapPage.map.infoWindow._window || element == gdMapPage.map.infoWindow._pointer || (element.id && element.id == "MapTipHoverContainerDiv") ) {
            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 == gdMapPage.map.infoWindow._window || element == gdMapPage.map.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;
}

