﻿
$(function () {
    // Setup outer accordion
    $('.accord-outer .section-body:gt(0)').hide();
    $('.accord-outer .section-head:gt(0)').addClass('section-head-closed');
    $('.accord-outer .section:lt(1)').addClass('section-open');

    // Setup product accordion
    $('.accord-inner .section-product-content').hide(); //hide all
    $('.accord-inner h3').addClass('closed'); //close all

    // Product range section click handler
    $('.accord-inner h3').click(function () {
        $('.accord-inner .open').addClass('closed');
        $('.accord-inner .open').next().slideUp(200);

        if ($(this).hasClass('open')) {
            $('.accord-inner .open').removeClass('open');
        }
        else {
            $('.accord-inner .open').removeClass('open');
            $(this).addClass('open').removeClass('closed');

            // Open the current section then scroll to it
            $(this).next().slideDown(200, function () {
                $('html, body').animate({
                    scrollTop: $(this).position().top
                }, 200);
            });
        }

        window.location.hash = cleanStringForUrlHash($(this));
    });

    //section click handlers
    $('#section-your-details h2').click(function () {
        closeAllOpenSections(this);
        openOnlyCurrentSection(this);
    });

    $('#section-new-account h2').click(function () {
        // Do not allow users to open disabled sections
        if ($(this).parent().hasClass('section-head-disabled')) {
            return;
        }
        //User can only get to this section if 'your details' has successfully validated.
        if (validateYourDetails()) {
            closeAllOpenSections(this);
            openOnlyCurrentSection(this);
        }
    });

    $('#section-product-range h2').click(function () {
        //User can only get to this section if 'your details' has successfully validated.
        if ($('.control_rdoNewCust input:checked').val() == 'Yes' || $('body.parties').size() == 1 ) {
            if (!validateNewAccountDetails()) {
                return false;
            }
        }
        if (validateYourDetails()) {
            closeAllOpenSections(this);
            openOnlyCurrentSection(this);
        }
    });


    // .NET may override this with a custom value if it detects cookies, so cater for cases where this may be toggled differently to what you expect from the js.
    var newAccountSectionHead = $('#section-new-account .section-head');
    if ($('.construction .control_rdoNewCust input:checked').val() == 'No') {
        newAccountSectionHead.addClass('section-head-disabled');
    } else {
        newAccountSectionHead.removeClass('section-head-disabled');
    }

    // Event handler for new customer radio button
    $('.construction .control_rdoNewCust input:radio').change(function () {
        if ($(this).val() == 'No') {
            newAccountSectionHead.addClass('section-head-disabled');
            newAccountSectionHead.removeClass('section-head-closed');
        } else {
            newAccountSectionHead.removeClass('section-head-disabled');
            newAccountSectionHead.addClass('section-head-closed');
        }
    });

    // Setup auto scrolling of the order summary
    if ($('.order-summary-wrapper').length > 0) {
        // Get the current position
        var initOrderSummaryTop = $('.order-summary-wrapper').offset().top;

        // Detect scrolling of the window
        $(window).scroll(function () {
            var newTop = $(document).scrollTop() - initOrderSummaryTop;

            // Only move the object if it is lower than the starting point
            if (newTop < 0) {
                newTop = 0;
            } else {
                // Add a buffer for prettiness
                newTop += 15;
            }

            // Move the summary immiediatly - don't clog the animation queue
            $('.order-summary-wrapper').animate({ marginTop: newTop }, { queue: false });
        });
    }
});

// Close all open sections
var closeAllOpenSections = function (section) {
    var sections = $(section).closest('.section').siblings().children(".section-body");
    sections.each(function () {
        if ($(this).parent('.section').hasClass('section-open')) {
            $(this).stop().slideUp().queue(function () {
                $(this).siblings('.section-head').addClass('section-head-closed');
            });
        }
    });
}

// Just open the section the user has clicked
var openOnlyCurrentSection = function (section) {
    $(section).parent().removeClass('section-head-closed');
    $(section).parent().siblings('.section-body').stop().slideDown(); //<- MUST call .stop() or the animation queue builds and this stops the slideDown.
    $('.accord-outer .section').removeClass('section-open');
    $(section).parent().parent().addClass('section-open');
}


var processProductRange = function () {
    if (validateProductRange()) {
        $('#section-product-range .section-body').stop().slideUp().queue(function () {
            $('#section-product-range .section-head').addClass('section-head-closed');
        });
        // Open product range
        $('#section-submit .section-head').removeClass('section-head-closed');
        $('#section-submit .section-body').stop().slideDown();
        $('.accord-outer .section').removeClass('section-open');
        $('#section-submit').addClass('section-open');
    }
}


var processYourDetails = function () {
    if (validateYourDetails()) {
        $('#section-your-details .section-body').stop().slideUp().queue(function () {
            $('#section-your-details .section-head').addClass('section-head-closed');
        });

        // Decide which section to open next
        if ($('.construction .control_rdoNewCust input:checked').val() == 'No') {
            // Returning Customer
            $('#section-product-range .section-head').removeClass('section-head-closed');
            $('#section-product-range .section-body').stop().slideDown()
            $('.accord-outer .section').removeClass('section-open');
            $('#product-range').addClass('section-open');
        } else {
            // New Customer
            $('#section-new-account .section-head').removeClass('section-head-closed');
            $('#section-new-account .section-body').stop().slideDown()
            $('.accord-outer .section').removeClass('section-open');
            $('#section-new-account').addClass('section-open');
        }
    }
}

var processNewAccountDetails = function () {
  if (validateNewAccountDetails()) {
        $('#section-new-account .section-body').stop().slideUp().queue(function () {
            $('#section-new-account .section-head').addClass('section-head-closed');
        });
        // Open product range
        $('#section-product-range .section-head').removeClass('section-head-closed');
        $('#section-product-range .section-body').stop().slideDown();
        $('.accord-outer .section').removeClass('section-open');
        $('#section-product-range').addClass('section-open');
    }
}

var showEventSubmit = function () {
    var eventContinueText = $('.events #section-your-details .section-continue-text');
    var eventContinueButton = $('.events .event-details-continue');
    var eventSubmitButton = $('.events .event-details-submit');

    if ($('.control_rdoNewCust input:checked').val() == 'No') {
        eventContinueText.hide();
        eventContinueButton.hide();
        eventSubmitButton.show();
    } else {
        eventContinueText.show();
        eventContinueButton.show();
        eventSubmitButton.hide();
    }
}

/* m.siedle: note: this function was moved to the individual booking/quote pages for greater flexibility */
/*
var validateYourDetails = function () {
    // Not currently supporting JS validation.
    return false;
}
var validateNewAccountDetails = function () {
    // Not currently supporting JS validation.
    return false;
}
*/

var nextClick = function (section) {
    switch (section) {
        case "yourDetails": processYourDetails(); break;
        case "newAccountDetails": processNewAccountDetails(); break;
        case "productRange": processProductRange(); break;
    }
    return false;
}

