﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Novasys.Maponair.WebContainer.MapFunction.js"/>

//UpdatePanel.js

//function attachPageLoading() {
//    Sys.Application.add_load(page_load);
//}

//function page_load() {
//    if (Sys != null && Sys.WebForms != null) {
//        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(onpageLoaded);
//    }
//    else {
//        alert("Sys.WebForms.PageRequestManager is null");
//    }
//}

// In der Funktion checkst du alle UpdatePanels, ob sie aktualisiert wurden. 
function onpageLoaded(sender, pageLoadedEventArgs) {

    var addedForm = false;

    //Set position of single hit
    try {
        SetSearchedAddress();
    }
    catch (e)
        { }


    //Set Route on FlashClient
    var routeId = document.getElementById('PortalRouteId').value;
    if (routeId == null || routeId == "") {
        //Remove all previous routes and markers
        RemoveMarkerInMap('MoaSearchMarker');
        RemoveMagnifyingGlass();
        RemoveAllMoaForms();
        RemoveAllRoutes();
        RemoveInstructionMarker();
    }
    else {
        if (myFlashClient != null && myFlashClient.RouteExists != null && !myFlashClient.RouteExists(routeId)) {

            RemoveMarkerInMap('MoaSearchMarker');
            RemoveMagnifyingGlass();
            RemoveAllMoaForms();
            RemoveAllRoutes();
            RemoveInstructionMarker();

            var token = document.getElementById('PortalToken').value;

            var polyLine = document.getElementById('PortalPolyLine').value;

            AddRouteToMapWithPolyline(routeId, polyLine);
        }
    }


    //Set Bounds of map
    var bounds = GetBounds();
    if (bounds) {
        if (myFlashClient != null) {
            try {
                myFlashClient.SetVisibleBounds(bounds, true);
                if (myFlashClient.GetZoomLevel() > 17) {
                    myFlashClient.SetZoomLevel(17);
                }
            }
            catch (e) {
                window.status = "Can't set bounds";
            }
        }
    }


    //SetCenterOfMap
    var adrOrig = document.getElementById('PortalCenterOfMap');
    if (adrOrig != null && adrOrig.value != "") {
        var adrPos = JSON.parse(adrOrig.value);
        if (adrPos != null) {
            if (adrPos.Longitude != null && adrPos.Longitude != '0') {
                try {
                    //                    alert("UpdatePanel.js SetCenterPosition long:" + adrPos.Longitude + " lat:" + adrPos.Latitude); 
                    myFlashClient.SetCenterPosition({ longitude: adrPos.Longitude, latitude: adrPos.Latitude }, 300);

                }
                catch (e) {
                    window.status = "Can't set center of map";
                }
            }
        }
    }



}

