﻿/*
'slidersRendered' is a global variable used to distinguish whether or not all sliders have been rendered (using RenderSlider).
Once RenderSlider is complete this variable is set to true.
This is due the fact that when creating slider elements, the 'change' event of a given select list (relating to a slider) is triggered,
and in turn any other scripts (like this) that utilise the 'change' event will also fire inadvertantly.
By checking the state of 'slidersRendered' we can ensure that we only bind change functions once the sliders have all been rendered.
*/
var slidersRendered = false; 

//bind up the functions for the results page
$(document).ready(
    function() {
        var form = $('form#resultsToolsForm');
        form.find('select.pagesize').change(
	        function() {
	            $('select').css('display', 'none');
	            form.submit();
	        }
        );
	    form.find('select.sortorder').change(
	        function() {
	            $('select').css('display', 'none');
	            form.submit();
	        }
        );
        
        //Finance Tools Show/Hide
        $('a.finance-title').click(
            function() {
                if ($('#finance-options-container').is(':visible'))
                {
                    $('#finance-options-container').hide();
                }
                else
                {
                    $('#finance-options-container').show();
                }
            }
        );
        $('a.pe-finance-options-update').click(
            function() {
                $('#finance-options-container').hide();
            }
        );
        
        //Show/hide model if manufacturer is/is not selected.
        showHideModels();
        
        //Remove true/false characters from comparison table.        
        $("table td.true").each(
            function(i) {                
                $(this).html("");
            }
        );
        $("table td.false").each(
            function(i) {                
                $(this).html("");
            }
        );
            
    }
);

//bind up the vehicle view functions
$('div.vehicle-images').ready(
    function() {
        //add selected class to first thumnail
        $('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.searchPanel.advanced').ready(
    function() {
        var form = $('form.searchPanel.advanced');

        //make the select lists auto postback
        form.find('select').change(
            function() {
                if (slidersRendered == false) return;                
                ShowLoadingPanel(true);
                //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() {
                //if ($(this).attr("id").indexOf($(this).val()) != -1) $(this).val("");
                ShowLoadingPanel(true);
                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) {
                //If the button is not a search or reset then do nothing...
                if (($(this).hasClass("search") == false) && ($(this).hasClass("reset") == false)) return;

                event.preventDefault();

                ShowLoadingPanel(true);

                form.find('select').each(
                    function() {
                        $(this).find('option:first').attr('selected', 'selected');
                    }
                );
                form.find('input').each(
                    function() {
                        var input = $(this);
                        //Reset all check boxes.
                        if (input.attr("type") == "checkbox") {
                            if (input.attr("checked")) input.removeAttr("checked");
                        }                        
                        //Reset all hidden fields.
                        if (input.attr("type") == "hidden") {
                            input.val("");
                        }
                    }
                );
                form.find('span.radio').each(
                    function() {
                        $(this).children('input').removeAttr("checked");
                        $(this).children('input:first').attr('checked', 'checked')
                    }
                );

                form.submit();
            }
        );
    }
);

//function to show model dropdown when manufacturer is selected.
function showHideModels() {        
    search_type = getCookie('search_type');
    if ($('form.searchPanel.' + search_type).html() == null) search_type = 'advanced';
    
    if ($('form.searchPanel.' + search_type + ' select.manufacturer').val() == "")
    { 
        $('form.searchPanel.' + search_type + ' div.model.dropdownlist select').attr({
            disabled : "disabled"
        });            
    }
    else
    {
        $('form.searchPanel.' + search_type + ' div.model.dropdownlist select').removeAttr("disabled");            
    }    
}

//function to display relevant search panel.
$('form.searchPanel.basic').ready(
    function() {
        //If more than one datafinder is needed then enable selection process (cars, commercial).
        var dfCount = $('form.searchPanel.basic').length;
        if (dfCount > 1) {
            $('fieldset.search-type').removeAttr("style");
            search_type = getCookie('search_type');
            if (search_type != '') {
                $('form.searchPanel.basic').hide();
                $('form.searchPanel.basic.' + search_type).show();
            }
            else { $('form.searchPanel.basic:gt(0)').hide(); }
            $('div.search-type input:checkbox').click(
				function() {
				    var className;
				    if ($(this).hasClass('selected') == false) {
				        if ($(this).hasClass('car') == true) {
				            className = "car";
				            $('input.car').attr('checked', 'checked');
				            $('input.commercial').removeAttr('checked');
				        }
				        else {
				            className = "commercial";
				            $('input.commercial').attr('checked', 'checked');
				            $('input.car').removeAttr('checked');
				        }
				        $('form.searchPanel.basic').hide();
				        $('form.searchPanel.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);
				        
                        //Show/hide model if manufacturer is/is not selected.
                        showHideModels();
				    }
				}
			);
        }
    }
);
//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.searchPanel.basic.car').ready(
    function() {
        var form = $('form.searchPanel.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() {
                ShowLoadingPanel(true);
                form.submit();
            }
        );

        //make the select lists auto postback
        form.find('span.radio input').click(
            function() {
                ShowLoadingPanel(true);
                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.searchPanel.basic.commercial').ready(
    function() {
        var form = $('form.searchPanel.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(true);
                form.submit();
            }
        );

        //make the select lists auto postback
        form.find('span.radio input').click(
            function() {
                ShowLoadingPanel(true);
                form.submit();
            }
        );
        
        //wire up the submit button
        form.find('input.search').click(
            function(event) {
                event.preventDefault();                                
                window.location.href = "/commercial/used-commercial.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('/MacraeAndDick/assets/flash/LOADINGBIG.swf', 'loader', '50', '50', '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(visible) {
    if (visible == true)
    {
        $('#criteriaContent').hide();
        $('#criteriaLoading').show();
    }
    else
    {
        $('#criteriaContent').show();
        $('#criteriaLoading').hide();    
    }
}

//Bind slider controls
$(document).ready(function() {    
    slidersRendered = false;
    RenderSliders();        
    slidersRendered = true;
});