// Functions for the Popular cars image list.
var startIndex = 0;

function changePopularCars(leftrigtharrow)
{
    var directn = leftrigtharrow;
    var myObjectArray = new Array(popCount);
    var myClassArray = new Array(popCount);
    var el;
    var foundAt;
    
    try
    {
        //Use passed server variable to set up arrays.  In theory, the value should be 9.
        for (i=0; i<popCount; i=i+1)
        {
            myObjectArray[i] = $get('popularCarsLink' + (i + 1));
            el = myObjectArray[i];
            myClassArray[i] = el.className;
        }       
        
        for (i=0; i<popCount; i=i+1)
        {
            if (myClassArray[i] != "hide")
            {
                foundAt = i;
                break;
            }
        }   
        
        if (directn == "right")
        {
            if ((foundAt + 3) < popCount)
            {
                myObjectArray[foundAt].className = 'hide';
                myObjectArray[foundAt + 1].className = 'hide';
                myObjectArray[foundAt + 2].className = 'hide';
                myObjectArray[foundAt + 3].className = 'show';
                if ((foundAt + 4) < popCount)
                {
                    myObjectArray[foundAt + 4].className = 'show';
                }
                if ((foundAt + 5) < popCount)
                {
                    myObjectArray[foundAt + 5].className = 'show';
                }
                if ((foundAt + 6) < popCount)
                {
                    removeCssClass($get('arrowRight'), 'invis');
                }
                else
                {
                    addCssClass($get('arrowRight'), 'invis');
                }
                removeCssClass($get('arrowLeft'), 'invis');
            }
        }
        else if (directn == "left")
        {
            if ((foundAt - 3) >= 0)
            {
                myObjectArray[foundAt].className = 'hide';
                if ((foundAt + 1) < popCount)
                {
                    myObjectArray[foundAt + 1].className = 'hide';
                }
                if ((foundAt + 2) < popCount)
                {
                    myObjectArray[foundAt + 2].className = 'hide';
                }
                myObjectArray[foundAt - 1].className = 'show';
                myObjectArray[foundAt - 2].className = 'show';
                myObjectArray[foundAt - 3].className = 'show';
                if ((foundAt - 6) >= 0)
                {
                    removeCssClass($get('arrowLeft'), 'invis');
                }
                else
                {
                    addCssClass($get('arrowLeft'), 'invis');
                }
                removeCssClass($get('arrowRight'), 'invis');
            }
        }
        else
        {
            //neither direction right or left was passed.  Assume initial page load.
            //check that a 4th item exists to verify that the right arrow should show
            if (popCount < 4)
            {
                addCssClass($get('arrowRight'), 'invis');
            }
        }
    }
    catch(err)
    {}
    //incase a vehicle name wraps and causes the page height to change, fix height for IE6 and 7
    fixIEHeight()
    fixPNG();

    return false;
}

var searchLF = new LinkFactory();
function popularSearch(hlText, path, qstring)
{
    // Save the URL so we know what one to use if the Zip Prompt is needed.
    searchLF._path = path;
    searchLF._qstring = qstring;
    
    zip = $get(popSearchZip);
    if (ValidateZip(zip.value))
    {
        popularSearchExec($get(popSearchZip).value);
    }
    else
    {
        zipPromptVar.Open(hlText.toUpperCase(), popularSearchExec);
    }
}

function popularSearchExec(sZip)
{
    $get(popSearchZip).value = sZip;
    
    // Insert the correct zip & distance into the URL.
    searchLF.ReplaceToken("xZIPx", sZip);
    searchLF.ReplaceToken("xDISTANCEx", $get(popSearchRange).value);
    
    fnNavToSearchResults(searchLF.FinalizeUrl());
}

function popMouseOver(el)
{
    addCssClass($get(el), 'underline');
}

function popMouseOut(el)
{
    removeCssClass($get(el), 'underline');
}

// END - Functions for the Popular cars image list.
