/**
 *
 */
(function($) {

    /**
     * Enable log and debug messages for the jquery log plugin.
     */
    $.log.enable = false;

    /**
     * Handle manipulation of the punchout form.
     */
    $.elementReady('div#current-event form', function() {
        /**
         * I only hide the advanced form fields on the punchout form if none of these fields has
         * failed validation. If one of these fields fails validation then it makes no sense to hide
         * them because then the user will not know why they could not punch out.
         */
        if (0 === $(this).find('label[for!=id_log]').parents('dt').next('dd').find('ul.errorlist li').length) {
            $(this).find('label[for!=id_log]').parents('dt').hide();
            $(this).find('label[for!=id_log]').parents('dt').next('dd').hide();
        }

        /**
         * Add a link that allows the user to toggle the visibility of the hidden form items. Then
         * a click handler must be added to this link that will preform the magic.
         */
        $(this).append('<p class="pout-details"><a href="#">More Details</a></p>');

        $(this).find('p.pout-details a').click(function() {
            $(this).parents('form').find('label').each(function() {
                if ('id_log' !== $(this).attr('for')) {
                    if ($(this).parents('dt').is(':hidden')) {
                        $(this).parents('dt').show();
                        $(this).parents('dt').next('dd').show();
                    } else {
                        $(this).parents('dt').hide();
                        $(this).parents('dt').next('dd').hide();
                    }
                }
            });
        });
    });

    /**
     * Handle manipulation of the report form.
     */
    $.elementReady('div#report-form form', function() {
        /**
         * I only hide the advanced form fields on the report form if none of these fields has
         * failed validation. If one of these fields fails validation then it makes no sense to hide
         * them because then the user will not know why they could not punch out.
         */
        if (0 === $(this).find('label[for!=id_date_from][for!=id_date_to]').parents('dt').next('dd').find('ul.errorlist li').length) {
            $(this).find('label[for!=id_date_from][for!=id_date_to]').parents('dt').hide();
            $(this).find('label[for!=id_date_from][for!=id_date_to]').parents('dt').next('dd').hide();
        }

        /**
         * Add a link that allows the user to toggle the visibility of the hidden form items. Then
         * a click handler must be added to this link that will preform the magic.
         */
        $(this).append('<p class="report-details"><a href="#">More Details</a></p>');

        $(this).find('p.report-details a').click(function() {
            $(this).parents('form').find('label').each(function() {
                if ('id_date_from' !== $(this).attr('for')
                        && 'id_date_to' !== $(this).attr('for')) {
                    if ($(this).parents('dt').is(':hidden')) {
                        $(this).parents('dt').show();
                        $(this).parents('dt').next('dd').show();
                    } else {
                        $(this).parents('dt').hide();
                        $(this).parents('dt').next('dd').hide();
                    }
                }
            });
        });
    });

    /**
     * I hide the not active elements from the bread crumbs in the timer. Breadcrumbs refer to the
     * lists showing companies, clients and projects.
     */
    $.elementReady('div#lists', function() {
        $('div.list').each(function() {
            /**
             * If there is nothing active within the list, then do not hide anything because
             * assuming that user needs to choose an item from this list so makes no sense to hide
             * this.
             */
            if (0 === $(this).find('li.active').length || 1 == $(this).find('li').length) {
                return;
            }

            /**
             * Hide all items in list that are not active
             */
            $(this).find('li').each(function() {
                if (!$(this).hasClass('active')) {
                    $(this).hide();
                }
            });

            /**
             * Change mouse cursor to pointer to indicate that the list title should be clicked on.
             * and set an informational title to appear when the mouse hovers over the link which
             * describes what will happen when clicked on.
             *
             * Also add the event handler that will show or hide the non active items within the
             * list.
             */
            with ($(this).find('h2:first')) {
                var show = "\u25b6";
                var hide = "\u25bc";
                var h2_value = text();
                attr('title', 'Show/Hide active ' + h2_value.toLowerCase());
                css('cursor', 'pointer');
                html('<span class="pointer">' + show + '</span>' + h2_value);

                click(function() {
                    $(this).siblings('ul').find('li').each(function() {
                        if (!$(this).hasClass('active')) {
                            if ($(this).is(':hidden')) {
                                find('span').html(hide);
                                $(this).show();
                            } else {
                                find('span').html(show);
                                $(this).hide();
                            }
                        }
                    });
                });
            }
        });
    });

    /**
     * I preform a little bit of magic with the help text for form fields and hide the form help
     * text by default and when the form field is focused, the help text will be shown in an
     * absolute positioned div. To do this, the styles for the help text need to be overwritten so
     * the hemp text will appear correctly. The styles must be set with javascript because the help
     * text needs to be displayed differently when javascript is disabled within the browser.
     */
    $(document).ready(function() {
        $('form span.help-text').each(function() {
            with ($(this)) {
                css('position', 'absolute');
                css('top', '0.5em');
                css('right', '0');
                css('width', '180px');
                css('background', '#339922');
                css('color', 'white');
                css('z-index', '1000');
                hide();
            }
        });

        $('form input, form textarea, form select').each(function() {
            $(this).focus(function() {
                $(this).parents('dd').find('span.help-text').show();
            });

            $(this).blur(function() {
                $(this).parents('dd').find('span.help-text').hide();
            });
        });
    });

    /**
     * When a message is displayed from the timer, I emove the message after 7 seconds. You can see
     * an example of this when pausing/unpausing a task.
     */
    $(document).ready(function() {
        if (1 === $('#message').length) {
            setTimeout(function() {
                $('#message').fadeOut('slow');
            }, 7000);
        }
    });

    /**
     * Hide all the login forms and do fancy javascript show/hide toggling...
     */
    $(document).ready(function() {
        var forms = new Array(
            'login-form-email',
            'login-form-openid',
            'login-form-usernamepassword'
        );

        for (var i = 0; i < forms.length; i++) {
            var form = $('#' + forms[i]);
            $('#' + form.attr('id') + '-toggle').click(function() {
                var id = $(this).attr('id').replace(/-toggle/, '');
                for (var j = 0; j < forms.length; j++) {
                    if (id != forms[j]) {
                        $('#' + forms[j]).slideUp();
                    }
                }

                $('#' + id).slideToggle();
            });
            form.hide();
        }
    });

}).call(window, jQuery);
