// Global

jQuery(document).ready(
    function() {

        _initFormFields();

        jQuery("ul.language-options").hide();

        jQuery("li.language").hover(
            function() {
                jQuery("a.language").addClass("active");
                jQuery("ul.language-options").slideDown();
            },
            function() {
                jQuery("a.language").removeClass("active");
                jQuery("ul.language-options").hide();
            }
        );


        jQuery("ul.profile-options").hide();

        jQuery("li.signed").hover(
            function() {
                jQuery("a.username").addClass("active");
                jQuery("ul.profile-options").slideDown();
            },
            function() {
                jQuery("a.username").removeClass("active");
                jQuery("ul.profile-options").hide();
            }
        );

        jQuery("a.brief-text div.text-on-hover p").hide();

        jQuery("a.brief-text").hover(
            function() {
                jQuery("a.brief-text div.text-on-hover span.more").css("bottom","9px");
                jQuery("a.brief-text div.text-on-hover p").show();
                jQuery("div.text-on-hover").animate({
                    top: 0
                },500);
            },
            function() {
                jQuery("a.brief-text div.text-on-hover span.more").css("bottom","120px");
                jQuery("a.brief-text div.text-on-hover p").hide();
                jQuery("div.text-on-hover").animate({
                    top: 113
                },500);
            }
        );

        // Glossary text

        jQuery("span.glossary-text").each(
            function() {
                var linkWidth = jQuery("a.link-glossary",this).width();

                jQuery("span.info",this).css("left", 0 - ((250 - linkWidth) / 2));
            }
        );

        jQuery("span.glossary-text span.info").hide();

        jQuery("span.glossary-text span.info").hover(
            function() {
                jQuery(this).show();
            } ,
            function() {
                jQuery(this).hide();
            }
        );

        jQuery("a.link-glossary").hover(
            function() {
                jQuery("span.info",jQuery(this).parent()).show();
                return false;
            },
            function() {
                jQuery("span.info",jQuery(this).parent()).hide();
                return false;
            }
        );

        // Smart navigation

        var windowHeight = jQuery(window).height();
        jQuery("#smart ul").css("height",windowHeight);

        jQuery("a.show-smart-nav").click(
            function() {
                if(jQuery(this).hasClass("active")) {
                    jQuery(this).removeClass("active");
                    jQuery(this).animate({
                        left: 0
                    },500);
                    jQuery("span.close-smart-nav").animate({
                        width: 0
                    },500);
                    jQuery("#smart ul").animate({
                        width: 0
                    },
                    {
                        duration: 500,
                        complete: function() {
                            jQuery(this).css("display","none");
                        }
                    });
                    return false;
                }
                else {
                    jQuery(this).animate({
                        left: 232
                    },500);
                    jQuery(this).addClass("active");
                    jQuery("span.close-smart-nav").css("display","block");
                    jQuery("span.close-smart-nav").css("width","0px");
                    jQuery("span.close-smart-nav").animate({
                        width: 232
                    },500);
                    jQuery("#smart ul").css("display","block");
                    jQuery("#smart ul").css("width","0px");
                    jQuery("#smart ul").animate({
                        width: 232
                    },500);
                    return false;
                }
            }
        );

        // Table

        jQuery("table").attr("cellspacing","0");

        // Glossary

        jQuery("a.result-link").click(
            function() {
                if(jQuery(this).hasClass("active")) {
                    jQuery(this).removeClass("active");
                    jQuery("div.glossary-detail",jQuery(this).parent()).slideUp();
                    window.location.hash = ' ';
                    return false;
                }
                else {
                    jQuery("a.result-link").removeClass("active");
                    jQuery(this).addClass("active");
                    jQuery("div.glossary-detail").slideUp();
                    jQuery("div.glossary-detail",jQuery(this).parent()).slideDown();
                    window.location.hash = jQuery(this).attr('href');
                    return false;
                }
            }
        );

        if (window.location.hash) {
            jQuery('a.result-link[href="' + window.location.hash + '"]').each(function() {
                jQuery(this).addClass('active');
                jQuery("div.glossary-detail",jQuery(this).parent()).slideDown();
            });
        }

        jQuery("a.glossary-detail-hide").click(
            function() {
                jQuery("div.glossary-detail").slideUp();
                jQuery("a.result-link").removeClass("active");
                return false;
            }
        );

        // Side menu

        // jQuery("a.side-menu-link").click(
        //     function() {
        //         if(jQuery(this).hasClass("active")) {
        //             jQuery(this).removeClass("active");
        //             jQuery("ul.submenu",jQuery(this).parent()).slideUp(1000);
        //             return false;
        //         }
        //         else {
        //             jQuery("a.side-menu-link").removeClass("active");
        //             jQuery(this).addClass("active");
        //             jQuery("ul.submenu",jQuery(this).parent()).slideDown(1000);
        //             return false;
        //         }
        //     }
        // );

        // Section height

        var headerHeight = jQuery("header").height();
        var footerHeight = jQuery("footer").height();
        var windowHeight = jQuery(window).height();
        var paddingTop = parseInt(jQuery("section").css("paddingTop"));
        var paddingBottom = parseInt(jQuery("section").css("paddingBottom"));

        jQuery("section").css("min-height",windowHeight - (footerHeight + headerHeight + paddingTop + paddingBottom));

        jQuery(window).resize(
            function() {
                var headerHeight = jQuery("header").height();
                var footerHeight = jQuery("footer").height();
                var windowHeight = jQuery(window).height();
                var paddingTop = parseInt(jQuery("section").css("paddingTop"));
                var paddingBottom = parseInt(jQuery("section").css("paddingBottom"));

                jQuery("section").css("min-height",windowHeight - (footerHeight + headerHeight + paddingTop + paddingBottom));
            }
        );

        // Track downloads of PDF files linked from article text (and from attachments section below article text)
        var pdfFile = new RegExp(".*\.pdf$", "i");
        jQuery("div.article a:not(.pdfemb-viewer)").each(function(){
          if(pdfFile.test(this.href)){
            jQuery(this).click(function(){
              var fileName = this.href.split(/[\\/]/).pop();
              var pageUrl = location.href;
              _gaq.push(['_trackEvent', 'File downloads', fileName, pageUrl]);
            });
          }
        });

    }
);

function _initFormFields() {

    // Placeholder

    jQuery(".placeholder").focus(
        function() {
            var defaultValue = this.defaultValue;

            if(jQuery(this).val() == defaultValue) {
                jQuery(this).val("");
            }
        }
    );

    jQuery(".placeholder").blur(
        function() {
            var defaultValue = this.defaultValue;

            if(jQuery(this).val() == "") {
                jQuery(this).val(defaultValue);
            }
        }
    );

    var cf = new CustomFormElements();

}