/**
 * Set the date of the custom date selectors to today's date.
 *
 * @param   String  dateSelectorDay     The selector for the Day picker element (eg. '#dayPicker').
 * @param   String  dateSelectorMonth   The selector for the Month picker element (eg. '#monthPicker').
 * @param   String  dateSelectorYear    The selector for the Year picker element (eg. '#yearPicker').
 *
 * @author  Dianne Castillo
 *
 */
var setDateSelectorToday = function(dateSelectorDay, dateSelectorMonth, dateSelectorYear) {

    // Get today's date
    var today = new Date();
    var dateDay = today.getDate();
    var dateMonth = today.getMonth() + 1;
    var dateYear = today.getFullYear();

    // Set day
    if($(dateSelectorDay).length) {
        $(dateSelectorDay).val(dateDay);
    }
    // Set month
    if ($(dateSelectorMonth).length) {
        var months = dateSelectorMonth + ' option';
        var pickMonth = months + '[value=\"' + dateMonth + '\"]';
        $(pickMonth).attr('selected', 'selected');
    }
    // Set year
    if ($(dateSelectorYear).length) {
        $(dateSelectorYear).val(dateYear);
    }

}; // END: setDateSelectorToday()

/**
 * Increase or decrease font size of content areas. Font size must use '%' as unit.
 * Content areas are any areas with a class of 'contentArea'. If a container with
 * a class of 'box' or 'colourBox' is in the content area, the container height
 * (in px) will also be changed.
 *
 * @param   {Object} ev         The triggering event.
 * @author  Dianne Castillo
 *
 */
