﻿var __DSSites = [];    // Array of dive sites
var __DSControls = []; // Array of textboxes/divs

// A consuming page can define this to be notified when 
// the sites have been loaded
var DSLoadComplete = null;

//
// Determines if the site is a match, based on partial strings.
//
function DSIsMatch(o,s)
{
    if (-1 < o.matchstring.indexOf(s.toLowerCase()))
    {
        return true;
    }
    else
    {
        return false;
    }
};

//
// A wrapper for the textbox and its associated popup div. The
// optional iframe is for IE6 and earlier as a workaround for
// the <select> bug.
//
function DSControl(textbox, div, iframe)
{
    this.textbox = textbox;
    this.div = div;
    this.iframe = iframe;
};

//
// Shows the popup dive site list
//
DSControl.prototype.show = function()
{
    this.div.style.display = 'block';
    this.div.style.width = this.textbox.clientWidth + 'px';
    if (this.iframe)
    {
        this.iframe.style.left = this.div.style.left;
        this.iframe.style.top = this.div.style.top;
        this.iframe.style.width = this.div.clientWidth + 'px';
        this.iframe.style.height = this.div.clientHeight + 'px';
        this.iframe.style.display = 'block';
    }
};

//
// Hides the popup dive site list
//
DSControl.prototype.hide = function()
{
    this.div.style.display = 'none';
    if (this.iframe)
    {
        this.iframe.style.display = 'none';
    }
};

//
// Initiates the call to PyD to fetch dive site information
//
function DSInit()
{
    try
    {
        new Ajax.Request('/AJAX/GetAllSites.ashx',
        {
            onSuccess: function(transport)
            {
                var json = transport.responseText.evalJSON();

                // Reload the array
                __DSSites = [];
                for (i = 0; i < json.length; i++)
                {
                    __DSSites.push(json[i]);
                }

                if (DSLoadComplete)
                {
                    DSLoadComplete();
                }
            },
            onFailure: function(transport)
            {
                error(transport);
            }
        });
    }
    catch (e)
    {
        error(e);
    }
};

function DSGetMatches(s)
{
    var i;
    var ret = [];
    for (i = 0; i < __DSSites.length; i++)
    {
        if (true == DSIsMatch(__DSSites[i], s))
        {
            ret[ret.length] = __DSSites[i];
        }
    }
    return ret;
};

function DSHilite(div, on)
{
    if (true == on)
    {
        div.style.backgroundColor = '#fff';
        div.style.color = '#009';
    }
    else
    {
        div.style.backgroundColor = '';
        div.style.color = '';
    }
};

function DSGetControl(obj)
{
    var i;
    for (i = 0; i < __DSControls.length; i++)
    {
        if (__DSControls[i].textbox == obj || __DSControls[i].div == obj)
        {
            return __DSControls[i];
        }
    }
    return null;
};

function DSHide(textbox)
{
    DSGetControl(textbox).hide();
};

function DSHandle(textbox)
{
    var ctl, html, matches, matchCount, i;
    var LIMIT = 8;

    matchCount = 0;
    html = '';
    ctl = DSGetControl(textbox);

    if (ctl)
    {
        if ('' != textbox.value)
        {
            matches = DSGetMatches(textbox.value);
            matchCount = matches.length;

            //            if (1 == matchCount)
            //            {
            //                DSSelect(ctl, matches[0].id);
            //            }
            //            else
            //            {        
            if (0 == matchCount)
            {
                html = 'No matches';
            }
            else
            {
                html = '<div class="smallText" style="font-weight:bold;margin-bottom:4px">We found these matches:</div>';
                for (i = 0; i < matchCount; i++)
                {
                    html += '<div style="cursor:pointer" onmousedown="DSSelect(DSGetControl(this.parentElement||this.parentNode),' + matches[i].id + ')" onmouseover="DSHilite(this,true)" onmouseout="DSHilite(this,false)">' + matches[i].name + '</div>';

                    if (LIMIT <= i)
                    {
                        html += '<span style="font-style:italic">+' + (matchCount - LIMIT) + ' more</span>';
                        break;
                    }
                }
            }
            //            }
        }

        if (0 < html.length)
        {
            ctl.div.innerHTML = html;

            if ('block' != ctl.div.style.display)
            {
                var pt = getAbsPos(textbox);
                ctl.div.style.left = pt.x + 'px';
                ctl.div.style.top = (pt.y + textbox.clientHeight + 2) + 'px';
            }

            ctl.show();
        }
        else
        {
            ctl.hide();
        }

        // Let the subscriber know we don't have a valid selection
        if (noSiteSelected && 1 != matchCount)
        {
            noSiteSelected(ctl);
        }
    }
};

function DSSelect(ctl, id)
{
    var i;
    if (diveSiteSelected)
    {
        for (i = 0; i < __DSSites.length; i++)
        {
            if (__DSSites[i].id == id)
            {
                diveSiteSelected(ctl, __DSSites[i]);
                break;
            }
        }
    }
};

function DSAttach(textbox)
{
    if (!textbox)
    {
        return;
    }
    
    // Bind to the key press event
    textbox.onkeyup = function()
    {
        DSHandle(this);
    };

    textbox.onblur = function()
    {
        DSHide(this);
    };

    // Create a DIV to show the matches
    var div = document.createElement('DIV');
    div.className = 'siteselectiondropdown';
    div.style.display = 'none';
    (document.body || document.documentElement).appendChild(div);

    // Perform check for IE6 and lower
    var iframe = null;
    var uaParts = window.navigator.userAgent.split(';');
    for (var i = 0; i < uaParts.length; i++)
    {
        if ('MSIE' == uaParts[i].trim().substring(0, 4))
        {
            if (7 > parseInt(uaParts[i].trim().substring(5)))
            {
                iframe = document.createElement('IFRAME');
                iframe.style.filter = 'Alpha(opacity=0)';
                iframe.style.position = 'absolute';
                iframe.style.zIndex = 1;
                iframe.style.display = 'none';
                (document.body || document.documentElement).appendChild(iframe);
            }
            break;
        }
    }

    __DSControls[__DSControls.length] = new DSControl(textbox, div, iframe);
};