
/*########################################################*/
/*-------------- directions widget --------------------*/

/*
 * DEV NOTE: there are 2 stub functions below that need to be populated with the real google maps API code:
 * -getDrivingDirections()
 * -onDirectionsLoaded()
 */

(function ($) {
    $.fn.initDirectionsWidget = function (locAddresses) {
        //for compass points directions
        //original code
        //$('ul.directions-groups-manager li input:radio').click(showDirectionsGroup);

        //new codem6
        $('ul.directions-groups-manager input:radio').click(showDirectionsGroup);

        function showDirectionsGroup(evt) {
            //original code            
            //$('div[class*="directions-group-"]').hide();
            //$('div.directions-group-' + evt.target.value).show();

            //new code
            $('div[class*="directions-group-"]').hide();
            $('div[class*="directions-group-' + evt.target.parentNode.className + '"]').show();
        }

        //--------------
        //for address directions
        var $searchField = $('input:text[name=start_address]');
        if ($searchField.length > 0) {
            $searchField.focus();
        }

        $(".directions-submit").click(getGoogleDirections);
        $(".directions-new-search").click(function () {
            showForm();
            $searchField.focus().val('');
            $('div.directions-field-wrapper label').data('inFieldLabel').checkForEmpty();
            return false;
        });

        function getGoogleDirections() {
            //stub function
            var dirType = $('input:radio[name=directions_type]:checked').val();
            var startAddress = $.trim($searchField.val());
            var endAddress;

            if (startAddress == '') {
                $('.directions-field-wrapper').addClass('error-border');
                return false;
            } else {
                $('.directions-field-wrapper').removeClass('error-border');
                endAddress = locAddresses[$('input:radio[name=end_address]:checked').val()];

                if (dirType == "car") {
                    getDrivingDirections(startAddress, endAddress);
                } else {
                    var qTemplate = "http://maps.google.ca/maps?f=d&hl=" + (GTAA_LANG || 'en') + "&dirflg=r&saddr={start}&daddr={end}";
                    qTemplate = qTemplate.replace('{start}', encodeURIComponent(startAddress));
                    qTemplate = qTemplate.replace('{end}', encodeURIComponent(endAddress));
                    window.open(qTemplate);
                }
            }

            return false;
        }

        function getDrivingDirections(startAddress, endAddress) {
            showProgress();

            //need this line here in order to show the google map correctly
            //problems occurs because the code attempts to redraw the map when the map-display div is hidden becasue its surrounding div is set to display:none
            $('div.directions-results-wrapper').show();

            var languageID = $('.curLang').val().toString();
            $.ajax({
                url: '../widgets/Directions/DirectionsList.ashx',
                data: { start: startAddress, end: endAddress, lang: languageID },
                success: function (retData) {
                    $('.direction-list-results').html(retData);
                },
                error: function (errorMessage) {
                    alert(errorMessage.valueOf());
                }
            });

            var directionDisplay;
            var directionsService;
            var map;

            directionsService = new google.maps.DirectionsService();
            directionsDisplay = new google.maps.DirectionsRenderer();

            var myOptions = {
                zoom: 7,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            map = new google.maps.Map(document.getElementById("map-display"), myOptions);
            directionsDisplay.setMap(map);

            var request = {
                origin: startAddress,
                destination: endAddress,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };

            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                }
            });

            setTimeout(onDirectionsLoaded(startAddress, endAddress), 2000);
            return false;
        }

        function onDirectionsLoaded(startAddress, endAddress) {
            $('.startLocation').html(startAddress);
            $('.endLocation').html(endAddress);
            showResults();
            return false;
        }

        function showProgress() {
            $('div.directions-form-wrapper').hide();
            $('div.directions-results-wrapper').hide();
            $('div.progress-white-on-dark').show();
            return false;
        }

        function showForm() {
            $('div.progress-white-on-dark').hide();
            $('div.directions-results-wrapper').hide();
            $('div.directions-form-wrapper').show();
            return false;
        }

        function showResults() {
            $('div.progress-white-on-dark').hide();
            $('div.directions-form-wrapper').hide();
            $('div.directions-results-wrapper').show();
            return false;
        }

        //--------------
        //for compass point directions pages

        /*
        http://maps.google.ca/maps
        ?f=d //displays "directions" form (two input boxes: from, to) 
        &saddr={start_address} //from address
        &daddr={end} Terminal+1,+Toronto-Pearson,+Mississauga,+Ontario //end address
        &hl=en //lang
        &dirflg=r //public transit

        &source=s_d
        &geocode=%3BFXKEmgIdDDVB-ykd9UvSDDkriDG8pNwBCGwrrA
        &mra=ls
        &ttype=dep
        &date=11%2F01%2F17
        &time=14:45
        &noexp=0
        &noal=0
        &sort=def
        &sll=43.680882,-79.61266
        &sspn=0.011204,0.025942
        &ie=UTF8
        &z=13
        &start=0
        */

    }
})(jQuery);

$(document).ready(function() {
    var locAddresses = {
        t1: $('.t1').val(),
        t3: $('.t3').val(),
        vpl: $('.vpl').val(),
        vpg: $('.vpg').val(),
        cpl: $('.cpl').val()
    };
    $.fn.initDirectionsWidget(locAddresses);
});
