﻿//bind up the functions for the results page
$('div.inner-content.results').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-images').ready(
    function() {
    //add selected class to first thumnail
    $('img.thumbnail.first').addClass("selected");
    
        //bind up the image switching function
        $('img.thumbnail').click(
            function() {
                var largeImage = $('img.main');
                var smallSrc = $(this).attr('src');
                largeImage.attr('src', smallSrc);
                $('img.thumbnail').removeClass("selected");
                $(this).addClass("selected");
            }
        );
    }
);

//bind up the advanced search criteria scripts
    $('form.panel-advanced').ready(
    function() {
        var form = $('form.panel-advanced');

        //make the select lists auto postback
        form.find('select').change(
            function() {
                ShowLoadingPanel();

                //if this is the manufacturer, reset before we submit
                if ($(this).hasClass('manufacturer')) {
                    ResetAllLowerDropDowns(form);
                }

                form.submit();
            }
        );

        //make checkbox lists autopostback
        form.find('input').click(
            function() {
                var checkboxContainer;
                var checkbox = $(this);
                if (checkbox.attr("type") == "checkbox") {
                    if (checkbox.parent().parent().hasClass("sum")) {
                        checkboxContainer = checkbox.parent().parent();
                        var sum = 0;
                        //loop through and sum up all checked values.
                        checkboxContainer.find('div input').each(
                            function() {
                                if ($(this).attr("type") == "checkbox") {
                                    if ($(this).attr("checked")) {
                                        sum += parseInt($(this).val());
                                    }
                                }
                            }
                        );
                        var hidden = checkboxContainer.find('input:first');
                        hidden.val(sum);
                        form.submit();
                    }
                }
            }
        );

        //now change the button to be a reset and resubmit button
        form.find('input.search').click(
            function(event) {
                event.preventDefault();

                ShowLoadingPanel();

                form.find('select').each(
                    function() {
                        $(this).find('option:first').attr('selected', 'selected');
                    }
                );
                form.find('input').each(
                    function() {
                        var checkbox = $(this);
                        //Reset all check boxes.
                        if (checkbox.attr("type") == "checkbox") {
                            if (checkbox.attr("checked")) checkbox.removeAttr("checked");
                        }
                        //Reset all hidden fields.
                        if (checkbox.attr("type") == "hidden") {
                            checkbox.val("");
                        }
                    }
                );

                form.submit();
            }
        );
    }
);

//bind up the basic search panel scripts
$('form.panel-basic').ready(
    function() {
        var form = $('form.panel-basic');
        
        //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();
                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 setup the flash preloader
$('div#criteriaLoading').ready(
    function() {
        var soLoader = new SWFObject('/theme2/assets/swf/preloader.swf', 'loader', '40', '40', '8', '#fff');
        soLoader.addParam('wmode', 'transparent');
        soLoader.write('searchCriteriaLoader');
    }
);

//function used to show the loading panel when the user updates their search criteria
function ShowLoadingPanel() {
    $('div#criteriaContent').hide();
    $('div#criteriaLoading').show();
}
