// -------------------------------- Functions
if (window.jQuery)
// Avoid more errors in IE 5.0
 {
    // Directly set flag for CSS to indicate JavaScript is enabled.
    // At this point "HTML" is the only accessable element.
    $(document.documentElement).addClass("jsEnabled");

    $(document).ready(function() {

        $("#menu").addClass("nobackground");

        $("#navwrapper").mouseover(function() {
            $("#menu").removeClass("nobackground");
        });

        $("#navwrapper").mouseout(function() {
            $("#menu").addClass("nobackground");
        });

        $("a.tooltip").umTooltip();

        // -------------------------------- Form input label flip
        // Activate 'overlabels' for compact forms
        $('label.overlabel')
        .each(function()
        {
            // Get field
            var label = $(this);
            var id = this.htmlFor || this.getAttribute('for');
            var field = null;
            if (!id || !(field = document.getElementById(id))) return;

            // Implement show/hide effect
            $(field).addClass("overlabel-js")
            // for setTimeout() below
            .focus(function() {
                label.hide();
            })
            .blur(function() {
                this.value === '' && label.show();
            });

            // Also for mouse click
            label.mousedown(function() {
                field.focus();
            });
        }
        );

        // Hide overlabels after the form is auto-filled in by the browser.
        setTimeout(function()
        {
            var fields = $("input.overlabel-js");
            var labels = $("label.overlabel");
            fields.each(function() {
                this.value !== '' && labels.filter("[for=" + this.id + "]").hide();
            });
        },
        50);


        // print the page
        $("#login .print a").click(function(event)
        {
            window.print();
        });

        // Advanced search
        $('#advancedsearchLink')
        .wrapInner('<a href="#advanced"></a>')
        .find('a')
        .click(function()
        {
            $('#advancedsearch').slideToggle("slow");
            return false;
        }
        );


        // -------------------------------- FAQ section

              $("#whccc_faq .faq_item dt").click(
                function(e) { 
                  var faqitem = $(this);
                  var faqanswer = faqitem.next(".faq_item dd");
                  // close all answers but the current
                  $(".selected").not(faqanswer).toggle(); 
                  $(".selected").not(faqanswer).toggleClass("selected"); 
                  // toggle the current
                  faqitem.next(".faq_item dd").toggle(); 
                  faqitem.next(".faq_item dd").toggleClass("selected"); 
                  return false; 
                }
              );
              
              
        // ------------------------------- Fix links
        // give links with rel="external" target="_blank" attribuut.
        $(document.links).filter('[href][rel=external]').attr("target", "_blank");

    });

}




// ------------------------------- Tooltip plugin
jQuery.fn.umTooltip = function()
 {
    this.hover(
    function(event)
    {
        this.t = this.alt || this.title;
        this.title = "";
        var tooltip = $("body").append("<div id='tooltip' style='display:none'><p>" + this.t + "</p></div>").children("#tooltip");
        var pos = _getTooltipPos(event, tooltip);
        tooltip.css(pos).fadeIn("fast");
    },
    function()
    {
        this.alt = this.t;
        this.title = this.t;
        $("#tooltip").remove();
    }
    )

    function _getTooltipPos(event, tooltip)
    {
        var e = $(event.target);
        var epos = e.offset();
        var ttWidth = tooltip.outerWidth();
        var ttHeight = tooltip.outerHeight(true);
        return {
            left: epos.left + (e.outerWidth() - ttWidth) / 2
            ,
            top: epos.top - ttHeight
        };
    }


    // -------------------- Make the whole row clickable
    $('#institutions tbody tr').click(function() {
        var href = $(this).find("a").attr("href");
        if (href) {
            window.location = href;
        }
    });


};


