﻿//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");
            }
        );
    }*/
        $('div.thumbnail.first').addClass("selected");    
        //bind up the image switching function
        $('div.thumbnail').click(
            function() {
                var largeImage = $('img.main');
                var smallSrc = $(this).find('img').attr('src');
                largeImage.attr('src', smallSrc);
                $('div.thumbnail').removeClass("selected");
                $(this).addClass("selected");
            }
        );
    }
    
);

//bind up the advanced search criteria scripts
$('form.panel-advanced select.model').ready(
    function() {
        if ($('form.panel-advanced select.manufacturer').val() == "")
        {         
            $('form.panel-advanced div.model.dropdownlist').attr({
                style : "display:none;"
            });
        }
        else
        {
            $('form.panel-advanced div.model.dropdownlist').removeAttr("style");
        }    
    }
);
$('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 the select lists auto postback
        form.find('span.radio input').click(
            function() {
                ShowLoadingPanel();
                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();
            }
        );
    }
);

//function to display relevant search panel.
$('form.panel-basic select.model').ready(
    function() {
        if ($('form.panel-basic select.manufacturer').val() == "")
        { 
            $('form.panel-basic div.model.dropdownlist').attr({
                style : "display:none;"
            });            
        }
        else
        {
            $('form.panel-basic div.model.dropdownlist').removeAttr("style");            
        }    
    }
);
$('form.panel-basic').ready(
    function() {
		//If more than one datafinder is needed then enable selection process (cars, commercial).
		var dfCount = $('form.panel-basic').length;
		if (dfCount > 1)
		{
			$('fieldset.search-type').removeAttr("style");
			search_type = getCookie('search_type');        
			if (search_type != '')         
			{
				$('form.panel-basic').hide();
				$('form.panel-basic.'+ search_type).show();
			}
			else { $('form.panel-basic:gt(0)').hide(); }            
			$('select.search-type').change(
				function() {
					var className = $(this).val();
					$('form.panel-basic').hide();
					$('form.panel-basic.' + className).show();
					//Store current search type in a cookie.  NOTE: this has no bearing on the UVL, it is only to drive the show/hide process for icon based search criteria.
					setCookie('search_type', className, 1);
				}
			);
		}
    }
);
//function to set a cookie.
function setCookie(c_name,value,expiredays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
//function to get a cookie.
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

//bind up the basic search panel scripts - car
$('form.panel-basic.car').ready(
    function() {
        var form = $('form.panel-basic.car');
        
        //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() {
                if ($(this).hasClass("search-type")) return;
                ShowLoadingPanel();
                form.submit();
            }
        );

        //make the select lists auto postback
        form.find('span.radio input').click(
            function() {
                ShowLoadingPanel();
                form.submit();
            }
        );
        
        //wire up the submit button
        form.find('input.search').click(
            function(event) {
                event.preventDefault();                                
                window.location.href = "/Used-Cars.aspx?st=Results";
            }
        );
    }
);

//bind up the basic search panel scripts - commercial
$('form.panel-basic.commercial').ready(
    function() {
        var form = $('form.panel-basic.commercial');
        
        //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();
            }
        );

        //make the select lists auto postback
        form.find('span.radio input').click(
            function() {
                ShowLoadingPanel();
                form.submit();
            }
        );
        
        //wire up the submit button
        form.find('input.search').click(
            function(event) {
                event.preventDefault();                                
                window.location.href = "/Used-Vans.aspx?st=Results";
            }
        );
    }
);

//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('/template2/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(type) {
    $('div#criteriaContent').hide();
    $('div#criteriaLoading').show();
}