/*********************************************************************************
* Mass Leg JQuery Tools Extension JavaScript                                     *
* File Name: massleg.jquery.tools.extension.js                                   *
* Date created: 06/01/2011                                                       *
* The contents of this file may not be copied, duplicated, or redistributed      *
* in any form without the prior written consent.                                 *
**********************************************************************************/


/*********************/
/** Tooltip Scripts **/
/*********************/

jQuery(document).ready(function ($) {

    var isFirefox = navigator.userAgent.indexOf("Firefox") != -1;

    $('[rel^="#tooltip"]').each(function () {

        // Modify links in firefox to remove the href attribute so tooltip will display on hover
        if (isFirefox && this.href) {
            // Remove href attribute and add a class so that the link still appears to be a link
            $(this).hover(
            // mouseenter
                function () {
                    $(this).data('href', this.href).removeAttr('href').addClass('link');
                    $(this).tooltip().show();
                },
            // mouseleave
                function () {
                    $(this).attr('href', $(this).data('href')).removeClass('link');
                }
            );
        }

        $(this).click(function (event) {
            event.preventDefault();
            $(this).tooltip().show();
            $(this).data('hasFocus', 'true'); // indication tip was triggered by a click, and not a mouseover
            $(this).tooltip().getTip().focus();
        });

        $(this).tooltip({
            tip: $(this).attr("rel"),
            effect: 'slide',
            events: { def: "mouseenter, mouseleave" // trigger element
                    , tooltip: "focus mouseenter, mouseleave" // tooltip element
            },
            position: 'top center',
            offset: [10, 0], // move down 10, right 0
            predelay: 50,
            onBeforeShow: function () {
                // Regardless of where the tooltip is positioned in DOM, append it to
                // the 'body' so that it displays correctly despite IE7 z-index issue
                this.getTip().appendTo('body');
                var tipElement = this.getTip();
                var toolTip = this;
                tipElement.attr("tabindex", -1).focus();
                tipElement.find(".refocus").focus(function () {
                    tipElement.find(".close").first().focus();
                });
                tipElement.find(".close").click(function () {
                    toolTip.hide();
                });
            },
            onHide: function () {
                // TODO Add If focus set via click (mouse-click or keyboard enter) return focus to trigger (also clear hasFocus data)
                // Return focus to the 'clicked' link when tooltip is closed (not for mouseover events)
                if (this.getTrigger().data().hasFocus) {
                    this.getTrigger().focus();
                    $('a.helpIcon').data('hasFocus', '');
                }
            }
        });



    });

    $("body").bind('keydown', function (e) {
        if (e.which == 27) { // Escape Key
            $('[rel^="#tooltip"]').each(function () {
                $(this).tooltip().hide();
            });
        }
    });

    //    $("body").bind('keydown', function (e) {
    //        if (e.which == 9) { // Tab Key
    //            $('[rel^="#tooltip"]').each(function () {
    //                $(this).tooltip().hide();
    //            });
    //        }
    //    });
});


    
/**********************/
/** Phototip Scripts **/
/**********************/

jQuery(document).ready(function ($) {

    var isFirefox = navigator.userAgent.indexOf("Firefox") != -1;

    $('[rel^="#phototip"]').each(function () {

        // Modify links in firefox to remove the href attribute so tooltip will display on hover
        if (isFirefox && this.href) {
            // Remove href-Attributes and set a link class so that the links still looks right
            $(this).hover(
                // mouseenter
                function () {
                    // Remove href-Attributes and set a link class so that the links still looks right
                    $(this).data('href', this.href).removeAttr('href').addClass('link');
                    $(this).tooltip().show();
                },
                // mouseleave
                function () {
                    $(this).attr('href', $(this).data('href')).removeClass('link');
                }
            );
        }

        $(this).click(function (event) {
            event.preventDefault();
            $(this).tooltip().show();
        });

        $(this).tooltip({
            tip: $(this).attr("rel"),
            effect: 'toggle',
            events: { def: "focus mouseenter, blur mouseleave" },
            position: 'center right',
            relative: true,
            offset: [0, 0],  // move down 0, right 0
            predelay: 50,
            onBeforeShow: function () {
                $('[rel^="#phototip"]').each(function () {
                    $(this).tooltip().hide();
                });
            }
        });

    });

    $("body").bind('keydown', function (e) {
        if (e.which == 27) { // Escape Key
            $('[rel^="#phototip"]').each(function () {
                $(this).tooltip().hide();
            });
        }
    });

});




/**********************************************/
/** Overlay Scripts (used for http requests) **/
/**********************************************/

jQuery(document).ready(function ($) {

    $('[rel^="#modal"]').overlay({
        top: 120,
        mask: {
            color: '#666666',
            opacity: 0.8,
            zIndex: 2000
        },
        // Populate the modal wrapper with the response from the href request.
        // Regardless of where the overlay is positioned in DOM, append it to
        // the 'body' so that it displays correctly despite IE7 z-index issue.
        onBeforeLoad: function () {
            var wrapper = this.getOverlay().find(".modalWrapper");
            wrapper.load(this.getTrigger().attr("href"));
            this.getOverlay().appendTo('body');
        },
        // Set the focus to the overlay so that screen readers and keyboard
        // navigation will begin at the top of the overlay content area.
        // Modify overlay to reestablish focus after cycling through the end.
        onLoad: function () {
            var overlayElement = this.getOverlay();
            overlayElement.attr("tabindex", -1).focus();
            overlayElement.find(".refocus").focus(function () {
                overlayElement.find(".close").first().focus();
            });
        },
        // Reset the content of the modal wrapper when the overlay is closed.
        // Return focus to the triggering element when the overlay is closed.
        onClose: function () {
            var wrapper = this.getOverlay().find(".modalWrapper");
            wrapper.html("");
            this.getTrigger().focus();
        }
        // effect: 'apple', // TODO needs to be downloaded to use
    });

});


/********************************************/
/** Overlay Scripts (used for inline divs) **/
/********************************************/

jQuery(document).ready(function ($) {

    $('[rel^="#overlay"]').overlay({
        top: 120,
        mask: {
            color: '#666666',
            opacity: 0.8,
            zIndex: 2000
        },
        // Regardless of where the overlay is positioned in DOM, append it to
        // the 'body' so that it displays correctly despite IE7 z-index issue.
        onBeforeLoad: function () {
            this.getOverlay().appendTo('body');
        },
        // Set the focus to the overlay so that screen readers and keyboard
        // navigation will begin at the top of the overlay content area.
        // Modify overlay to reestablish focus after cycling through the end.
        onLoad: function () {
            var overlayElement = this.getOverlay();
            overlayElement.attr("tabindex", -1).focus();
            overlayElement.find(".refocus").focus(function () {
                overlayElement.find(".close").first().focus();
            });
        },
        // Return focus to the triggering element when the overlay is closed.
        onClose: function () {
            this.getTrigger().focus();
        }
        //effect: 'apple', // TODO needs to be downloaded to use
    });

});




/**********************/
/** Scroller Scripts **/
/**********************/

jQuery(document).ready(function ($) {

    // initialize scrollable and return the programming API
    var api = $("#scroll").scrollable({
        items: '#tools',
        circular: 'true',
        mousewheel: 'true',
        easing: 'swing'
        // use the navigator plugin
    }).navigator().autoscroll({ interval: 7400 }).data("scrollable");

});

