// $Id: jquery_x.js 42 2011-07-24 12:54:46Z Ian $

$(function() {
    // Create popup window
    $('.pu').click(function(e) {
        e.preventDefault();
        window.open($(this).attr('href'));
    });

    // 'back to top' link
    $('.back-to-top-link').click(function(e) {
        e.preventDefault();
        $(window).scrollTop($('#top').position().top);
    });

    // Auto-focus name field of contact form
    if ($('#frm-contact').length > 0) {
        $('#name').focus().addClass('focused-field');
    }

    // Field focus highlight
    $('#frm-contact input[type="text"], #frm-contact textarea').focus(function() {
        $(this).addClass('focused-field');
    }).blur(function() {
        $(this).removeClass('focused-field');
    });

    // Form button hover
    $('.form-button').mouseover(function() {
        $(this).addClass('form-button-hover');
    }).mouseout(function() {
        $(this).removeClass('form-button-hover').removeClass('form-button-click');
    }).mousedown(function() {
        $(this).addClass('form-button-click');
    }).mouseup(function() {
        $(this).removeClass('form-button-click');
    });

    // Validate contact form
    $('#frm-contact').submit(function() {
        var firstErrorField = '';
        var formError = false;

        // Clear any previous field error messages
        $('.field-error').remove();

        $('.required').each(function() {
            var id = '#' + $(this).attr('id');

            if ($(this).val().trim() == '') {
                // Field error
                formError = true;
                addFieldError($(this));

                if (firstErrorField == '') {
                    firstErrorField = $(this).attr('id');
                }
            }
        });

        if (firstErrorField != '') {
            $('#' + firstErrorField).focus();
        }

        if (formError) {
            return false;
        }

        return true;
    });

    // Align image row
    alignImageRows();

    // Add colorbox to images
    $('.images-container a').colorbox({transition:'fade'});

    // Font size selector
    $('.font-size-selector a').click(function(e) {
        var idBits = $(this).attr('id').split('-');
        var fontSize = idBits[2];

        $.cookie('font_size', fontSize, {expires: 30, path: '/'});

        // Resize font dynamically for this page
        if (fontSize == 'medium') {
            fontSizeEm = '1.2em';
        } else if (fontSize == 'large') {
            fontSizeEm = '1.4em';
        } else {
            fontSizeEm = '1.0em';
        }

        $('body').css({
            'font-size':        fontSizeEm
        });

        e.preventDefault();
    });
});

function addFieldError(field) {
    $(field).after(
        $('<div />', {
            'class':    'field-error',
            'html':     '&bull; This field is required'
        })
    );
}

function alignImageRows() {
    $('.images-container ul').each(function() {
        var listWidth = $(this).outerWidth();
        var containerWidth = $(this).parent().width();

        $(this).css('left', (containerWidth / 2) - (listWidth / 2));
    });
}