var textResize = function(ev) {

    // Get clicked button
    var buttonResizeText = ev.target;
    // IE6 doesn't like multiple classes, so use the parent container
    // for checking if the control is disabled
    var buttonResizeTextContainer = $(buttonResizeText).parent('li');
    // Text resize buttons
    var buttonTextIncrease = '.textIncrease';
    var buttonTextDecrease = '.textDecrease';
    var buttonTextIncreaseContainer = $(buttonTextIncrease).parent('li');
    var buttonTextDecreaseContainer = $(buttonTextDecrease).parent('li');

    // Get content areas to resize
    var contentArea = '.contentArea';

    // Configure zoom options:
    // Max number of times text size can be increased
    var TEXT_RESIZE_MAX_STEPS = 3;
    // Percentage increment of text increase/decrease
    var TEXT_RESIZE_INCREMENT = 20;
    // Home page content boxes need to be slightly bigger than text resize factor
    // to ensure content will still fit
    var BOX_RESIZE_MULTIPLIER = 1.8;

    // Text resize control is disabled
    if($(buttonResizeTextContainer).hasClass('disabled')) {
        // Take no action
        return;
    }
    // Text resize control is not disabled
    else {

        // Determine current text zoom level
        var textZoomPattern = /[ ]*textZoom([\d]+)/;
        var buttonResizeTextClass = $(buttonResizeText).attr('class');
        var zoomSet = buttonResizeTextClass.match(textZoomPattern);
        var currentZoomLevel = zoomSet ? parseInt(zoomSet[1], 10) : 0;
        var currentZoomClass = zoomSet ? 'textZoom' + currentZoomLevel : 'textZoom0';
        var newZoomLevel;
        var newTextSize = 100;

        // Calculate text size increase
        if($(buttonResizeText).hasClass('textIncrease')) {

            newZoomLevel = currentZoomLevel + 1;
            // Calculate new font size
            newTextSize = newTextSize + (newZoomLevel * TEXT_RESIZE_INCREMENT);

        }
        // Calculate text size decrease
        else if($(buttonResizeText).hasClass('textDecrease')) {

            newZoomLevel = currentZoomLevel - 1;
            // Lower cap of 0
            if(newZoomLevel < 0) {
                newZoomLevel = 0;
            }
            // Calculate new font size
            newTextSize = newTextSize + (newZoomLevel * TEXT_RESIZE_INCREMENT);
            // Lower cap of 100
            if(newTextSize < 100) {
                newTextSize = 100;
            }

        }
        // Unknown state: should never get here
        else {
            return;
        }

        // Toggle Text Decrease/Increase buttons
        switch(newZoomLevel) {
            case 0:
                $(buttonTextDecreaseContainer).addClass('disabled');
                break;
            case TEXT_RESIZE_MAX_STEPS:
                $(buttonTextIncreaseContainer).addClass('disabled');
                break;
            default:
                $(buttonTextDecreaseContainer).removeClass('disabled');
                $(buttonTextIncreaseContainer).removeClass('disabled');
                break;
        }

        // Track zoom changes on zoom buttons
        var newZoomClass = 'textZoom' + newZoomLevel;
        $(buttonTextDecrease).removeClass(currentZoomClass);
        $(buttonTextDecrease).addClass(newZoomClass);
        $(buttonTextIncrease).removeClass(currentZoomClass);
        $(buttonTextIncrease).addClass(newZoomClass);

        // Apply new font size
        var newTextSizePercentage = newTextSize + '%';
        $(contentArea).css('font-size', newTextSizePercentage);

        // Home Page: resize content boxes
//        var pageHome = 'body#Home';

        // Get content area container
        var contentAreaBoxes = $(contentArea + ' .box');

        // Check container to see if it's a box that needs resizing
        $(contentAreaBoxes).each(function(i, el){

            // Is content box?
            if($(el).hasClass('box') || $(el).hasClass('colourBox')) {

                // Content box height:
                // Get fixed height from CSS because jQuery height() function
                // doesn't work correctly in IE6
                var currentBoxHeight = $(el).css('height');
                // Remove units and symbols
                currentBoxHeight = currentBoxHeight.replace(/[^0-9]/g, '');
                var newBoxHeight;
                var newBoxHeightPixels;

                // Calculate box resize factor:
                // Increase text resize increment
                // to ensure there's enough space to fit the content
                newBoxHeight = TEXT_RESIZE_INCREMENT * BOX_RESIZE_MULTIPLIER;
                // Convert text resize increment from percentage to decimal
                newBoxHeight = 1 + (newBoxHeight / 100);                        

                // Enlarge box height
                if($(buttonResizeText).hasClass('textIncrease')) {
                    // Calculate new home page box height:
                    // Multiply current box height by box resize factor
                    newBoxHeight = currentBoxHeight * newBoxHeight;
                }
                // Reduce box height
                else if($(buttonResizeText).hasClass('textDecrease')) {
                    // Calculate new home page box height:
                    // Divide current box height by box resize factor
                    newBoxHeight = currentBoxHeight / newBoxHeight;
                }
                // Unknown state: should never get here
                else {
                    return;
                }

                // Convert box height to integer
                newBoxHeight = parseInt(newBoxHeight, 10);
                newBoxHeightPixels = newBoxHeight + 'px';
                // Set new box height
                $(el).height(newBoxHeightPixels);
            }

        }); // END: search for content boxes to resize

    }

}; // END: textResize()

/**
 * Toggle hover styles for IE6.
 *
 * @author  Dianne Castillo
 *
 */
var toggleToolHover = function(ev) {

    // Event type
    var eventType = ev.type;
    // Tool container
    var buttonTool = ev.target;
    var buttonToolContainer = $(buttonTool).parent('li');

    // Tool is disabled
    if($(buttonToolContainer).hasClass('disabled')) {
        $(buttonTool).css('cursor', 'default');
    }
    else {
        // Set dynamic styles instead of classes because
        // IE6 doesn't like multiple classes
        switch(eventType) {
            case 'mouseenter':
                $(buttonTool).css('cursor', 'pointer');
                break;
            case 'mouseleave':
                $(buttonTool).css('cursor', 'default');
                break;
            default:
                break;
        }
    }

}; // END: toggleToolHover()

