﻿/// <reference path="SearchPanel.ascx"/> /// SearchPanel = {} is defined in SearchPanel control

SearchPanel.collapsed = true;
SearchPanel.eventsWired = false;

SearchPanel.GetOffsets = function ()
{
    var searchPanelMode = SearchPanel.selectedSearchPanelMode;
    var offsetX = 0, offsetY = 35; // default offsets

    switch( searchPanelMode)
    {
        case 'Clients':
            offsetX = 20;
            break;
        case 'Home':
            offsetX = 0;
            offsetY = 108;
            break;
        case 'MyAppointments':
        case 'SearchResults':
            offsetX = 19;
            offsetY = 43;
            break;
        default:
            break;
    }
    
    return { x: offsetX, y: offsetY };
}

SearchPanel.btnSearch_click = function (supressSimilarSearch)
{
    // if user is on confirmation page, make sure they understanding a search will cancel the appointment
    if (window.unloadMessage)
    {
        if (!confirm(window.unloadMessage))
        {
            return false;
        }
    }

    var offsets = SearchPanel.GetOffsets();

    // Destroy previous bubble if it exists.
    if (window.InfoBubbleBase)
    {
        window.InfoBubbleBase.dispose();
        window.InfoBubbleBase = null;
    }

    var txtLocation = SearchPanel.txtLocation;
    var txtParameters = SearchPanel.txtParameters;

    var location = '';
    var parameters = '';

    var tightLocation = txtLocation.value.replace(/([\s\W])+/g, '').toLowerCase();

    var states = { 'alabama': true, 'alaska': true, 'arizona': true, 'arkansas': true, 'california': true, 'colorado': true, 'connecticut': true, 'delaware': true, 'districtofcolumbia': true, 'florida': true, 'georgia': true, 'hawaii': true, 'idaho': true, 'illinois': true, 'indiana': true, 'iowa': true, 'kansas': true, 'kentucky': true, 'louisiana': true, 'maine': true, 'maryland': true, 'massachusetts': true, 'michigan': true, 'minnesota': true, 'mississippi': true, 'missouri': true, 'montana': true, 'nebraska': true, 'nevada': true, 'newhampshire': true, 'newjersey': true, 'newmexico': true, 'newyork': true, 'northcarolina': true, 'northdakota': true, 'ohio': true, 'oklahoma': true, 'oregon': true, 'pennsylvania': true, 'rhodeisland': true, 'southcarolina': true, 'southdakota': true, 'tennessee': true, 'texas': true, 'utah': true, 'vermont': true, 'virginia': true, 'washington': true, 'westvirginia': true, 'wisconsin': true, 'wyoming': true, 'ontario': true, 'quebec': true, 'novascotia': true, 'newbrunswick': true, 'manitoba': true, 'britishcolumbia': true, 'princeedwardisland': true, 'pei': true, 'saskatchewan': true, 'alberta': true, 'newfoundlandandlabrador': true, 'newfoundland': true };

    if (tightLocation === SearchPanel.originalTightLocation || tightLocation === '' || tightLocation.length <= 2 || states[tightLocation] === true)
    { // require a city (no empty string, no state codes, no simple state search)
        if (tightLocation != 'newyork' && tightLocation != 'washington')
        { // city name
            var div = document.createElement('div');
            div.innerHTML = 'Please provide both city and state or city and province and search again.';

            ShowBubble('We need some additional info!', '', BubbleType.Exclamation, div, txtLocation, { width: 264, height: 36, offsetX: offsets.x, offsetY: offsets.y });

            return false;
        }
    }

    if (tightLocation == 'newyork')
        location = 'New York, NY';
    else if (tightLocation == 'washington')
        location = 'Washington D.C.';

    if (txtParameters.value != SearchPanel.parametersText)
        parameters = txtParameters.value;

    SearchPanel.btnSearch.disabled = true;

    // Check for similar locations
    window.searchLocation = location || txtLocation.value;
    window.searchParameters = parameters;

    if (!supressSimilarSearch)
    {
        PageMethods.FindSimilarLocations(txtLocation.value, SearchPanel.FindSimilarLocations_Success, SearchPanel.FindSimilarLocations_Fail);
    }
    else
    {
        SearchPanel.btnSearch.disabled = false;
        window.location.href = '/SearchResults.aspx?w=' + encodeURIComponent(window.searchLocation) + '&q=' + encodeURIComponent(window.searchParameters);
    }

    return false;
}

SearchPanel.FindSimilarLocations_Success = function(value)
{
    var txtLocation = SearchPanel.txtLocation;
    var btnSearch = SearchPanel.btnSearch;
    var offsets = SearchPanel.GetOffsets();

    btnSearch.disabled = false;

    if (!value || value.length === 0)
    {
        //window.location.href = '/SearchResults.aspx?w=' + window.searchLocation + '&q=' + window.searchParameters;
        var searchLocation = decodeURIComponent(window.searchLocation);

        var div = document.createElement('div');
        div.innerHTML = 'Please check the spelling and try again.';

        ShowBubble('We can\'t find that location', '', BubbleType.Exclamation, div, txtLocation, { width: 240, height: 34, offsetX: offsets.x, offsetY: offsets.y });
    }
    else if (value.length === 1)
    {
        window.location.href = '/SearchResults.aspx?w=' + encodeURIComponent(value[0]) + '&q=' + encodeURIComponent(window.searchParameters);
    }
    else if (value.length > 1)
    {
        var similarNameArea = new Element('div');

        for (var i = 0; i < value.length; i++)
        {
            var div = document.createElement('div');

            var span = document.createElement('span');
            span.className = 'similarLocation';
            span.innerHTML = value[i];

            span.onclick = function()
            {
                txtLocation.value = this.innerHTML;
                SearchPanel.btnSearch_click(true);
            };

            div.appendChild(span);

            similarNameArea.appendChild(div);
        }

        ShowBubble('Please select a location', '', BubbleType.Question, similarNameArea, txtLocation, { width: 240, offsetX: offsets.x, offsetY: offsets.y });
    }
}

SearchPanel.FindSimilarLocations_Fail = function(value)
{   // submit query as-is
    SearchPanel.btnSearch.disabled = false;

    window.location.href = '/SearchResults.aspx?w=' + encodeURIComponent(window.searchLocation) + '&q=' + encodeURIComponent(window.searchParameters);
}

SearchPanel.hideInfoBubble = function ()
{
    // Destroy previous bubble if it exists.
    if (window.InfoBubbleBase)
    {
        window.InfoBubbleBase.dispose();
        window.InfoBubbleBase = null;
    }
}

SearchPanel.executeSearchEvent = function (ev)
{
    if (ev.key === 'enter' || ev.type === 'click')
    {
        SearchPanel.btnSearch_click(false);

        return false;
    }
}

SearchPanel.txtLocation.addEvent('keydown', SearchPanel.hideInfoBubble);
SearchPanel.txtParameters.addEvent('keydown', SearchPanel.hideInfoBubble);
SearchPanel.txtParameters.addEvent('focus', SearchPanel.hideInfoBubble);
SearchPanel.txtLocation.addEvent('keydown', SearchPanel.executeSearchEvent);
SearchPanel.txtParameters.addEvent('keydown', SearchPanel.executeSearchEvent);

SearchPanel.btnSearch.addEvent('click', SearchPanel.executeSearchEvent);
