﻿//bind up the functions for the results page
$('div.vehicle-list-container').ready(
    function() {
        $(this).find('select.pagesize').change(
	        function() {
                $('select').css('display', 'none');
                $("form#resultsToolsForm").submit();
	        }
        );
	    $(this).find('select.sortorder').change(
	        function() {
	            $('select').css('display', 'none');
	            $("form#resultsToolsForm").submit();
	        }
        );
    }
);
        
//bind up the vehicle view functions
$('div.vehicle-view-container').ready(
    function() {
        //bind up the image switching function
        $('img.carImage').click(
            function() {
                var largeImage = $('img.carMainImage');
                var smallSrc = $(this).attr('src');
                largeImage.attr('src',smallSrc);
            }
        );
    }
);

//bind up the advanced search criteria scripts
$('form.panel-advanced').ready(
    function() {
        var form = $('form.panel-advanced');
        var content = form.find('div.panel-content');
        $('.panel-content-loading').css('visibility', 'hidden');
        
        //make the select lists auto postback
        form.find('select').change(
            function() {
                ShowLoadingPanel(content);
                              
                //if this is the manufacturer, reset before we submit
                if($(this).hasClass('manufacturer')) {
                    ResetAllLowerDropDowns(form);
                }                
                
                form.submit();
            }
        );
        
        //now change the button to be a reset and resubmit button
        form.find('input').click(
            function(event) {
                event.preventDefault();

                ShowLoadingPanel(content);
                
                form.find('select').each(
                    function() {
                        $(this).find('option:first').attr('selected','selected');
                    }
                );
                form.submit();
            }
        );
    }
);

//bind up the basic search panel scripts
$('form.panel-basic').ready(
    function() {
        var form = $('form.panel-basic');
        var content = form.find('div.panel-content');
        $('.panel-content-loading').css('visibility','hidden');
        
        //handle whether the form action is invalid, if on the homepage it defaults to / which is incorrect
        if(form.attr('action') == '/') {
            form.attr('action','/Home.aspx');
        }        

        //make the select lists auto postback
        form.find('select').change(
            function() {
                ShowLoadingPanel(content);         
                form.submit();
            }
        );
        
        //wire up the submit button
        form.find('input').click(
            function(event) {
                event.preventDefault();
                window.location.href = form.find('input.datafinder-location').val();    
            }
        );
    }
);

//function used to reset all dropdowns except for the make
function ResetAllLowerDropDowns(form) {
    form.find('select').each(
        function() {
            if(!$(this).hasClass('manufacturer')) {
                $(this).find('option:first').attr('selected', 'selected');
            }
        }
    );
}

//function used to show the loading panel when the user updates their search criteria
function ShowLoadingPanel(panel) {
    panel.css('visibility', 'hidden');
    $('.panel-content-loading').css('visibility','visible');
}

//function used to setup the flash preloader
$('.panel-content-loading').ready(
    function () {
        var soLoader = new SWFObject('/theme1/assets/swf/preloader.swf','loader','40','40','8','#fff');
        soLoader.addParam('wmode','transparent');
        soLoader.write('searchCriteriaLoader');
    }
);