﻿
function GetElementByID(obj) {
    return document.getElementById(obj);
}

/*
Updates the main page by sending a request to ajax manager component.
*/
function UpdatePage(PageLoading) {
    try {

        StartAJAXRequest();
        map.clearOverlays();

        // Add the custom tiling back to the map. If this
        // has been called as a result of a refresh then
        // the tiles will be refreshed.
        setupMapForCustomTiling();

        //Need to reload Traffic dashboard
        //camera url will be attached to a random number generator, camera image has its own timeout value
        //Sign will get reloaded when a call to AddSignEntities(entities['Sign']) is made..
        //Reload incident viewer, so we can see new events
        LoadIncidentViewerPage();
        DisplayViewerType('Inc');        
        
        MakeAjaxCallToTheWebService(PageLoading);

    }
    catch (e) {
        alert(e.name + "\n" + e.message + " - UpdatePage");
    }
}

function MakeAjaxCallToTheWebService(PageLoading) {

    $.ajax({
        type: "POST",
        url: "DataProvider.asmx/LoadEntities",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: ErrorMsg,
        success: LoadData,
        timeout: 120000
    });
}

/*
* The parameter data is populated by the calling service and it contains returned data from the method call.
*/
function LoadData(data) {

    var entities = data.d;

    AddCameraEntities(entities['Camera']);
    AddSignEntities(entities['Sign']);
    AddIncidentEntities(entities['Incident']);
    AddCameraTourEntities(entities['CameraTour']);
    AddDetectorStationEntities(entities['Speed']);
    
    Dispose(data);

}

function ErrorMsg(errorMessage) {

    alert(errorMessage.responseText);
       
}

/*
* Dispose the data object and does some grunt work to set toolbar states based on the saved cookies.
*/
function Dispose(data) {

    data.d.length = 0; data.d = null; data = null;

    EndAJAXResponse();

    if (BrowserDetect.browser == "Explorer")
        CollectGarbage();
}


function pop_Video(URL) {
    eval("winvideo = window.open('video.aspx?URL='+URL,'winvideo','status=no,width=324,height=300,scrollbars=no')");
    winvideo.focus();

}
function CheckCenter() {
    var points = map.getCenter().toString();
    points = points.replace('(', '');
    points = points.replace(')', '');
    points = points.split(',');
    var y = Math.round(points[0] * 1000000) / 1000000; //formula gives 6 decimal places and converts string to number
    var x = Math.round(points[1] * 1000000) / 1000000; //formula gives 6 decimal places and converts string to number
    setCookie('sessionStateLatCenter', y);
    setCookie('sessionStateLngCenter', x);
}

/**
* Resets the zoom level if it exceeds the allowable value. This method also refreshes the data so 
* that the GPolylines can be painted again.
*/
function CheckZoom(oldZ, newZ) {
    //variable to see if zoom level is exceeding allowable value.
    var maxZoomLevelReached = false;

    if (newZ > _maxZ || newZ < _minZ) {
        maxZoomLevelReached = true; //set it true to prevent repainting of GPolylines
        map.setZoom(oldZ);
    }

    setCookie('sessionStateZoomLevel', newZ / 1); //dividing by 1 converts string to number

}
