function loadTrackingTab() {
    refreshTrackingData(0);
    if ($('#GeoLatitudeCurrent').length > 0) {


        /* $('#timer').timer({
             duration: '1m',
             callback: function () {
                 refreshTrackingData(1);
             },
             repeat: true //repeatedly calls the callback you specify
         });*/
        trackingTimerId = window.setInterval(function () { refreshTrackingData(1); }, 60000);
    }
}
var trackingTimerId;

function hookAuthToleave() {
    $('.submitAuthToLeave').click(function () {
        $.post('/Public/Consignment/TrackingAuthToLeave', { Id: $('#IdConsignment').val(), AuthToLeaveName: $('#AuthToLeaveName').val(), AuthToLeaveNote: $('#AuthToLeaveNote').val(), PuAtKy:$('#PuAtKy').val() }, function (result) {
            $('.authToLeaveNote').html(result.Html);
            hookAuthToleave();
        });
    });
}

function hookReDelivery() {
    if ($('.submitReDelivery').length > 0) {
        InitReDeliveryDate();
        $('.submitReDelivery').click(function () {
            $.post('/Public/Consignment/TrackingReDelivery', { Id: $('#IdConsignment').val(), ReDeliveryDate: $('#ReDeliveryDate').val(), PuAtKy: $('#PuAtKy').val() }, function (result) {
                $('.redeliveryDiv').html(result.Html);
                hookReDelivery();
            });
        });
    }
}

function refreshTrackingData(timer) {
    $enableAjaxLoading = false;
    if (timer == 0)
        $('.trackingtab').addClass("loading");
    $('.trackingtab').html('');
    $.post('/Portal/Consignment/Tracking', { id: $('#IdConsignment').val() }, function (result) {
        $('.trackingtab').html(result);
        initTrackingTab();

        $('.trackingtab').removeClass("loading");
        $enableAjaxLoading = true;
    });
}


function leaveTrackingTab() {
    clearInterval(trackingTimerId);
    // $('#timer').timer('remove');
}


var mapsApiLoaded = false;
function initTrackingTab() {
    if ($('#GeoLatitudeCurrent').length > 0) {
       

        if ($('#x_c_sa').val() == 'true') {
            if (mapsApiLoaded) {
                setupMapsProvider1();
            } else {
                $.cachedScript('https://maps.google.com/maps/api/js?sensor=false&key=' + $('#gmkey').val()).done(function (script, textStatus) {
                    $.cachedScript('/Resource/KingboardScript/maps/markerwithlabel').done(function (script, textStatus) {
                        mapsApiLoaded = true;
                        setupMapsProvider1();
                    });
                });
            }
        } else {
            if (mapsApiLoaded) {
                setupMapsProvider2();
            } else {
                $.cachedScript('https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.0/mapbox-gl.js').done(function (script, textStatus) {
                    setupMapsProvider2();
                });
            }
        }
    }


    hookAuthToleave();
    hookReDelivery();

    $('a.trackingLink').on('click', function () {
        copyToClipboard($(this).attr('rel'));
        var win = window.open($(this).attr('href2'), '_blank');
        if (win) {
            win.focus();
        } else {
            alert('Please allow popups for this website');
        }
    });

    $('#publictrackingview').change(function () {
        $('.scanhistorydiv').html('');
        $('.scanhistorydiv').addClass('loading');
        $.post('/Portal/Consignment/TrackingScanView', { id: $('#IdConsignment').val(), id2: $('#publictrackingview').prop('checked') }, function (result) {
            $('.scanhistorydiv').html(result);
            $('.scanhistorydiv').removeClass('loading');

        });
    });
}

function setupMapsProvider1() {//googlemaps
    var GeoLatitudeCurrent = $('#GeoLatitudeCurrent').val();
    var GeoLongitudeCurrent = $('#GeoLongitudeCurrent').val();

    var GeoLatitudeReceiver = $('#GeoLatitudeReceiver').val();
    var GeoLongitudeReceiver = $('#GeoLongitudeReceiver').val();

    var GeoLatitudeSender = $('#GeoLatitudeSender').val();
    var GeoLongitudeSender = $('#GeoLongitudeSender').val();

    var GeoCurrentReadingTime = $('#GeoCurrentReadingTime').val();

    var GeoCurrentTitle = $('#GeoCurrentTitle').val();
    if (GeoCurrentTitle == '')
        GeoCurrentTitle = 'Current Location';
    var GeoCurrentHide = $('#GeoCurrentHide').val();

    $('#map').transvirtualmap({ mapTagName: 'contracking' });
    $('#map').transvirtualmap("clearMarkers");
    $('#map').transvirtualmap("clearPolyline");
    if (GeoCurrentHide.toLowerCase() == 'false') {
        if (GeoCurrentTitle == 'PendingPickup')
            $('#map').transvirtualmap("addMarker", GeoLatitudeCurrent, GeoLongitudeCurrent, 'Sender', 'Consignment', '', 0, '');
        else
            $('#map').transvirtualmap("addMarker", GeoLatitudeCurrent, GeoLongitudeCurrent, GeoCurrentTitle, 'Truck', '', 0, '');
    }
    $('#map').transvirtualmap("addMarker", GeoLatitudeReceiver, GeoLongitudeReceiver, 'Receiver', 'Consignment', '', 0, '');
    $('#map').transvirtualmap("addMarker", GeoLatitudeSender, GeoLongitudeSender, 'Sender', 'Consignment', '', 0, '');
}

function setupMapsProvider2() {//mapbox
    var GeoLatitudeCurrent = $('#GeoLatitudeCurrent').val();
    var GeoLongitudeCurrent = $('#GeoLongitudeCurrent').val();

    var GeoLatitudeReceiver = $('#GeoLatitudeReceiver').val();
    var GeoLongitudeReceiver = $('#GeoLongitudeReceiver').val();

    var GeoLatitudeSender = $('#GeoLatitudeSender').val();
    var GeoLongitudeSender = $('#GeoLongitudeSender').val();

    var GeoCurrentReadingTime = $('#GeoCurrentReadingTime').val();

    var GeoCurrentTitle = $('#GeoCurrentTitle').val();
    if (GeoCurrentTitle == '')
        GeoCurrentTitle = 'Current Location';
    var GeoCurrentHide = $('#GeoCurrentHide').val();


    $('#map').transvirtualmapbox({ mapTagName: 'contracking' });
    //     $('#map').transvirtualmapbox("clearMarkers");
    //     $('#map').transvirtualmapbox("clearPolyline");
    if (GeoCurrentHide.toLowerCase() == 'false') {
        if (GeoCurrentTitle == 'PendingPickup')
            $('#map').transvirtualmapbox("addMarker", GeoLatitudeCurrent, GeoLongitudeCurrent, 'Sender', 'Consignment', '', 0, '');
        else
            $('#map').transvirtualmapbox("addMarker", GeoLatitudeCurrent, GeoLongitudeCurrent, GeoCurrentTitle, 'Truck', '', 0, '');
    }
    $('#map').transvirtualmapbox("addMarker", GeoLatitudeReceiver, GeoLongitudeReceiver, 'Receiver', 'Consignment', '', 0, '');
    $('#map').transvirtualmapbox("addMarker", GeoLatitudeSender, GeoLongitudeSender, 'Sender', 'Consignment', '', 0, '');
}