﻿
// Global Variables
var resultsList = null;
var listItems = null;
var loadAgenciesHandler = null;

(function($) {
    
    window["load_AutoOwners_AgencyLocator_ucSearchResultsList"] = function(obj) {
        var ucSearchResultsListLoadFunctionInterval = setInterval(function() { 
            if (typeof VEMap !== "undefined") 
            {
                if (ucSearchResultsListLoadFunctionInterval)
                {
                    clearInterval(ucSearchResultsListLoadFunctionInterval);
                }
                
                ucSearchResultsListLoadFunction(obj);
            } 
        }, 100);
    };
    
    
    var ucSearchResultsListLoadFunction = function(obj) {
        
        resultsList = $("#search_results");
        
        // Attach event handlers.
        
        $("#search_results li").mouseenter(function(event) {
            selectAgency($(this));
        }).mouseleave(function(event) {
            if (ucSearchResultsMap)
            {
                if (ucSearchResultsMap.SelectedClusterID !== null)
                {
                    ucSearchResultsMap.hideClusterInfo();
                }
            }
        });
        
        
        $("a.get_directions").live("click", function(event) {
            event.preventDefault();
            showDirections();
        });
        
        
        loadAgenciesHandler = function() {
            updateSearchResultsMap(obj); // Load the agency points on the map.
        };
    };
    
    
    function selectAgency(listItem)
    {
        if (ucSearchResultsMap && listItem && listItem.length > 0)
        {
            var agencyIndex = parseInt(listItem.find(".number").text()) - 1;
            ucSearchResultsMap.selectAgency(agencyIndex);
            
            resultsList.find("li.selected").removeClass("selected");
            listItem.addClass("selected");
            
            ucSearchResultsMap.SelectedAgency.ListItem = listItem;
            ucSearchResultsMap.SelectedAgency.ListItem.DoNotSerialize = true;
        }
    }
    
    
    function showDirections()
    {
        if (ucDirections && ucSearchResultsMap)
        {
            ucSearchResultsMap.disableMap();
            
            ucDirections.RenderDirectionsControl(ucSearchResultsMap.SelectedAgency.ID, {
                autoUpdateHtml: true,
                onSuccess: function(results) {
                    var popup = $("#popup");
                    popup.fadeIn("medium");
                    
                    $("h2.agent_count").hide();
                    $("html,body").scrollTop($("#directions .address_panel").offset().top - 10);
                    
                    ucSearchResultsMap.hideInfoBox();
                }
            });
            
            if (ucAdvancedSearch)
            {
                ucAdvancedSearch.hideSearchOptions();
            }
        }
    }
    
    
    function updateSearchResultsMap(obj)
    {
        if (ucSearchResultsMap)
        {
            if (!obj.getState()["Agencies"])
            {
                ucSearchResultsMap.showStartingView();
            }
            else
            {
                ucSearchResultsMap.clearAgencies();
                ucSearchResultsMap.addAgencies(obj.getState().Agencies, obj.getState().Centroid);
                delete obj.getState().Agencies;
                delete obj.getState().Centroid;
                
                ucSearchResultsMap.showAllAgenciesInView();
            }
        }
    }
    
    
    function getCurrentPageNumber()
    {
        return parseInt($(".pager .current").text());
    }
    

})(jQuery.noConflict());