﻿var ridgeway = {};
ridgeway.aspNetValidation = (function () {
    function init() {
        //Hi-jack the ASP.NET error display only if required
        if (typeof (Page_ClientValidate) != "undefined") {
            ValidatorUpdateDisplay = ridgeway.aspNetValidation.validatorUpdateDisplay;
            AspPage_ClientValidate = Page_ClientValidate;
            Page_ClientValidate = ridgeway.aspNetValidation.clientValidate;
        }

        //Add validation statuses to any validation controls that may be visible (i.e. visible when the page loads)
        $('span.validation:visible,span.EditingFormErrorLabel:visible').each(function () {
            addValidationStatus($(this));
        });
    };

    function validatorUpdateDisplay(val) {
        var $val = $(val);
        if (val.isvalid) {
            //Hide the validation control
            $val.hide();

            //Remove the validation status if there are no more validaiton controls visible
            if ($val.parent().find('span.validation:visible,span.EditingFormErrorLabel:visible').length == 0) {
                removeValidationStatus($val);
            }
        }
        else {
            //Show the validation control
            $val.show();

            //Add the validation status
            addValidationStatus($val);
        }
    };

    function clientValidate(validationGroup) {
        var valid = AspPage_ClientValidate(validationGroup);
        if (!valid) {
            //$(this).parent().addClass('invalidInput');
        }
        else {
            //$(this).parent().removeClass('invalidInput');
        }
        return valid;
    };

    function addValidationStatus(obj) {
        if (obj.hasClass('validation') || obj.hasClass('EditingFormErrorLabel')) {
            //We'll look for a validation container to set the status on
            var vc = obj.closest('.validationContainer');
            if (vc.length == 0) {
                //Fall back to .formInput
                vc = obj.closest('.formInput');
            }
            vc.addClass('invalidInput');
        }
    };

    function removeValidationStatus(obj) {
        if (obj.hasClass('validation') || obj.hasClass('EditingFormErrorLabel')) {
            //We'll look for the validation container to remove the status
            var vc = obj.closest('.validationContainer');
            if (vc.length == 0) {
                //Fall back to .formInput
                vc = obj.closest('.formInput');
            }
            vc.removeClass('invalidInput');
        }
    };

    return {
        init: init,
        validatorUpdateDisplay: validatorUpdateDisplay,
        clientValidate: clientValidate
    }
})();

var pennyMallory = {};
pennyMallory.core = (function () {
    function init() {
        //Setup the nav - this is wrapped in a timeout because when the fonts are applied via font-face the width of the nav items change and that affects the navHighlight script
        setTimeout(function () {
            $('#siteNav nav ul').navHighlight({
                defaultHighlightProps: { left: -100, width: 17, opacity: 0 }
            });
        }, 500);

        //Setup CTA and Feature transitions
        $('#siteCTA a').hover(function () {
            $(this).stop().animate({ backgroundColor: '#bf1e2e' }, 400, 'easeOutQuad');
        },
        function () {
            $(this).stop().animate({ backgroundColor: '#5e8e85' }, 400, 'easeOutQuad');
        });
        $('.linkFeature').hover(function () {
            $(this).stop().animate({ backgroundColor: '#cfddda' }, 400, 'easeOutQuad');
        },
        function () {
            $(this).stop().animate({ backgroundColor: '#ffffff' }, 400, 'easeOutQuad');
        });

        //Setup the slideshow (Home page)
        $('.slideshow').slideshow();

        //Setup the story panels (Penny's Journey page)
        $('.storyPanels').storyPanels();

        //Setup any date pickers
        $('.datePicker').datePicker({
            useWheel: false,
            showDayArrows: true
        });

        //Setup any videos
        $('a.video').jwVideo({
            jwPlayerBasePath: '/PennyMallory/resources/flash/jwplayer/',
            skin: 'glow'
        });

        // equalise the heights of feature boxes
        $('.equaliseHeight').sameHeights();

        // target http based links to new windows
        $('a[href^=http]').attr('target', '_blank');
    };

    return {
        init: init
    }
})();

$(document).ready(function () {
    ridgeway.aspNetValidation.init();

    pennyMallory.core.init();
});
