/*
    Copyright (c) 2009, SpatialPoint, LLC.
    
    www.spatialpoint.com
*/
$(document).ready(function() 
{
    // Checks for Enter key, and submits geocode request.
    function testForEnter(e)
    {
        if (isKeyPressed(e, 13))
        { 
            findLocation(); 
            
            return false;
        }
    }
    
    // For input fields and State selection lists.
    $("input.locationFinder, select.locationFinder").keypress(testForEnter);

    // Use this to reset several forms at once.
    $("a.clear").click(function() 
    {
        $("form").each(function() 
        {
            this.reset();
        });

        // Hide matches/results.
        $("div.form_row_matches").css("display", "none");    
        
        // Remove pushpin.
        removePushpin();

        // Perform custom actions.
        onClearLocation();
    });

    $("a.find").click(function() 
    {
        findLocation();
    });

    // Location Finder tabs.
    $("a.form_tab").click(function() 
    {
        setActiveFinderTab(this);
    });

    // Map tabs.
    $("a.map_tab").click(function() 
    {
        setActiveMapTab(this);
    });

    $("a.generaltab").click(function() 
    {
        setActiveMapTab($get("tab_generalcoverage"));
    });

    $("a.dualbandtab").click(function() 
    {
        setActiveMapTab($get("tab_dualband"));
    });

    $("a.button.close").click(function() 
    {
	    $("div.popup").css("display", "none");
    });

    $("a#tab_dualband").click(function() 
    {
	    $("div.popup").css("display", "none");
	    $("div#popup_dualband").css("display", "block");
    });

    $("input.phoneSurvey").click(function()
    {
        function getSortedKeys(hashtable)
        {
            var list = [""];
            
            for (var i in hashtable)
            {
                list.push(i);
            }
            
            list.sort();

            return list;
        }

        // Hide messages.
        $("div.band_message").css("display", "none");
        
        // Clear lookups.
        clearOptions("mfg");
        clearOptions("model");

        setOptions("mfg", getSortedKeys(phones[this.id]));

        $get("mfg").phoneType = this.id;
    });

    $("select#mfg").change(function()
    {
        $("div.band_message").css("display", "none");

        var mfg = this.options[this.selectedIndex].text;

        if (mfg == "")
        {
            clearOptions("model");
        }
        else
        {
            setOptions("model", [""].concat(getFieldValues(phones[this.phoneType][mfg], "DisplayName")));
        }
    });
    
    $("select#model").change(function()
    {
        function getPhone(manufacturer, model)
        {
            for (var i = 0; i < phoneList.length; i++)
            {
                var phone = phoneList[i];
                
                if ((phone.Manufacturer == manufacturer) && (phone.DisplayName == model))
                {
                    return phone;
                }
            }
            
            return "Unknown";
        }

        $("div.band_message").css("display", "none");
        
        var sel1 = $get("mfg");
        var sel2 = $get("model");

        if ((sel1.selectedIndex > 0) && (sel2.selectedIndex > 0))
        {
            var phone = getPhone(sel1.options[sel1.selectedIndex].text, sel2.options[sel2.selectedIndex].text);
            
            if (phone.Band == "Dual-band")
            {
                $("#dualBandMessage").css("display", "block");
            }
            else
            {
                $("#triBandMessage").css("display", "block");
            }
            
            if (phone.Acquisition == "MetroFLASH")
            {
                $("#metroFLASHMessage").css("display", "block");
            }
        }
    });

    // Construct a hashtable of phones broken down first by if they're MetroFlash or not, and then by manufacturer.
    function loadPhones()
    {
        var acquisition;
        var manufacturer;
        
        for (var i = 0; i < phoneList.length; i++)
        {
            // Fix phoneList DisplayName.
            phoneList[i].DisplayName = phoneList[i].DisplayName.replace("�", "™");
            
            acquisition = phoneList[i].Acquisition;
            key = phoneList[i].Manufacturer;
            
            if (phones[acquisition][key])
            {
                phones[acquisition][key].push(phoneList[i]);
            }
            else
            {
                phones[acquisition][key] = [phoneList[i]];
            }
        }
        
        // Select the MetroPCS type phones by default.
        $("#MetroPCS").click();
    }
        
    // Change png to gif references if/where appropriate.
    function updateGraphicsReferences()
    {
        // Change over to GIFs for the spheres if necessary.
        for (var i = 0; i < dots.length; i++)
        {
            dots[i] += (imageFormat == ".png") ? ".png" : ".gif";
        }

        if (imageFormat != ".png")
        {
            // Perform a global change to the "GIF" version.
            
            $("img.homeArea").attr("src", dots[0]);
            $("img.extendedArea").attr("src", dots[1]);
            $("img.futureArea").attr("src", dots[2]);
        }
    }

    updateGraphicsReferences();
    
    loadPhones();

    // Perform the remaining (map oriented) setup routines.   
    appSetup();
});
