﻿var postponedYear = null;
var lastMenuItemName = null;
var nbrMenu = null;

//Start loading the menu
//When the menu is still loading, the requested value is saved in postponedYear
function BeginLoadMenu() {
    var CallbackValue = cbxYear.GetValue();
    if (nbrMenu != null && nbrMenu.GetSelectedItem() != null)
        lastMenuItemName = nbrMenu.GetSelectedItem().name;
    if (pnlCallbackMenu.InCallback())
        postponedYear = CallbackValue;
    else
        pnlCallbackMenu.PerformCallback(CallbackValue);
}

//Callback function invoked after the menu is loaded
//If postponedYear contains a value, another ajax call is started
function EndLoadMenu() {     
    printBietenjaar.innerHTML = "Bietenjaar " + cbxYear.GetValue();
    if (postponedYear != null) {
        pnlCallbackMenu.PerformCallback(postponedYear);
        postponedYear = null;
    }
    else {
        var Item = nbrMenu.GetItemByName(lastMenuItemName);
        if (Item != null) {
            nbrMenu.SetActiveGroup(Item.group);
            nbrMenu.SetSelectedItem(Item);
        }
        else {

        }
        printBietenjaar.innerHTML = "Bietenjaar " + cbxYear.GetValue();
        BeginLoadContent();
    }
}

var postponedContent = null;
var pnlChart = null;
var lastChartType = 0;

//Start loading a content
//When another content is still loading, the requested value is saved in postponedContent
function BeginLoadContent() {
    if (pnlChart != null)
        pnlChart.SetClientVisible(false);
    var CallbackValue = nbrMenu.GetSelectedItem().name + ";" + cbxYear.GetValue();
    if (pnlCallbackContent.InCallback())
        postponedContent = CallbackValue;
    else
        pnlCallbackContent.PerformCallback(CallbackValue);
}

//Callback function invoked after a content is loaded
//If postponedContent contains a value, another ajax call is started
function EndLoadContent() {
    if (postponedContent != null) {
        pnlCallbackContent.PerformCallback(postponedContent);
        postponedContent = null;
    }
    else {
        lastChartType = 0;
        //Add the noprint class to the border cells of the rounded panel
        $("#pnlCallbackContent>table>tbody>tr>td>table>tbody>tr>td:not([class])").addClass("noprint");
        //Remove the top border of the second row in the table to prevent double borders
        $("#pnlCallbackContent_pnlContent_divTable>div>table>tbody>tr:eq(1)>td").css("border-top-style", "none");
        //Remove the left border of the second column in the table to prevent double borders
        //Note: the jQuery function nth-child is one-based instead of zero-based.
        $("#pnlCallbackContent_pnlContent_divTable>div>table>tbody>tr>td:nth-child(2)").css("border-left-style", "none");
    }
}

var postponedChart = null;

//Start loading a chart
//When another chart is still loading, the requested value is saved in postponedChart
function BeginLoadChart(requestedValue) {
    if (pnlCallbackChart.InCallback())
        postponedChart = requestedValue;
    else
        pnlCallbackChart.PerformCallback(requestedValue);
}

//Callback function invoked after a chart is loaded
//If postponedChart contains a value, another ajax call is started
function EndLoadChart() {
    if (postponedChart != null) {
        pnlCallbackChart.PerformCallback(postponedChart);
        postponedChart = null;
    }
    else {
        if (pnlChart != null) {
            lastChartType = pnlChart.cpLastChartType;
            if (lastChartType == 1) {
                GetMap();
            }
        }
        //Add the noprint class to the border cells of the rounded panel
        $("#pnlCallbackChart>table>tbody>tr>td>table>tbody>tr>td:not([class])").addClass("noprint");
    }
}

var map = null;
var layer = null;
var pinid = 0;

//Loads the map
function GetMap() {
    //Dispose previous map
    DisposeMap();
    
    map = new VEMap('pnlCallbackChart_pnlChart_pnlMap');
    mapOptions = new VEMapOptions();
    mapOptions.EnableBirdseye = false;
    mapOptions.EnableDashboardLabels = false;

    mapOptions.LoadBaseTiles = false;
    var bFixed = true;
    
    map.LoadMap(new VELatLong(52.18004324429272, 5.251450013862794), 7, VEMapStyle.Aerial, bFixed, VEMapMode.Mode2D, false, null, mapOptions);
    map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
    map.SetMouseWheelZoomToCenter(false);
    //map.EnableShapeDisplayThreshold(false);
    CreateTileLayers();
    CreateLayer();
    PageMethods.GetMapData(pnlChart.cpMapIDs, OnSucceeded, OnFailed);
}

//Dispose the map
function DisposeMap() {
    if (map != null) {
        map.Dispose();
        map = null;
    }
}

//Create the tile layers
function CreateTileLayers() {         
    //White background to hide the default VE images
    var tileSourceSpec = new VETileSourceSpecification("white", pnlChart.cpWhiteLayer);            
    tileSourceSpec.ZIndex = 2;
    map.AddTileLayer(tileSourceSpec, true);
    
    //Actual map layer
    var tileSourceSpec = new VETileSourceSpecification("maptiles", pnlChart.cpLayerFolder);
    tileSourceSpec.ZIndex = 3;
    map.AddTileLayer(tileSourceSpec, true);
}
         
//Create the layer for the shapes
function CreateLayer() {
    layer = new VEShapeLayer();
    map.AddShapeLayer(layer);
}

//Add a pushpin to the map layer
function AddPushpin(shapedata) {

    var shapePoints = new Array();
    for (x = 0; x < shapedata.Points.length; x++) {
        shapePoints[x] = new VELatLong(shapedata.Points[x].Latitude, shapedata.Points[x].Longitude);
    }

    var shape = new VEShape(VEShapeType.Pushpin, shapePoints);

    shape.SetCustomIcon("<div style='color:Black;font-family: Tahoma; font-size: 11px;background-color:White;white-space: nowrap; padding-right: 2px; padding-left: 2px;'>" + shapedata.IconText + "</div>");
    //Set the info box
    //map.ClearInfoBoxStyles();
    shape.SetTitle(shapedata.Title);
    //shape.SetDescription(infobox);         

    pinid++;
    layer.AddShape(shape);
}


// Callback function invoked on successful 
// completion of the page method.
function OnSucceeded(result, userContext, methodName) {
    if (methodName == "GetMapData") {
        for (y = 0; y < result.length; y++) {
            AddPushpin(result[y]);
        }
    }
}

// Callback function invoked on failure 
// of the page method.
function OnFailed(error, userContext, methodName) {
    if (error !== null) {
        alert("An error occurred: " + error.get_message());
    }
}
