﻿/*
    Handle the reservation form.  Heavily dependent on form's structure.
    Dependencies: 
        jquery.ui.datepicker.js
        Cyg.Framework.Web.DateTime.js
*/

ReservationForm = function() {
    var _suiteStudioNLIDs = undefined;
    
    // need to populate some hidden fields based on the new value
    ArrivalDate_change = function() {
        var arrival = new Date($("#ArrivalTextBox").val());
        var departure = new Date($("#DepartureTextBox").val());

        if (departure.getTime() <= arrival.getTime()) {
            var newDeparture = DateTime.addDays(arrival, 1);
            $("#DepartureTextBox").val(DateTime.shortDateFormat(newDeparture));
            departure = new Date($("#DepartureTextBox").val());
        }

        var days = DateTime.dateDiff(arrival, departure);
        $('#NightsTextBox').val(days);
    };
    
    Location_change = function() {
        var selected = $('#LocationSelect option:selected').text();
        var isBowlingGreen = selected.indexOf("Bowling Green") != -1;
        var isTuscaloosa = selected.indexOf("Tuscaloosa") != -1;

        if (isBowlingGreen || isTuscaloosa) {
            $("#ReservationForm .suites-or-studios").slideDown('fast');
        }
        else {
            $("#ReservationForm .suites-or-studios").slideUp('fast');
        }
    };

    // Cleanest way to handle suites vs. studios is to hard code :\
    SuitesOrStudios_change = function() {
        var value = $(".suites-or-studios input:checked").val();

        if (value === 'Suites') {
            $('#LocationSelect option:contains("Bowling Green")').val(_suiteStudioNLIDs.bowlingGreen_suites);
            $('#LocationSelect option:contains("Tuscaloosa")').val(_suiteStudioNLIDs.tuscaloosa_suites);
        }
        else if (value === 'Studios') {
            $('#LocationSelect option:contains("Bowling Green")').val(_suiteStudioNLIDs.bowlingGreen_studios);
            $('#LocationSelect option:contains("Tuscaloosa")').val(_suiteStudioNLIDs.tuscaloosa_studios);
        }
    };

    var obj = {
        init: function(suiteStudioNLIDs) {
            _suiteStudioNLIDs = suiteStudioNLIDs;
            
            $('input[type="text"].datetime').datepicker({
                minDate: (new Date()),
                dateFormat: 'm/d/yy',
                onShow: function() {
                    $('object[type="application/x-shockwave-flash"]').hide(); // Flash elements obstruct datepickers, so hide them.
                },
                onHide: function() {
                    $('object[type="application/x-shockwave-flash"]').show();
                }
            });

            // not valid XHTML Strict, do it like this so we validate
            $("#ReservationForm").attr('target', '_blank');

            // Initially hide the cancellation form, but show it if requested.
            $("#CancellationForm").hide();
            $("#ToggleResFormButton").click(function() {
                obj.toggle();
                return false;
            });

            // Event handlers
            $('#ArrivalTextBox,#DepartureTextBox').change(ArrivalDate_change);
            ArrivalDate_change();
            
            if (_suiteStudioNLIDs !== undefined) {
                // Eliminate duplicate location options.
                var last = undefined;
                $("#LocationSelect option").each(function() {
                    var current = $(this).text();
                    if (last && last == current) {
                        $(this).remove();
                    }
                    last = current;
                });
            
                $('#LocationSelect').change(Location_change);
                Location_change();
                $('.suites-or-studios input').click(SuitesOrStudios_change);
                SuitesOrStudios_change();
                
                // Provide tooltips for suites vs. studios
                $('.suites-or-studios input').prev('label').andSelf().tooltip({
                    opacity: 1,
                    bodyHandler: function() {
                        var selected = $('#LocationSelect option:selected').text();
                        var isBowlingGreen = selected.indexOf("Bowling Green") != -1;
                        var isTuscaloosa = selected.indexOf("Tuscaloosa") != -1;

                        var location = '';
                        var type = $(this).parent('div.row').find(':radio').val().toLowerCase();

                        if (isBowlingGreen) {
                            location = "bowlinggreen";
                        }
                        else if (isTuscaloosa) {
                            location = "tuscaloosa";
                        }
                        else {
                            return '';
                        }

                        return $('.' + location + '-' + type + '-tooltip').html();
                    }
                });
            }
            
        },
        toggle: function() {
            var toggleButton = $("#ToggleResFormButton");

            if ($('#CancellationForm').is(':visible')) {
                $('#CancellationForm').hide();
                $('#BookingForm').fadeIn();
                toggleButton.text('View/Change a reservation');
                $('#ReservationForm')
                    .attr('action', 'https://res.windsurfercrs.com/bbe/page2.aspx')
                    .attr('method', 'get');
                $('#ReservationForm input:submit').val('Book a Room');
            }
            else {
                $('#BookingForm').hide();
                $('#CancellationForm').fadeIn();
                toggleButton.text('Book a reservation');
                $('#ReservationForm')
                    .attr('action', 'https://res.windsurfercrs.com/bbe/page5.aspx')
                    .attr('method', 'get');
                $('#ReservationForm input:submit').val('Find Reservation');
            }
        }
    }

    return obj;
} ();