﻿$(function() {

    


    $('#derivativeTab li a').click(function() {

        var $selected = $(this).text();

        if ($selected == "Technical Information") {
            $('#standardOptionList').fadeOut('fast');
            $('#optionList').fadeOut('fast');
            $('#technicalList').fadeIn();
        } else if ($selected == "Selectable Options") {
            $('#technicalList').fadeOut('fast');
            $('#standardOptionList').fadeOut('fast');
            $('#optionList').fadeIn();
        } else {
            $('#technicalList').fadeOut('fast');
            $('#optionList').fadeOut('fast');
            $('#standardOptionList').fadeIn();
        }

        return false;
    });


    $('#carLinks').click(function() {

        $('#vanManufacturers').fadeOut('fast');
        $('#carManufacturers').fadeIn();
        return false;
    });

    $('#vanLinks').click(function() {

        $('#carManufacturers').fadeOut('fast');
        $('#vanManufacturers').fadeIn();
        return false;
    });


    var cascadeDictionary =
    {
        'CarClassification': "Manufacturer.mvc/GetByClassification?classification=",
        'CarManufacturer': "ModelRange.mvc/GetForManufacturer?manufacturerId=",
        'CarModelRange': "Model.mvc/GetForModelRange?modelRangeId="
    }

    var updateSelectBox = function(selectBoxIdToUpdate, items, setFromCookie) {

        var selectBox = $('#' + selectBoxIdToUpdate);
        var descriptionOption = $(":first", selectBox);
        $(selectBox).empty();
        $(selectBox).append(descriptionOption);
        $.each(items, function() {
            $(selectBox).append("<option value='" + this.Id + "'>" + this.Title + "</option");
        });

        if (setFromCookie) {
            var cookieValue = $.cookie(selectBoxIdToUpdate);
            if (cookieValue == null) return;
            if (cookieValue == "0") return;


            $.each(selectBox.children(), function(index) {
                if (cookieValue == this.value) {
                    $(selectBox).val(this.value);
                    FireChange(selectBoxIdToUpdate, cascadeDictionary[selectBoxIdToUpdate], this.value, true)
                    return false;
                }

            });
        }

    }

    $('#QuickQuoteForm').submit(function() {

        var modelId = $('#CarModel').val();

        if (modelId != null && modelId != "0") {
            return true;
        } else {
            alert("Please select a car or van model before proceeding.");
        }
        return false;
    });

    var FireChange = function(selectBoxId, queryAjaxUrl, selectBoxValue, setFromCookie) {


        if (cascadeDictionary[selectBoxId] == null) return;

        var selectBoxIdToUpdate = $('#' + selectBoxId).parent().next().children('select')[0].id;

        var url = baseUrl + queryAjaxUrl + selectBoxValue;

        $.cookie(selectBoxId, selectBoxValue);

        $.ajax({
            'type': "GET",
            'url': url,
            'dataType': 'json',
            success: function(response) {
                updateSelectBox(selectBoxIdToUpdate, response, setFromCookie);
            }
        });
    }

    var SetupQuickQuoteCheckBox = function(selectBoxId, queryAjaxUrl, selectBoxIdToUpdate, setFromCookie) {

        $('#' + selectBoxId).change(function() {

            $('#' + selectBoxId).parent().nextAll().children('select').each(function() {

                if (this.id.substr(0, 3) == "Car") {

                    var selectBox = $(this);
                    var descriptionOption = $(":first", selectBox);
                    $(selectBox).empty();
                    $(selectBox).append(descriptionOption);
                }
            })



            if ($(this).val() != "0") {
                FireChange(selectBoxId, queryAjaxUrl, $('#' + selectBoxId).val(), false);
            }
        });


    }



    $.each($('select'), function() {
        this.selectedIndex = 0;

    });

    SetupQuickQuoteCheckBox("CarClassification",
                            "Manufacturer.mvc/GetByClassification?classification=",
                            "CarManufacturer");

    SetupQuickQuoteCheckBox("CarManufacturer",
                            "ModelRange.mvc/GetForManufacturer?manufacturerId=",
                            "CarModelRange");

    SetupQuickQuoteCheckBox("CarModelRange",
                            "Model.mvc/GetForModelRange?modelRangeId=",
                            "CarModel");


    var classCookie = $.cookie('CarClassification');
    if (classCookie != null) {
        $("#CarClassification").val(classCookie);
        FireChange("CarClassification", "Manufacturer.mvc/GetByClassification?classification=", classCookie, true);

    }

});
