﻿// This functionality is used to combine expanded panel functionality and town search ajax

// It overrides destination panel links, so that when you click on them, rather than redirecting to that page,
// the anchor's text is displayed in the town search text box

// When a town search occurs, the destination panel links are rebound (since the newly returned links
// won't have been overridden).

// The town search text box will only accept values from either clicking on a link, or pressing the enter key.
// In this case, the standard watermark functionality isn't suitable, so the blur is applied when the text box is
// focussed, and removed when the user clicks a link or presses enter. (It's complicated).

// When a user selects a location, a cookie is created with the current destination code

function bindAdvancedDestinationExpandedPanelFunctionality(options) {

    // Options:

    if (options === undefined || options === null)
        options = {};

    options.expandedPanelID = options.expandedPanelID === undefined ? "" : options.expandedPanelID;
    options.inputFieldID = options.inputFieldID === undefined ? "" : options.inputFieldID;
    options.inputFieldWatermark = options.inputFieldWatermark === undefined ? "" : options.inputFieldWatermark;
    options.hiddenFieldID = options.hiddenFieldID === undefined ? "" : options.hiddenFieldID;
    options.cookieName = options.cookieName === undefined ? "" : options.cookieName;
    options.townSearchParameters = options.townSearchParameters === undefined ? {} : options.townSearchParameters;

    if ((options.expandedPanelID != "") && (options.inputFieldID != "") && (options.hiddenFieldID != "") && (options.townSearchParameters.resultFieldID != "")) {

        // Bind the links within the Destinations expanded panel to populate the Destinations TextBox
        overrideExpandedPanelLinks({
            expandedPanelID: options.expandedPanelID,
            inputFieldID: options.inputFieldID,
            hiddenFieldID: options.hiddenFieldID,
            cookieName: options.cookieName
        });

        // Construct callback options (as used by the Town Search callback functions)
        var callBackParameters = {
            expandedPanelID: options.expandedPanelID,
            inputFieldID: options.inputFieldID,
            hiddenFieldID: options.hiddenFieldID,
            cookieName: options.cookieName
        }

        // Construct / replace the townSearch parameters
        // Needs to reference call back functions in this file
        var townSearchParameters = options.townSearchParameters;
        townSearchParameters.enterCallBack = "overrideEnterKeyUp";
        townSearchParameters.enterCallBackParameters = callBackParameters;
        townSearchParameters.completeCallBack = "overrideExpandedPanelLinks";
        townSearchParameters.completeCallBackParameters = callBackParameters;

        // If no input field has been provided in the parameters, just use the inputFieldID that has been provided
        townSearchParameters.inputFieldID = townSearchParameters.inputFieldID === undefined ? options.inputFieldID : townSearchParameters.inputFieldID;

        // Bind the town search
        bindTownSearchFunctionalityToInputField(townSearchParameters);

        // Apply Watermark - This process is done differently to the usual 
        $(options.inputFieldID).applyWatermark(options.inputFieldWatermark, { alwaysClearOnFocus: true, applyBlurEvent: false });

        $(options.inputFieldID).on("focusin", function () {
            $(options.inputFieldID).bindWatermarkBlurEvent("", $(options.inputFieldID).val(), true);
        });

        // Seems that the StyledSelect functionality detaches click events
        // So every time the destination panel opens, we re-apply functionality to the other
        // drop down lists so they can hide the expanded panel if they're clicked
        $(options.inputFieldID).focusin(function () {
            $(".styledSelect li.closed").click(function () {
                hideExpandedPanel(options.expandedPanelID);
            });
        });
    }
}

function overrideExpandedPanelLinks(options) {

    // Remove any pre-bound click events
    $(options.expandedPanelID + " li a").off("click");

    $(options.expandedPanelID + " li a").on("click", function (event) {

        // Prevent the browser from following the link
        event.preventDefault();
        populateSearchTextBox(options.inputFieldID, $(this), options.hiddenFieldID, options.cookieName);
        hideExpandedPanel(options.expandedPanelID);
    });
}

function overrideEnterKeyUp(event, anchor, options) {

    populateSearchTextBox(options.inputFieldID, anchor, options.hiddenFieldID, options.cookieName);
    hideExpandedPanel(options.expandedPanelID);
}

function populateSearchTextBox(textBox, anchor, hiddenFieldID, cookieName) {

    var anchorText = anchor.text();

    // Populate the textbox with the anchor's text
    $(textBox).val(anchorText);

    // Retrieve the destination's code from the rel attribute
    var destinationCode = anchor.attr("rel");

    // Populate to a hidden field, so the search process can retrieve the current destination
    if ($(hiddenFieldID) != null) {
        $(hiddenFieldID).val(destinationCode);
    }

    // Re-apply the watermark functionality
    $(textBox).off("blur");
    $(textBox).bindWatermarkFocusEvent("", anchorText, true);

    generateDestinationCookie(cookieName, destinationCode);
}

function generateDestinationCookie(cookieName, destinationCode) {

    if ((destinationCode != null) && (destinationCode != undefined) && (cookieName != "")) {

        // Call to CookieManagement.js
        createCookie(cookieName, destinationCode, "");
    }
}
