﻿$(window).load(function () {

    // get position and size of centre finder form
    // then set the position of the advanced options forms to appear directly beneath
    if ($(".search-popup").length > 0) {
        var pos = $(".centre-finder").position();
        var newX = pos.left;
        var newY = pos.top + ($(".centre-finder").outerHeight());
        $(".search-popup").css('top', newY + 'px');
        $(".search-popup").css('left', newX + 'px');
        $(".search-popup").css('position', 'absolute');

        $("#ShowSearchOptions").attr('class', 'hide-button');
        ShowSearchOptions();
    }

    if ($('#DeliveryAddressSection').length >= 1) {
        $('#DeliveryAddressSection').hide('fast');
        $('#BillingAddressSection').hide('fast');
    }

    $('a#ShowSearchOptions').click(function () {
        $(this).attr('class', 'hide-button');
        ShowSearchOptions();
    });

    $('.CategoryDropDown').change(function () {
        if ($('.hide-button').length > 0) {
            ShowSearchOptions();
        }
    });

    // Pop-up options for where to take part in gold challenge.

    setOptionsPopup();
    $(".showDisableCentres input").click(function () {
        setOptionsPopup();
    });

    function setOptionsPopup() {
        if ($(".showDisableCentres input").is(":checked")) {
            $(".options-popup").fadeIn();
        }
        else {
            $(".options-popup").hide();
        }
    };

    // Textbox watermark for gold challenge where to take part
    $(".txtLocation").focus(function () {
        if($(".txtLocation").val() == "Location...")
            $(".txtLocation").val('');
    });

    $(".txtLocation").blur(function () {
        if ($(".txtLocation").val() == '')
            $(".txtLocation").val('Location...');
    });

    // Show more search options button show options dependent on selected dropdown option:

    function ShowSearchOptions() {
        var category = $('.CategoryDropDown').val();
        if (category == "club") {
            $('.search-popup').hide();
            $('#ClubSearchOptions').fadeIn();
        }
        if (category == "training") {
            $('.search-popup').hide();
            $('#TrainingSearchOptions').fadeIn();
        }
        if (category == "test") {
            $('.search-popup').hide();
            $('#TestSearchOptions').fadeIn();
        }
        if (category == "marina") {
            $('.search-popup').hide();
            $('#MarinaSearchOptions').fadeIn();
        }
        if (category == "sailability") {
            $('.search-popup').hide();
            $('#SailabilitySearchOptions').fadeIn();
        }
        if (category == "onboard") {
            $('.search-popup').hide();
        }
        if (category == "team15") {
            $('.search-popup').hide();
        }
    };

    // OnClick of close button, close the current pop up

    $('a.CloseSearchOptions').click(function () {
        $('a#ShowSearchOptions').removeClass();
        $('.search-popup').hide();
    });


    // ----------- courses table hide/show functionalilty ------------ //

    // hide details & close btn on page load
    $('.hidden-cell').children('table').hide();
    $('.HideCoursesDetails').hide();

    // click events hooked up to functions
    $('.ShowCoursesDetails').click(function () {
        $(this).fadeOut('fast', function () {
            $(this).siblings('.HideCoursesDetails').fadeIn('fast');
        });
        $(this).parent('td').css('background-color', '#C9E8F1');
        $(this).parent('td').siblings('td').css('background-color', '#C9E8F1');
        $(this).parent('td').parent('tr').next('tr').children('td').children('table').slideDown()
    });

    // Hide the course's details table
    $('.HideCoursesDetails').click(function () {
        $(this).fadeOut('fast', function () {
            $(this).siblings('.ShowCoursesDetails').fadeIn('fast');
        });
        $(this).parent('td').css('background-color', '#fff');
        $(this).parent('td').siblings('td').css('background-color', '#fff');
        $(this).parent('td').parent('tr').next('tr').children('td').children('table').slideUp('fast')
    });


    // ----------- registration form hide/show functionalilty ------------ //

    // --- Billing address
    $('input#ShowBillingAddressForm').click(function () {
        $('#BillingAddressSection').slideDown('slow');
    });
    $('input#HideBillingAddressForm').click(function () {
        $('#BillingAddressSection').slideUp('slow');
    });

    // --- Delivery address
    $('input#ShowDeliveryAddress').click(function () {
        $('#DeliveryAddressSection').slideDown('slow');
    });
    $('input.HideDeliveryAddress').click(function () {
        $('#DeliveryAddressSection').slideUp('slow');
    });

    // ----------- apply date picker to date textboxes ------------ //

    if ($('.DOBText').length >= 1) {
        $('.DOBText').datepick({ dateFormat: 'dd/mm/yy' });
    }

    if ($('.DateText').length >= 1) {
        $('.DateText').datepick({ dateFormat: 'dd/mm/yy', yearRange: '-20:+20' });
    }

    // ----------- add padlocks to member only links ------------ //

    if ($('.locked').length >= 1) {
        $('.locked').append("<img src='/Style Library/images/icon-padlock2.gif' alt='members only' border='0' class='icon-padlock' />");
    }
    if ($('.unlocked').length >= 1) {
        $('.unlocked').append("<img src='/Style Library/images/icon-padlock-unlocked2.gif' alt='members only' border='0' class='icon-padlock' />");
    }

});

